test_timeout_helper.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 <atomic>
  22. #include <chrono>
  23. #include <condition_variable>
  24. #include <future>
  25. #include <memory>
  26. #include <mutex>
  27. #include <thread>
  28. #include "timeout_helper.hpp"
  29. namespace cnstream {
  30. class TimeoutHelperTest {
  31. public:
  32. explicit TimeoutHelperTest(TimeoutHelper* th) : th_(th) {}
  33. std::thread& getThread() { return th_->handle_th_; }
  34. float getTime() { return th_->timeout_; }
  35. void setTime(float time) { th_->timeout_ = time; }
  36. TimeoutHelper::State getState() { return th_->state_; }
  37. void setState(int number) { th_->state_ = TimeoutHelper::State(number); }
  38. // condition_variable do not support move or reference
  39. void ConditionNotify() { th_->cond_.notify_one(); }
  40. int get_timeout_print_cnt() { return static_cast<int>(th_->timeout_print_cnt_); }
  41. void set_timeout_print_cnt(int number) { th_->timeout_print_cnt_ = number; }
  42. private:
  43. TimeoutHelper* th_;
  44. };
  45. TEST(Inferencer, TimeoutHelper_Constructor) {
  46. std::shared_ptr<TimeoutHelper> th = nullptr;
  47. EXPECT_NO_THROW(th = std::make_shared<TimeoutHelper>()); // 此时创建handle_th_线程
  48. TimeoutHelperTest th_test(th.get());
  49. std::thread& Threadhandle = th_test.getThread();
  50. EXPECT_EQ(Threadhandle.joinable(), true);
  51. }
  52. TEST(Inferencer, TimeoutHelper_SetTimeout) {
  53. std::shared_ptr<TimeoutHelper> th = std::make_shared<TimeoutHelper>();
  54. EXPECT_NO_THROW(th->SetTimeout(12.56));
  55. TimeoutHelperTest th_test(th.get());
  56. EXPECT_EQ(th->SetTimeout(-1), 1);
  57. }
  58. TEST(Inferencer, TimeoutHelper_Reset) {
  59. std::shared_ptr<TimeoutHelper> th = std::make_shared<TimeoutHelper>();
  60. std::function<void()> Func = NULL;
  61. TimeoutHelperTest th_test(th.get());
  62. th_test.setState(3);
  63. EXPECT_EQ(th->Reset(Func), 1);
  64. Func = []() -> void {};
  65. th_test.setState(0);
  66. th->Reset(Func);
  67. EXPECT_EQ(static_cast<int>(th_test.getState()), 2);
  68. Func = []() -> void {};
  69. th_test.setState(2);
  70. th->Reset(Func);
  71. EXPECT_EQ(static_cast<int>(th_test.getState()), 1);
  72. Func = nullptr;
  73. th_test.setState(0);
  74. EXPECT_EQ(th->Reset(Func), 0);
  75. EXPECT_EQ(static_cast<int>(th_test.getState()), 0);
  76. }
  77. TEST(Inferencer, TimeoutHelper_HandleFunc) {
  78. TimeoutHelper helper;
  79. const double timeout = 40; // ms
  80. helper.SetTimeout(timeout);
  81. helper.LockOperator();
  82. std::promise<std::chrono::steady_clock::time_point> task_call_promise;
  83. auto task = [&task_call_promise] () {
  84. task_call_promise.set_value(std::chrono::steady_clock::now());
  85. return;
  86. };
  87. auto task_submit_time = std::chrono::steady_clock::now();
  88. helper.Reset(task);
  89. helper.UnlockOperator();
  90. auto task_call_time = task_call_promise.get_future().get();
  91. std::chrono::duration<double, std::milli> used_time = task_call_time - task_submit_time;
  92. EXPECT_GE(used_time.count(), timeout);
  93. }
  94. } // namespace cnstream