TDengine/source/libs/transport/test/transUT.cpp

507 lines
12 KiB
C++
Raw Normal View History

2022-01-26 11:05:20 +00:00
/*
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
*
* This program is free software: you can use, redistribute, and/or modify
* it under the terms of the GNU Affero General Public License, version 3 * or later ("AGPL"), as published by the Free
* Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <gtest/gtest.h>
#include <cstdio>
#include <cstring>
2022-03-04 05:25:39 +00:00
#include "tdatablock.h"
2022-02-21 14:36:00 +00:00
#include "tglobal.h"
2022-02-27 03:05:46 +00:00
#include "tlog.h"
2023-07-06 02:03:31 +00:00
#include "tmisce.h"
2022-05-21 03:18:32 +00:00
#include "transLog.h"
2022-03-04 05:25:39 +00:00
#include "trpc.h"
2023-07-06 02:03:31 +00:00
#include "tversion.h"
2022-01-26 11:05:20 +00:00
using namespace std;
2022-02-21 14:36:00 +00:00
const char *label = "APP";
const char *secret = "secret";
const char *user = "user";
const char *ckey = "ckey";
2022-01-26 11:05:20 +00:00
2022-02-21 14:36:00 +00:00
class Server;
int port = 7000;
// server process
2022-03-15 11:54:06 +00:00
// server except
2022-03-19 15:38:18 +00:00
2022-03-14 14:21:30 +00:00
typedef void (*CB)(void *parent, SRpcMsg *pMsg, SEpSet *pEpSet);
2022-03-15 11:54:06 +00:00
static void processContinueSend(void *parent, SRpcMsg *pMsg, SEpSet *pEpSet);
2022-03-20 10:08:33 +00:00
static void processReleaseHandleCb(void *parent, SRpcMsg *pMsg, SEpSet *pEpSet);
static void processRegisterFailure(void *parent, SRpcMsg *pMsg, SEpSet *pEpSet);
2022-02-21 14:36:00 +00:00
static void processReq(void *parent, SRpcMsg *pMsg, SEpSet *pEpSet);
// client process;
static void processResp(void *parent, SRpcMsg *pMsg, SEpSet *pEpSet);
class Client {
public:
void Init(int nThread) {
memcpy(tsTempDir, TD_TMP_DIR_PATH, strlen(TD_TMP_DIR_PATH));
2022-03-14 14:21:30 +00:00
memset(&rpcInit_, 0, sizeof(rpcInit_));
rpcInit_.localPort = 0;
rpcInit_.label = (char *)label;
rpcInit_.numOfThreads = nThread;
rpcInit_.cfp = processResp;
rpcInit_.user = (char *)user;
rpcInit_.parent = this;
rpcInit_.connType = TAOS_CONN_CLIENT;
2023-07-06 02:03:31 +00:00
taosVersionStrToInt(version, &(rpcInit_.compatibilityVer));
2022-03-14 14:21:30 +00:00
this->transCli = rpcOpen(&rpcInit_);
2022-02-21 14:36:00 +00:00
tsem_init(&this->sem, 0, 0);
2022-01-26 12:35:13 +00:00
}
2022-02-21 14:36:00 +00:00
void SetResp(SRpcMsg *pMsg) {
// set up resp;
this->resp = *pMsg;
}
SRpcMsg *Resp() { return &this->resp; }
2022-03-14 14:21:30 +00:00
void Restart(CB cb) {
rpcClose(this->transCli);
rpcInit_.cfp = cb;
2023-07-06 02:03:31 +00:00
taosVersionStrToInt(version, &(rpcInit_.compatibilityVer));
2022-03-14 14:21:30 +00:00
this->transCli = rpcOpen(&rpcInit_);
}
2022-03-16 05:30:03 +00:00
void Stop() {
rpcClose(this->transCli);
this->transCli = NULL;
}
2022-02-08 13:23:43 +00:00
2022-02-21 14:36:00 +00:00
void SendAndRecv(SRpcMsg *req, SRpcMsg *resp) {
2022-02-08 13:23:43 +00:00
SEpSet epSet = {0};
epSet.inUse = 0;
2022-02-21 14:36:00 +00:00
addEpIntoEpSet(&epSet, "127.0.0.1", 7000);
2022-02-08 13:23:43 +00:00
2022-02-21 14:36:00 +00:00
rpcSendRequest(this->transCli, &epSet, req, NULL);
SemWait();
*resp = this->resp;
2022-02-08 13:23:43 +00:00
}
2022-03-15 15:47:37 +00:00
void SendAndRecvNoHandle(SRpcMsg *req, SRpcMsg *resp) {
2022-05-16 06:13:07 +00:00
if (req->info.handle != NULL) {
rpcReleaseHandle(req->info.handle, TAOS_CONN_CLIENT);
req->info.handle = NULL;
2022-03-15 15:47:37 +00:00
}
SendAndRecv(req, resp);
}
2022-02-21 14:36:00 +00:00
void SemWait() { tsem_wait(&this->sem); }
void SemPost() { tsem_post(&this->sem); }
void Reset() {}
~Client() {
if (this->transCli) rpcClose(this->transCli);
2022-01-26 11:05:20 +00:00
}
private:
2022-02-21 14:36:00 +00:00
tsem_t sem;
2022-03-14 14:21:30 +00:00
SRpcInit rpcInit_;
2022-10-13 05:59:18 +00:00
void *transCli;
2022-02-21 14:36:00 +00:00
SRpcMsg resp;
};
class Server {
public:
Server() {
memcpy(tsTempDir, TD_TMP_DIR_PATH, strlen(TD_TMP_DIR_PATH));
2022-03-15 11:54:06 +00:00
memset(&rpcInit_, 0, sizeof(rpcInit_));
2022-05-19 09:38:11 +00:00
memcpy(rpcInit_.localFqdn, "localhost", strlen("localhost"));
2022-03-15 11:54:06 +00:00
rpcInit_.localPort = port;
rpcInit_.label = (char *)label;
rpcInit_.numOfThreads = 5;
rpcInit_.cfp = processReq;
rpcInit_.user = (char *)user;
rpcInit_.connType = TAOS_CONN_SERVER;
2023-07-06 02:03:31 +00:00
taosVersionStrToInt(version, &(rpcInit_.compatibilityVer));
2022-02-21 14:36:00 +00:00
}
void Start() {
2022-03-15 11:54:06 +00:00
this->transSrv = rpcOpen(&this->rpcInit_);
2022-02-21 14:36:00 +00:00
taosMsleep(1000);
}
2022-03-19 15:38:18 +00:00
void SetSrvContinueSend(CB cb) {
this->Stop();
rpcInit_.cfp = cb;
this->Start();
}
2022-02-21 14:36:00 +00:00
void Stop() {
if (this->transSrv == NULL) return;
rpcClose(this->transSrv);
this->transSrv = NULL;
}
2022-03-19 15:38:18 +00:00
void SetSrvSend(void (*cfp)(void *parent, SRpcMsg *pMsg, SEpSet *pEpSet)) {
2022-03-15 11:54:06 +00:00
this->Stop();
rpcInit_.cfp = cfp;
this->Start();
}
2022-02-21 14:36:00 +00:00
void Restart() {
this->Stop();
this->Start();
}
~Server() {
if (this->transSrv) rpcClose(this->transSrv);
this->transSrv = NULL;
}
private:
2022-03-15 11:54:06 +00:00
SRpcInit rpcInit_;
2022-10-13 05:59:18 +00:00
void *transSrv;
2022-02-21 14:36:00 +00:00
};
static void processReq(void *parent, SRpcMsg *pMsg, SEpSet *pEpSet) {
SRpcMsg rpcMsg = {0};
rpcMsg.pCont = rpcMallocCont(100);
rpcMsg.contLen = 100;
2022-05-16 06:13:07 +00:00
rpcMsg.info = pMsg->info;
2022-02-21 14:36:00 +00:00
rpcMsg.code = 0;
rpcSendResponse(&rpcMsg);
}
2022-03-15 11:54:06 +00:00
static void processContinueSend(void *parent, SRpcMsg *pMsg, SEpSet *pEpSet) {
for (int i = 0; i < 10; i++) {
SRpcMsg rpcMsg = {0};
rpcMsg.pCont = rpcMallocCont(100);
rpcMsg.contLen = 100;
2022-05-16 06:13:07 +00:00
rpcMsg.info = pMsg->info;
2022-03-15 11:54:06 +00:00
rpcMsg.code = 0;
rpcSendResponse(&rpcMsg);
}
}
2022-03-20 10:08:33 +00:00
static void processReleaseHandleCb(void *parent, SRpcMsg *pMsg, SEpSet *pEpSet) {
SRpcMsg rpcMsg = {0};
rpcMsg.pCont = rpcMallocCont(100);
rpcMsg.contLen = 100;
2022-05-16 06:13:07 +00:00
rpcMsg.info = pMsg->info;
2022-03-20 10:08:33 +00:00
rpcMsg.code = 0;
rpcSendResponse(&rpcMsg);
2022-07-04 07:01:51 +00:00
rpcReleaseHandle(&pMsg->info, TAOS_CONN_SERVER);
2022-03-20 10:08:33 +00:00
}
static void processRegisterFailure(void *parent, SRpcMsg *pMsg, SEpSet *pEpSet) {
{
SRpcMsg rpcMsg1 = {0};
rpcMsg1.pCont = rpcMallocCont(100);
rpcMsg1.contLen = 100;
2022-05-16 06:13:07 +00:00
rpcMsg1.info = pMsg->info;
2022-03-20 10:08:33 +00:00
rpcMsg1.code = 0;
rpcRegisterBrokenLinkArg(&rpcMsg1);
}
taosMsleep(10);
SRpcMsg rpcMsg = {0};
rpcMsg.pCont = rpcMallocCont(100);
rpcMsg.contLen = 100;
2022-05-16 06:13:07 +00:00
rpcMsg.info = pMsg->info;
2022-03-20 10:08:33 +00:00
rpcMsg.code = 0;
rpcSendResponse(&rpcMsg);
}
2022-02-21 14:36:00 +00:00
// client process;
static void processResp(void *parent, SRpcMsg *pMsg, SEpSet *pEpSet) {
Client *client = (Client *)parent;
client->SetResp(pMsg);
client->SemPost();
2022-03-16 05:30:03 +00:00
tDebug("received resp");
2022-02-21 14:36:00 +00:00
}
2022-03-14 14:21:30 +00:00
static void initEnv() {
dDebugFlag = 143;
vDebugFlag = 0;
mDebugFlag = 143;
cDebugFlag = 0;
jniDebugFlag = 0;
tmrDebugFlag = 143;
uDebugFlag = 143;
rpcDebugFlag = 143;
qDebugFlag = 0;
wDebugFlag = 0;
sDebugFlag = 0;
tsdbDebugFlag = 0;
tsLogEmbedded = 1;
tsAsyncLog = 0;
std::string path = TD_TMP_DIR_PATH "transport";
2022-03-15 11:54:06 +00:00
// taosRemoveDir(path.c_str());
2022-03-14 14:21:30 +00:00
taosMkDir(path.c_str());
tstrncpy(tsLogDir, path.c_str(), PATH_MAX);
if (taosInitLog("taosdlog", 1) != 0) {
printf("failed to init log file\n");
}
}
2022-03-15 11:54:06 +00:00
2022-02-21 14:36:00 +00:00
class TransObj {
public:
TransObj() {
2022-03-14 14:21:30 +00:00
initEnv();
2022-02-21 14:36:00 +00:00
cli = new Client;
cli->Init(1);
srv = new Server;
srv->Start();
}
2022-03-14 14:21:30 +00:00
2022-03-15 11:54:06 +00:00
void RestartCli(CB cb) {
//
cli->Restart(cb);
}
void StopSrv() {
//
srv->Stop();
}
// call when link broken, and notify query or fetch stop
void SetSrvContinueSend(void (*cfp)(void *parent, SRpcMsg *pMsg, SEpSet *pEpSet)) {
///////
srv->SetSrvContinueSend(cfp);
2022-03-14 14:21:30 +00:00
}
2022-02-21 14:36:00 +00:00
void RestartSrv() { srv->Restart(); }
2022-03-20 10:08:33 +00:00
void StopCli() {
2022-03-16 05:30:03 +00:00
///////
cli->Stop();
}
2022-02-21 14:36:00 +00:00
void cliSendAndRecv(SRpcMsg *req, SRpcMsg *resp) { cli->SendAndRecv(req, resp); }
2022-03-15 15:47:37 +00:00
void cliSendAndRecvNoHandle(SRpcMsg *req, SRpcMsg *resp) { cli->SendAndRecvNoHandle(req, resp); }
2022-03-15 11:54:06 +00:00
2022-02-21 14:36:00 +00:00
~TransObj() {
delete cli;
delete srv;
}
private:
Client *cli;
Server *srv;
2022-01-26 11:05:20 +00:00
};
class TransEnv : public ::testing::Test {
protected:
virtual void SetUp() {
// set up trans obj
tr = new TransObj();
}
virtual void TearDown() {
// tear down
delete tr;
}
TransObj *tr = NULL;
};
2022-01-26 12:35:13 +00:00
2022-03-14 14:21:30 +00:00
TEST_F(TransEnv, 01sendAndRec) {
2022-03-19 15:38:18 +00:00
for (int i = 0; i < 10; i++) {
2022-03-14 14:21:30 +00:00
SRpcMsg req = {0}, resp = {0};
req.msgType = 0;
req.pCont = rpcMallocCont(10);
req.contLen = 10;
tr->cliSendAndRecv(&req, &resp);
assert(resp.code == 0);
}
}
2022-02-21 14:36:00 +00:00
TEST_F(TransEnv, 02StopServer) {
for (int i = 0; i < 1; i++) {
SRpcMsg req = {0}, resp = {0};
req.msgType = 0;
2022-05-19 09:38:11 +00:00
req.info.ahandle = (void *)0x35;
2022-02-21 14:36:00 +00:00
req.pCont = rpcMallocCont(10);
req.contLen = 10;
tr->cliSendAndRecv(&req, &resp);
assert(resp.code == 0);
}
SRpcMsg req = {0}, resp = {0};
2022-05-19 09:38:11 +00:00
req.info.ahandle = (void *)0x35;
2022-02-21 14:36:00 +00:00
req.msgType = 1;
req.pCont = rpcMallocCont(10);
req.contLen = 10;
tr->StopSrv();
// tr->RestartSrv();
tr->cliSendAndRecv(&req, &resp);
assert(resp.code != 0);
2022-01-26 12:35:13 +00:00
}
2022-03-15 11:54:06 +00:00
TEST_F(TransEnv, clientUserDefined) {
tr->RestartSrv();
for (int i = 0; i < 10; i++) {
SRpcMsg req = {0}, resp = {0};
req.msgType = 0;
req.pCont = rpcMallocCont(10);
req.contLen = 10;
tr->cliSendAndRecv(&req, &resp);
assert(resp.code == 0);
}
//////////////////
}
2022-03-14 14:21:30 +00:00
TEST_F(TransEnv, cliPersistHandle) {
2022-03-15 11:54:06 +00:00
SRpcMsg resp = {0};
2022-10-13 05:59:18 +00:00
void *handle = NULL;
2022-03-15 11:54:06 +00:00
for (int i = 0; i < 10; i++) {
2022-03-21 10:09:23 +00:00
SRpcMsg req = {0};
2022-05-16 06:13:07 +00:00
req.info = resp.info;
req.info.persistHandle = 1;
2022-03-21 10:09:23 +00:00
2022-03-15 11:54:06 +00:00
req.msgType = 1;
req.pCont = rpcMallocCont(10);
req.contLen = 10;
tr->cliSendAndRecv(&req, &resp);
2022-03-19 15:38:18 +00:00
// if (i == 5) {
// std::cout << "stop server" << std::endl;
// tr->StopSrv();
//}
// if (i >= 6) {
// EXPECT_TRUE(resp.code != 0);
//}
2022-05-16 06:13:07 +00:00
handle = resp.info.handle;
2022-03-15 11:54:06 +00:00
}
2022-03-19 15:38:18 +00:00
rpcReleaseHandle(handle, TAOS_CONN_CLIENT);
for (int i = 0; i < 10; i++) {
SRpcMsg req = {0};
req.msgType = 1;
req.pCont = rpcMallocCont(10);
req.contLen = 10;
tr->cliSendAndRecv(&req, &resp);
}
taosMsleep(1000);
2022-03-15 15:47:37 +00:00
//////////////////
}
2022-03-20 10:08:33 +00:00
TEST_F(TransEnv, srvReleaseHandle) {
2022-03-15 15:47:37 +00:00
SRpcMsg resp = {0};
2022-03-20 10:08:33 +00:00
tr->SetSrvContinueSend(processReleaseHandleCb);
// tr->Restart(processReleaseHandleCb);
2022-10-13 05:59:18 +00:00
void *handle = NULL;
2022-03-21 08:37:19 +00:00
SRpcMsg req = {0};
2022-03-20 10:08:33 +00:00
for (int i = 0; i < 1; i++) {
2022-03-21 08:37:19 +00:00
memset(&req, 0, sizeof(req));
2022-05-16 06:13:07 +00:00
req.info = resp.info;
req.info.persistHandle = 1;
2022-03-15 15:47:37 +00:00
req.msgType = 1;
req.pCont = rpcMallocCont(10);
req.contLen = 10;
2022-03-20 10:08:33 +00:00
tr->cliSendAndRecv(&req, &resp);
// tr->cliSendAndRecvNoHandle(&req, &resp);
2022-03-15 15:47:37 +00:00
EXPECT_TRUE(resp.code == 0);
}
//////////////////
}
2022-06-15 00:06:29 +00:00
// reopen later
// TEST_F(TransEnv, cliReleaseHandleExcept) {
// SRpcMsg resp = {0};
// SRpcMsg req = {0};
// for (int i = 0; i < 3; i++) {
// memset(&req, 0, sizeof(req));
// req.info = resp.info;
// req.info.persistHandle = 1;
// req.info.ahandle = (void *)1234;
// req.msgType = 1;
// req.pCont = rpcMallocCont(10);
// req.contLen = 10;
// tr->cliSendAndRecv(&req, &resp);
// if (i == 1) {
// std::cout << "stop server" << std::endl;
// tr->StopSrv();
// }
// if (i > 1) {
// EXPECT_TRUE(resp.code != 0);
// }
// }
// //////////////////
//}
2022-03-15 11:54:06 +00:00
TEST_F(TransEnv, srvContinueSend) {
tr->SetSrvContinueSend(processContinueSend);
2022-03-21 08:37:19 +00:00
SRpcMsg req = {0}, resp = {0};
2022-03-15 11:54:06 +00:00
for (int i = 0; i < 10; i++) {
2022-05-19 09:38:11 +00:00
// memset(&req, 0, sizeof(req));
// memset(&resp, 0, sizeof(resp));
// req.msgType = 1;
// req.pCont = rpcMallocCont(10);
// req.contLen = 10;
// tr->cliSendAndRecv(&req, &resp);
2022-03-15 11:54:06 +00:00
}
2022-03-19 16:00:16 +00:00
taosMsleep(1000);
2022-03-14 14:21:30 +00:00
}
2022-03-15 15:47:37 +00:00
TEST_F(TransEnv, srvPersistHandleExcept) {
2022-03-16 05:30:03 +00:00
tr->SetSrvContinueSend(processContinueSend);
2022-03-18 01:17:56 +00:00
// tr->SetCliPersistFp(cliPersistHandle);
2022-03-16 05:30:03 +00:00
SRpcMsg resp = {0};
2022-03-21 08:37:19 +00:00
SRpcMsg req = {0};
2022-03-16 05:30:03 +00:00
for (int i = 0; i < 5; i++) {
2022-05-19 09:38:11 +00:00
// memset(&req, 0, sizeof(req));
// req.info = resp.info;
// req.msgType = 1;
// req.pCont = rpcMallocCont(10);
// req.contLen = 10;
// tr->cliSendAndRecv(&req, &resp);
// if (i > 2) {
// tr->StopCli();
// break;
//}
2022-03-16 05:30:03 +00:00
}
taosMsleep(2000);
// conn broken
2022-03-14 14:21:30 +00:00
//
}
2022-03-16 13:48:19 +00:00
TEST_F(TransEnv, cliPersistHandleExcept) {
tr->SetSrvContinueSend(processContinueSend);
SRpcMsg resp = {0};
2022-03-21 08:37:19 +00:00
SRpcMsg req = {0};
2022-03-16 13:48:19 +00:00
for (int i = 0; i < 5; i++) {
2022-05-19 09:38:11 +00:00
// memset(&req, 0, sizeof(req));
// req.info = resp.info;
// req.msgType = 1;
// req.pCont = rpcMallocCont(10);
// req.contLen = 10;
// tr->cliSendAndRecv(&req, &resp);
// if (i > 2) {
// tr->StopSrv();
// break;
//}
2022-03-16 13:48:19 +00:00
}
taosMsleep(2000);
// conn broken
//
}
2022-03-14 14:21:30 +00:00
2022-03-16 05:30:03 +00:00
TEST_F(TransEnv, multiCliPersistHandleExcept) {
// conn broken
2022-03-14 14:21:30 +00:00
}
2022-03-20 10:08:33 +00:00
TEST_F(TransEnv, queryExcept) {
tr->SetSrvContinueSend(processRegisterFailure);
SRpcMsg resp = {0};
2022-03-21 08:37:19 +00:00
SRpcMsg req = {0};
2022-05-19 09:38:11 +00:00
// for (int i = 0; i < 5; i++) {
// memset(&req, 0, sizeof(req));
// req.info = resp.info;
// req.info.persistHandle = 1;
// req.msgType = 1;
// req.pCont = rpcMallocCont(10);
// req.contLen = 10;
// tr->cliSendAndRecv(&req, &resp);
// if (i == 2) {
// rpcReleaseHandle(resp.info.handle, TAOS_CONN_CLIENT);
// tr->StopCli();
// break;
// }
//}
2022-03-20 10:08:33 +00:00
taosMsleep(4 * 1000);
}
2022-03-15 11:54:06 +00:00
TEST_F(TransEnv, noResp) {
2022-03-16 13:48:19 +00:00
SRpcMsg resp = {0};
2022-03-21 08:37:19 +00:00
SRpcMsg req = {0};
2022-05-19 09:38:11 +00:00
// for (int i = 0; i < 5; i++) {
// memset(&req, 0, sizeof(req));
// req.info.noResp = 1;
// req.msgType = 1;
// req.pCont = rpcMallocCont(10);
// req.contLen = 10;
// tr->cliSendAndRecv(&req, &resp);
//}
// taosMsleep(2000);
2022-03-16 13:48:19 +00:00
2022-03-15 11:54:06 +00:00
// no resp
}