hello_test.cpp 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  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-- > 10)
  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-- > 10)
  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::checkSyncTimeout(Communicator *comm)
  526. {
  527. HelloPrx prx = getObj<HelloPrx>(comm, "HelloAdapter");
  528. prx->tars_set_timeout(2000);
  529. prx->tars_async_timeout(3000);
  530. prx->testTimeout(1);
  531. ASSERT_TRUE(true);
  532. try
  533. {
  534. prx->testTimeout(4);
  535. }
  536. catch (exception& ex)
  537. {
  538. ASSERT_TRUE(true);
  539. }
  540. }
  541. void HelloTest::checkASyncTimeout(Communicator *comm)
  542. {
  543. HelloPrx prx = getObj<HelloPrx>(comm, "HelloAdapter");
  544. prx->tars_set_timeout(2000);
  545. prx->tars_async_timeout(3000);
  546. std::atomic<int> callback_count;
  547. ClientHelloCallback *c = new ClientHelloCallback(TC_Common::now2us(), 0, _count, _buffer, callback_count);
  548. HelloPrxCallbackPtr p = c;
  549. prx->async_testTimeout(p, 1);
  550. wait(1500);
  551. ASSERT_TRUE(c->callback);
  552. c->callback = false;
  553. prx->async_testTimeout(p, 5);
  554. wait(4000);
  555. ASSERT_FALSE(c->callback);
  556. ASSERT_TRUE(c->callback_exception);
  557. }
  558. void HelloTest::checkStat(Communicator *comm, int reportCount)
  559. {
  560. HelloPrx prx = getObj<HelloPrx>(comm, "HelloAdapter");
  561. prx->tars_set_timeout(2000);
  562. prx->tars_async_timeout(3000);
  563. int count = reportCount;
  564. while(count-- > 0)
  565. {
  566. string out;
  567. prx->testHello(count, _buffer, out);
  568. TC_Common::sleep(1);
  569. }
  570. }
  571. void HelloTest::forEach(function<void()> func)
  572. {
  573. int i = 2;
  574. for(i = 0; i <= TC_EpollServer::NET_THREAD_MERGE_HANDLES_CO; i++)
  575. {
  576. HelloServer server;
  577. startServer(server, (TC_EpollServer::SERVER_OPEN_COROUTINE) i);
  578. func();
  579. stopServer(server);
  580. }
  581. }
  582. void HelloTest::forEach(function<void(Communicator *comm)> func)
  583. {
  584. int i = 0;//TC_EpollServer::NET_THREAD_MERGE_HANDLES_CO;
  585. for(i = 0; i <= TC_EpollServer::NET_THREAD_MERGE_HANDLES_CO; i++)
  586. {
  587. HelloServer server;
  588. startServer(server, (TC_EpollServer::SERVER_OPEN_COROUTINE) i);
  589. func(server.getCommunicator().get());
  590. stopServer(server);
  591. }
  592. }
  593. void HelloTest::forEachInCoroutine(function<void(Communicator *comm)> func)
  594. {
  595. int i = 0;
  596. for(i = 0; i <= TC_EpollServer::NET_THREAD_MERGE_HANDLES_CO; i++)
  597. {
  598. HelloServer server;
  599. startServer(server, (TC_EpollServer::SERVER_OPEN_COROUTINE) i);
  600. {
  601. std::thread cor_call([&]()
  602. {
  603. auto scheduler = TC_CoroutineScheduler::create();
  604. //设置到协程中
  605. ServantProxyThreadData::getData()->_sched = scheduler;
  606. scheduler->go([&]()
  607. {
  608. scheduler->setNoCoroutineCallback([=](TC_CoroutineScheduler *s)
  609. {
  610. s->terminate();
  611. });
  612. func(server.getCommunicator().get());
  613. });
  614. scheduler->run();
  615. });
  616. cor_call.join();
  617. }
  618. stopServer(server);
  619. }
  620. }
  621. void HelloTest::funcInCoroutine(function<void()> func, bool setSched)
  622. {
  623. std::thread cor_call([&]()
  624. {
  625. auto scheduler = TC_CoroutineScheduler::create();
  626. if(setSched)
  627. {
  628. //设置到协程中
  629. ServantProxyThreadData::getData()->_sched = scheduler;
  630. }
  631. scheduler->go([&]()
  632. {
  633. scheduler->setNoCoroutineCallback([=](TC_CoroutineScheduler *s)
  634. { s->terminate(); });
  635. func();
  636. });
  637. scheduler->run();
  638. });
  639. cor_call.join();
  640. }
  641. void HelloTest::transGlobalCommunicator(function<void(Communicator *)> func, Communicator *comm)
  642. {
  643. forEach([&](Communicator *c){
  644. func(comm);
  645. });
  646. // LOG_CONSOLE_DEBUG << "succ" << endl;
  647. }
  648. void HelloTest::transServerCommunicator(function<void(Communicator *)> func)
  649. {
  650. forEach([&](Communicator *c){
  651. func(c);
  652. });
  653. // LOG_CONSOLE_DEBUG << "succ" << endl;
  654. }
  655. void HelloTest::transAllocCommunicator(function<void(Communicator *)> func)
  656. {
  657. forEach([&](Communicator *c){
  658. shared_ptr<Communicator> comm = std::make_shared<Communicator>();
  659. func(comm.get());
  660. });
  661. // LOG_CONSOLE_DEBUG << "succ" << endl;
  662. }
  663. void HelloTest::transComplexCommunicator(function<void(Communicator *)> func, Communicator *comm)
  664. {
  665. forEach([&](Communicator *c){
  666. shared_ptr<Communicator> allocComm = std::make_shared<Communicator>();
  667. std::thread thAlloc1([&](){
  668. func(allocComm.get());
  669. });
  670. std::thread thServer1([&](){
  671. func(c);
  672. });
  673. std::thread thGlobal1([&](){
  674. func(c);
  675. });
  676. thAlloc1.join();
  677. thServer1.join();
  678. thGlobal1.join();
  679. });
  680. // LOG_CONSOLE_DEBUG << "succ" << endl;
  681. }
  682. void HelloTest::transInCoroutineGlobalCommunicator(function<void(Communicator *)> func, Communicator *comm)
  683. {
  684. forEachInCoroutine([&](Communicator *c){
  685. func(comm);
  686. });
  687. // LOG_CONSOLE_DEBUG << "succ" << endl;
  688. }
  689. void HelloTest::transInCoroutineServerCommunicator(function<void(Communicator *)> func)
  690. {
  691. forEachInCoroutine([&](Communicator *c){
  692. func(c);
  693. });
  694. // LOG_CONSOLE_DEBUG << "succ" << endl;
  695. }
  696. void HelloTest::transInCoroutineAllocCommunicator(function<void(Communicator *)> func)
  697. {
  698. forEachInCoroutine([&](Communicator *c){
  699. shared_ptr<Communicator> comm = std::make_shared<Communicator>();
  700. func(comm.get());
  701. });
  702. // LOG_CONSOLE_DEBUG << "succ" << endl;
  703. }
  704. void HelloTest::transInCoroutineComplexCommunicator(function<void(Communicator *)> func, Communicator *comm)
  705. {
  706. forEachInCoroutine([&](Communicator *c)
  707. {
  708. {
  709. shared_ptr<Communicator> allocComm = std::make_shared<Communicator>();
  710. std::thread thAlloc1([&]()
  711. { func(allocComm.get()); });
  712. std::thread thServer1([&]()
  713. { func(c); });
  714. std::thread thGlobal1([&]()
  715. { func(c); });
  716. thAlloc1.join();
  717. thServer1.join();
  718. thGlobal1.join();
  719. }
  720. });
  721. // LOG_CONSOLE_DEBUG << "succ" << endl;
  722. }
  723. shared_ptr<Communicator> HelloTest::getCommunicator()
  724. {
  725. shared_ptr<Communicator> c = std::make_shared<Communicator>();
  726. TC_Config conf = CLIENT_CONFIG();
  727. c->setProperty(conf);
  728. c->setProperty("sendqueuelimit", "1000000");
  729. return c;
  730. }