unit-test support TARS_SHARED_PTR

ruanshudong_dev
ruanshudong 2024-02-26 16:17:10 +08:00
parent 98dc06f2c8
commit e0f1a47b84
16 changed files with 56 additions and 49 deletions

View File

@ -106,7 +106,7 @@ void asyncCall(int c)
//发起远程调用
for (int i = 0; i < c; ++i)
{
HelloPrxCallbackPtr p = new HelloCallback(t, i, c);
HelloPrxCallbackPtr p(new HelloCallback(t, i, c));
try
{

View File

@ -66,7 +66,7 @@ public:
int _iOut;
string _sOut;
};
typedef tars::TC_AutoPtr<AServantCoroCallback> AServantCoroCallbackPtr;
// typedef tars::TC_AutoPtr<AServantCoroCallback> AServantCoroCallbackPtr;
int BServantImp::test(tars::TarsCurrentPtr current) { return 0;}
@ -111,21 +111,21 @@ tars::Int32 BServantImp::testCoroParallel(const std::string& sIn, std::string &s
int iIn = 5;
CoroParallelBasePtr sharedPtr = new CoroParallelBase(2);
CoroParallelBasePtr sharedPtr(new CoroParallelBase(2));
AServantCoroCallbackPtr cb1 = new AServantCoroCallback();
AServantCoroPrxCallbackPtr cb1(new AServantCoroCallback());
cb1->setCoroParallelBasePtr(sharedPtr);
_pPrx->coro_testInt(cb1, iIn);
AServantCoroCallbackPtr cb2 = new AServantCoroCallback();
AServantCoroPrxCallbackPtr cb2(new AServantCoroCallback());
cb2->setCoroParallelBasePtr(sharedPtr);
_pPrx->coro_testStr(cb2, sIn);
coroWhenAll(sharedPtr);
if(cb1->_iRet == 0 && cb2->_iRet == 0)
if(((AServantCoroCallback*)(cb1.get()))->_iRet == 0 && ((AServantCoroCallback*)(cb2.get()))->_iRet == 0)
{
sOut = cb2->_sOut;
sOut = ((AServantCoroCallback*)(cb2.get()))->_sOut;
iRet = 0;
}

View File

@ -55,7 +55,7 @@ public:
int _iOut;
string _sOut;
};
typedef tars::TC_AutoPtr<BServantCoroCallback> BServantCoroCallbackPtr;
// typedef tars::TC_AutoPtr<BServantCoroCallback> BServantCoroCallbackPtr;
////////////////////////////////////////////
//继承框架的协程类
@ -93,11 +93,11 @@ void TestCoroutine::handle()
{
CoroParallelBasePtr sharedPtr = new CoroParallelBase(2);
BServantCoroCallbackPtr cb1 = new BServantCoroCallback();
BServantCoroPrxCallbackPtr cb1(new BServantCoroCallback());
cb1->setCoroParallelBasePtr(sharedPtr);
_prx->coro_testCoroSerial(cb1, sIn);
BServantCoroCallbackPtr cb2 = new BServantCoroCallback();
BServantCoroPrxCallbackPtr cb2(new BServantCoroCallback());
cb2->setCoroParallelBasePtr(sharedPtr);
_prx->coro_testCoroParallel(cb2, sIn);
@ -105,7 +105,7 @@ void TestCoroutine::handle()
// cout << "ret1:" << cb1->_sOut << "|ret2:" << cb2->_sOut << endl;
if(cb1->_iRet == 0 && cb2->_iRet == 0 && cb1->_iException == 0 && cb2->_iException == 0)
if(((BServantCoroCallback*)(cb1.get()))->_iRet == 0 && ((BServantCoroCallback*)(cb2.get()))->_iRet == 0 && ((BServantCoroCallback*)(cb1.get()))->_iException == 0 && ((BServantCoroCallback*)(cb2.get()))->_iException == 0)
{
++sum;
}

View File

@ -158,7 +158,7 @@ void asyncCall(int c)
{
try
{
CustomCallBackPtr cb = new CustomCallBack();
ServantProxyCallbackPtr cb(new CustomCallBack());
param.servantPrx->rpc_call_async(param.servantPrx->tars_gen_requestid(), "doCustomFunc", buffer.c_str(), buffer.length(), cb);
}
catch(exception& e)

View File

@ -61,7 +61,10 @@ int main(int argc, char *argv[])
_comm->setProperty(conf);
HelloPrx pPrx = _comm->stringToProxy<HelloPrx>(helloObj);
pPrx->tars_set_push_callback(new PushCallbackImp());
ServantProxyCallbackPtr cbPush(new PushCallbackImp());
pPrx->tars_set_push_callback(cbPush);
pPrx->registerPush();
while(g_count < 10)

View File

@ -127,7 +127,7 @@ RecvThread::RecvThread(int second):_second(second), _bTerminate(false)
void RecvThread::run(void)
{
TestPushCallBackPtr cbPush = new TestPushCallBack();
ServantProxyCallbackPtr cbPush(new TestPushCallBack());
_prx->tars_set_push_callback(cbPush);
string buf("heartbeat");
@ -139,7 +139,7 @@ void RecvThread::run(void)
{
try
{
TestPushCallBackPtr cb = new TestPushCallBack();
ServantProxyCallbackPtr cb(new TestPushCallBack());
_prx->rpc_call_async(_prx->tars_gen_requestid(), "printResult", buf.c_str(), buf.length(), cb);
}
catch(TarsException& e)

View File

@ -114,7 +114,7 @@ void asyncCall(int c)
//发起远程调用
for (int i = 0; i < c; ++i)
{
HelloPrxCallbackPtr p = new HelloCallback(t, i, c);
HelloPrxCallbackPtr p(new HelloCallback(t, i, c));
try
{
@ -263,7 +263,7 @@ void asyncTupCall(int c)
vector<char> buff;
req.encode(buff);
ServantProxyCallbackPtr p = new TupCallback(t, i, c);
ServantProxyCallbackPtr p(new TupCallback(t, i, c));
param.pPrx->rpc_call_async(req.getRequestId(), "testHello", buff.data(), buff.size(), p);
}

View File

@ -68,7 +68,7 @@ tars::Int32 ProxyImp::testProxy(const std::string& sIn, std::string &sOut, tars:
{
current->setResponse(false);
TestApp::HelloPrxCallbackPtr cb = new HelloCallback(current);
TestApp::HelloPrxCallbackPtr cb(new HelloCallback(current));
_prx->tars_set_timeout(3000)->async_testHello(cb,sIn);
}

View File

@ -111,7 +111,7 @@ void asyncCall(int c)
if(request_count - callback_count < 100) {
i++;
request_count++;
HelloPrxCallbackPtr p = new HelloCallback(t, i, c);
HelloPrxCallbackPtr p(new HelloCallback(t, i, c));
try {
param.pPrx->async_testHello(p, buffer);
}

View File

@ -278,7 +278,7 @@ void HelloTest::asyncCustom(Communicator *comm, const string &adapter)
//发起远程调用
for (int j = 0; j < _count; ++j)
{
CustomCallBackPtr cb = new CustomCallBack(callback_count);
ServantProxyCallbackPtr cb(new CustomCallBack(callback_count));
prx->rpc_call_async(prx->tars_gen_requestid(), "doCustomFunc", _buffer.c_str(), _buffer.length(), cb);
}
@ -297,7 +297,8 @@ void HelloTest::testPush(Communicator *comm, const string &adapter)
prx->tars_set_protocol(prot);
PushCallBackPtr cbPush = new PushCallBack();
PushCallBack *c = new PushCallBack();
ServantProxyCallbackPtr cbPush(c);
prx->tars_set_push_callback(cbPush);
string buf("heartbeat");
@ -305,20 +306,22 @@ void HelloTest::testPush(Communicator *comm, const string &adapter)
int count = 2;
while(count-- > 0)
{
cbPush->_onconnect = false;
cbPush->_onclose = false;
c->_onconnect = false;
c->_onclose = false;
PushCallBackPtr cb = new PushCallBack();
PushCallBack *cNew = new PushCallBack();
ServantProxyCallbackPtr cb(cNew);
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);
ASSERT_TRUE(cNew->_onconnect);
ASSERT_TRUE(cNew->_onprintresult);
wait(10000);
ASSERT_TRUE(cbPush->_onclose);
ASSERT_TRUE(c->_onclose);
}
}
@ -333,24 +336,25 @@ void HelloTest::testReconnect(Communicator *comm, const string &adapter )
prx->tars_set_protocol(prot);
prx->tars_reconnect(1);
PushCallBackPtr cbPush = new PushCallBack();
PushCallBack *c = new PushCallBack();
ServantProxyCallbackPtr cbPush(c);
prx->tars_set_push_callback(cbPush);
string buf("heartbeat");
cbPush->_onconnect = false;
cbPush->_onclose = false;
c->_onconnect = false;
c->_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);
ASSERT_TRUE(c->_onclose);
TC_Common::msleep(2000);
ASSERT_TRUE(cbPush->_onconnect);
ASSERT_TRUE(c->_onconnect);
}
@ -493,7 +497,7 @@ void HelloTest::checkASyncOnce(HelloPrx prx)
atomic<int> callback_count{0};
HelloPrxCallbackPtr p = new ClientHelloCallback(prx, callback_count);
HelloPrxCallbackPtr p(new ClientHelloCallback(prx, callback_count));
prx->async_testHello(p, 0, _buffer);
@ -548,7 +552,7 @@ void HelloTest::checkASync(Communicator *comm, const string &adapter)
//发起远程调用
for (int j = 0; j < _count; ++j)
{
HelloPrxCallbackPtr p = new ClientHelloCallback(TC_Common::now2us(), j, _count, _buffer, callback_count);
HelloPrxCallbackPtr p(new ClientHelloCallback(TC_Common::now2us(), j, _count, _buffer, callback_count));
prx->async_testHello(p, j, _buffer);
}
@ -647,7 +651,7 @@ void HelloTest::checkTransASyncASync(Communicator *comm)
//发起远程调用
for (int j = 0; j < _count; ++j)
{
HelloPrxCallbackPtr p = new ClientHelloCallback(TC_Common::now2us(), j, _count, _buffer, callback_count);
HelloPrxCallbackPtr p(new ClientHelloCallback(TC_Common::now2us(), j, _count, _buffer, callback_count));
prx->async_testTrans(p, j, _buffer);
}
@ -689,7 +693,7 @@ void HelloTest::checkTransASyncSync(Communicator *comm)
//发起远程调用
for (int j = 0; j < _count; ++j)
{
HelloPrxCallbackPtr p = new ClientHelloCallback(TC_Common::now2us(), j, _count, _buffer, callback_count);
HelloPrxCallbackPtr p(new ClientHelloCallback(TC_Common::now2us(), j, _count, _buffer, callback_count));
prx->async_testSyncTrans(p, j, _buffer);
}
@ -806,7 +810,7 @@ void HelloTest::checkASyncTimeout(Communicator *comm)
std::atomic<int> callback_count;
ClientHelloCallback *c = new ClientHelloCallback(TC_Common::now2us(), 0, _count, _buffer, callback_count);
HelloPrxCallbackPtr p = c;
HelloPrxCallbackPtr p(c);
prx->async_testTimeout(p, 1);

View File

@ -58,7 +58,7 @@ public:
};
typedef tars::TC_AutoPtr<PushCallBack> PushCallBackPtr;
// typedef tars::TC_AutoPtr<PushCallBack> PushCallBackPtr;
struct ClientHelloCallback : public HelloPrxCallback
{
@ -185,7 +185,7 @@ public:
std::atomic<int> &callback_count;
};
typedef tars::TC_AutoPtr<CustomCallBack> CustomCallBackPtr;
// typedef tars::TC_AutoPtr<CustomCallBack> CustomCallBackPtr;
class HelloTest : public testing::Test
{

View File

@ -126,7 +126,7 @@ TEST_F(HelloTest, rpcASyncThreadFinish)
//发起远程调用
for (int j = 0; j < count; ++j)
{
HelloPrxCallbackPtr p = new ClientHelloCallback(TC_Common::now2us(), j, count, _buffer, callback_count);
HelloPrxCallbackPtr p(new ClientHelloCallback(TC_Common::now2us(), j, count, _buffer, callback_count));
prx->async_testHello(p, j, _buffer);
}

View File

@ -59,7 +59,7 @@ TEST_F(HelloTest, push)
HelloPrx prx = comm->stringToProxy<HelloPrx>(obj);
RegisterPushCallBackPtr callback = new RegisterPushCallBack();
ServantProxyCallbackPtr callback(new RegisterPushCallBack());
prx->tars_set_push_callback(callback);
@ -69,7 +69,7 @@ TEST_F(HelloTest, push)
TC_Common::msleep(50);
ASSERT_TRUE(callback->_msg == msg);
ASSERT_TRUE(((RegisterPushCallBack*)callback.get())->_msg == msg);
stopServer(ws);
}
@ -86,7 +86,7 @@ TEST_F(HelloTest, pushClose)
HelloPrx prx = comm->stringToProxy<HelloPrx>(obj);
RegisterPushCallBackPtr callback = new RegisterPushCallBack();
ServantProxyCallbackPtr callback(new RegisterPushCallBack);
prx->tars_set_push_callback(callback);
@ -96,13 +96,13 @@ TEST_F(HelloTest, pushClose)
TC_Common::msleep(50);
ASSERT_TRUE(callback->_msg == msg);
ASSERT_TRUE(((RegisterPushCallBack*)callback.get())->_msg == msg);
prx->tars_close();
TC_Common::msleep(50);
ASSERT_TRUE(callback->_close);
ASSERT_TRUE(((RegisterPushCallBack*)callback.get())->_close);
stopServer(ws);
}

View File

@ -109,8 +109,8 @@ TEST_F(HelloTest, winServerAsync)
//发起远程调用
for (int j = 0; j < _count; ++j)
{
HelloPrxCallbackPtr p = new ClientHelloCallback(TC_Common::now2us(), j, _count, _buffer,
callback_count);
HelloPrxCallbackPtr p(new ClientHelloCallback(TC_Common::now2us(), j, _count, _buffer,
callback_count));
prx->async_testHello(p, j, _buffer);
}

View File

@ -61,7 +61,7 @@ int HelloImp::testTrans(int index, const string &s, string &r, CurrentPtr curren
{
current->setResponse(false);
HelloPrxCallbackPtr p = new HelloCallback(current, index, s);
HelloPrxCallbackPtr p(new HelloCallback(current, index, s));
_helloPrx->async_testHello(p, index, s);
}

View File

@ -32,7 +32,7 @@ void TranImp::async_call(tars::CurrentPtr current, vector<char> &response)
TLOGDEBUG("async_call: begin remote call, req len:" << req.sBuffer.size() << endl);
/*a-sync-call*/
ServantProxyCallbackPtr cb = new ServantCallback("ServantCallback", this, current);
ServantProxyCallbackPtr cb(new ServantCallback("ServantCallback", this, current));
_servantPrx->rpc_call_async(req.iRequestId, req.sFuncName, request.data(), request.size(), cb);
current->setResponse(false);
}