test_win_server.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. 
  2. #include "hello_test.h"
  3. #include "../server/WinServer.h"
  4. TEST_F(HelloTest, winServerInCoroutine)
  5. {
  6. funcInCoroutine([&]()
  7. {
  8. auto comm = getCommunicator();
  9. WinServer ws;
  10. startServer(ws, WIN_CONFIG());
  11. string obj = getObj(ws.getConfig(), "WinAdapter");
  12. HelloPrx prx = comm->stringToProxy<HelloPrx>(obj);
  13. string out;
  14. prx->testHello(0, _buffer, out);
  15. ASSERT_TRUE(_buffer == out);
  16. stopServer(ws);
  17. },
  18. true);
  19. }
  20. TEST_F(HelloTest, winServerGlobalInCoroutine)
  21. {
  22. auto comm = getCommunicator();
  23. WinServer ws;
  24. startServer(ws, WIN_CONFIG());
  25. string obj = getObj(ws.getConfig(), "WinAdapter");
  26. {
  27. std::thread cor_call([&]()
  28. {
  29. auto scheduler = TC_CoroutineScheduler::create();
  30. //设置到协程中
  31. ServantProxyThreadData::getData()->_sched = scheduler;
  32. scheduler->go([&]()
  33. {
  34. scheduler->setNoCoroutineCallback([=](TC_CoroutineScheduler* s)
  35. {
  36. s->terminate();
  37. });
  38. HelloPrx prx = comm->stringToProxy<HelloPrx>(obj);
  39. string out;
  40. prx->testHello(0, _buffer, out);
  41. ASSERT_TRUE(_buffer == out);
  42. });
  43. scheduler->run();
  44. });
  45. cor_call.join();
  46. }
  47. stopServer(ws);
  48. }
  49. TEST_F(HelloTest, winServer)
  50. {
  51. shared_ptr<Communicator> c = getCommunicator();
  52. WinServer ws;
  53. startServer(ws, WIN_CONFIG(), (TC_EpollServer::SERVER_OPEN_COROUTINE)0);
  54. string obj = getObj(ws.getConfig(), "WinAdapter");
  55. HelloPrx prx = c->stringToProxy<HelloPrx>(obj);
  56. string out;
  57. prx->testHello(0, _buffer, out);
  58. stopServer(ws);
  59. }
  60. TEST_F(HelloTest, winServerAsync)
  61. {
  62. shared_ptr<Communicator> c = getCommunicator();
  63. for(int i = 0; i <= TC_EpollServer::NET_THREAD_MERGE_HANDLES_CO; i++)
  64. {
  65. WinServer ws;
  66. startServer(ws, WIN_CONFIG(), (TC_EpollServer::SERVER_OPEN_COROUTINE)i);
  67. atomic<int> callback_count{ 0 };
  68. string obj = getObj(ws.getConfig(), "WinAdapter");
  69. HelloPrx prx = c->stringToProxy<HelloPrx>(obj);
  70. //发起远程调用
  71. for (int j = 0; j < _count; ++j)
  72. {
  73. HelloPrxCallbackPtr p = new ClientHelloCallback(TC_Common::now2us(), j, _count, _buffer,
  74. callback_count);
  75. prx->async_testHello(p, j, _buffer);
  76. }
  77. waitForFinish(callback_count, _count);
  78. ASSERT_TRUE(callback_count == _count);
  79. stopServer(ws);
  80. }
  81. }
  82. TEST_F(HelloTest, winServerHandleDestroy)
  83. {
  84. int i = 1;//TC_EpollServer::NET_THREAD_MERGE_HANDLES_CO;
  85. for(i = 0; i <= TC_EpollServer::NET_THREAD_MERGE_HANDLES_CO; i++)
  86. {
  87. g_handleDestroy = 0;
  88. WinServer ws;
  89. startServer(ws, WIN_CONFIG(), (TC_EpollServer::SERVER_OPEN_COROUTINE) i);
  90. string obj = getObj(ws.getConfig(), "WinAdapter");
  91. HelloPrx prx = ws.getCommunicator()->stringToProxy<HelloPrx>(obj);
  92. string out;
  93. prx->testHello(0, _buffer, out);
  94. ASSERT_TRUE(_buffer == out);
  95. stopServer(ws);
  96. ASSERT_TRUE(g_handleDestroy == TC_Common::strto<int>(ws.getConfig().get("/tars/application/server/WinAdapter<threads>")));
  97. }
  98. }
  99. TEST_F(HelloTest, winServerFdLeak)
  100. {
  101. int i = 0;
  102. //先跑一边, 把需要分配的句柄都分配完
  103. for (i = 0; i <= TC_EpollServer::NET_THREAD_MERGE_HANDLES_CO; i++)
  104. {
  105. WinServer ws;
  106. startServer(ws, WIN_CONFIG(), (TC_EpollServer::SERVER_OPEN_COROUTINE)i);
  107. string obj = getObj(ws.getConfig(), "WinAdapter");
  108. HelloPrx prx = ws.getCommunicator()->stringToProxy<HelloPrx>(obj);
  109. string out;
  110. //网络通信器有多个, 就要调用多次
  111. prx->testHello(0, _buffer, out);
  112. ASSERT_TRUE(_buffer == out);
  113. prx->testHello(0, _buffer, out);
  114. ASSERT_TRUE(_buffer == out);
  115. stopServer(ws);
  116. }
  117. //第二遍检查句柄是否泄漏
  118. for (i = 0; i <= TC_EpollServer::NET_THREAD_MERGE_HANDLES_CO; i++)
  119. {
  120. int ofdCount = getFdCounts();
  121. {
  122. WinServer ws;
  123. startServer(ws, WIN_CONFIG(), (TC_EpollServer::SERVER_OPEN_COROUTINE)i);
  124. string obj = getObj(ws.getConfig(), "WinAdapter");
  125. HelloPrx prx = ws.getCommunicator()->stringToProxy<HelloPrx>(obj);
  126. string out;
  127. //网络通信器有多个, 就要调用多次
  128. prx->testHello(0, _buffer, out);
  129. ASSERT_TRUE(_buffer == out);
  130. prx->testHello(0, _buffer, out);
  131. ASSERT_TRUE(_buffer == out);
  132. stopServer(ws);
  133. }
  134. // TC_Common::sleep(1000);
  135. int nfdCount = getFdCounts();
  136. //linux下才有效
  137. LOG_CONSOLE_DEBUG << "old fd count:" << ofdCount << ", new fd count:" << nfdCount << endl;
  138. // ASSERT_TRUE(ofdCount == nfdCount);
  139. }
  140. }
  141. TEST_F(HelloTest, winServerRestart)
  142. {
  143. int i = 0;//TC_EpollServer::NET_THREAD_MERGE_HANDLES_CO;
  144. for(i = 0; i <= TC_EpollServer::NET_THREAD_MERGE_HANDLES_CO; i++)
  145. {
  146. WinServer ws;
  147. int count = 10;
  148. while(count-->0)
  149. {
  150. startServer(ws, WIN_CONFIG(), (TC_EpollServer::SERVER_OPEN_COROUTINE)i);
  151. string obj = getObj(ws.getConfig(), "WinAdapter");
  152. HelloPrx prx = ws.getCommunicator()->stringToProxy<HelloPrx>(obj);
  153. string out;
  154. prx->testHello(0, _buffer, out);
  155. ASSERT_TRUE(_buffer == out);
  156. stopServer(ws);
  157. }
  158. }
  159. }
  160. TEST_F(HelloTest, winLog)
  161. {
  162. WinServer ws;
  163. startServer(ws, WIN_CONFIG());
  164. string log1 = ServerConfig::Application + FILE_SEP + ServerConfig::ServerName + FILE_SEP + ServerConfig::Application + "." + ServerConfig::ServerName + ".test1.log";
  165. string log2 = ServerConfig::Application + FILE_SEP + ServerConfig::ServerName + FILE_SEP + ServerConfig::Application + "." + ServerConfig::ServerName + ".test2.log";
  166. TC_File::removeFile(log1, false);
  167. TC_File::removeFile(log2, false);
  168. ASSERT_TRUE(!TC_File::isFileExist(log1));
  169. ASSERT_TRUE(!TC_File::isFileExist(log2));
  170. TLOGEXDEBUG("test1", "test log"<< endl);
  171. TLOGEXDEBUG("test2", "test log"<< endl);
  172. LOG_CONSOLE_DEBUG << log1 << endl;
  173. ASSERT_TRUE(TC_File::isFileExist(log1));
  174. ASSERT_TRUE(TC_File::isFileExist(log2));
  175. stopServer(ws);
  176. }
  177. TEST_F(HelloTest, winServerCo)
  178. {
  179. {
  180. WinServer ws;
  181. startServer(ws, WIN_CONFIG(), TC_EpollServer::NET_THREAD_MERGE_HANDLES_THREAD);
  182. string obj = getObj(ws.getConfig(), "WinAdapter");
  183. HelloPrx prx = ws.getCommunicator()->stringToProxy<HelloPrx>(obj);
  184. string out;
  185. bool co = prx->testCoro(_buffer, out);
  186. EXPECT_FALSE(co);
  187. stopServer(ws);
  188. }
  189. {
  190. WinServer ws;
  191. startServer(ws, WIN_CONFIG(), TC_EpollServer::NET_THREAD_MERGE_HANDLES_CO);
  192. string obj = getObj(ws.getConfig(), "WinAdapter");
  193. HelloPrx prx = ws.getCommunicator()->stringToProxy<HelloPrx>(obj);
  194. string out;
  195. bool co = prx->testCoro(_buffer, out);
  196. EXPECT_TRUE(co);
  197. stopServer(ws);
  198. }
  199. {
  200. WinServer ws;
  201. startServer(ws, WIN_CONFIG(), TC_EpollServer::NET_THREAD_QUEUE_HANDLES_THREAD);
  202. string obj = getObj(ws.getConfig(), "WinAdapter");
  203. HelloPrx prx = ws.getCommunicator()->stringToProxy<HelloPrx>(obj);
  204. string out;
  205. bool co = prx->testCoro(_buffer, out);
  206. EXPECT_FALSE(co);
  207. stopServer(ws);
  208. }
  209. {
  210. WinServer ws;
  211. startServer(ws, WIN_CONFIG(), TC_EpollServer::NET_THREAD_QUEUE_HANDLES_CO);
  212. string obj = getObj(ws.getConfig(), "WinAdapter");
  213. HelloPrx prx = ws.getCommunicator()->stringToProxy<HelloPrx>(obj);
  214. string out;
  215. bool co = prx->testCoro(_buffer, out);
  216. EXPECT_TRUE(co);
  217. stopServer(ws);
  218. }
  219. }
  220. TEST_F(HelloTest, winServerHashTag)
  221. {
  222. WinServer ws;
  223. startServer(ws, WIN_CONFIG(), TC_EpollServer::NET_THREAD_MERGE_HANDLES_THREAD);
  224. string obj = getObj(ws.getConfig(), "WinAdapter");
  225. size_t pos = obj.find_first_of("@");
  226. if(pos != string::npos) {
  227. obj = obj.substr(0, pos) + "#9999" + obj.substr(pos);
  228. }else{
  229. obj += "#9999";
  230. }
  231. HelloPrx prx = ws.getCommunicator()->stringToProxy<HelloPrx>(obj);
  232. string out;
  233. bool co = prx->testCoro(_buffer, out);
  234. EXPECT_FALSE(co);
  235. stopServer(ws);
  236. }