test_tc_timer.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. #include "util/tc_timer.h"
  2. #include "util/tc_common.h"
  3. #include "util/tc_cron.h"
  4. #include "util/tc_epoller.h"
  5. #include <cmath>
  6. #include "gtest/gtest.h"
  7. #include <iostream>
  8. #include <vector>
  9. using namespace std;
  10. using namespace tars;
  11. class UtilTimerTest : public testing::Test
  12. {
  13. public:
  14. //添加日志
  15. static void SetUpTestCase()
  16. {
  17. }
  18. static void TearDownTestCase()
  19. {
  20. }
  21. virtual void SetUp() //TEST跑之前会执行SetUp
  22. {
  23. }
  24. virtual void TearDown() //TEST跑完之后会执行TearDown
  25. {
  26. }
  27. };
  28. class TestClass
  29. {
  30. public:
  31. void test1()
  32. {
  33. _data.push_back(std::make_pair(TNOWMS, 0));
  34. }
  35. void test2(int64_t i)
  36. {
  37. _data.push_back(std::make_pair(TNOWMS, i));
  38. }
  39. void test3(int i)
  40. {
  41. _data.push_back(std::make_pair(TNOWMS, i));
  42. }
  43. void testCron()
  44. {
  45. _data.push_back(std::make_pair(TNOWMS, 0));
  46. }
  47. void testCron2()
  48. {
  49. _data.push_back(std::make_pair(TNOWMS, 0));
  50. }
  51. vector<pair<int64_t, int64_t>> _data;
  52. };
  53. TEST_F(UtilTimerTest, testDelayTask1)
  54. {
  55. TC_Timer timer;
  56. timer.startTimer(1);
  57. shared_ptr<TestClass> tPtr = std::make_shared<TestClass>();
  58. timer.postDelayed(100, std::bind(&TestClass::test2, tPtr.get(), std::placeholders::_1), 111);
  59. TC_Common::msleep(106);
  60. ASSERT_TRUE(tPtr->_data.size() == 1);
  61. timer.stopTimer();
  62. }
  63. TEST_F(UtilTimerTest, testDelayTask2)
  64. {
  65. TC_Timer timer;
  66. timer.startTimer(1);
  67. shared_ptr<TestClass> tPtr = std::make_shared<TestClass>();
  68. uint64_t id2 = timer.postDelayed(100, std::bind(&TestClass::test2, tPtr.get(), std::placeholders::_1), 111);
  69. TC_Common::msleep(5);
  70. ASSERT_TRUE(tPtr->_data.size() == 0);
  71. timer.erase(id2);
  72. timer.stopTimer();
  73. }
  74. TEST_F(UtilTimerTest, testRepeatTask)
  75. {
  76. TC_Timer timer;
  77. shared_ptr<TestClass> tPtr = std::make_shared<TestClass>();
  78. timer.startTimer(1);
  79. uint64_t id2 = timer.postRepeated(50, false, std::bind(&TestClass::test1, tPtr.get()));
  80. TC_Common::msleep(1080);
  81. //由于精度原因, 有一定误差
  82. cout << tPtr->_data.size() << endl;
  83. ASSERT_TRUE(tPtr->_data.size() >= 20);
  84. ASSERT_TRUE(tPtr->_data.size() <= 21);
  85. for (int i = 0; i < 9; i++)
  86. {
  87. int diff = fabs(tPtr->_data[i + 1].first - tPtr->_data[i].first);
  88. ASSERT_TRUE(diff <= 56);
  89. }
  90. timer.erase(id2);
  91. TC_Common::msleep(100);
  92. ASSERT_TRUE(tPtr->_data.size() >= 20);
  93. ASSERT_TRUE(tPtr->_data.size() <= 21);
  94. timer.stopTimer();
  95. }
  96. struct AAA
  97. {
  98. AAA() { };
  99. ~AAA() { };
  100. void test() {}
  101. int xxx = 100;
  102. };
  103. void fff(const std::shared_ptr<AAA>& ppp)
  104. {
  105. ppp->xxx++;
  106. // cout << TC_Common::now2ms() << ", " << ppp->xxx << endl;
  107. }
  108. TEST_F(UtilTimerTest, testTimerMem)
  109. {
  110. TC_Timer timer;
  111. timer.startTimer(1);
  112. std::shared_ptr<AAA> pa = std::make_shared<AAA>();
  113. //right
  114. {
  115. auto func = [pa]
  116. {
  117. fff(pa);
  118. };
  119. int xxx = pa->xxx;
  120. timer.postDelayed(100, func);
  121. TC_Common::msleep(120);
  122. ASSERT_TRUE(pa->xxx - xxx == 1);
  123. }
  124. //right
  125. {
  126. auto funcb = std::bind(fff, pa);
  127. int xxx = pa->xxx;
  128. timer.postDelayed(100, funcb);
  129. TC_Common::msleep(120);
  130. ASSERT_TRUE(pa->xxx - xxx == 1);
  131. }
  132. //right
  133. {
  134. int xxx = pa->xxx;
  135. timer.postDelayed(100, fff, pa);
  136. TC_Common::msleep(120);
  137. ASSERT_TRUE(pa->xxx - xxx == 1);
  138. }
  139. //right
  140. {
  141. int xxx = pa->xxx;
  142. timer.postDelayed(100, std::bind(fff, std::ref(pa)));
  143. TC_Common::msleep(120);
  144. ASSERT_TRUE(pa->xxx - xxx == 1);
  145. }
  146. //wrong, unkown reason
  147. // if (1)
  148. {
  149. int xxx = pa->xxx;
  150. timer.postDelayed(100, std::bind(fff, pa));
  151. TC_Common::msleep(120);
  152. ASSERT_TRUE(pa->xxx - xxx == 1);
  153. }
  154. timer.stopTimer();
  155. }
  156. TEST_F(UtilTimerTest, testTimerRepeatMem)
  157. {
  158. TC_Timer timer;
  159. timer.startTimer(1);
  160. std::shared_ptr<AAA> pa = std::make_shared<AAA>();
  161. int xxx = pa->xxx;
  162. auto func = [pa] {
  163. fff(pa);
  164. };
  165. int64_t taskId = timer.postRepeated(50, false, func);
  166. TC_Common::msleep(1020);
  167. //注意timer有精度的问题, 精度只能到5ms, 所以这里有一定的误差!
  168. int diff = pa->xxx - xxx;
  169. cout << diff << endl;
  170. ASSERT_TRUE(diff >= 19);
  171. ASSERT_TRUE(diff <= 20);
  172. timer.erase(taskId);
  173. TC_Common::msleep(1000);
  174. diff = pa->xxx - xxx;
  175. ASSERT_TRUE(diff >= 19);
  176. ASSERT_TRUE(diff <= 20);
  177. timer.stopTimer();
  178. }
  179. TEST_F(UtilTimerTest, testRepeatUseCount)
  180. {
  181. TC_Timer timer;
  182. timer.startTimer(1);
  183. std::shared_ptr<AAA> pa = std::make_shared<AAA>();
  184. ASSERT_TRUE(pa.use_count() == 1);
  185. int64_t taskId = timer.postRepeated(50, false, std::bind(&AAA::test, pa));
  186. ASSERT_TRUE(pa.use_count() == 2);
  187. timer.erase(taskId);
  188. ASSERT_TRUE(pa.use_count() == 1);
  189. timer.postRepeated(50, false, std::bind(&AAA::test, pa));
  190. ASSERT_TRUE(pa.use_count() == 2);
  191. timer.clear();
  192. ASSERT_TRUE(pa.use_count() == 1);
  193. timer.stopTimer();
  194. }