test_base.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*************************************************************************
  2. * Copyright (C) [2019] by Cambricon, Inc. All rights reserved
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  13. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  15. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. * THE SOFTWARE.
  19. *************************************************************************/
  20. #ifndef INFER_SERVER_TEST_BASE_H_
  21. #define INFER_SERVER_TEST_BASE_H_
  22. #include <gtest/gtest.h>
  23. #include <unistd.h>
  24. #include <iostream>
  25. #include <string>
  26. #include "cnis/infer_server.h"
  27. inline std::string GetExePath() {
  28. constexpr int PATH_MAX_LENGTH = 1024;
  29. char path[PATH_MAX_LENGTH];
  30. int cnt = readlink("/proc/self/exe", path, PATH_MAX_LENGTH);
  31. if (cnt < 0 || cnt >= PATH_MAX_LENGTH) {
  32. return "";
  33. }
  34. if (path[cnt - 1] == '/') {
  35. path[cnt - 1] = '\0';
  36. } else {
  37. path[cnt] = '\0';
  38. }
  39. std::string result(path);
  40. return std::string(path).substr(0, result.find_last_of('/') + 1);
  41. }
  42. class InferServerTest : public testing::Test {
  43. protected:
  44. void SetMluContext() { infer_server::SetCurrentDevice(device_id_); }
  45. void SetUp() override { SetMluContext(); }
  46. void TearDown() override {}
  47. static constexpr int device_id_ = 0;
  48. };
  49. class TestProcessor : public infer_server::ProcessorForkable<TestProcessor> {
  50. public:
  51. TestProcessor() noexcept : infer_server::ProcessorForkable<TestProcessor>("TestProcessor") {
  52. std::cout << "[TestProcessor] Construct\n";
  53. }
  54. ~TestProcessor() { std::cout << "[TestProcessor] Destruct\n"; }
  55. infer_server::Status Process(infer_server::PackagePtr data) noexcept override {
  56. std::cout << "[TestProcessor] Process\n";
  57. if (!initialized_) return infer_server::Status::ERROR_BACKEND;
  58. return infer_server::Status::SUCCESS;
  59. }
  60. infer_server::Status Init() noexcept override {
  61. std::cout << "[TestProcessor] Init\n";
  62. initialized_ = true;
  63. return infer_server::Status::SUCCESS;
  64. }
  65. private:
  66. bool initialized_{false};
  67. };
  68. #endif // INFER_SERVER_TEST_BASE_H_