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

268 lines
6.8 KiB
C++
Raw Normal View History

2022-03-01 08:19:42 +00:00
#include <stdio.h>
#include "syncEnv.h"
#include "syncIO.h"
#include "syncInt.h"
#include "syncMessage.h"
2022-03-03 06:52:30 +00:00
#include "syncUtil.h"
2022-03-01 08:19:42 +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");
}
#define PING_MSG_LEN 20
2022-03-01 08:42:37 +00:00
void test1() {
2022-03-01 10:35:07 +00:00
sTrace("test1: ---- syncPingSerialize, syncPingDeserialize");
2022-03-01 08:19:42 +00:00
char msg[PING_MSG_LEN];
snprintf(msg, sizeof(msg), "%s", "test ping");
2022-03-01 10:35:07 +00:00
SyncPing* pMsg = syncPingBuild(PING_MSG_LEN);
2022-03-03 06:52:30 +00:00
pMsg->srcId.addr = syncUtilAddr2U64("127.0.0.1", 1111);
pMsg->srcId.vgId = 100;
pMsg->destId.addr = syncUtilAddr2U64("127.0.0.1", 2222);
pMsg->destId.vgId = 100;
2022-03-01 10:35:07 +00:00
memcpy(pMsg->data, msg, PING_MSG_LEN);
2022-03-01 08:19:42 +00:00
{
2022-03-01 10:35:07 +00:00
cJSON* pJson = syncPing2Json(pMsg);
2022-03-01 08:19:42 +00:00
char* serialized = cJSON_Print(pJson);
2022-03-03 06:52:30 +00:00
printf("\n%s\n\n", serialized);
2022-03-01 08:19:42 +00:00
free(serialized);
cJSON_Delete(pJson);
}
2022-03-01 10:35:07 +00:00
uint32_t bufLen = pMsg->bytes;
2022-03-01 08:19:42 +00:00
char* buf = (char*)malloc(bufLen);
2022-03-01 10:35:07 +00:00
syncPingSerialize(pMsg, buf, bufLen);
2022-03-01 08:42:37 +00:00
2022-03-01 10:35:07 +00:00
SyncPing* pMsg2 = (SyncPing*)malloc(pMsg->bytes);
syncPingDeserialize(buf, bufLen, pMsg2);
2022-03-01 08:19:42 +00:00
{
2022-03-01 10:35:07 +00:00
cJSON* pJson = syncPing2Json(pMsg2);
2022-03-01 08:19:42 +00:00
char* serialized = cJSON_Print(pJson);
2022-03-03 06:52:30 +00:00
printf("\n%s\n\n", serialized);
2022-03-01 08:19:42 +00:00
free(serialized);
cJSON_Delete(pJson);
}
2022-03-01 10:35:07 +00:00
syncPingDestroy(pMsg);
syncPingDestroy(pMsg2);
2022-03-01 08:19:42 +00:00
free(buf);
2022-03-01 08:42:37 +00:00
}
void test2() {
2022-03-01 10:35:07 +00:00
sTrace("test2: ---- syncPing2RpcMsg, syncPingFromRpcMsg");
2022-03-01 08:42:37 +00:00
char msg[PING_MSG_LEN];
snprintf(msg, sizeof(msg), "%s", "hello raft");
2022-03-01 10:35:07 +00:00
SyncPing* pMsg = syncPingBuild(PING_MSG_LEN);
2022-03-03 06:52:30 +00:00
pMsg->srcId.addr = syncUtilAddr2U64("127.0.0.1", 3333);
2022-03-01 10:35:07 +00:00
pMsg->srcId.vgId = 200;
2022-03-03 06:52:30 +00:00
pMsg->destId.addr = syncUtilAddr2U64("127.0.0.1", 4444);
pMsg->destId.vgId = 200;
2022-03-01 10:35:07 +00:00
memcpy(pMsg->data, msg, PING_MSG_LEN);
2022-03-01 08:42:37 +00:00
{
2022-03-01 10:35:07 +00:00
cJSON* pJson = syncPing2Json(pMsg);
2022-03-01 08:42:37 +00:00
char* serialized = cJSON_Print(pJson);
2022-03-03 06:52:30 +00:00
printf("\n%s\n\n", serialized);
2022-03-01 08:42:37 +00:00
free(serialized);
cJSON_Delete(pJson);
}
SRpcMsg rpcMsg;
2022-03-01 10:35:07 +00:00
syncPing2RpcMsg(pMsg, &rpcMsg);
SyncPing* pMsg2 = (SyncPing*)malloc(pMsg->bytes);
syncPingFromRpcMsg(&rpcMsg, pMsg2);
2022-03-01 08:42:37 +00:00
rpcFreeCont(rpcMsg.pCont);
{
2022-03-01 10:35:07 +00:00
cJSON* pJson = syncPing2Json(pMsg2);
2022-03-01 08:42:37 +00:00
char* serialized = cJSON_Print(pJson);
2022-03-03 06:52:30 +00:00
printf("\n%s\n\n", serialized);
2022-03-01 08:42:37 +00:00
free(serialized);
cJSON_Delete(pJson);
}
2022-03-01 10:35:07 +00:00
syncPingDestroy(pMsg);
syncPingDestroy(pMsg2);
2022-03-01 08:42:37 +00:00
}
2022-03-01 10:35:07 +00:00
void test3() {
sTrace("test3: ---- syncPingReplySerialize, syncPingReplyDeserialize");
char msg[PING_MSG_LEN];
snprintf(msg, sizeof(msg), "%s", "test ping");
SyncPingReply* pMsg = syncPingReplyBuild(PING_MSG_LEN);
2022-03-03 06:52:30 +00:00
pMsg->srcId.addr = syncUtilAddr2U64("127.0.0.1", 5555);
pMsg->srcId.vgId = 100;
pMsg->destId.addr = syncUtilAddr2U64("127.0.0.1", 6666);
pMsg->destId.vgId = 100;
2022-03-01 10:35:07 +00:00
memcpy(pMsg->data, msg, PING_MSG_LEN);
{
cJSON* pJson = syncPingReply2Json(pMsg);
char* serialized = cJSON_Print(pJson);
2022-03-03 06:52:30 +00:00
printf("\n%s\n\n", serialized);
2022-03-01 10:35:07 +00:00
free(serialized);
cJSON_Delete(pJson);
}
uint32_t bufLen = pMsg->bytes;
char* buf = (char*)malloc(bufLen);
syncPingReplySerialize(pMsg, buf, bufLen);
SyncPingReply* pMsg2 = (SyncPingReply*)malloc(pMsg->bytes);
syncPingReplyDeserialize(buf, bufLen, pMsg2);
{
cJSON* pJson = syncPingReply2Json(pMsg2);
char* serialized = cJSON_Print(pJson);
2022-03-03 06:52:30 +00:00
printf("\n%s\n\n", serialized);
2022-03-01 10:35:07 +00:00
free(serialized);
cJSON_Delete(pJson);
}
syncPingReplyDestroy(pMsg);
syncPingReplyDestroy(pMsg2);
free(buf);
}
void test4() {
sTrace("test4: ---- syncPingReply2RpcMsg, syncPingReplyFromRpcMsg");
char msg[PING_MSG_LEN];
snprintf(msg, sizeof(msg), "%s", "hello raft");
SyncPingReply* pMsg = syncPingReplyBuild(PING_MSG_LEN);
2022-03-03 06:52:30 +00:00
pMsg->srcId.addr = syncUtilAddr2U64("127.0.0.1", 7777);
pMsg->srcId.vgId = 100;
pMsg->destId.addr = syncUtilAddr2U64("127.0.0.1", 8888);
pMsg->destId.vgId = 100;
2022-03-01 10:35:07 +00:00
memcpy(pMsg->data, msg, PING_MSG_LEN);
{
cJSON* pJson = syncPingReply2Json(pMsg);
char* serialized = cJSON_Print(pJson);
2022-03-03 06:52:30 +00:00
printf("\n%s\n\n", serialized);
2022-03-01 10:35:07 +00:00
free(serialized);
cJSON_Delete(pJson);
}
SRpcMsg rpcMsg;
syncPingReply2RpcMsg(pMsg, &rpcMsg);
SyncPingReply* pMsg2 = (SyncPingReply*)malloc(pMsg->bytes);
syncPingReplyFromRpcMsg(&rpcMsg, pMsg2);
rpcFreeCont(rpcMsg.pCont);
{
cJSON* pJson = syncPingReply2Json(pMsg2);
char* serialized = cJSON_Print(pJson);
2022-03-03 06:52:30 +00:00
printf("\n%s\n\n", serialized);
2022-03-01 10:35:07 +00:00
free(serialized);
cJSON_Delete(pJson);
}
syncPingReplyDestroy(pMsg);
syncPingReplyDestroy(pMsg2);
}
2022-03-03 06:52:30 +00:00
void test5() {
sTrace("test5: ---- syncRequestVoteSerialize, syncRequestVoteDeserialize");
SyncRequestVote* pMsg = syncRequestVoteBuild();
pMsg->srcId.addr = syncUtilAddr2U64("127.0.0.1", 1234);
pMsg->srcId.vgId = 100;
pMsg->destId.addr = syncUtilAddr2U64("8.8.8.8", 5678);
pMsg->destId.vgId = 100;
pMsg->currentTerm = 20;
pMsg->lastLogIndex = 21;
pMsg->lastLogTerm = 22;
{
cJSON* pJson = syncRequestVote2Json(pMsg);
char* serialized = cJSON_Print(pJson);
printf("\n%s\n\n", serialized);
free(serialized);
cJSON_Delete(pJson);
}
uint32_t bufLen = pMsg->bytes;
char* buf = (char*)malloc(bufLen);
syncRequestVoteSerialize(pMsg, buf, bufLen);
SyncRequestVote* pMsg2 = (SyncRequestVote*)malloc(pMsg->bytes);
syncRequestVoteDeserialize(buf, bufLen, pMsg2);
{
cJSON* pJson = syncRequestVote2Json(pMsg2);
char* serialized = cJSON_Print(pJson);
printf("\n%s\n\n", serialized);
free(serialized);
cJSON_Delete(pJson);
}
syncRequestVoteDestroy(pMsg);
syncRequestVoteDestroy(pMsg2);
free(buf);
}
void test6() {
sTrace("test6: ---- syncRequestVoteReplySerialize, syncRequestVoteReplyDeserialize");
SyncRequestVoteReply* pMsg = SyncRequestVoteReplyBuild();
pMsg->srcId.addr = syncUtilAddr2U64("127.0.0.1", 1234);
pMsg->srcId.vgId = 100;
pMsg->destId.addr = syncUtilAddr2U64("8.8.8.8", 5678);
pMsg->destId.vgId = 100;
pMsg->term = 20;
pMsg->voteGranted = 1;
{
cJSON* pJson = syncRequestVoteReply2Json(pMsg);
char* serialized = cJSON_Print(pJson);
printf("\n%s\n\n", serialized);
free(serialized);
cJSON_Delete(pJson);
}
uint32_t bufLen = pMsg->bytes;
char* buf = (char*)malloc(bufLen);
syncRequestVoteReplySerialize(pMsg, buf, bufLen);
SyncRequestVoteReply* pMsg2 = (SyncRequestVoteReply*)malloc(pMsg->bytes);
syncRequestVoteReplyDeserialize(buf, bufLen, pMsg2);
{
cJSON* pJson = syncRequestVoteReply2Json(pMsg2);
char* serialized = cJSON_Print(pJson);
printf("\n%s\n\n", serialized);
free(serialized);
cJSON_Delete(pJson);
}
syncRequestVoteReplyDestroy(pMsg);
syncRequestVoteReplyDestroy(pMsg2);
free(buf);
}
2022-03-01 08:42:37 +00:00
int main() {
// taosInitLog((char*)"syncPingTest.log", 100000, 10);
tsAsyncLog = 0;
sDebugFlag = 143 + 64;
test1();
test2();
2022-03-01 10:35:07 +00:00
test3();
test4();
2022-03-03 06:52:30 +00:00
test5();
test6();
2022-03-01 08:19:42 +00:00
return 0;
}