test_tc_epoller.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. #include "util/tc_epoller.h"
  2. #include "util/tc_common.h"
  3. #include "util/tc_logger.h"
  4. #include <thread>
  5. #include <iostream>
  6. #include <cmath>
  7. #include "gtest/gtest.h"
  8. using namespace tars;
  9. class UtilEpollTest : public testing::Test
  10. {
  11. public:
  12. //添加日志
  13. static void SetUpTestCase()
  14. {
  15. // cout<<"SetUpTestCase"<<endl;
  16. }
  17. static void TearDownTestCase()
  18. {
  19. // cout<<"TearDownTestCase"<<endl;
  20. }
  21. virtual void SetUp() //TEST跑之前会执行SetUp
  22. {
  23. // cout<<"SetUp"<<endl;
  24. }
  25. virtual void TearDown() //TEST跑完之后会执行TearDown
  26. {
  27. // cout<<"TearDown"<<endl;
  28. }
  29. };
  30. TEST_F(UtilEpollTest, OUTEvents)
  31. {
  32. TC_Epoller epoller;
  33. epoller.create(1024);
  34. //用udp句柄, 方便唤醒, tcp句柄还得构建连接后才能唤醒
  35. TC_Socket fd;
  36. fd.createSocket(SOCK_DGRAM, AF_INET);
  37. shared_ptr<TC_Epoller::EpollInfo> epollInfo = epoller.createEpollInfo(fd.getfd());
  38. int out = 0;
  39. map<uint32_t, TC_Epoller::EpollInfo::EVENT_CALLBACK> callbacks;
  40. callbacks[EPOLLOUT] = [&](const shared_ptr<TC_Epoller::EpollInfo> &epollInfo){
  41. ++out;
  42. return true;
  43. };
  44. epollInfo->registerCallback(callbacks, EPOLLIN | EPOLLOUT);
  45. std::thread start([&]{
  46. TC_Common::msleep(5);
  47. int i = 100;
  48. while(i--) {
  49. int l = out;
  50. epollInfo->mod(EPOLLOUT);
  51. TC_Common::msleep(5);
  52. ASSERT_TRUE(out - l == 1);
  53. }
  54. TC_Common::msleep(5);
  55. epoller.terminate();
  56. });
  57. epoller.loop(1000);
  58. // delete epollInfo;
  59. start.join();
  60. }
  61. TEST_F(UtilEpollTest, close)
  62. {
  63. TC_Epoller epoller;
  64. epoller.create(1024);
  65. //用udp句柄, 方便唤醒, tcp句柄还得构建连接后才能唤醒
  66. TC_Socket fd;
  67. fd.createSocket(SOCK_DGRAM, AF_INET);
  68. bool c = false;
  69. {
  70. shared_ptr<TC_Epoller::EpollInfo> epollInfo = epoller.createEpollInfo(fd.getfd());
  71. map<uint32_t, TC_Epoller::EpollInfo::EVENT_CALLBACK> callbacks;
  72. callbacks[EPOLLOUT] = [&](const shared_ptr<TC_Epoller::EpollInfo>& epollInfo)
  73. {
  74. return false;
  75. };
  76. epollInfo->registerCallback(callbacks, 0);
  77. epollInfo->cookie(&c, [&](void* p)
  78. {
  79. ASSERT_TRUE(c == false);
  80. c = true;
  81. ASSERT_TRUE(&c == p);
  82. });
  83. std::thread start([&]
  84. {
  85. TC_Common::msleep(20);
  86. epollInfo->add(EPOLLOUT);
  87. TC_Common::msleep(100);
  88. epoller.terminate();
  89. });
  90. epoller.loop(1000);
  91. start.join();
  92. // LOG_CONSOLE_DEBUG << epollInfo.use_count() << endl;
  93. epollInfo.reset();
  94. }
  95. ASSERT_TRUE(c);
  96. }
  97. TEST_F(UtilEpollTest, notify)
  98. {
  99. TC_Epoller epoller;
  100. epoller.create(1024);
  101. //用udp句柄, 方便唤醒, tcp句柄还得构建连接后才能唤醒
  102. TC_Socket fd;
  103. fd.createSocket(SOCK_DGRAM, AF_INET);
  104. bool idle = false;
  105. epoller.idle([&]{
  106. idle = true;
  107. });
  108. std::thread start([&]{
  109. TC_Common::msleep(5);
  110. epoller.notify();
  111. TC_Common::msleep(5);
  112. ASSERT_TRUE(idle);
  113. epoller.terminate();
  114. });
  115. epoller.loop(10000);
  116. start.join();
  117. }
  118. class EpollClass
  119. {
  120. public:
  121. ~EpollClass()
  122. {
  123. }
  124. void test()
  125. {
  126. _data.push_back(std::make_pair(TNOWMS, 0));
  127. }
  128. vector<pair<int64_t, int64_t>> _data;
  129. };
  130. TEST_F(UtilEpollTest, testEpollTimer)
  131. {
  132. TC_Epoller epoller;
  133. epoller.create(1024);
  134. shared_ptr<EpollClass> tPtr = std::make_shared<EpollClass>();
  135. std::thread start([&]{
  136. epoller.postRepeated(100, false, std::bind(&EpollClass::test, tPtr.get()));
  137. TC_Common::msleep(1120);
  138. epoller.terminate();
  139. ASSERT_TRUE(tPtr->_data.size() >= 10);
  140. ASSERT_TRUE(tPtr->_data.size() <= 11);
  141. for(int i = 0; i < 9; i++) {
  142. //注意毫秒的精度问题
  143. ASSERT_TRUE(fabs(tPtr->_data[i+1].first - tPtr->_data[i].first) <= 120);
  144. }
  145. });
  146. epoller.loop(200);
  147. start.join();
  148. }
  149. TEST_F(UtilEpollTest, testDelayTask)
  150. {
  151. TC_Timer timer;
  152. timer.startTimer(1);
  153. shared_ptr<EpollClass> tPtr = std::make_shared<EpollClass>();
  154. timer.postDelayed(50, std::bind(&EpollClass::test, tPtr.get()));
  155. timer.postDelayed(100, std::bind(&EpollClass::test, tPtr.get()));
  156. timer.postDelayed(200, std::bind(&EpollClass::test, tPtr.get()));
  157. TC_Common::msleep(306);
  158. ASSERT_TRUE(tPtr->_data.size() == 3);
  159. timer.stopTimer();
  160. }
  161. TEST_F(UtilEpollTest, testSharePtr)
  162. {
  163. TC_Timer timer;
  164. timer.startTimer(1);
  165. shared_ptr<EpollClass> tPtr = std::make_shared<EpollClass>();
  166. timer.postDelayed(50, std::bind(&EpollClass::test, tPtr));
  167. TC_Common::msleep(306);
  168. LOG_CONSOLE_DEBUG << tPtr->_data.size() << endl;
  169. ASSERT_TRUE(tPtr->_data.size() == 1);
  170. timer.stopTimer();
  171. }
  172. TEST_F(UtilEpollTest, testEpollSyncCallback)
  173. {
  174. TC_Epoller epoller;
  175. epoller.create(1024);
  176. shared_ptr<EpollClass> tPtr = std::make_shared<EpollClass>();
  177. std::thread start([&]{
  178. TC_Common::msleep(100);
  179. epoller.syncCallback([&](){
  180. tPtr->test();
  181. });
  182. ASSERT_TRUE(tPtr->_data.size() == 1);
  183. epoller.terminate();
  184. });
  185. epoller.loop(200);
  186. start.join();
  187. }
  188. TEST_F(UtilEpollTest, testEpollAsyncCallback)
  189. {
  190. TC_Epoller epoller;
  191. epoller.create(1024);
  192. shared_ptr<EpollClass> tPtr = std::make_shared<EpollClass>();
  193. std::thread start([&]{
  194. TC_Common::msleep(100);
  195. epoller.asyncCallback([&](){
  196. tPtr->test();
  197. });
  198. TC_Common::msleep(100);
  199. epoller.terminate();
  200. ASSERT_TRUE(tPtr->_data.size() == 1);
  201. });
  202. epoller.loop(200);
  203. start.join();
  204. }