test_tc_epoller.cpp 5.3 KB

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