hello_test.cpp 24 KB

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