test_tasknode.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*************************************************************************
  2. * Copyright (C) [2020] 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. #include <gtest/gtest.h>
  21. #include <future>
  22. #include <memory>
  23. #include "cnis/infer_server.h"
  24. #include "cnis/processor.h"
  25. #include "core/engine.h"
  26. #include "core/request_ctrl.h"
  27. #include "test_base.h"
  28. namespace infer_server {
  29. TEST(InferServerCore, TaskNode) {
  30. std::shared_ptr<Processor> proc = std::make_shared<TestProcessor>();
  31. proc->Init();
  32. std::promise<void> tasknode_notify_flag;
  33. auto empty_response_func = [](Status, PackagePtr) {};
  34. auto empty_notifier_func = [](const RequestControl*) {};
  35. std::unique_ptr<RequestControl> ctrl(new RequestControl(empty_response_func, empty_notifier_func, "", 1, 2));
  36. PriorityThreadPool tp(nullptr, 2);
  37. TaskNode task_node(proc, []() {}, &tp);
  38. auto end_node = task_node.Fork([&tasknode_notify_flag]() { tasknode_notify_flag.set_value(); });
  39. task_node.Link(&end_node);
  40. // TaskNode operator()
  41. auto input = Package::Create(2);
  42. input->data[0]->ctrl = ctrl.get();
  43. input->data[0]->index = 0;
  44. input->data[1]->ctrl = ctrl.get();
  45. input->data[1]->index = 1;
  46. ASSERT_NO_THROW(task_node(input));
  47. auto tasknode_notify_ret = tasknode_notify_flag.get_future().wait_for(std::chrono::seconds(1));
  48. ASSERT_EQ(std::future_status::ready, tasknode_notify_ret);
  49. }
  50. } // namespace infer_server