12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010 |
-
- #include "hello_test.h"
- /*
- 响应包解码函数,根据特定格式解码从服务端收到的数据,解析为BasePacket
- */
- static TC_NetWorkBuffer::PACKET_TYPE pushResponse(TC_NetWorkBuffer &in, ResponsePacket& rsp)
- {
- size_t len = sizeof(tars::Int32);
- if (in.getBufferLength() < len)
- {
- return TC_NetWorkBuffer::PACKET_LESS;
- }
- string header;
- in.getHeader(len, header);
- assert(header.size() == len);
- tars::Int32 iHeaderLen = 0;
- ::memcpy(&iHeaderLen, header.c_str(), sizeof(tars::Int32));
- iHeaderLen = ntohl(iHeaderLen);
- //做一下保护,长度大于M
- if (iHeaderLen > 100000 || iHeaderLen < (int)sizeof(unsigned int))
- {
- throw TarsDecodeException("packet length too long or too short,len:" + TC_Common::tostr(iHeaderLen));
- }
- //包没有接收全
- if (in.getBufferLength() < (uint32_t)iHeaderLen)
- {
- return TC_NetWorkBuffer::PACKET_LESS;
- }
- in.moveHeader(sizeof(iHeaderLen));
- tars::Int32 iRequestId = 0;
- string sRequestId;
- in.getHeader(sizeof(iRequestId), sRequestId);
- in.moveHeader(sizeof(iRequestId));
- rsp.iRequestId = ntohl(*((unsigned int *)(sRequestId.c_str())));
- len = iHeaderLen - sizeof(iHeaderLen) - sizeof(iRequestId);
- in.getHeader(len, rsp.sBuffer);
- in.moveHeader(len);
- return TC_NetWorkBuffer::PACKET_FULL;
- }
- /*
- 请求包编码函数,本函数的打包格式为
- 整个包长度(4字节)+iRequestId(4字节)+包内容
- */
- static shared_ptr<TC_NetWorkBuffer::Buffer> pushRequest(const RequestPacket& request, TC_Transceiver *)
- {
- shared_ptr<TC_NetWorkBuffer::Buffer> buff = std::make_shared<TC_NetWorkBuffer::Buffer>();
- unsigned int net_bufflength = htonl(request.sBuffer.size()+8);
- unsigned char * bufflengthptr = (unsigned char*)(&net_bufflength);
- vector<char> buffer;
- buffer.resize(request.sBuffer.size()+8);
- memcpy(buffer.data(), bufflengthptr, sizeof(unsigned int));
- unsigned int netrequestId = htonl(request.iRequestId);
- unsigned char * netrequestIdptr = (unsigned char*)(&netrequestId);
- memcpy(buffer.data() + sizeof(unsigned int), netrequestIdptr, sizeof(unsigned int));
- memcpy(buffer.data() + sizeof(unsigned int) * 2, request.sBuffer.data(), request.sBuffer.size());
- buff->addBuffer(buffer);
- return buff;
- }
- //The response packet decoding function decodes the data received from the server according to the specific format and resolves it to theBasePacket
- static TC_NetWorkBuffer::PACKET_TYPE customResponse(TC_NetWorkBuffer &in, ResponsePacket& rsp)
- {
- size_t len = sizeof(tars::Int32);
- if (in.getBufferLength() < len)
- {
- return TC_NetWorkBuffer::PACKET_LESS;
- }
- string header;
- in.getHeader(len, header);
- assert(header.size() == len);
- tars::Int32 iHeaderLen = 0;
- ::memcpy(&iHeaderLen, header.c_str(), sizeof(tars::Int32));
- iHeaderLen = ntohl(iHeaderLen);
- if (iHeaderLen > 100000 || iHeaderLen < (int)sizeof(unsigned int))
- {
- throw TarsDecodeException("packet length too long or too short,len:" + TC_Common::tostr(iHeaderLen));
- }
- if (in.getBufferLength() < (uint32_t)iHeaderLen)
- {
- return TC_NetWorkBuffer::PACKET_LESS;
- }
- in.moveHeader(sizeof(iHeaderLen));
- tars::Int32 iRequestId = 0;
- string sRequestId;
- in.getHeader(sizeof(iRequestId), sRequestId);
- in.moveHeader(sizeof(iRequestId));
- rsp.iRequestId = ntohl(*((unsigned int *)(sRequestId.c_str())));
- len = iHeaderLen - sizeof(iHeaderLen) - sizeof(iRequestId);
- in.getHeader(len, rsp.sBuffer);
- in.moveHeader(len);
- return TC_NetWorkBuffer::PACKET_FULL;
- }
- /*
- Whole package length (4 bytes) + irequestid (4 bytes) + package content
- */
- static shared_ptr<TC_NetWorkBuffer::Buffer> customRequest(RequestPacket& request, TC_Transceiver *)
- {
- shared_ptr<TC_NetWorkBuffer::Buffer> buff = std::make_shared<TC_NetWorkBuffer::Buffer>();
- unsigned int net_bufflength = htonl(request.sBuffer.size()+8);
- unsigned char * bufflengthptr = (unsigned char*)(&net_bufflength);
- vector<char> buffer;
- buffer.resize(request.sBuffer.size()+8);
- memcpy(buffer.data(), bufflengthptr, sizeof(unsigned int));
- unsigned int netrequestId = htonl(request.iRequestId);
- unsigned char * netrequestIdptr = (unsigned char*)(&netrequestId);
- memcpy(buffer.data() + sizeof(unsigned int), netrequestIdptr, sizeof(unsigned int));
- memcpy(buffer.data() + sizeof(unsigned int) * 2, request.sBuffer.data(), request.sBuffer.size());
- buff->addBuffer(buffer);
- return buff;
- }
- //回调函数
- void ClientHelloCallback::callback_testTrans(int ret, const string &r)
- {
- ASSERT_TRUE(ret == 0);
- ASSERT_TRUE(r == buffer);
- callback_count++;
- if(callback_count == count-1)
- {
- // int64_t cost = TC_Common::now2us() - start;
- // LOG_CONSOLE_DEBUG << "callback_testTrans count:" << count << ", " << cost << " us, avg:" << 1.*cost/count << "us" << endl;
- }
- }
- //回调函数
- void ClientHelloCallback::callback_testHello(int ret, const string &r)
- {
- callback_count++;
- if(!_prx)
- {
- ASSERT_TRUE(ret == 0);
- ASSERT_TRUE(r == buffer);
- if (callback_count == count - 1)
- {
- // int64_t cost = TC_Common::now2us() - start;
- // LOG_CONSOLE_DEBUG << "callback_testHello count:" << count << ", " << cost << " us, avg:" << 1. * cost / count << "us" << endl;
- }
- }
- else
- {
- string out;
- int code = _prx->testHello(0, r, out);
- ASSERT_TRUE(code == 0);
- ASSERT_TRUE(out == r);
- ++callback_count;
- }
- }
- void ClientHelloCallback::callback_testSyncTrans(tars::Int32 ret, const std::string& r)
- {
- ASSERT_TRUE(ret == 0);
- ASSERT_TRUE(r == buffer);
- callback_count++;
- if(callback_count == count-1)
- {
- // int64_t cost = TC_Common::now2us() - start;
- // LOG_CONSOLE_DEBUG << "callback_testSyncTrans count:" << count << ", " << cost << " us, avg:" << 1.*cost/count << "us" << endl;
- }
- }
- void HelloTest::startServer(HelloServer &server, TC_EpollServer::SERVER_OPEN_COROUTINE openCoroutine)
- {
- TC_Config conf = CONFIG();
- conf.set("/tars/application/server<opencoroutine>", TC_Common::tostr(openCoroutine));
- ASSERT_TRUE(conf.get("/tars/application/server<opencoroutine>") == TC_Common::tostr(openCoroutine));
- server.main(conf.tostr());
- ASSERT_TRUE(ServerConfig::OpenCoroutine == openCoroutine);
- server.start();
- server.waitForReady();
- }
- TC_Endpoint HelloTest::getEndpoint(const string &adapter)
- {
- return TC_Endpoint(_conf.get("/tars/application/server/" + adapter + "<endpoint>"));
- }
- void HelloTest::wait(int millseconds)
- {
- if(TC_CoroutineScheduler::scheduler())
- {
- TC_CoroutineScheduler::scheduler()->sleep(millseconds);
- }
- else
- {
- TC_Common::msleep(millseconds);
- }
- }
- void HelloTest::syncCustom(Communicator *comm, const string &adapter )
- {
- ServantPrx prx = getObj<ServantPrx>(comm, adapter);
- ProxyProtocol prot;
- prot.requestFunc = customRequest;
- prot.responseFunc = customResponse;
- prx->tars_set_protocol(prot);
- int64_t start = TC_Common::now2us();
- string out;
- //发起远程调用
- for (int j = 0; j < _count; ++j) {
- ResponsePacket rsp;
- prx->rpc_call(prx->tars_gen_requestid(), "doCustomFunc", _buffer.c_str(), _buffer.length(), rsp);
- }
- int64_t cost = TC_Common::now2us() - start;
- LOG_CONSOLE_DEBUG << "count:" << _count << ", " << cost << " us, avg:" << 1.*cost/_count << "us" << endl;
- }
- void HelloTest::asyncCustom(Communicator *comm, const string &adapter)
- {
- ServantPrx prx = getObj<ServantPrx>(comm, adapter);
- ProxyProtocol prot;
- prot.requestFunc = customRequest;
- prot.responseFunc = customResponse;
- prx->tars_set_protocol(prot);
- atomic<int> callback_count{0};
- //发起远程调用
- for (int j = 0; j < _count; ++j)
- {
- CustomCallBackPtr cb = new CustomCallBack(callback_count);
- prx->rpc_call_async(prx->tars_gen_requestid(), "doCustomFunc", _buffer.c_str(), _buffer.length(), cb);
- }
- waitForFinish(callback_count, _count);
- ASSERT_TRUE(callback_count == _count);
- }
- void HelloTest::testPush(Communicator *comm, const string &adapter)
- {
- ServantPrx prx = getObj<ServantPrx>(comm, adapter);
- ProxyProtocol prot;
- prot.requestFunc = pushRequest;
- prot.responseFunc = pushResponse;
- prx->tars_set_protocol(prot);
- PushCallBackPtr cbPush = new PushCallBack();
- prx->tars_set_push_callback(cbPush);
- string buf("heartbeat");
- int count = 2;
- while(count-- > 0)
- {
- cbPush->_onconnect = false;
- cbPush->_onclose = false;
- PushCallBackPtr cb = new PushCallBack();
- prx->rpc_call_async(prx->tars_gen_requestid(), "printResult", buf.c_str(), buf.length(), cb);
- wait(1000);
- ASSERT_TRUE(cbPush->_onconnect);
- ASSERT_TRUE(cb->_onprintresult);
- wait(10000);
- ASSERT_TRUE(cbPush->_onclose);
- }
- }
- void HelloTest::testReconnect(Communicator *comm, const string &adapter )
- {
- ServantPrx prx = getObj<ServantPrx>(comm, adapter);
- ProxyProtocol prot;
- prot.requestFunc = pushRequest;
- prot.responseFunc = pushResponse;
- prx->tars_set_protocol(prot);
- prx->tars_reconnect(1);
- PushCallBackPtr cbPush = new PushCallBack();
- prx->tars_set_push_callback(cbPush);
- string buf("heartbeat");
- cbPush->_onconnect = false;
- cbPush->_onclose = false;
- ResponsePacket rsp;
- prx->rpc_call(prx->tars_gen_requestid(), "printResult", buf.c_str(), buf.length(), rsp);
- TC_Common::msleep(10500);
- ASSERT_TRUE(cbPush->_onclose);
- TC_Common::msleep(2000);
- ASSERT_TRUE(cbPush->_onconnect);
- }
- void HelloTest::checkWup()
- {
- UniPacket<> req;
- req.setRequestId(1);
- req.setServantName("TestApp.HelloServer.HelloObj");
- req.setFuncName("testHello");
- req.put<int>("index", 1);
- req.put<string>("s", _buffer);
- string buff = "";
- req.encode(buff);
- // int64_t start = TC_Common::now2us();
- TC_Endpoint ep = getEndpoint("HelloAdapter");
- //可以直接发送请求给HelloServer 或者 发送请求给TranServer
- TC_TCPClient client;
- client.init(ep.getHost(), ep.getPort(), 3000);
- for (int i = 0; i < _count; i++)
- {
- char recvBuff[1024] = { 0 };
- size_t recvLen = sizeof(recvBuff);
- int ret = client.sendRecv(buff.c_str(), buff.length(), recvBuff, recvLen);
- ASSERT_TRUE(ret == 0);
- TarsUniPacket<> rsp;
- rsp.decode(recvBuff, recvLen);
- string retStr = "";
- ret = rsp.getTarsResultCode();
- ASSERT_TRUE(ret == 0);
- rsp.get("r", retStr);
- ASSERT_TRUE(retStr == _buffer);
- }
- // int64_t cost = TC_Common::now2us() - start;
- // LOG_CONSOLE_DEBUG << "count:" << _count << ", " << cost << " us, avg:" << 1.*cost/_count << "us" << endl;
- }
- void HelloTest::checkSyncHttp(Communicator *comm, const string &adapter, bool close)
- {
- ServantPrx prx = getObj<ServantPrx>(comm, adapter);
- prx->tars_set_protocol(ServantProxy::PROTOCOL_HTTP1, 10);
- int64_t start = TC_Common::now2us();
- string out;
- //发起远程调用
- for (int j = 0; j < _count; ++j) {
- string buff = _buffer + "-" + TC_Common::tostr(j) + "-" + TC_Common::tostr(TC_Thread::CURRENT_THREADID());
- shared_ptr<TC_HttpRequest> req = std::make_shared<TC_HttpRequest>();
- req->setPostRequest("http://tars.com/hello", buff, true);
- if(close)
- {
- req->setHeader("Connection", "close");
- }
- else
- {
- req->setHeader("Connection", "keep-alive");
- }
- shared_ptr<TC_HttpResponse> rsp;
- prx->http_call("hello", req, rsp);
- ASSERT_TRUE(req->getContent() == rsp->getContent());
- }
- int64_t cost = TC_Common::now2us() - start;
- LOG_CONSOLE_DEBUG << "count:" << _count << ", " << cost << " us, avg:" << 1.*cost/_count << "us" << endl;
- }
- void HelloTest::checkASyncHttp(Communicator *comm, const string &adapter, bool close)
- {
- atomic<int> callback_count{0};
- ServantPrx prx = getObj<ServantPrx>(comm, adapter);
- prx->tars_set_protocol(ServantProxy::PROTOCOL_HTTP1, 10);
- //发起远程调用
- for (int j = 0; j < _count; ++j)
- {
- string buff = _buffer + "-" + TC_Common::tostr(j) + "-" + TC_Common::tostr(TC_Thread::CURRENT_THREADID());
- shared_ptr<TC_HttpRequest> req = std::make_shared<TC_HttpRequest>();
- req->setPostRequest("http://tars.com/hello", buff, true);
- if(close)
- {
- req->setHeader("Connection", "close");
- }
- else
- {
- req->setHeader("Connection", "keep-alive");
- }
- HttpCallbackPtr p = new HelloHttpCallback(TC_Common::now2us(), j, _count, buff, callback_count);
- prx->http_call_async("hello", req, p);
- }
- waitForFinish(callback_count, _count);
- ASSERT_TRUE(callback_count == _count);
- }
- void HelloTest::checkSyncOnce(HelloPrx prx)
- {
- // HelloPrx prx = getObj<HelloPrx>(comm, adapter);
- // int64_t start = TC_Common::now2us();
- string out;
- //发起远程调用
- prx->testHello(0, _buffer, out);
- ASSERT_TRUE(_buffer == out);
- }
- void HelloTest::checkASyncOnce(HelloPrx prx)
- {
- // HelloPrx prx = getObj<HelloPrx>(comm, adapter);
- atomic<int> callback_count{0};
- HelloPrxCallbackPtr p = new ClientHelloCallback(prx, callback_count);
- prx->async_testHello(p, 0, _buffer);
- wait(100);
- ASSERT_TRUE(callback_count == 2);
- }
- void HelloTest::checkSync(Communicator *comm, const string &adapter)
- {
- HelloPrx prx = getObj<HelloPrx>(comm, adapter);
- int64_t start = TC_Common::now2us();
- string out;
- //发起远程调用
- for (int j = 0; j < _count; ++j) {
- prx->testHello(j, _buffer, out);
- ASSERT_TRUE(_buffer == out);
- }
- int64_t cost = TC_Common::now2us() - start;
- LOG_CONSOLE_DEBUG << "count:" << _count << ", " << cost << " us, avg:" << 1.*cost/_count << "us" << endl;
- }
- void HelloTest::waitForFinish(atomic<int> &callback_count, int count)
- {
- int lastCallbackCount = 0;
- while (callback_count != count)
- {
- wait(100);
- if(lastCallbackCount == callback_count)
- {
- LOG_CONSOLE_DEBUG << "no callback: " << callback_count << ", " << count << endl;
- }
- lastCallbackCount = callback_count;
- }
- // LOG_CONSOLE_DEBUG << "callback: " << callback_count << ", " << _count << endl;
- }
- void HelloTest::checkASync(Communicator *comm, const string &adapter)
- {
- atomic<int> callback_count{0};
- HelloPrx prx = getObj<HelloPrx>(comm, adapter);
- //发起远程调用
- for (int j = 0; j < _count; ++j)
- {
- HelloPrxCallbackPtr p = new ClientHelloCallback(TC_Common::now2us(), j, _count, _buffer, callback_count);
- prx->async_testHello(p, j, _buffer);
- }
- waitForFinish(callback_count, _count);
- ASSERT_TRUE(callback_count == _count);
- }
- void HelloTest::checkWupTransSync(Communicator *comm)
- {
- ServantPrx prx = getObj<ServantPrx>(comm, "TransWupAdapter");
- UniPacket<> req;
- req.setRequestId(1);
- req.setServantName("TestApp.HelloServer.TransDstObj");
- req.setFuncName("testHello");
- req.put<string>("s", _buffer);
- ProxyProtocol proto;
- proto.requestFunc = ProxyProtocol::streamRequest;
- proto.responseFunc = ProxyProtocol::tupResponse;
- prx->tars_set_protocol(proto);
- int64_t start = TC_Common::now2us();
- //发起远程调用
- for (int i = 0; i < _count; ++i)
- {
- req.setRequestId(prx->tars_gen_requestid());
- req.put<int>("index", i);
- vector<char> buff;
- req.encode(buff);
- ResponsePacket done;
- prx->rpc_call(req.getRequestId(), "testHello", buff.data(), buff.size(), done);
- TarsUniPacket<> rsp;
- rsp.decode(done.sBuffer.data(), done.sBuffer.size());
- string retStr = "";
- int ret = rsp.getTarsResultCode();
- ASSERT_TRUE(ret == 0);
- rsp.get("r", retStr);
- ASSERT_TRUE(retStr == _buffer);
- }
- int64_t cost = TC_Common::now2us() - start;
- LOG_CONSOLE_DEBUG << "count:" << _count << ", " << cost << " us, avg:" << 1.*cost/_count << "us" << endl;
- }
- void HelloTest::checkTransSyncASync(Communicator *comm)
- {
- HelloPrx prx = getObj<HelloPrx>(comm, "TransAdapter");
- int64_t start = TC_Common::now2us();
- string out;
- //发起远程调用
- for (int j = 0; j < _count; ++j) {
- prx->testTrans(j, _buffer, out);
- ASSERT_TRUE(_buffer == out);
- }
- int64_t cost = TC_Common::now2us() - start;
- LOG_CONSOLE_DEBUG << "count:" << _count << ", " << cost << " us, avg:" << 1.*cost/_count << "us" << endl;
- }
- void HelloTest::checkTransASyncASync(Communicator *comm)
- {
- string adapter = "TransAdapter";
- atomic<int> callback_count{0};
- HelloPrx prx = getObj<HelloPrx>(comm, adapter);
- //发起远程调用
- for (int j = 0; j < _count; ++j)
- {
- HelloPrxCallbackPtr p = new ClientHelloCallback(TC_Common::now2us(), j, _count, _buffer, callback_count);
- prx->async_testTrans(p, j, _buffer);
- }
- waitForFinish(callback_count, _count);
- ASSERT_TRUE(callback_count == _count);
- }
- void HelloTest::checkTransSyncSync(Communicator *comm)
- {
- HelloPrx prx = getObj<HelloPrx>(comm, "TransAdapter");
- int64_t start = TC_Common::now2us();
- string out;
- //发起远程调用
- for (int j = 0; j < _count; ++j) {
- prx->testSyncTrans(j, _buffer, out);
- ASSERT_TRUE(_buffer == out);
- }
- int64_t cost = TC_Common::now2us() - start;
- LOG_CONSOLE_DEBUG << "count:" << _count << ", " << cost << " us, avg:" << 1.*cost/_count << "us" << endl;
- }
- void HelloTest::checkTransASyncSync(Communicator *comm)
- {
- string adapter = "TransAdapter";
- atomic<int> callback_count{0};
- HelloPrx prx = getObj<HelloPrx>(comm, adapter);
- //发起远程调用
- for (int j = 0; j < _count; ++j)
- {
- HelloPrxCallbackPtr p = new ClientHelloCallback(TC_Common::now2us(), j, _count, _buffer, callback_count);
- prx->async_testSyncTrans(p, j, _buffer);
- }
- waitForFinish(callback_count, _count);
- ASSERT_TRUE(callback_count == _count);
- }
- void HelloTest::rpcFromRegistry(Communicator *comm)
- {
- //test get ip from registry
- HelloPrx qPrx = comm->stringToProxy<HelloPrx>("TestApp.RpcServer.HelloObj");
- string out;
- int ret = qPrx->testHello(0, _buffer, out);
- ASSERT_TRUE(ret == 0);
- ASSERT_TRUE(out == _buffer);
- //test rpc loop
- int count = 10;
- while(count-- > 10)
- {
- int ret;
- string out1, out2;
- ret = qPrx->testPid(out1);
- ASSERT_TRUE(ret == 0);
- ret = qPrx->testPid(out2);
- ASSERT_TRUE(ret == 0);
- ASSERT_TRUE(out1 != out2);
- }
- //test rpc hash
- count = 10;
- while(count-- > 10)
- {
- vector<string> vout;
- std::thread hash([&](){
- string out;
- int ret = qPrx->tars_hash(TC_Thread::CURRENT_THREADID())->testPid(out);
- vout.push_back(out);
- ASSERT_TRUE(ret == 0);
- });
- hash.join();
- for(size_t i = 0; i < vout.size()-1; i++)
- {
- ASSERT_TRUE(vout[i] == vout[i+1]);
- }
- }
- }
- void HelloTest::checkSyncTimeout(Communicator *comm)
- {
- HelloPrx prx = getObj<HelloPrx>(comm, "HelloAdapter");
- prx->tars_set_timeout(2000);
- prx->tars_async_timeout(3000);
- prx->testTimeout(1);
- ASSERT_TRUE(true);
- try
- {
- prx->testTimeout(4);
- }
- catch (exception& ex)
- {
- ASSERT_TRUE(true);
- }
- }
- void HelloTest::checkASyncTimeout(Communicator *comm)
- {
- HelloPrx prx = getObj<HelloPrx>(comm, "HelloAdapter");
- prx->tars_set_timeout(2000);
- prx->tars_async_timeout(3000);
- std::atomic<int> callback_count;
- ClientHelloCallback *c = new ClientHelloCallback(TC_Common::now2us(), 0, _count, _buffer, callback_count);
- HelloPrxCallbackPtr p = c;
- prx->async_testTimeout(p, 1);
- wait(1500);
- ASSERT_TRUE(c->callback);
- c->callback = false;
- prx->async_testTimeout(p, 5);
- wait(4000);
- ASSERT_FALSE(c->callback);
- ASSERT_TRUE(c->callback_exception);
- }
- void HelloTest::checkStat(Communicator *comm, int reportCount)
- {
- HelloPrx prx = getObj<HelloPrx>(comm, "HelloAdapter");
- prx->tars_set_timeout(2000);
- prx->tars_async_timeout(3000);
- int count = reportCount;
- while(count-- > 0)
- {
- string out;
- prx->testHello(count, _buffer, out);
- TC_Common::sleep(1);
- }
- }
- void HelloTest::forEach(function<void()> func)
- {
- int i = 2;
- for(i = 0; i <= TC_EpollServer::NET_THREAD_MERGE_HANDLES_CO; i++)
- {
- HelloServer server;
- startServer(server, (TC_EpollServer::SERVER_OPEN_COROUTINE) i);
- func();
- stopServer(server);
- }
- }
- void HelloTest::forEach(function<void(Communicator *comm)> func)
- {
- int i = 0;//TC_EpollServer::NET_THREAD_MERGE_HANDLES_CO;
- for(i = 0; i <= TC_EpollServer::NET_THREAD_MERGE_HANDLES_CO; i++)
- {
- HelloServer server;
- startServer(server, (TC_EpollServer::SERVER_OPEN_COROUTINE) i);
- func(server.getCommunicator().get());
- stopServer(server);
- }
- }
- void HelloTest::forEachInCoroutine(function<void(Communicator *comm)> func)
- {
- int i = 0;
- for(i = 0; i <= TC_EpollServer::NET_THREAD_MERGE_HANDLES_CO; i++)
- {
- HelloServer server;
- startServer(server, (TC_EpollServer::SERVER_OPEN_COROUTINE) i);
- {
- std::thread cor_call([&]()
- {
- auto scheduler = TC_CoroutineScheduler::create();
- //设置到协程中
- ServantProxyThreadData::getData()->_sched = scheduler;
- scheduler->go([&]()
- {
- scheduler->setNoCoroutineCallback([=](TC_CoroutineScheduler *s)
- {
- s->terminate();
- });
- func(server.getCommunicator().get());
-
- });
- scheduler->run();
- });
- cor_call.join();
- }
- stopServer(server);
- }
- }
- void HelloTest::funcInCoroutine(function<void()> func, bool setSched)
- {
- std::thread cor_call([&]()
- {
- auto scheduler = TC_CoroutineScheduler::create();
- if(setSched)
- {
- //设置到协程中
- ServantProxyThreadData::getData()->_sched = scheduler;
- }
- scheduler->go([&]()
- {
- scheduler->setNoCoroutineCallback([=](TC_CoroutineScheduler *s)
- { s->terminate(); });
- func();
- });
- scheduler->run();
- });
- cor_call.join();
- }
- void HelloTest::transGlobalCommunicator(function<void(Communicator *)> func, Communicator *comm)
- {
- forEach([&](Communicator *c){
- func(comm);
- });
- // LOG_CONSOLE_DEBUG << "succ" << endl;
- }
- void HelloTest::transServerCommunicator(function<void(Communicator *)> func)
- {
- forEach([&](Communicator *c){
- func(c);
- });
- // LOG_CONSOLE_DEBUG << "succ" << endl;
- }
- void HelloTest::transAllocCommunicator(function<void(Communicator *)> func)
- {
- forEach([&](Communicator *c){
- shared_ptr<Communicator> comm = std::make_shared<Communicator>();
- func(comm.get());
- });
- // LOG_CONSOLE_DEBUG << "succ" << endl;
- }
- void HelloTest::transComplexCommunicator(function<void(Communicator *)> func, Communicator *comm)
- {
- forEach([&](Communicator *c){
- shared_ptr<Communicator> allocComm = std::make_shared<Communicator>();
- std::thread thAlloc1([&](){
- func(allocComm.get());
- });
- std::thread thServer1([&](){
- func(c);
- });
- std::thread thGlobal1([&](){
- func(c);
- });
- thAlloc1.join();
- thServer1.join();
- thGlobal1.join();
- });
- // LOG_CONSOLE_DEBUG << "succ" << endl;
- }
- void HelloTest::transInCoroutineGlobalCommunicator(function<void(Communicator *)> func, Communicator *comm)
- {
- forEachInCoroutine([&](Communicator *c){
- func(comm);
- });
- // LOG_CONSOLE_DEBUG << "succ" << endl;
- }
- void HelloTest::transInCoroutineServerCommunicator(function<void(Communicator *)> func)
- {
- forEachInCoroutine([&](Communicator *c){
- func(c);
- });
- // LOG_CONSOLE_DEBUG << "succ" << endl;
- }
- void HelloTest::transInCoroutineAllocCommunicator(function<void(Communicator *)> func)
- {
- forEachInCoroutine([&](Communicator *c){
- shared_ptr<Communicator> comm = std::make_shared<Communicator>();
- func(comm.get());
- });
- // LOG_CONSOLE_DEBUG << "succ" << endl;
- }
- void HelloTest::transInCoroutineComplexCommunicator(function<void(Communicator *)> func, Communicator *comm)
- {
- forEachInCoroutine([&](Communicator *c)
- {
- {
- shared_ptr<Communicator> allocComm = std::make_shared<Communicator>();
- std::thread thAlloc1([&]()
- { func(allocComm.get()); });
- std::thread thServer1([&]()
- { func(c); });
- std::thread thGlobal1([&]()
- { func(c); });
- thAlloc1.join();
- thServer1.join();
- thGlobal1.join();
- }
- });
- // LOG_CONSOLE_DEBUG << "succ" << endl;
- }
- shared_ptr<Communicator> HelloTest::getCommunicator()
- {
- shared_ptr<Communicator> c = std::make_shared<Communicator>();
- TC_Config conf = CLIENT_CONFIG();
- c->setProperty(conf);
- c->setProperty("sendqueuelimit", "1000000");
- return c;
- }
|