hello_test.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035
  1. 
  2. #include "hello_test.h"
  3. /*
  4. 响应包解码函数,根据特定格式解码从服务端收到的数据,解析为BasePacket
  5. */
  6. static TC_NetWorkBuffer::PACKET_TYPE pushResponse(TC_NetWorkBuffer &in, ResponsePacket& rsp)
  7. {
  8. size_t len = sizeof(tars::Int32);
  9. if (in.getBufferLength() < len)
  10. {
  11. return TC_NetWorkBuffer::PACKET_LESS;
  12. }
  13. string header;
  14. in.getHeader(len, header);
  15. assert(header.size() == len);
  16. tars::Int32 iHeaderLen = 0;
  17. ::memcpy(&iHeaderLen, header.c_str(), sizeof(tars::Int32));
  18. iHeaderLen = ntohl(iHeaderLen);
  19. //做一下保护,长度大于M
  20. if (iHeaderLen > 100000 || iHeaderLen < (int)sizeof(unsigned int))
  21. {
  22. throw TarsDecodeException("packet length too long or too short,len:" + TC_Common::tostr(iHeaderLen));
  23. }
  24. //包没有接收全
  25. if (in.getBufferLength() < (uint32_t)iHeaderLen)
  26. {
  27. return TC_NetWorkBuffer::PACKET_LESS;
  28. }
  29. in.moveHeader(sizeof(iHeaderLen));
  30. tars::Int32 iRequestId = 0;
  31. string sRequestId;
  32. in.getHeader(sizeof(iRequestId), sRequestId);
  33. in.moveHeader(sizeof(iRequestId));
  34. rsp.iRequestId = ntohl(*((unsigned int *)(sRequestId.c_str())));
  35. len = iHeaderLen - sizeof(iHeaderLen) - sizeof(iRequestId);
  36. in.getHeader(len, rsp.sBuffer);
  37. in.moveHeader(len);
  38. return TC_NetWorkBuffer::PACKET_FULL;
  39. }
  40. /*
  41. 请求包编码函数,本函数的打包格式为
  42. 整个包长度(4字节)+iRequestId(4字节)+包内容
  43. */
  44. static shared_ptr<TC_NetWorkBuffer::Buffer> pushRequest(const RequestPacket& request, TC_Transceiver *)
  45. {
  46. shared_ptr<TC_NetWorkBuffer::Buffer> buff = std::make_shared<TC_NetWorkBuffer::Buffer>();
  47. unsigned int net_bufflength = htonl(request.sBuffer.size()+8);
  48. unsigned char * bufflengthptr = (unsigned char*)(&net_bufflength);
  49. vector<char> buffer;
  50. buffer.resize(request.sBuffer.size()+8);
  51. memcpy(buffer.data(), bufflengthptr, sizeof(unsigned int));
  52. unsigned int netrequestId = htonl(request.iRequestId);
  53. unsigned char * netrequestIdptr = (unsigned char*)(&netrequestId);
  54. memcpy(buffer.data() + sizeof(unsigned int), netrequestIdptr, sizeof(unsigned int));
  55. memcpy(buffer.data() + sizeof(unsigned int) * 2, request.sBuffer.data(), request.sBuffer.size());
  56. buff->addBuffer(buffer);
  57. return buff;
  58. }
  59. //The response packet decoding function decodes the data received from the server according to the specific format and resolves it to theBasePacket
  60. static TC_NetWorkBuffer::PACKET_TYPE customResponse(TC_NetWorkBuffer &in, ResponsePacket& rsp)
  61. {
  62. size_t len = sizeof(tars::Int32);
  63. if (in.getBufferLength() < len)
  64. {
  65. return TC_NetWorkBuffer::PACKET_LESS;
  66. }
  67. string header;
  68. in.getHeader(len, header);
  69. assert(header.size() == len);
  70. tars::Int32 iHeaderLen = 0;
  71. ::memcpy(&iHeaderLen, header.c_str(), sizeof(tars::Int32));
  72. iHeaderLen = ntohl(iHeaderLen);
  73. if (iHeaderLen > 100000 || iHeaderLen < (int)sizeof(unsigned int))
  74. {
  75. throw TarsDecodeException("packet length too long or too short,len:" + TC_Common::tostr(iHeaderLen));
  76. }
  77. if (in.getBufferLength() < (uint32_t)iHeaderLen)
  78. {
  79. return TC_NetWorkBuffer::PACKET_LESS;
  80. }
  81. in.moveHeader(sizeof(iHeaderLen));
  82. tars::Int32 iRequestId = 0;
  83. string sRequestId;
  84. in.getHeader(sizeof(iRequestId), sRequestId);
  85. in.moveHeader(sizeof(iRequestId));
  86. rsp.iRequestId = ntohl(*((unsigned int *)(sRequestId.c_str())));
  87. len = iHeaderLen - sizeof(iHeaderLen) - sizeof(iRequestId);
  88. in.getHeader(len, rsp.sBuffer);
  89. in.moveHeader(len);
  90. return TC_NetWorkBuffer::PACKET_FULL;
  91. }
  92. /*
  93. Whole package length (4 bytes) + irequestid (4 bytes) + package content
  94. */
  95. static shared_ptr<TC_NetWorkBuffer::Buffer> customRequest(RequestPacket& request, TC_Transceiver *)
  96. {
  97. shared_ptr<TC_NetWorkBuffer::Buffer> buff = std::make_shared<TC_NetWorkBuffer::Buffer>();
  98. unsigned int net_bufflength = htonl(request.sBuffer.size()+8);
  99. unsigned char * bufflengthptr = (unsigned char*)(&net_bufflength);
  100. vector<char> buffer;
  101. buffer.resize(request.sBuffer.size()+8);
  102. memcpy(buffer.data(), bufflengthptr, sizeof(unsigned int));
  103. unsigned int netrequestId = htonl(request.iRequestId);
  104. unsigned char * netrequestIdptr = (unsigned char*)(&netrequestId);
  105. memcpy(buffer.data() + sizeof(unsigned int), netrequestIdptr, sizeof(unsigned int));
  106. memcpy(buffer.data() + sizeof(unsigned int) * 2, request.sBuffer.data(), request.sBuffer.size());
  107. buff->addBuffer(buffer);
  108. return buff;
  109. }
  110. //回调函数
  111. void ClientHelloCallback::callback_testTrans(int ret, const string &r)
  112. {
  113. ASSERT_TRUE(ret == 0);
  114. ASSERT_TRUE(r == buffer);
  115. callback_count++;
  116. if(callback_count == count-1)
  117. {
  118. // int64_t cost = TC_Common::now2us() - start;
  119. // LOG_CONSOLE_DEBUG << "callback_testTrans count:" << count << ", " << cost << " us, avg:" << 1.*cost/count << "us" << endl;
  120. }
  121. }
  122. //回调函数
  123. void ClientHelloCallback::callback_testHello(int ret, const string &r)
  124. {
  125. callback_count++;
  126. if(!_prx)
  127. {
  128. ASSERT_TRUE(ret == 0);
  129. ASSERT_TRUE(r == buffer);
  130. if (callback_count == count - 1)
  131. {
  132. // int64_t cost = TC_Common::now2us() - start;
  133. // LOG_CONSOLE_DEBUG << "callback_testHello count:" << count << ", " << cost << " us, avg:" << 1. * cost / count << "us" << endl;
  134. }
  135. }
  136. else
  137. {
  138. string out;
  139. int code = _prx->testHello(0, r, out);
  140. ASSERT_TRUE(code == 0);
  141. ASSERT_TRUE(out == r);
  142. ++callback_count;
  143. }
  144. }
  145. void ClientHelloCallback::callback_testSyncTrans(tars::Int32 ret, const std::string& r)
  146. {
  147. ASSERT_TRUE(ret == 0);
  148. ASSERT_TRUE(r == buffer);
  149. callback_count++;
  150. if(callback_count == count-1)
  151. {
  152. // int64_t cost = TC_Common::now2us() - start;
  153. // LOG_CONSOLE_DEBUG << "callback_testSyncTrans count:" << count << ", " << cost << " us, avg:" << 1.*cost/count << "us" << endl;
  154. }
  155. }
  156. void HelloTest::startServer(HelloServer &server, TC_EpollServer::SERVER_OPEN_COROUTINE openCoroutine)
  157. {
  158. TC_Config conf = CONFIG();
  159. conf.set("/tars/application/server<opencoroutine>", TC_Common::tostr(openCoroutine));
  160. ASSERT_TRUE(conf.get("/tars/application/server<opencoroutine>") == TC_Common::tostr(openCoroutine));
  161. server.main(conf.tostr());
  162. ASSERT_TRUE(ServerConfig::OpenCoroutine == openCoroutine);
  163. server.start();
  164. server.waitForReady();
  165. }
  166. TC_Endpoint HelloTest::getEndpoint(const string &adapter)
  167. {
  168. return TC_Endpoint(_conf.get("/tars/application/server/" + adapter + "<endpoint>"));
  169. }
  170. void HelloTest::wait(int millseconds)
  171. {
  172. if(TC_CoroutineScheduler::scheduler())
  173. {
  174. TC_CoroutineScheduler::scheduler()->sleep(millseconds);
  175. }
  176. else
  177. {
  178. TC_Common::msleep(millseconds);
  179. }
  180. }
  181. void HelloTest::syncCustom(Communicator *comm, const string &adapter )
  182. {
  183. ServantPrx prx = getObj<ServantPrx>(comm, adapter);
  184. ProxyProtocol prot;
  185. prot.requestFunc = customRequest;
  186. prot.responseFunc = customResponse;
  187. prx->tars_set_protocol(prot);
  188. int64_t start = TC_Common::now2us();
  189. string out;
  190. //发起远程调用
  191. for (int j = 0; j < _count; ++j) {
  192. ResponsePacket rsp;
  193. prx->rpc_call(prx->tars_gen_requestid(), "doCustomFunc", _buffer.c_str(), _buffer.length(), rsp);
  194. }
  195. int64_t cost = TC_Common::now2us() - start;
  196. LOG_CONSOLE_DEBUG << "count:" << _count << ", " << cost << " us, avg:" << 1.*cost/_count << "us" << endl;
  197. }
  198. void HelloTest::asyncCustom(Communicator *comm, const string &adapter)
  199. {
  200. ServantPrx prx = getObj<ServantPrx>(comm, adapter);
  201. ProxyProtocol prot;
  202. prot.requestFunc = customRequest;
  203. prot.responseFunc = customResponse;
  204. prx->tars_set_protocol(prot);
  205. atomic<int> callback_count{0};
  206. //发起远程调用
  207. for (int j = 0; j < _count; ++j)
  208. {
  209. CustomCallBackPtr cb = new CustomCallBack(callback_count);
  210. prx->rpc_call_async(prx->tars_gen_requestid(), "doCustomFunc", _buffer.c_str(), _buffer.length(), cb);
  211. }
  212. waitForFinish(callback_count, _count);
  213. ASSERT_TRUE(callback_count == _count);
  214. }
  215. void HelloTest::testPush(Communicator *comm, const string &adapter)
  216. {
  217. ServantPrx prx = getObj<ServantPrx>(comm, adapter);
  218. ProxyProtocol prot;
  219. prot.requestFunc = pushRequest;
  220. prot.responseFunc = pushResponse;
  221. prx->tars_set_protocol(prot);
  222. PushCallBackPtr cbPush = new PushCallBack();
  223. prx->tars_set_push_callback(cbPush);
  224. string buf("heartbeat");
  225. int count = 2;
  226. while(count-- > 0)
  227. {
  228. cbPush->_onconnect = false;
  229. cbPush->_onclose = false;
  230. PushCallBackPtr cb = new PushCallBack();
  231. prx->rpc_call_async(prx->tars_gen_requestid(), "printResult", buf.c_str(), buf.length(), cb);
  232. wait(1000);
  233. ASSERT_TRUE(cbPush->_onconnect);
  234. ASSERT_TRUE(cb->_onprintresult);
  235. wait(10000);
  236. ASSERT_TRUE(cbPush->_onclose);
  237. }
  238. }
  239. void HelloTest::testReconnect(Communicator *comm, const string &adapter )
  240. {
  241. ServantPrx prx = getObj<ServantPrx>(comm, adapter);
  242. ProxyProtocol prot;
  243. prot.requestFunc = pushRequest;
  244. prot.responseFunc = pushResponse;
  245. prx->tars_set_protocol(prot);
  246. prx->tars_reconnect(1);
  247. PushCallBackPtr cbPush = new PushCallBack();
  248. prx->tars_set_push_callback(cbPush);
  249. string buf("heartbeat");
  250. cbPush->_onconnect = false;
  251. cbPush->_onclose = false;
  252. ResponsePacket rsp;
  253. prx->rpc_call(prx->tars_gen_requestid(), "printResult", buf.c_str(), buf.length(), rsp);
  254. TC_Common::msleep(10500);
  255. ASSERT_TRUE(cbPush->_onclose);
  256. TC_Common::msleep(2000);
  257. ASSERT_TRUE(cbPush->_onconnect);
  258. }
  259. void HelloTest::checkWup()
  260. {
  261. UniPacket<> req;
  262. req.setRequestId(1);
  263. req.setServantName("TestApp.HelloServer.HelloObj");
  264. req.setFuncName("testHello");
  265. req.put<int>("index", 1);
  266. req.put<string>("s", _buffer);
  267. string buff = "";
  268. req.encode(buff);
  269. // int64_t start = TC_Common::now2us();
  270. TC_Endpoint ep = getEndpoint("HelloAdapter");
  271. //可以直接发送请求给HelloServer 或者 发送请求给TranServer
  272. TC_TCPClient client;
  273. client.init(ep.getHost(), ep.getPort(), 3000);
  274. for (int i = 0; i < _count; i++)
  275. {
  276. char recvBuff[1024] = { 0 };
  277. size_t recvLen = sizeof(recvBuff);
  278. int ret = client.sendRecv(buff.c_str(), buff.length(), recvBuff, recvLen);
  279. ASSERT_TRUE(ret == 0);
  280. TarsUniPacket<> rsp;
  281. rsp.decode(recvBuff, recvLen);
  282. string retStr = "";
  283. ret = rsp.getTarsResultCode();
  284. ASSERT_TRUE(ret == 0);
  285. rsp.get("r", retStr);
  286. ASSERT_TRUE(retStr == _buffer);
  287. }
  288. // int64_t cost = TC_Common::now2us() - start;
  289. // LOG_CONSOLE_DEBUG << "count:" << _count << ", " << cost << " us, avg:" << 1.*cost/_count << "us" << endl;
  290. }
  291. void HelloTest::checkSyncHttp(Communicator *comm, const string &adapter, bool close)
  292. {
  293. ServantPrx prx = getObj<ServantPrx>(comm, adapter);
  294. prx->tars_set_protocol(ServantProxy::PROTOCOL_HTTP1, 10);
  295. int64_t start = TC_Common::now2us();
  296. string out;
  297. //发起远程调用
  298. for (int j = 0; j < _count; ++j) {
  299. string buff = _buffer + "-" + TC_Common::tostr(j) + "-" + TC_Common::tostr(TC_Thread::CURRENT_THREADID());
  300. shared_ptr<TC_HttpRequest> req = std::make_shared<TC_HttpRequest>();
  301. req->setPostRequest("http://tars.com/hello", buff, true);
  302. if(close)
  303. {
  304. req->setHeader("Connection", "close");
  305. }
  306. else
  307. {
  308. req->setHeader("Connection", "keep-alive");
  309. }
  310. shared_ptr<TC_HttpResponse> rsp;
  311. prx->http_call("hello", req, rsp);
  312. ASSERT_TRUE(req->getContent() == rsp->getContent());
  313. }
  314. int64_t cost = TC_Common::now2us() - start;
  315. LOG_CONSOLE_DEBUG << "count:" << _count << ", " << cost << " us, avg:" << 1.*cost/_count << "us" << endl;
  316. }
  317. void HelloTest::checkASyncHttp(Communicator *comm, const string &adapter, bool close)
  318. {
  319. atomic<int> callback_count{0};
  320. ServantPrx prx = getObj<ServantPrx>(comm, adapter);
  321. prx->tars_set_protocol(ServantProxy::PROTOCOL_HTTP1, 10);
  322. //发起远程调用
  323. for (int j = 0; j < _count; ++j)
  324. {
  325. string buff = _buffer + "-" + TC_Common::tostr(j) + "-" + TC_Common::tostr(TC_Thread::CURRENT_THREADID());
  326. shared_ptr<TC_HttpRequest> req = std::make_shared<TC_HttpRequest>();
  327. req->setPostRequest("http://tars.com/hello", buff, true);
  328. if(close)
  329. {
  330. req->setHeader("Connection", "close");
  331. }
  332. else
  333. {
  334. req->setHeader("Connection", "keep-alive");
  335. }
  336. HttpCallbackPtr p = new HelloHttpCallback(TC_Common::now2us(), j, _count, buff, callback_count);
  337. prx->http_call_async("hello", req, p);
  338. }
  339. waitForFinish(callback_count, _count);
  340. ASSERT_TRUE(callback_count == _count);
  341. }
  342. void HelloTest::checkSyncOnce(HelloPrx prx)
  343. {
  344. // HelloPrx prx = getObj<HelloPrx>(comm, adapter);
  345. // int64_t start = TC_Common::now2us();
  346. string out;
  347. //发起远程调用
  348. prx->testHello(0, _buffer, out);
  349. ASSERT_TRUE(_buffer == out);
  350. }
  351. void HelloTest::checkASyncOnce(HelloPrx prx)
  352. {
  353. // HelloPrx prx = getObj<HelloPrx>(comm, adapter);
  354. atomic<int> callback_count{0};
  355. HelloPrxCallbackPtr p = new ClientHelloCallback(prx, callback_count);
  356. prx->async_testHello(p, 0, _buffer);
  357. wait(100);
  358. ASSERT_TRUE(callback_count == 2);
  359. }
  360. void HelloTest::checkSync(Communicator *comm, const string &adapter)
  361. {
  362. HelloPrx prx = getObj<HelloPrx>(comm, adapter);
  363. int64_t start = TC_Common::now2us();
  364. string out;
  365. //发起远程调用
  366. for (int j = 0; j < _count; ++j) {
  367. prx->testHello(j, _buffer, out);
  368. ASSERT_TRUE(_buffer == out);
  369. }
  370. int64_t cost = TC_Common::now2us() - start;
  371. LOG_CONSOLE_DEBUG << "count:" << _count << ", " << cost << " us, avg:" << 1.*cost/_count << "us" << endl;
  372. }
  373. void HelloTest::waitForFinish(atomic<int> &callback_count, int count)
  374. {
  375. int lastCallbackCount = 0;
  376. while (callback_count != count)
  377. {
  378. wait(100);
  379. if(lastCallbackCount == callback_count)
  380. {
  381. LOG_CONSOLE_DEBUG << "no callback: " << callback_count << ", " << count << endl;
  382. }
  383. lastCallbackCount = callback_count;
  384. }
  385. // LOG_CONSOLE_DEBUG << "callback: " << callback_count << ", " << _count << endl;
  386. }
  387. void HelloTest::checkASync(Communicator *comm, const string &adapter)
  388. {
  389. atomic<int> callback_count{0};
  390. HelloPrx prx = getObj<HelloPrx>(comm, adapter);
  391. //发起远程调用
  392. for (int j = 0; j < _count; ++j)
  393. {
  394. HelloPrxCallbackPtr p = new ClientHelloCallback(TC_Common::now2us(), j, _count, _buffer, callback_count);
  395. prx->async_testHello(p, j, _buffer);
  396. }
  397. waitForFinish(callback_count, _count);
  398. ASSERT_TRUE(callback_count == _count);
  399. }
  400. void HelloTest::checkWupTransSync(Communicator *comm)
  401. {
  402. ServantPrx prx = getObj<ServantPrx>(comm, "TransWupAdapter");
  403. UniPacket<> req;
  404. req.setRequestId(1);
  405. req.setServantName("TestApp.HelloServer.TransDstObj");
  406. req.setFuncName("testHello");
  407. req.put<string>("s", _buffer);
  408. ProxyProtocol proto;
  409. proto.requestFunc = ProxyProtocol::streamRequest;
  410. proto.responseFunc = ProxyProtocol::tupResponse;
  411. prx->tars_set_protocol(proto);
  412. int64_t start = TC_Common::now2us();
  413. //发起远程调用
  414. for (int i = 0; i < _count; ++i)
  415. {
  416. req.setRequestId(prx->tars_gen_requestid());
  417. req.put<int>("index", i);
  418. vector<char> buff;
  419. req.encode(buff);
  420. ResponsePacket done;
  421. prx->rpc_call(req.getRequestId(), "testHello", buff.data(), buff.size(), done);
  422. TarsUniPacket<> rsp;
  423. rsp.decode(done.sBuffer.data(), done.sBuffer.size());
  424. string retStr = "";
  425. int ret = rsp.getTarsResultCode();
  426. ASSERT_TRUE(ret == 0);
  427. rsp.get("r", retStr);
  428. ASSERT_TRUE(retStr == _buffer);
  429. }
  430. int64_t cost = TC_Common::now2us() - start;
  431. LOG_CONSOLE_DEBUG << "count:" << _count << ", " << cost << " us, avg:" << 1.*cost/_count << "us" << endl;
  432. }
  433. void HelloTest::checkTransSyncASync(Communicator *comm)
  434. {
  435. HelloPrx prx = getObj<HelloPrx>(comm, "TransAdapter");
  436. int64_t start = TC_Common::now2us();
  437. string out;
  438. //发起远程调用
  439. for (int j = 0; j < _count; ++j) {
  440. prx->testTrans(j, _buffer, out);
  441. ASSERT_TRUE(_buffer == out);
  442. }
  443. int64_t cost = TC_Common::now2us() - start;
  444. LOG_CONSOLE_DEBUG << "count:" << _count << ", " << cost << " us, avg:" << 1.*cost/_count << "us" << endl;
  445. }
  446. void HelloTest::checkTransASyncASync(Communicator *comm)
  447. {
  448. string adapter = "TransAdapter";
  449. atomic<int> callback_count{0};
  450. HelloPrx prx = getObj<HelloPrx>(comm, adapter);
  451. //发起远程调用
  452. for (int j = 0; j < _count; ++j)
  453. {
  454. HelloPrxCallbackPtr p = new ClientHelloCallback(TC_Common::now2us(), j, _count, _buffer, callback_count);
  455. prx->async_testTrans(p, j, _buffer);
  456. }
  457. waitForFinish(callback_count, _count);
  458. ASSERT_TRUE(callback_count == _count);
  459. }
  460. void HelloTest::checkTransSyncSync(Communicator *comm)
  461. {
  462. HelloPrx prx = getObj<HelloPrx>(comm, "TransAdapter");
  463. int64_t start = TC_Common::now2us();
  464. string out;
  465. //发起远程调用
  466. for (int j = 0; j < _count; ++j) {
  467. prx->testSyncTrans(j, _buffer, out);
  468. ASSERT_TRUE(_buffer == out);
  469. }
  470. int64_t cost = TC_Common::now2us() - start;
  471. LOG_CONSOLE_DEBUG << "count:" << _count << ", " << cost << " us, avg:" << 1.*cost/_count << "us" << endl;
  472. }
  473. void HelloTest::checkTransASyncSync(Communicator *comm)
  474. {
  475. string adapter = "TransAdapter";
  476. atomic<int> callback_count{0};
  477. HelloPrx prx = getObj<HelloPrx>(comm, adapter);
  478. //发起远程调用
  479. for (int j = 0; j < _count; ++j)
  480. {
  481. HelloPrxCallbackPtr p = new ClientHelloCallback(TC_Common::now2us(), j, _count, _buffer, callback_count);
  482. prx->async_testSyncTrans(p, j, _buffer);
  483. }
  484. waitForFinish(callback_count, _count);
  485. ASSERT_TRUE(callback_count == _count);
  486. }
  487. void HelloTest::rpcFromRegistry(Communicator *comm)
  488. {
  489. //test get ip from registry
  490. HelloPrx qPrx = comm->stringToProxy<HelloPrx>("TestApp.RpcServer.HelloObj");
  491. string out;
  492. int ret = qPrx->testHello(0, _buffer, out);
  493. ASSERT_TRUE(ret == 0);
  494. ASSERT_TRUE(out == _buffer);
  495. //test rpc loop
  496. int count = 10;
  497. while(count-- > 0)
  498. {
  499. int ret;
  500. string out1, out2;
  501. ret = qPrx->testPid(out1);
  502. ASSERT_TRUE(ret == 0);
  503. ret = qPrx->testPid(out2);
  504. ASSERT_TRUE(ret == 0);
  505. ASSERT_TRUE(out1 != out2);
  506. }
  507. //test rpc hash
  508. count = 10;
  509. while(count-- > 0)
  510. {
  511. vector<string> vout;
  512. std::thread hash([&](){
  513. string out;
  514. int ret = qPrx->tars_hash(TC_Thread::CURRENT_THREADID())->testPid(out);
  515. vout.push_back(out);
  516. ASSERT_TRUE(ret == 0);
  517. });
  518. hash.join();
  519. for(size_t i = 0; i < vout.size()-1; i++)
  520. {
  521. ASSERT_TRUE(vout[i] == vout[i+1]);
  522. }
  523. }
  524. }
  525. void HelloTest::rpcConHashFromRegistry(Communicator *comm)
  526. {
  527. HelloPrx prx = comm->stringToProxy<HelloPrx>("TestApp.RpcServer.HelloObj");
  528. //test rpc consistent hash
  529. int count = 10;
  530. while(count-- > 0)
  531. {
  532. std::thread conHash([&](){
  533. std::set<std::string> servInfos;
  534. for (int j = 0; j < 10; ++j) {
  535. std::string serverInfo;
  536. int ret = prx->tars_consistent_hash(TC_Thread::CURRENT_THREADID())->testConHash(serverInfo);
  537. LOG_CONSOLE_DEBUG << "hashCode:" << TC_Thread::CURRENT_THREADID() << ", serverInfo:" << serverInfo << endl;
  538. servInfos.emplace(serverInfo);
  539. ASSERT_TRUE(ret == 0);
  540. }
  541. ASSERT_TRUE(servInfos.size() == 1);
  542. });
  543. conHash.join();
  544. }
  545. }
  546. void HelloTest::checkSyncTimeout(Communicator *comm)
  547. {
  548. HelloPrx prx = getObj<HelloPrx>(comm, "HelloAdapter");
  549. prx->tars_set_timeout(2000);
  550. prx->tars_async_timeout(3000);
  551. prx->testTimeout(1);
  552. ASSERT_TRUE(true);
  553. try
  554. {
  555. prx->testTimeout(4);
  556. }
  557. catch (exception& ex)
  558. {
  559. ASSERT_TRUE(true);
  560. }
  561. }
  562. void HelloTest::checkASyncTimeout(Communicator *comm)
  563. {
  564. HelloPrx prx = getObj<HelloPrx>(comm, "HelloAdapter");
  565. prx->tars_set_timeout(2000);
  566. prx->tars_async_timeout(3000);
  567. std::atomic<int> callback_count;
  568. ClientHelloCallback *c = new ClientHelloCallback(TC_Common::now2us(), 0, _count, _buffer, callback_count);
  569. HelloPrxCallbackPtr p = c;
  570. prx->async_testTimeout(p, 1);
  571. wait(1500);
  572. ASSERT_TRUE(c->callback);
  573. c->callback = false;
  574. prx->async_testTimeout(p, 5);
  575. wait(4000);
  576. ASSERT_FALSE(c->callback);
  577. ASSERT_TRUE(c->callback_exception);
  578. }
  579. void HelloTest::checkStat(Communicator *comm, int reportCount)
  580. {
  581. HelloPrx prx = getObj<HelloPrx>(comm, "HelloAdapter");
  582. prx->tars_set_timeout(2000);
  583. prx->tars_async_timeout(3000);
  584. int count = reportCount;
  585. while(count-- > 0)
  586. {
  587. string out;
  588. prx->testHello(count, _buffer, out);
  589. TC_Common::sleep(1);
  590. }
  591. }
  592. void HelloTest::forEach(function<void()> func)
  593. {
  594. int i = 2;
  595. for(i = 0; i <= TC_EpollServer::NET_THREAD_MERGE_HANDLES_CO; i++)
  596. {
  597. HelloServer server;
  598. startServer(server, (TC_EpollServer::SERVER_OPEN_COROUTINE) i);
  599. func();
  600. stopServer(server);
  601. }
  602. }
  603. void HelloTest::forEach(function<void(Communicator *comm)> func)
  604. {
  605. int i = 0;//TC_EpollServer::NET_THREAD_MERGE_HANDLES_CO;
  606. for(i = 0; i <= TC_EpollServer::NET_THREAD_MERGE_HANDLES_CO; i++)
  607. {
  608. HelloServer server;
  609. startServer(server, (TC_EpollServer::SERVER_OPEN_COROUTINE) i);
  610. func(server.getCommunicator().get());
  611. stopServer(server);
  612. }
  613. }
  614. void HelloTest::forEachInCoroutine(function<void(Communicator *comm)> func)
  615. {
  616. int i = 0;
  617. for(i = 0; i <= TC_EpollServer::NET_THREAD_MERGE_HANDLES_CO; i++)
  618. {
  619. HelloServer server;
  620. startServer(server, (TC_EpollServer::SERVER_OPEN_COROUTINE) i);
  621. {
  622. std::thread cor_call([&]()
  623. {
  624. auto scheduler = TC_CoroutineScheduler::create();
  625. //设置到协程中
  626. ServantProxyThreadData::getData()->_sched = scheduler;
  627. scheduler->go([&]()
  628. {
  629. scheduler->setNoCoroutineCallback([=](TC_CoroutineScheduler *s)
  630. {
  631. s->terminate();
  632. });
  633. func(server.getCommunicator().get());
  634. });
  635. scheduler->run();
  636. });
  637. cor_call.join();
  638. }
  639. stopServer(server);
  640. }
  641. }
  642. void HelloTest::funcInCoroutine(function<void()> func, bool setSched)
  643. {
  644. std::thread cor_call([&]()
  645. {
  646. auto scheduler = TC_CoroutineScheduler::create();
  647. if(setSched)
  648. {
  649. //设置到协程中
  650. ServantProxyThreadData::getData()->_sched = scheduler;
  651. }
  652. scheduler->go([&]()
  653. {
  654. scheduler->setNoCoroutineCallback([=](TC_CoroutineScheduler *s)
  655. { s->terminate(); });
  656. func();
  657. });
  658. scheduler->run();
  659. });
  660. cor_call.join();
  661. }
  662. void HelloTest::transGlobalCommunicator(function<void(Communicator *)> func, Communicator *comm)
  663. {
  664. forEach([&](Communicator *c){
  665. func(comm);
  666. });
  667. // LOG_CONSOLE_DEBUG << "succ" << endl;
  668. }
  669. void HelloTest::transServerCommunicator(function<void(Communicator *)> func)
  670. {
  671. forEach([&](Communicator *c){
  672. func(c);
  673. });
  674. // LOG_CONSOLE_DEBUG << "succ" << endl;
  675. }
  676. void HelloTest::transAllocCommunicator(function<void(Communicator *)> func)
  677. {
  678. forEach([&](Communicator *c){
  679. shared_ptr<Communicator> comm = std::make_shared<Communicator>();
  680. func(comm.get());
  681. });
  682. // LOG_CONSOLE_DEBUG << "succ" << endl;
  683. }
  684. void HelloTest::transComplexCommunicator(function<void(Communicator *)> func, Communicator *comm)
  685. {
  686. forEach([&](Communicator *c){
  687. shared_ptr<Communicator> allocComm = std::make_shared<Communicator>();
  688. std::thread thAlloc1([&](){
  689. func(allocComm.get());
  690. });
  691. std::thread thServer1([&](){
  692. func(c);
  693. });
  694. std::thread thGlobal1([&](){
  695. func(c);
  696. });
  697. thAlloc1.join();
  698. thServer1.join();
  699. thGlobal1.join();
  700. });
  701. // LOG_CONSOLE_DEBUG << "succ" << endl;
  702. }
  703. void HelloTest::transInCoroutineGlobalCommunicator(function<void(Communicator *)> func, Communicator *comm)
  704. {
  705. forEachInCoroutine([&](Communicator *c){
  706. func(comm);
  707. });
  708. // LOG_CONSOLE_DEBUG << "succ" << endl;
  709. }
  710. void HelloTest::transInCoroutineServerCommunicator(function<void(Communicator *)> func)
  711. {
  712. forEachInCoroutine([&](Communicator *c){
  713. func(c);
  714. });
  715. // LOG_CONSOLE_DEBUG << "succ" << endl;
  716. }
  717. void HelloTest::transInCoroutineAllocCommunicator(function<void(Communicator *)> func)
  718. {
  719. forEachInCoroutine([&](Communicator *c){
  720. shared_ptr<Communicator> comm = std::make_shared<Communicator>();
  721. func(comm.get());
  722. });
  723. // LOG_CONSOLE_DEBUG << "succ" << endl;
  724. }
  725. void HelloTest::transInCoroutineComplexCommunicator(function<void(Communicator *)> func, Communicator *comm)
  726. {
  727. forEachInCoroutine([&](Communicator *c)
  728. {
  729. {
  730. shared_ptr<Communicator> allocComm = std::make_shared<Communicator>();
  731. std::thread thAlloc1([&]()
  732. { func(allocComm.get()); });
  733. std::thread thServer1([&]()
  734. { func(c); });
  735. std::thread thGlobal1([&]()
  736. { func(c); });
  737. thAlloc1.join();
  738. thServer1.join();
  739. thGlobal1.join();
  740. }
  741. });
  742. // LOG_CONSOLE_DEBUG << "succ" << endl;
  743. }
  744. shared_ptr<Communicator> HelloTest::getCommunicator()
  745. {
  746. shared_ptr<Communicator> c = std::make_shared<Communicator>();
  747. TC_Config conf = CLIENT_CONFIG();
  748. c->setProperty(conf);
  749. c->setProperty("sendqueuelimit", "1000000");
  750. return c;
  751. }