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

101 lines
2.6 KiB
C++
Raw Normal View History

2022-10-27 09:38:33 +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");
}
2022-10-27 09:59:16 +00:00
SyncLocalCmd *createMsg() {
SyncLocalCmd *pMsg = syncLocalCmdBuild(1000);
2022-10-27 09:38:33 +00:00
pMsg->srcId.addr = syncUtilAddr2U64("127.0.0.1", 1234);
pMsg->srcId.vgId = 100;
pMsg->destId.addr = syncUtilAddr2U64("127.0.0.1", 5678);
pMsg->destId.vgId = 100;
2022-10-27 09:59:16 +00:00
pMsg->sdNewTerm = 123;
pMsg->cmd = SYNC_LOCAL_CMD_STEP_DOWN;
2022-10-27 09:38:33 +00:00
return pMsg;
}
void test1() {
2022-10-27 09:59:16 +00:00
SyncLocalCmd *pMsg = createMsg();
syncLocalCmdLog2((char *)"test1:", pMsg);
syncLocalCmdDestroy(pMsg);
2022-10-27 09:38:33 +00:00
}
void test2() {
2022-10-27 09:59:16 +00:00
SyncLocalCmd *pMsg = createMsg();
2022-10-27 09:38:33 +00:00
uint32_t len = pMsg->bytes;
char *serialized = (char *)taosMemoryMalloc(len);
2022-10-27 09:59:16 +00:00
syncLocalCmdSerialize(pMsg, serialized, len);
SyncLocalCmd *pMsg2 = syncLocalCmdBuild(1000);
syncLocalCmdDeserialize(serialized, len, pMsg2);
syncLocalCmdLog2((char *)"test2: syncLocalCmdSerialize -> syncLocalCmdDeserialize ", pMsg2);
2022-10-27 09:38:33 +00:00
taosMemoryFree(serialized);
2022-10-27 09:59:16 +00:00
syncLocalCmdDestroy(pMsg);
syncLocalCmdDestroy(pMsg2);
2022-10-27 09:38:33 +00:00
}
void test3() {
2022-10-27 09:59:16 +00:00
SyncLocalCmd *pMsg = createMsg();
2022-10-27 09:38:33 +00:00
uint32_t len;
2022-10-27 09:59:16 +00:00
char *serialized = syncLocalCmdSerialize2(pMsg, &len);
SyncLocalCmd *pMsg2 = syncLocalCmdDeserialize2(serialized, len);
syncLocalCmdLog2((char *)"test3: syncLocalCmdSerialize3 -> syncLocalCmdDeserialize2 ", pMsg2);
2022-10-27 09:38:33 +00:00
taosMemoryFree(serialized);
2022-10-27 09:59:16 +00:00
syncLocalCmdDestroy(pMsg);
syncLocalCmdDestroy(pMsg2);
2022-10-27 09:38:33 +00:00
}
void test4() {
2022-10-27 09:59:16 +00:00
SyncLocalCmd *pMsg = createMsg();
2022-10-27 09:38:33 +00:00
SRpcMsg rpcMsg;
2022-10-27 09:59:16 +00:00
syncLocalCmd2RpcMsg(pMsg, &rpcMsg);
SyncLocalCmd *pMsg2 = (SyncLocalCmd *)taosMemoryMalloc(rpcMsg.contLen);
syncLocalCmdFromRpcMsg(&rpcMsg, pMsg2);
syncLocalCmdLog2((char *)"test4: syncLocalCmd2RpcMsg -> syncLocalCmdFromRpcMsg ", pMsg2);
2022-10-27 09:38:33 +00:00
rpcFreeCont(rpcMsg.pCont);
2022-10-27 09:59:16 +00:00
syncLocalCmdDestroy(pMsg);
syncLocalCmdDestroy(pMsg2);
2022-10-27 09:38:33 +00:00
}
void test5() {
2022-10-27 09:59:16 +00:00
SyncLocalCmd *pMsg = createMsg();
2022-10-27 09:38:33 +00:00
SRpcMsg rpcMsg;
2022-10-27 09:59:16 +00:00
syncLocalCmd2RpcMsg(pMsg, &rpcMsg);
SyncLocalCmd *pMsg2 = syncLocalCmdFromRpcMsg2(&rpcMsg);
syncLocalCmdLog2((char *)"test5: syncLocalCmd2RpcMsg -> syncLocalCmdFromRpcMsg2 ", pMsg2);
2022-10-27 09:38:33 +00:00
rpcFreeCont(rpcMsg.pCont);
2022-10-27 09:59:16 +00:00
syncLocalCmdDestroy(pMsg);
syncLocalCmdDestroy(pMsg2);
2022-10-27 09:38:33 +00:00
}
int main() {
2022-10-27 09:59:16 +00:00
gRaftDetailLog = true;
2022-10-27 09:38:33 +00:00
tsAsyncLog = 0;
sDebugFlag = DEBUG_TRACE + DEBUG_SCREEN + DEBUG_FILE;
logTest();
test1();
test2();
test3();
test4();
test5();
return 0;
}