test_communicator.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. 
  2. #include "hello_test.h"
  3. #include "servant/CommunicatorEpoll.h"
  4. //
  5. //TEST_F(HelloTest, testNotifyCommunicatorEpoll)
  6. //{
  7. // shared_ptr<Communicator> c = getCommunicator();
  8. //
  9. // int netThread = TC_Common::strto<int>(c->getProperty("netthread"));
  10. //
  11. // HelloServer server;
  12. // startServer(server, (TC_EpollServer::SERVER_OPEN_COROUTINE) 0);
  13. //
  14. // auto v1 = c->getAllCommunicatorEpoll();
  15. //
  16. // ASSERT_TRUE(v1.size() == 0);
  17. //
  18. // HelloPrx prx = getObj<HelloPrx>(c.get(), "HelloAdapter");
  19. //
  20. // std::thread th1([&]()
  21. // { checkSyncOnce(prx); });
  22. //
  23. // TC_Common::msleep(100);
  24. //
  25. // auto v2 = c->getAllCommunicatorEpoll();
  26. // //两个公有通信器
  27. // ASSERT_TRUE(v2.size() == (size_t)netThread);
  28. //
  29. // std::thread th2([&]()
  30. // { checkSyncOnce(prx); });
  31. // TC_Common::msleep(100);
  32. //
  33. // auto v3 = c->getAllCommunicatorEpoll();
  34. // //通信器个数不变
  35. // ASSERT_TRUE(v3.size() == (size_t)netThread);
  36. //
  37. // funcInCoroutine([&]()
  38. // {
  39. // checkSyncOnce(prx);
  40. // auto v4 = c->getAllCommunicatorEpoll();
  41. // //为设置协程的情况下, 仍然是两个通信器
  42. // ASSERT_TRUE(v4.size() == (size_t)netThread);
  43. // },
  44. // false);
  45. //
  46. // funcInCoroutine([&]()
  47. // {
  48. // checkSyncOnce(prx);
  49. // auto v5 = c->getAllCommunicatorEpoll();
  50. // //设置了协程, 会增加一个私有协程网络通信器
  51. // ASSERT_TRUE(v5.size() == (size_t)(netThread + 1));
  52. // },
  53. // true);
  54. //
  55. // auto v6 = c->getAllCommunicatorEpoll();
  56. // //之前的业务线程释放了, 会减少一个私有协程网络通信器
  57. // ASSERT_TRUE(v6.size() == (size_t)netThread);
  58. //
  59. // funcInCoroutine([&]()
  60. // {
  61. // checkASyncOnce(prx);
  62. // //设置协程, 会增加一个私有网路通信器, 同时异步回调在私有网络通信器中
  63. // auto v7 = c->getAllCommunicatorEpoll();
  64. // ASSERT_TRUE(v7.size() == (size_t)(netThread + 1));
  65. // });
  66. //
  67. // th1.join();
  68. // th2.join();
  69. //
  70. // stopServer(server);
  71. //
  72. //}
  73. //
  74. //int getNotify(shared_ptr<Communicator> c)
  75. //{
  76. // if(c->getCommunicatorEpollNum() <= 0)
  77. // {
  78. // return 0;
  79. // }
  80. // auto n = c->getCommunicatorEpoll(0)->getNotify();
  81. //
  82. // int sum = 0;
  83. // for(size_t i = 0; i< MAX_CLIENT_NOTIFYEVENT_NUM; i++)
  84. // {
  85. // if(n[i] != NULL)
  86. // {
  87. // ++sum;
  88. // }
  89. // }
  90. // return sum;
  91. //}
  92. //
  93. ////测试notify句柄不泄露!
  94. //TEST_F(HelloTest, testNotifyBussThreadQuitOnce)
  95. //{
  96. // shared_ptr<Communicator> c = getCommunicator();
  97. //
  98. // HelloPrx prx = getObj<HelloPrx>(c.get(), "HelloAdapter");
  99. //
  100. // HelloServer server;
  101. // startServer(server, (TC_EpollServer::SERVER_OPEN_COROUTINE) 0);
  102. //
  103. // //先把服务器端的强制都创建好
  104. // for (int i = 0; i < 20; i++)
  105. // {
  106. // checkSyncOnce(prx);
  107. // }
  108. //
  109. // for (int i = 0; i < 3; i++)
  110. // {
  111. // size_t sp_count = ServantProxyThreadData::g_immortal->getList().size();
  112. // size_t notify_count = getNotify(c);
  113. //
  114. // {
  115. // std::thread th1([&]()
  116. // { checkSyncOnce(prx); });
  117. //
  118. // th1.join();
  119. // }
  120. // TC_Common::msleep(10);
  121. //
  122. // //私有线程数据释放了, 通知句柄数不增加
  123. // ASSERT_TRUE(ServantProxyThreadData::g_immortal->getList().size() == sp_count);
  124. // ASSERT_TRUE(getNotify(c) == (int)notify_count);
  125. // }
  126. //
  127. // stopServer(server);
  128. //
  129. //}
  130. //
  131. ////测试notify句柄不泄露!
  132. //TEST_F(HelloTest, testNotifyBussThreadQuit)
  133. //{
  134. // shared_ptr<Communicator> c = getCommunicator();
  135. // HelloPrx prx = getObj<HelloPrx>(c.get(), "HelloAdapter");
  136. //
  137. // HelloServer server;
  138. // startServer(server, (TC_EpollServer::SERVER_OPEN_COROUTINE) 0);
  139. //
  140. //// for(int i = 0; i < 5000; i++)
  141. // for(int i = 0; i < 1000; i++)
  142. // {
  143. // {
  144. // std::thread th1([&]()
  145. // {
  146. // checkSyncOnce(prx);
  147. // });
  148. //
  149. // th1.join();
  150. // }
  151. // TC_Common::msleep(10);
  152. //
  153. // int num = getNotify(c);
  154. // ASSERT_TRUE(num < 5);
  155. // }
  156. //
  157. // stopServer(server);
  158. //
  159. //}
  160. //
  161. //TEST_F(HelloTest, testNotifyCommunicatorQuit)
  162. //{
  163. // HelloServer server;
  164. // startServer(server, (TC_EpollServer::SERVER_OPEN_COROUTINE) 0);
  165. //
  166. // for(int i = 0; i < 1000; i++)
  167. // {
  168. // shared_ptr<Communicator> c = getCommunicator();
  169. // HelloPrx prx = getObj<HelloPrx>(c.get(), "HelloAdapter");
  170. //
  171. // {
  172. //
  173. // std::thread th1([&]()
  174. // {
  175. // try
  176. // {
  177. // checkSyncOnce(prx);
  178. // }
  179. // catch(exception &ex)
  180. // {
  181. // LOG_CONSOLE_DEBUG << i << ", " << ex.what() << endl;
  182. // }
  183. //
  184. // c.reset();
  185. // });
  186. //
  187. // th1.join();
  188. // }
  189. //
  190. // }
  191. //
  192. // stopServer(server);
  193. //
  194. //}
  195. TEST_F(HelloTest, testNotifyProxyQuit)
  196. {
  197. HelloServer server;
  198. startServer(server, (TC_EpollServer::SERVER_OPEN_COROUTINE)0);
  199. shared_ptr<Communicator> c = getCommunicator();
  200. HelloPrx prx = getObj<HelloPrx>(c.get(), "HelloAdapter");
  201. int count = 10;
  202. while (count-- > 0)
  203. {
  204. vector<std::thread*> vt;
  205. for (int i = 0; i < 1; i++)
  206. {
  207. vt.push_back(new std::thread([&]()
  208. {
  209. checkSyncOnce(prx);
  210. }));
  211. }
  212. for (auto v: vt)
  213. {
  214. v->join();
  215. delete v;
  216. }
  217. // TC_Common::sleep(3);
  218. }
  219. stopServer(server);
  220. }
  221. TEST_F(HelloTest, testCommunicatorGetResourcesInfo)
  222. {
  223. HelloServer server;
  224. startServer(server, (TC_EpollServer::SERVER_OPEN_COROUTINE)0);
  225. shared_ptr<Communicator> c = getCommunicator();
  226. HelloPrx prx = getObj<HelloPrx>(c.get(), "HelloAdapter");
  227. int count = 10;
  228. while (count-- > 0)
  229. {
  230. vector<std::thread*> vt;
  231. for (int i = 0; i < 1; i++)
  232. {
  233. vt.push_back(new std::thread([&]()
  234. {
  235. checkSyncOnce(prx);
  236. }));
  237. }
  238. for (auto v: vt)
  239. {
  240. v->join();
  241. delete v;
  242. }
  243. }
  244. string buf = c->getResourcesInfo();
  245. LOG_CONSOLE_DEBUG << buf << endl;
  246. stopServer(server);
  247. }
  248. #if 0
  249. TEST_F(HelloTest, testNotifyCtrlC)
  250. {
  251. std::thread th([](){
  252. TC_Common::sleep(2);
  253. kill(getpid(), SIGINT);
  254. });
  255. HelloServer server;
  256. startServer(server, (TC_EpollServer::SERVER_OPEN_COROUTINE) 0);
  257. HelloPrx prx = getObj<HelloPrx>(server.getCommunicator(), "HelloAdapter");
  258. for(int i = 0; i < 10000; i++)
  259. {
  260. checkSyncOnce(prx);
  261. }
  262. stopServer(server);
  263. }
  264. TEST_F(HelloTest, testNotifyCtrlCGlobalCommunicator)
  265. {
  266. std::thread th([](){
  267. TC_Common::sleep(2);
  268. kill(getpid(), SIGINT);
  269. });
  270. shared_ptr<Communicator> c = std::make_shared<Communicator>();
  271. HelloServer server;
  272. startServer(server, (TC_EpollServer::SERVER_OPEN_COROUTINE) 0);
  273. for(int i = 0; i < 10000; i++)
  274. {
  275. checkSync(c.get());
  276. }
  277. stopServer(server);
  278. }
  279. #endif