test_timer.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 <chrono>
  22. #include <future>
  23. #include <string>
  24. #include <thread>
  25. #include "util/timer.h"
  26. TEST(InferServerUtil, Timer) {
  27. infer_server::Timer t1;
  28. auto task = [](const std::string& msg) { std::cout << msg << "\n"; };
  29. EXPECT_TRUE(t1.NotifyEvery(10, task, "timer1: print every 10ms"));
  30. EXPECT_FALSE(t1.NotifyAfter(10, task, "timer1: this line should not be printed"));
  31. infer_server::Timer t2;
  32. EXPECT_TRUE(t2.NotifyEvery(30, task, "timer2: print every 30ms"));
  33. infer_server::Timer t3;
  34. std::promise<void> pro1;
  35. auto start = std::chrono::steady_clock::now();
  36. int wait_time = 100;
  37. EXPECT_TRUE(t3.NotifyAfter(wait_time, [&pro1]() {
  38. std::cout << "timer3: after 100 ms" << std::endl;
  39. pro1.set_value();
  40. }));
  41. pro1.get_future().get();
  42. auto end = std::chrono::steady_clock::now();
  43. std::chrono::duration<double, std::milli> dura = end - start;
  44. EXPECT_GE(dura.count(), wait_time);
  45. EXPECT_NEAR(dura.count(), wait_time, 1);
  46. std::cout << "cancel timer1 and timer2\n";
  47. t1.Cancel();
  48. t2.Cancel();
  49. std::cout << "timer1: will say 'I'm back' after 5ms\n";
  50. std::promise<void> pro2;
  51. wait_time = 5;
  52. start = std::chrono::steady_clock::now();
  53. EXPECT_TRUE(t1.NotifyAfter(wait_time, [&pro2]() {
  54. std::cout << "timer1: I'm back\n";
  55. pro2.set_value();
  56. }));
  57. EXPECT_TRUE(t2.NotifyAfter(wait_time - 1, task, "timer2: this line should not be printed"));
  58. t2.Cancel();
  59. EXPECT_TRUE(t2.NotifyAfter(0, task, "timer2: speak at once"));
  60. pro2.get_future().get();
  61. end = std::chrono::steady_clock::now();
  62. dura = end - start;
  63. EXPECT_GE(dura.count(), wait_time);
  64. EXPECT_NEAR(dura.count(), wait_time, 1);
  65. std::promise<void> pro3;
  66. wait_time = 26;
  67. start = std::chrono::steady_clock::now();
  68. EXPECT_TRUE(t1.NotifyAfter(wait_time, [&pro3]() {
  69. std::cout << "timer1: final print after 26ms" << std::endl;
  70. pro3.set_value();
  71. }));
  72. EXPECT_TRUE(t2.NotifyEvery(5, task, "timer2: print every 5ms"));
  73. pro3.get_future().get();
  74. end = std::chrono::steady_clock::now();
  75. dura = end - start;
  76. EXPECT_GE(dura.count(), wait_time);
  77. EXPECT_NEAR(dura.count(), wait_time, 1);
  78. }