test_processor.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #include <gtest/gtest.h>
  2. #include <iostream>
  3. #include <map>
  4. #include <memory>
  5. #include <string>
  6. #include "cnis/infer_server.h"
  7. #include "fixture.h"
  8. namespace infer_server {
  9. namespace {
  10. std::map<std::string, int> g_param_set{{"number1", 1}, {"number2", 2}, {"number4", 4}};
  11. class TestProcessor : public ProcessorForkable<TestProcessor> {
  12. public:
  13. TestProcessor() noexcept : ProcessorForkable("TestProcessor") { std::cout << "[TestProcessor] Construct\n"; }
  14. ~TestProcessor() { std::cout << "[TestProcessor] Destruct\n"; }
  15. Status Process(PackagePtr data) noexcept override {
  16. std::cout << "[TestProcessor] Process\n";
  17. if (!initialized_) return Status::ERROR_BACKEND;
  18. for (auto& it : g_param_set) {
  19. if (!HaveParam(it.first)) return Status::INVALID_PARAM;
  20. if (GetParam<int>(it.first) != it.second) return Status::INVALID_PARAM;
  21. }
  22. return Status::SUCCESS;
  23. }
  24. Status Init() noexcept override {
  25. std::cout << "[TestProcessor] Init\n";
  26. initialized_ = true;
  27. return Status::SUCCESS;
  28. }
  29. private:
  30. bool initialized_{false};
  31. };
  32. TEST_F(InferServerTestAPI, Processor) {
  33. TestProcessor processor;
  34. for (auto& it : g_param_set) {
  35. processor.SetParams(it.first, it.second);
  36. }
  37. ASSERT_EQ(processor.Init(), Status::SUCCESS);
  38. ASSERT_EQ(processor.Process({}), Status::SUCCESS);
  39. auto fork = processor.Fork();
  40. ASSERT_TRUE(fork);
  41. ASSERT_NE(fork.get(), &processor);
  42. ASSERT_EQ(fork->Process({}), Status::SUCCESS);
  43. // two processor should have independent params
  44. processor.PopParam<int>(g_param_set.begin()->first);
  45. EXPECT_TRUE(fork->HaveParam(g_param_set.begin()->first));
  46. }
  47. } // namespace
  48. } // namespace infer_server