test_task.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. #include <gtest/gtest.h>
  21. #include <chrono>
  22. #include <future>
  23. #include <memory>
  24. #include <thread>
  25. #include <vector>
  26. #include "infer_task.hpp"
  27. namespace cnstream {
  28. TEST(Inferencer, InferTask_Constructor) {
  29. InferTaskSptr task;
  30. ASSERT_NO_THROW(task = std::make_shared<InferTask>([]() -> int { return 0; }));
  31. }
  32. TEST(Inferencer, InferTask_BindFrontTask) {
  33. InferTask task([]() -> int { return 0; });
  34. InferTaskSptr front_task = std::make_shared<InferTask>([]() -> int { return 0; });
  35. ASSERT_NO_THROW(task.BindFrontTask(front_task));
  36. }
  37. TEST(Inferencer, InferTask_BindFrontTask_NULL) {
  38. InferTask task([]() -> int { return 0; });
  39. InferTaskSptr front_task = NULL;
  40. ASSERT_NO_THROW(task.BindFrontTask(front_task));
  41. }
  42. TEST(Inferencer, InferTask_BindFrontTasks) {
  43. InferTask task([]() -> int { return 0; });
  44. InferTaskSptr front_task = std::make_shared<InferTask>([]() -> int { return 0; });
  45. InferTaskSptr front_task2 = std::make_shared<InferTask>([]() -> int { return 0; });
  46. std::vector<InferTaskSptr> front_tasks = {front_task, front_task2};
  47. ASSERT_NO_THROW(task.BindFrontTasks(front_tasks));
  48. }
  49. TEST(Inferencer, InferTask_Execute) {
  50. InferTask task([]() -> int { return 1000; });
  51. EXPECT_EQ(1000, task.Execute());
  52. }
  53. TEST(Inferencer, InferTask_WaitForTaskComplete) {
  54. InferTask task([]() -> int { return 0; });
  55. task.Execute();
  56. ASSERT_NO_THROW(task.WaitForTaskComplete());
  57. }
  58. TEST(Inferencer, InferTask_WaitForFrontTasksComplete) {
  59. InferTask task([]() -> int { return 0; });
  60. InferTaskSptr front_task = std::make_shared<InferTask>([]() -> int { return 0; });
  61. InferTaskSptr front_task2 = std::make_shared<InferTask>([]() -> int { return 0; });
  62. std::vector<InferTaskSptr> front_tasks = {front_task, front_task2};
  63. task.BindFrontTasks(front_tasks);
  64. front_task->Execute();
  65. front_task2->Execute();
  66. ASSERT_NO_THROW(task.WaitForFrontTasksComplete());
  67. }
  68. TEST(Inferencer, InferTask_RemoveResourceAfterExecute) {
  69. std::shared_ptr<int> resource = std::make_shared<int>(1);
  70. InferTask task([=]() -> int { return *resource; });
  71. EXPECT_EQ(resource.use_count(), 2l);
  72. task.Execute();
  73. EXPECT_EQ(resource.use_count(), 1l);
  74. }
  75. TEST(Inferencer, InferTask_ExecuteSequence) {
  76. std::chrono::steady_clock::time_point task0_tp, task1_tp, task2_tp;
  77. InferTaskSptr task0 = std::make_shared<InferTask>([&task0_tp]() -> int {
  78. task0_tp = std::chrono::steady_clock::now();
  79. return 0;
  80. });
  81. InferTaskSptr task1 = std::make_shared<InferTask>([&task1_tp]() -> int {
  82. task1_tp = std::chrono::steady_clock::now();
  83. return 0;
  84. });
  85. InferTaskSptr task2 = std::make_shared<InferTask>([&task2_tp]() -> int {
  86. task2_tp = std::chrono::steady_clock::now();
  87. return 0;
  88. });
  89. task1->BindFrontTask(task0);
  90. task2->BindFrontTask(task1);
  91. std::future<void> future2 = std::async(std::launch::async, [&task2]() {
  92. task2->WaitForFrontTasksComplete();
  93. task2->Execute();
  94. });
  95. std::future<void> future1 = std::async(std::launch::async, [&task1]() {
  96. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  97. task1->WaitForFrontTasksComplete();
  98. task1->Execute();
  99. });
  100. std::future<void> future0 = std::async(std::launch::async, [&task0]() {
  101. std::this_thread::sleep_for(std::chrono::milliseconds(300));
  102. task0->WaitForFrontTasksComplete();
  103. task0->Execute();
  104. });
  105. future2.get();
  106. future1.get();
  107. future0.get();
  108. EXPECT_GT(task2_tp.time_since_epoch().count(), task1_tp.time_since_epoch().count());
  109. EXPECT_GT(task1_tp.time_since_epoch().count(), task0_tp.time_since_epoch().count());
  110. }
  111. } // namespace cnstream