TDengine/source/libs/sync/test/syncClientRequestTest.cpp

99 lines
2.9 KiB
C++
Raw Normal View History

2022-03-11 09:08:27 +00:00
#include <gtest/gtest.h>
#include <stdio.h>
#include "syncIO.h"
#include "syncInt.h"
#include "syncMessage.h"
#include "syncUtil.h"
void logTest() {
sTrace("--- sync log test: trace");
sDebug("--- sync log test: debug");
sInfo("--- sync log test: info");
sWarn("--- sync log test: warn");
sError("--- sync log test: error");
sFatal("--- sync log test: fatal");
}
SyncClientRequest *createMsg() {
SRpcMsg rpcMsg;
memset(&rpcMsg, 0, sizeof(rpcMsg));
rpcMsg.msgType = 12345;
rpcMsg.contLen = 20;
rpcMsg.pCont = rpcMallocCont(rpcMsg.contLen);
2022-03-11 11:11:38 +00:00
strcpy((char *)rpcMsg.pCont, "hello rpc");
2022-11-07 12:31:26 +00:00
SyncClientRequest *pMsg = syncClientRequestBuild(&rpcMsg, 123, true, 1000);
2022-04-18 13:50:56 +00:00
rpcFreeCont(rpcMsg.pCont);
2022-03-11 09:08:27 +00:00
return pMsg;
}
void test1() {
SyncClientRequest *pMsg = createMsg();
2022-04-18 13:50:56 +00:00
syncClientRequestLog2((char *)"test1:", pMsg);
2022-03-11 09:08:27 +00:00
syncClientRequestDestroy(pMsg);
}
void test2() {
SyncClientRequest *pMsg = createMsg();
uint32_t len = pMsg->bytes;
2022-10-13 06:06:27 +00:00
char *serialized = (char *)taosMemoryMalloc(len);
2022-03-11 09:08:27 +00:00
syncClientRequestSerialize(pMsg, serialized, len);
2022-11-07 12:31:26 +00:00
SyncClientRequest *pMsg2 = syncClientRequestAlloc(pMsg->dataLen);
2022-03-11 09:08:27 +00:00
syncClientRequestDeserialize(serialized, len, pMsg2);
2022-04-18 13:50:56 +00:00
syncClientRequestLog2((char *)"test2: syncClientRequestSerialize -> syncClientRequestDeserialize ", pMsg2);
2022-03-11 09:08:27 +00:00
2022-03-25 16:29:53 +00:00
taosMemoryFree(serialized);
2022-03-11 09:08:27 +00:00
syncClientRequestDestroy(pMsg);
syncClientRequestDestroy(pMsg2);
}
void test3() {
SyncClientRequest *pMsg = createMsg();
uint32_t len;
2022-10-13 06:06:27 +00:00
char *serialized = syncClientRequestSerialize2(pMsg, &len);
2022-03-11 09:08:27 +00:00
SyncClientRequest *pMsg2 = syncClientRequestDeserialize2(serialized, len);
2022-04-18 13:50:56 +00:00
syncClientRequestLog2((char *)"test3: syncClientRequestSerialize3 -> syncClientRequestDeserialize2 ", pMsg2);
2022-03-11 09:08:27 +00:00
2022-03-25 16:29:53 +00:00
taosMemoryFree(serialized);
2022-03-11 09:08:27 +00:00
syncClientRequestDestroy(pMsg);
syncClientRequestDestroy(pMsg2);
}
void test4() {
SyncClientRequest *pMsg = createMsg();
2022-11-07 12:31:26 +00:00
SRpcMsg rpcMsg = {0};
2022-03-11 09:08:27 +00:00
syncClientRequest2RpcMsg(pMsg, &rpcMsg);
2022-03-25 16:29:53 +00:00
SyncClientRequest *pMsg2 = (SyncClientRequest *)taosMemoryMalloc(rpcMsg.contLen);
2022-03-11 09:08:27 +00:00
syncClientRequestFromRpcMsg(&rpcMsg, pMsg2);
2022-04-18 13:50:56 +00:00
syncClientRequestLog2((char *)"test4: syncClientRequest2RpcMsg -> syncClientRequestFromRpcMsg ", pMsg2);
2022-03-11 09:08:27 +00:00
2022-04-18 13:50:56 +00:00
rpcFreeCont(rpcMsg.pCont);
2022-03-11 09:08:27 +00:00
syncClientRequestDestroy(pMsg);
syncClientRequestDestroy(pMsg2);
}
void test5() {
SyncClientRequest *pMsg = createMsg();
2022-11-07 12:31:26 +00:00
SRpcMsg rpcMsg = {0};
2022-03-11 09:08:27 +00:00
syncClientRequest2RpcMsg(pMsg, &rpcMsg);
SyncClientRequest *pMsg2 = syncClientRequestFromRpcMsg2(&rpcMsg);
2022-04-18 13:50:56 +00:00
syncClientRequestLog2((char *)"test5: syncClientRequest2RpcMsg -> syncClientRequestFromRpcMsg2 ", pMsg2);
2022-03-11 09:08:27 +00:00
2022-04-18 13:50:56 +00:00
rpcFreeCont(rpcMsg.pCont);
2022-03-11 09:08:27 +00:00
syncClientRequestDestroy(pMsg);
syncClientRequestDestroy(pMsg2);
}
int main() {
tsAsyncLog = 0;
2022-04-18 13:50:56 +00:00
sDebugFlag = DEBUG_TRACE + DEBUG_SCREEN + DEBUG_FILE;
2022-03-11 09:08:27 +00:00
logTest();
test1();
test2();
test3();
test4();
test5();
return 0;
}