From 12050c9a2a0734290f83821e33ca2f283f3fb8ae Mon Sep 17 00:00:00 2001 From: Minghao Li Date: Tue, 1 Mar 2022 16:42:37 +0800 Subject: [PATCH] sync encode test --- source/libs/sync/inc/syncMessage.h | 2 + source/libs/sync/src/syncMessage.c | 12 +++--- source/libs/sync/test/syncEncodeTest.cpp | 55 +++++++++++++++++++++--- 3 files changed, 58 insertions(+), 11 deletions(-) diff --git a/source/libs/sync/inc/syncMessage.h b/source/libs/sync/inc/syncMessage.h index 5f73601576..121314c589 100644 --- a/source/libs/sync/inc/syncMessage.h +++ b/source/libs/sync/inc/syncMessage.h @@ -129,6 +129,8 @@ void syncPingDeserialize(const char* buf, uint32_t len, SyncPing* pSyncPing); void syncPing2RpcMsg(const SyncPing* pSyncPing, SRpcMsg* pRpcMsg); +void syncPingFromRpcMsg(const SRpcMsg* pRpcMsg, SyncPing* pSyncPing); + cJSON* syncPing2Json(const SyncPing* pSyncPing); #ifdef __cplusplus diff --git a/source/libs/sync/src/syncMessage.c b/source/libs/sync/src/syncMessage.c index dc584c4e7b..80dd184018 100644 --- a/source/libs/sync/src/syncMessage.c +++ b/source/libs/sync/src/syncMessage.c @@ -53,13 +53,13 @@ void syncPingDeserialize(const char* buf, uint32_t len, SyncPing* pSyncPing) { void syncPing2RpcMsg(const SyncPing* pSyncPing, SRpcMsg* pRpcMsg) { pRpcMsg->msgType = pSyncPing->msgType; - uint32_t bufLen = pSyncPing->bytes; - char* buf = malloc(bufLen); - syncPingSerialize(pSyncPing, buf, bufLen); - pRpcMsg->contLen = bufLen; + pRpcMsg->contLen = pSyncPing->bytes; pRpcMsg->pCont = rpcMallocCont(pRpcMsg->contLen); - memcpy(pRpcMsg->pCont, buf, pRpcMsg->contLen); - free(buf); + syncPingSerialize(pSyncPing, pRpcMsg->pCont, pRpcMsg->contLen); +} + +void syncPingFromRpcMsg(const SRpcMsg* pRpcMsg, SyncPing* pSyncPing) { + syncPingDeserialize(pRpcMsg->pCont, pRpcMsg->contLen, pSyncPing); } cJSON* syncPing2Json(const SyncPing* pSyncPing) { diff --git a/source/libs/sync/test/syncEncodeTest.cpp b/source/libs/sync/test/syncEncodeTest.cpp index 1e8007e422..029e2bd496 100644 --- a/source/libs/sync/test/syncEncodeTest.cpp +++ b/source/libs/sync/test/syncEncodeTest.cpp @@ -15,10 +15,8 @@ void logTest() { #define PING_MSG_LEN 20 -int main() { - // taosInitLog((char*)"syncPingTest.log", 100000, 10); - tsAsyncLog = 0; - sDebugFlag = 143 + 64; +void test1() { + sTrace("test1: ----"); char msg[PING_MSG_LEN]; snprintf(msg, sizeof(msg), "%s", "test ping"); @@ -40,7 +38,7 @@ int main() { uint32_t bufLen = pSyncPing->bytes; char* buf = (char*)malloc(bufLen); syncPingSerialize(pSyncPing, buf, bufLen); - + SyncPing* pSyncPing2 = (SyncPing*)malloc(pSyncPing->bytes); syncPingDeserialize(buf, bufLen, pSyncPing2); @@ -55,6 +53,53 @@ int main() { syncPingDestroy(pSyncPing); syncPingDestroy(pSyncPing2); free(buf); +} + +void test2() { + sTrace("test2: ----"); + + char msg[PING_MSG_LEN]; + snprintf(msg, sizeof(msg), "%s", "hello raft"); + SyncPing* pSyncPing = syncPingBuild(PING_MSG_LEN); + pSyncPing->srcId.addr = 100; + pSyncPing->srcId.vgId = 200; + pSyncPing->destId.addr = 300; + pSyncPing->destId.vgId = 400; + memcpy(pSyncPing->data, msg, PING_MSG_LEN); + + { + cJSON* pJson = syncPing2Json(pSyncPing); + char* serialized = cJSON_Print(pJson); + printf("SyncPing: \n%s\n\n", serialized); + free(serialized); + cJSON_Delete(pJson); + } + + SRpcMsg rpcMsg; + syncPing2RpcMsg(pSyncPing, &rpcMsg); + SyncPing* pSyncPing2 = (SyncPing*)malloc(pSyncPing->bytes); + syncPingFromRpcMsg(&rpcMsg, pSyncPing2); + rpcFreeCont(rpcMsg.pCont); + + { + cJSON* pJson = syncPing2Json(pSyncPing2); + char* serialized = cJSON_Print(pJson); + printf("SyncPing2: \n%s\n\n", serialized); + free(serialized); + cJSON_Delete(pJson); + } + + syncPingDestroy(pSyncPing); + syncPingDestroy(pSyncPing2); +} + +int main() { + // taosInitLog((char*)"syncPingTest.log", 100000, 10); + tsAsyncLog = 0; + sDebugFlag = 143 + 64; + + test1(); + test2(); return 0; }