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

103 lines
3.2 KiB
C++
Raw Permalink Normal View History

2022-03-11 07:05:10 +00:00
#include <gtest/gtest.h>
2022-11-10 04:43:23 +00:00
#include "syncTest.h"
2022-03-11 07:05:10 +00:00
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");
}
SyncAppendEntriesReply *createMsg() {
2022-04-18 13:50:56 +00:00
SyncAppendEntriesReply *pMsg = syncAppendEntriesReplyBuild(1000);
2022-03-11 07:05:10 +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;
pMsg->success = true;
pMsg->matchIndex = 77;
pMsg->term = 33;
2022-12-07 14:24:47 +00:00
// pMsg->privateTerm = 44;
pMsg->startTime = taosGetTimestampMs();
2022-03-11 11:52:59 +00:00
return pMsg;
2022-03-11 07:05:10 +00:00
}
void test1() {
SyncAppendEntriesReply *pMsg = createMsg();
2022-04-18 13:50:56 +00:00
syncAppendEntriesReplyLog2((char *)"test1:", pMsg);
2022-03-11 07:05:10 +00:00
syncAppendEntriesReplyDestroy(pMsg);
}
void test2() {
SyncAppendEntriesReply *pMsg = createMsg();
2022-03-11 09:08:27 +00:00
uint32_t len = pMsg->bytes;
2022-10-13 06:06:27 +00:00
char *serialized = (char *)taosMemoryMalloc(len);
2022-03-11 07:05:10 +00:00
syncAppendEntriesReplySerialize(pMsg, serialized, len);
2022-04-18 13:50:56 +00:00
SyncAppendEntriesReply *pMsg2 = syncAppendEntriesReplyBuild(1000);
2022-03-11 07:05:10 +00:00
syncAppendEntriesReplyDeserialize(serialized, len, pMsg2);
2022-04-18 13:50:56 +00:00
syncAppendEntriesReplyLog2((char *)"test2: syncAppendEntriesReplySerialize -> syncAppendEntriesReplyDeserialize ",
pMsg2);
2022-03-11 07:05:10 +00:00
2022-03-25 16:29:53 +00:00
taosMemoryFree(serialized);
2022-03-11 07:05:10 +00:00
syncAppendEntriesReplyDestroy(pMsg);
syncAppendEntriesReplyDestroy(pMsg2);
}
void test3() {
SyncAppendEntriesReply *pMsg = createMsg();
2022-03-11 09:08:27 +00:00
uint32_t len;
2022-10-13 06:06:27 +00:00
char *serialized = syncAppendEntriesReplySerialize2(pMsg, &len);
2022-03-11 07:05:10 +00:00
SyncAppendEntriesReply *pMsg2 = syncAppendEntriesReplyDeserialize2(serialized, len);
2022-04-18 13:50:56 +00:00
syncAppendEntriesReplyLog2((char *)"test3: syncAppendEntriesReplySerialize3 -> syncAppendEntriesReplyDeserialize2 ",
pMsg2);
2022-03-11 07:05:10 +00:00
2022-03-25 16:29:53 +00:00
taosMemoryFree(serialized);
2022-03-11 07:05:10 +00:00
syncAppendEntriesReplyDestroy(pMsg);
syncAppendEntriesReplyDestroy(pMsg2);
}
void test4() {
SyncAppendEntriesReply *pMsg = createMsg();
2022-03-11 09:08:27 +00:00
SRpcMsg rpcMsg;
2022-03-11 07:05:10 +00:00
syncAppendEntriesReply2RpcMsg(pMsg, &rpcMsg);
2022-04-18 13:50:56 +00:00
SyncAppendEntriesReply *pMsg2 = syncAppendEntriesReplyBuild(1000);
2022-03-11 07:05:10 +00:00
syncAppendEntriesReplyFromRpcMsg(&rpcMsg, pMsg2);
2022-04-18 13:50:56 +00:00
syncAppendEntriesReplyLog2((char *)"test4: syncAppendEntriesReply2RpcMsg -> syncAppendEntriesReplyFromRpcMsg ",
pMsg2);
2022-03-11 07:05:10 +00:00
2022-04-18 13:50:56 +00:00
rpcFreeCont(rpcMsg.pCont);
2022-03-11 07:05:10 +00:00
syncAppendEntriesReplyDestroy(pMsg);
syncAppendEntriesReplyDestroy(pMsg2);
}
void test5() {
SyncAppendEntriesReply *pMsg = createMsg();
2022-03-11 09:08:27 +00:00
SRpcMsg rpcMsg;
2022-03-11 07:05:10 +00:00
syncAppendEntriesReply2RpcMsg(pMsg, &rpcMsg);
SyncAppendEntriesReply *pMsg2 = syncAppendEntriesReplyFromRpcMsg2(&rpcMsg);
2022-04-18 13:50:56 +00:00
syncAppendEntriesReplyLog2((char *)"test5: syncAppendEntriesReply2RpcMsg -> syncAppendEntriesReplyFromRpcMsg2 ",
pMsg2);
2022-03-11 07:05:10 +00:00
2022-04-18 13:50:56 +00:00
rpcFreeCont(rpcMsg.pCont);
2022-03-11 07:05:10 +00:00
syncAppendEntriesReplyDestroy(pMsg);
syncAppendEntriesReplyDestroy(pMsg2);
}
int main() {
gRaftDetailLog = true;
2022-03-11 07:05:10 +00:00
tsAsyncLog = 0;
2022-04-18 13:50:56 +00:00
sDebugFlag = DEBUG_TRACE + DEBUG_SCREEN + DEBUG_FILE;
2022-03-11 07:05:10 +00:00
logTest();
test1();
test2();
test3();
test4();
test5();
return 0;
}