提交 7469d62f 编写于 作者: M Minghao Li

sync refactor

上级 a0a62227
...@@ -218,17 +218,23 @@ typedef struct SyncAppendEntries { ...@@ -218,17 +218,23 @@ typedef struct SyncAppendEntries {
char data[]; char data[];
} SyncAppendEntries; } SyncAppendEntries;
#define SYNC_APPEND_ENTRIES_FIX_LEN \
(sizeof(uint32_t) + sizeof(uint32_t) + sizeof(SRaftId) + sizeof(SRaftId) + sizeof(SyncIndex) + sizeof(SyncTerm) + \
sizeof(SyncIndex) + sizeof(uint32_t))
SyncAppendEntries* syncAppendEntriesBuild(uint32_t dataLen); SyncAppendEntries* syncAppendEntriesBuild(uint32_t dataLen);
void syncAppendEntriesDestroy(SyncAppendEntries* pMsg); void syncAppendEntriesDestroy(SyncAppendEntries* pMsg);
void syncAppendEntriesSerialize(const SyncAppendEntries* pMsg, char* buf, uint32_t bufLen); void syncAppendEntriesSerialize(const SyncAppendEntries* pMsg, char* buf, uint32_t bufLen);
void syncAppendEntriesDeserialize(const char* buf, uint32_t len, SyncAppendEntries* pMsg); void syncAppendEntriesDeserialize(const char* buf, uint32_t len, SyncAppendEntries* pMsg);
char* syncAppendEntriesSerialize2(const SyncAppendEntries* pMsg, uint32_t* len);
SyncAppendEntries* syncAppendEntriesDeserialize2(const char* buf, uint32_t len);
void syncAppendEntries2RpcMsg(const SyncAppendEntries* pMsg, SRpcMsg* pRpcMsg); void syncAppendEntries2RpcMsg(const SyncAppendEntries* pMsg, SRpcMsg* pRpcMsg);
void syncAppendEntriesFromRpcMsg(const SRpcMsg* pRpcMsg, SyncAppendEntries* pMsg); void syncAppendEntriesFromRpcMsg(const SRpcMsg* pRpcMsg, SyncAppendEntries* pMsg);
SyncAppendEntries* syncAppendEntriesFromRpcMsg2(const SRpcMsg* pRpcMsg);
cJSON* syncAppendEntries2Json(const SyncAppendEntries* pMsg); cJSON* syncAppendEntries2Json(const SyncAppendEntries* pMsg);
char* syncAppendEntries2Str(const SyncAppendEntries* pMsg);
// for debug ----------------------
void syncAppendEntriesPrint(const SyncAppendEntries* pMsg);
void syncAppendEntriesPrint2(char* s, const SyncAppendEntries* pMsg);
void syncAppendEntriesLog(const SyncAppendEntries* pMsg);
void syncAppendEntriesLog2(char* s, const SyncAppendEntries* pMsg);
// --------------------------------------------- // ---------------------------------------------
typedef struct SyncAppendEntriesReply { typedef struct SyncAppendEntriesReply {
...@@ -245,9 +251,19 @@ SyncAppendEntriesReply* syncAppendEntriesReplyBuild(); ...@@ -245,9 +251,19 @@ SyncAppendEntriesReply* syncAppendEntriesReplyBuild();
void syncAppendEntriesReplyDestroy(SyncAppendEntriesReply* pMsg); void syncAppendEntriesReplyDestroy(SyncAppendEntriesReply* pMsg);
void syncAppendEntriesReplySerialize(const SyncAppendEntriesReply* pMsg, char* buf, uint32_t bufLen); void syncAppendEntriesReplySerialize(const SyncAppendEntriesReply* pMsg, char* buf, uint32_t bufLen);
void syncAppendEntriesReplyDeserialize(const char* buf, uint32_t len, SyncAppendEntriesReply* pMsg); void syncAppendEntriesReplyDeserialize(const char* buf, uint32_t len, SyncAppendEntriesReply* pMsg);
char* syncAppendEntriesReplySerialize2(const SyncAppendEntriesReply* pMsg, uint32_t* len);
SyncAppendEntriesReply* syncAppendEntriesReplyDeserialize2(const char* buf, uint32_t len);
void syncAppendEntriesReply2RpcMsg(const SyncAppendEntriesReply* pMsg, SRpcMsg* pRpcMsg); void syncAppendEntriesReply2RpcMsg(const SyncAppendEntriesReply* pMsg, SRpcMsg* pRpcMsg);
void syncAppendEntriesReplyFromRpcMsg(const SRpcMsg* pRpcMsg, SyncAppendEntriesReply* pMsg); void syncAppendEntriesReplyFromRpcMsg(const SRpcMsg* pRpcMsg, SyncAppendEntriesReply* pMsg);
SyncAppendEntriesReply* syncAppendEntriesReplyFromRpcMsg2(const SRpcMsg* pRpcMsg);
cJSON* syncAppendEntriesReply2Json(const SyncAppendEntriesReply* pMsg); cJSON* syncAppendEntriesReply2Json(const SyncAppendEntriesReply* pMsg);
char* syncAppendEntriesReply2Str(const SyncAppendEntriesReply* pMsg);
// for debug ----------------------
void syncAppendEntriesReplyPrint(const SyncAppendEntriesReply* pMsg);
void syncAppendEntriesReplyPrint2(char* s, const SyncAppendEntriesReply* pMsg);
void syncAppendEntriesReplyLog(const SyncAppendEntriesReply* pMsg);
void syncAppendEntriesReplyLog2(char* s, const SyncAppendEntriesReply* pMsg);
#ifdef __cplusplus #ifdef __cplusplus
} }
......
...@@ -700,7 +700,7 @@ void syncRequestVoteReplyLog2(char* s, const SyncRequestVoteReply* pMsg) { ...@@ -700,7 +700,7 @@ void syncRequestVoteReplyLog2(char* s, const SyncRequestVoteReply* pMsg) {
// ---- message process SyncAppendEntries---- // ---- message process SyncAppendEntries----
SyncAppendEntries* syncAppendEntriesBuild(uint32_t dataLen) { SyncAppendEntries* syncAppendEntriesBuild(uint32_t dataLen) {
uint32_t bytes = SYNC_APPEND_ENTRIES_FIX_LEN + dataLen; uint32_t bytes = sizeof(SyncAppendEntries) + dataLen;
SyncAppendEntries* pMsg = malloc(bytes); SyncAppendEntries* pMsg = malloc(bytes);
memset(pMsg, 0, bytes); memset(pMsg, 0, bytes);
pMsg->bytes = bytes; pMsg->bytes = bytes;
...@@ -723,7 +723,26 @@ void syncAppendEntriesSerialize(const SyncAppendEntries* pMsg, char* buf, uint32 ...@@ -723,7 +723,26 @@ void syncAppendEntriesSerialize(const SyncAppendEntries* pMsg, char* buf, uint32
void syncAppendEntriesDeserialize(const char* buf, uint32_t len, SyncAppendEntries* pMsg) { void syncAppendEntriesDeserialize(const char* buf, uint32_t len, SyncAppendEntries* pMsg) {
memcpy(pMsg, buf, len); memcpy(pMsg, buf, len);
assert(len == pMsg->bytes); assert(len == pMsg->bytes);
assert(pMsg->bytes == SYNC_APPEND_ENTRIES_FIX_LEN + pMsg->dataLen); assert(pMsg->bytes == sizeof(SyncAppendEntries) + pMsg->dataLen);
}
char* syncAppendEntriesSerialize2(const SyncAppendEntries* pMsg, uint32_t* len) {
char* buf = malloc(pMsg->bytes);
assert(buf != NULL);
syncAppendEntriesSerialize(pMsg, buf, pMsg->bytes);
if (len != NULL) {
*len = pMsg->bytes;
}
return buf;
}
SyncAppendEntries* syncAppendEntriesDeserialize2(const char* buf, uint32_t len) {
uint32_t bytes = *((uint32_t*)buf);
SyncAppendEntries* pMsg = malloc(bytes);
assert(pMsg != NULL);
syncAppendEntriesDeserialize(buf, len, pMsg);
assert(len == pMsg->bytes);
return pMsg;
} }
void syncAppendEntries2RpcMsg(const SyncAppendEntries* pMsg, SRpcMsg* pRpcMsg) { void syncAppendEntries2RpcMsg(const SyncAppendEntries* pMsg, SRpcMsg* pRpcMsg) {
...@@ -738,6 +757,11 @@ void syncAppendEntriesFromRpcMsg(const SRpcMsg* pRpcMsg, SyncAppendEntries* pMsg ...@@ -738,6 +757,11 @@ void syncAppendEntriesFromRpcMsg(const SRpcMsg* pRpcMsg, SyncAppendEntries* pMsg
syncAppendEntriesDeserialize(pRpcMsg->pCont, pRpcMsg->contLen, pMsg); syncAppendEntriesDeserialize(pRpcMsg->pCont, pRpcMsg->contLen, pMsg);
} }
SyncAppendEntries* syncAppendEntriesFromRpcMsg2(const SRpcMsg* pRpcMsg) {
SyncAppendEntries* pMsg = syncAppendEntriesDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen);
return pMsg;
}
cJSON* syncAppendEntries2Json(const SyncAppendEntries* pMsg) { cJSON* syncAppendEntries2Json(const SyncAppendEntries* pMsg) {
char u64buf[128]; char u64buf[128];
...@@ -785,13 +809,53 @@ cJSON* syncAppendEntries2Json(const SyncAppendEntries* pMsg) { ...@@ -785,13 +809,53 @@ cJSON* syncAppendEntries2Json(const SyncAppendEntries* pMsg) {
cJSON_AddStringToObject(pRoot, "commit_index", u64buf); cJSON_AddStringToObject(pRoot, "commit_index", u64buf);
cJSON_AddNumberToObject(pRoot, "dataLen", pMsg->dataLen); cJSON_AddNumberToObject(pRoot, "dataLen", pMsg->dataLen);
cJSON_AddStringToObject(pRoot, "data", pMsg->data); char* s;
s = syncUtilprintBin((char*)(pMsg->data), pMsg->dataLen);
cJSON_AddStringToObject(pRoot, "data", s);
free(s);
s = syncUtilprintBin2((char*)(pMsg->data), pMsg->dataLen);
cJSON_AddStringToObject(pRoot, "data2", s);
free(s);
cJSON* pJson = cJSON_CreateObject(); cJSON* pJson = cJSON_CreateObject();
cJSON_AddItemToObject(pJson, "SyncAppendEntries", pRoot); cJSON_AddItemToObject(pJson, "SyncAppendEntries", pRoot);
return pJson; return pJson;
} }
char* syncAppendEntries2Str(const SyncAppendEntries* pMsg) {
cJSON* pJson = syncAppendEntries2Json(pMsg);
char* serialized = cJSON_Print(pJson);
cJSON_Delete(pJson);
return serialized;
}
// for debug ----------------------
void syncAppendEntriesPrint(const SyncAppendEntries* pMsg) {
char* serialized = syncAppendEntries2Str(pMsg);
printf("syncAppendEntriesPrint | len:%lu | %s \n", strlen(serialized), serialized);
fflush(NULL);
free(serialized);
}
void syncAppendEntriesPrint2(char* s, const SyncAppendEntries* pMsg) {
char* serialized = syncAppendEntries2Str(pMsg);
printf("syncAppendEntriesPrint2 | len:%lu | %s | %s \n", strlen(serialized), s, serialized);
fflush(NULL);
free(serialized);
}
void syncAppendEntriesLog(const SyncAppendEntries* pMsg) {
char* serialized = syncAppendEntries2Str(pMsg);
sTrace("syncAppendEntriesLog | len:%lu | %s", strlen(serialized), serialized);
free(serialized);
}
void syncAppendEntriesLog2(char* s, const SyncAppendEntries* pMsg) {
char* serialized = syncAppendEntries2Str(pMsg);
sTrace("syncAppendEntriesLog2 | len:%lu | %s | %s", strlen(serialized), s, serialized);
free(serialized);
}
// ---- message process SyncAppendEntriesReply---- // ---- message process SyncAppendEntriesReply----
SyncAppendEntriesReply* syncAppendEntriesReplyBuild() { SyncAppendEntriesReply* syncAppendEntriesReplyBuild() {
uint32_t bytes = sizeof(SyncAppendEntriesReply); uint32_t bytes = sizeof(SyncAppendEntriesReply);
...@@ -818,6 +882,25 @@ void syncAppendEntriesReplyDeserialize(const char* buf, uint32_t len, SyncAppend ...@@ -818,6 +882,25 @@ void syncAppendEntriesReplyDeserialize(const char* buf, uint32_t len, SyncAppend
assert(len == pMsg->bytes); assert(len == pMsg->bytes);
} }
char* syncAppendEntriesReplySerialize2(const SyncAppendEntriesReply* pMsg, uint32_t* len) {
char* buf = malloc(pMsg->bytes);
assert(buf != NULL);
syncAppendEntriesReplySerialize(pMsg, buf, pMsg->bytes);
if (len != NULL) {
*len = pMsg->bytes;
}
return buf;
}
SyncAppendEntriesReply* syncAppendEntriesReplyDeserialize2(const char* buf, uint32_t len) {
uint32_t bytes = *((uint32_t*)buf);
SyncAppendEntriesReply* pMsg = malloc(bytes);
assert(pMsg != NULL);
syncAppendEntriesReplyDeserialize(buf, len, pMsg);
assert(len == pMsg->bytes);
return pMsg;
}
void syncAppendEntriesReply2RpcMsg(const SyncAppendEntriesReply* pMsg, SRpcMsg* pRpcMsg) { void syncAppendEntriesReply2RpcMsg(const SyncAppendEntriesReply* pMsg, SRpcMsg* pRpcMsg) {
memset(pRpcMsg, 0, sizeof(*pRpcMsg)); memset(pRpcMsg, 0, sizeof(*pRpcMsg));
pRpcMsg->msgType = pMsg->msgType; pRpcMsg->msgType = pMsg->msgType;
...@@ -830,6 +913,11 @@ void syncAppendEntriesReplyFromRpcMsg(const SRpcMsg* pRpcMsg, SyncAppendEntriesR ...@@ -830,6 +913,11 @@ void syncAppendEntriesReplyFromRpcMsg(const SRpcMsg* pRpcMsg, SyncAppendEntriesR
syncAppendEntriesReplyDeserialize(pRpcMsg->pCont, pRpcMsg->contLen, pMsg); syncAppendEntriesReplyDeserialize(pRpcMsg->pCont, pRpcMsg->contLen, pMsg);
} }
SyncAppendEntriesReply* syncAppendEntriesReplyFromRpcMsg2(const SRpcMsg* pRpcMsg) {
SyncAppendEntriesReply* pMsg = syncAppendEntriesReplyDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen);
return pMsg;
}
cJSON* syncAppendEntriesReply2Json(const SyncAppendEntriesReply* pMsg) { cJSON* syncAppendEntriesReply2Json(const SyncAppendEntriesReply* pMsg) {
char u64buf[128]; char u64buf[128];
...@@ -874,4 +962,38 @@ cJSON* syncAppendEntriesReply2Json(const SyncAppendEntriesReply* pMsg) { ...@@ -874,4 +962,38 @@ cJSON* syncAppendEntriesReply2Json(const SyncAppendEntriesReply* pMsg) {
cJSON* pJson = cJSON_CreateObject(); cJSON* pJson = cJSON_CreateObject();
cJSON_AddItemToObject(pJson, "SyncAppendEntriesReply", pRoot); cJSON_AddItemToObject(pJson, "SyncAppendEntriesReply", pRoot);
return pJson; return pJson;
}
char* syncAppendEntriesReply2Str(const SyncAppendEntriesReply* pMsg) {
cJSON* pJson = syncAppendEntriesReply2Json(pMsg);
char* serialized = cJSON_Print(pJson);
cJSON_Delete(pJson);
return serialized;
}
// for debug ----------------------
void syncAppendEntriesReplyPrint(const SyncAppendEntriesReply* pMsg) {
char* serialized = syncAppendEntriesReply2Str(pMsg);
printf("syncAppendEntriesReplyPrint | len:%lu | %s \n", strlen(serialized), serialized);
fflush(NULL);
free(serialized);
}
void syncAppendEntriesReplyPrint2(char* s, const SyncAppendEntriesReply* pMsg) {
char* serialized = syncAppendEntriesReply2Str(pMsg);
printf("syncAppendEntriesReplyPrint2 | len:%lu | %s | %s \n", strlen(serialized), s, serialized);
fflush(NULL);
free(serialized);
}
void syncAppendEntriesReplyLog(const SyncAppendEntriesReply* pMsg) {
char* serialized = syncAppendEntriesReply2Str(pMsg);
sTrace("syncAppendEntriesReplyLog | len:%lu | %s", strlen(serialized), serialized);
free(serialized);
}
void syncAppendEntriesReplyLog2(char* s, const SyncAppendEntriesReply* pMsg) {
char* serialized = syncAppendEntriesReply2Str(pMsg);
sTrace("syncAppendEntriesReplyLog2 | len:%lu | %s | %s", strlen(serialized), s, serialized);
free(serialized);
} }
\ No newline at end of file
...@@ -17,8 +17,10 @@ add_executable(syncVotesRespondTest "") ...@@ -17,8 +17,10 @@ add_executable(syncVotesRespondTest "")
add_executable(syncIndexMgrTest "") add_executable(syncIndexMgrTest "")
add_executable(syncLogStoreTest "") add_executable(syncLogStoreTest "")
add_executable(syncEntryTest "") add_executable(syncEntryTest "")
add_executable(syncRequestVote "") add_executable(syncRequestVoteTest "")
add_executable(syncRequestVoteReply "") add_executable(syncRequestVoteReplyTest "")
add_executable(syncAppendEntriesTest "")
add_executable(syncAppendEntriesReplyTest "")
target_sources(syncTest target_sources(syncTest
...@@ -97,13 +99,21 @@ target_sources(syncEntryTest ...@@ -97,13 +99,21 @@ target_sources(syncEntryTest
PRIVATE PRIVATE
"syncEntryTest.cpp" "syncEntryTest.cpp"
) )
target_sources(syncRequestVote target_sources(syncRequestVoteTest
PRIVATE PRIVATE
"syncRequestVote.cpp" "syncRequestVoteTest.cpp"
) )
target_sources(syncRequestVoteReply target_sources(syncRequestVoteReplyTest
PRIVATE PRIVATE
"syncRequestVoteReply.cpp" "syncRequestVoteReplyTest.cpp"
)
target_sources(syncAppendEntriesTest
PRIVATE
"syncAppendEntriesTest.cpp"
)
target_sources(syncAppendEntriesReplyTest
PRIVATE
"syncAppendEntriesReplyTest.cpp"
) )
...@@ -202,12 +212,22 @@ target_include_directories(syncEntryTest ...@@ -202,12 +212,22 @@ target_include_directories(syncEntryTest
"${CMAKE_SOURCE_DIR}/include/libs/sync" "${CMAKE_SOURCE_DIR}/include/libs/sync"
"${CMAKE_CURRENT_SOURCE_DIR}/../inc" "${CMAKE_CURRENT_SOURCE_DIR}/../inc"
) )
target_include_directories(syncRequestVote target_include_directories(syncRequestVoteTest
PUBLIC
"${CMAKE_SOURCE_DIR}/include/libs/sync"
"${CMAKE_CURRENT_SOURCE_DIR}/../inc"
)
target_include_directories(syncRequestVoteReplyTest
PUBLIC
"${CMAKE_SOURCE_DIR}/include/libs/sync"
"${CMAKE_CURRENT_SOURCE_DIR}/../inc"
)
target_include_directories(syncAppendEntriesTest
PUBLIC PUBLIC
"${CMAKE_SOURCE_DIR}/include/libs/sync" "${CMAKE_SOURCE_DIR}/include/libs/sync"
"${CMAKE_CURRENT_SOURCE_DIR}/../inc" "${CMAKE_CURRENT_SOURCE_DIR}/../inc"
) )
target_include_directories(syncRequestVoteReply target_include_directories(syncAppendEntriesReplyTest
PUBLIC PUBLIC
"${CMAKE_SOURCE_DIR}/include/libs/sync" "${CMAKE_SOURCE_DIR}/include/libs/sync"
"${CMAKE_CURRENT_SOURCE_DIR}/../inc" "${CMAKE_CURRENT_SOURCE_DIR}/../inc"
...@@ -290,11 +310,19 @@ target_link_libraries(syncEntryTest ...@@ -290,11 +310,19 @@ target_link_libraries(syncEntryTest
sync sync
gtest_main gtest_main
) )
target_link_libraries(syncRequestVote target_link_libraries(syncRequestVoteTest
sync
gtest_main
)
target_link_libraries(syncRequestVoteReplyTest
sync
gtest_main
)
target_link_libraries(syncAppendEntriesTest
sync sync
gtest_main gtest_main
) )
target_link_libraries(syncRequestVoteReply target_link_libraries(syncAppendEntriesReplyTest
sync sync
gtest_main gtest_main
) )
......
#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");
}
SyncAppendEntriesReply *createMsg() {
SyncAppendEntriesReply *pMsg = syncAppendEntriesReplyBuild();
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;
}
void test1() {
SyncAppendEntriesReply *pMsg = createMsg();
syncAppendEntriesReplyPrint2((char *)"test1:", pMsg);
syncAppendEntriesReplyDestroy(pMsg);
}
void test2() {
SyncAppendEntriesReply *pMsg = createMsg();
uint32_t len = pMsg->bytes;
char * serialized = (char *)malloc(len);
syncAppendEntriesReplySerialize(pMsg, serialized, len);
SyncAppendEntriesReply *pMsg2 = syncAppendEntriesReplyBuild();
syncAppendEntriesReplyDeserialize(serialized, len, pMsg2);
syncAppendEntriesReplyPrint2((char *)"test2: syncAppendEntriesReplySerialize -> syncAppendEntriesReplyDeserialize ", pMsg2);
free(serialized);
syncAppendEntriesReplyDestroy(pMsg);
syncAppendEntriesReplyDestroy(pMsg2);
}
void test3() {
SyncAppendEntriesReply *pMsg = createMsg();
uint32_t len;
char * serialized = syncAppendEntriesReplySerialize2(pMsg, &len);
SyncAppendEntriesReply *pMsg2 = syncAppendEntriesReplyDeserialize2(serialized, len);
syncAppendEntriesReplyPrint2((char *)"test3: syncAppendEntriesReplySerialize3 -> syncAppendEntriesReplyDeserialize2 ",
pMsg2);
free(serialized);
syncAppendEntriesReplyDestroy(pMsg);
syncAppendEntriesReplyDestroy(pMsg2);
}
void test4() {
SyncAppendEntriesReply *pMsg = createMsg();
SRpcMsg rpcMsg;
syncAppendEntriesReply2RpcMsg(pMsg, &rpcMsg);
SyncAppendEntriesReply *pMsg2 = syncAppendEntriesReplyBuild();
syncAppendEntriesReplyFromRpcMsg(&rpcMsg, pMsg2);
syncAppendEntriesReplyPrint2((char *)"test4: syncAppendEntriesReply2RpcMsg -> syncAppendEntriesReplyFromRpcMsg ", pMsg2);
syncAppendEntriesReplyDestroy(pMsg);
syncAppendEntriesReplyDestroy(pMsg2);
}
void test5() {
SyncAppendEntriesReply *pMsg = createMsg();
SRpcMsg rpcMsg;
syncAppendEntriesReply2RpcMsg(pMsg, &rpcMsg);
SyncAppendEntriesReply *pMsg2 = syncAppendEntriesReplyFromRpcMsg2(&rpcMsg);
syncAppendEntriesReplyPrint2((char *)"test5: syncAppendEntriesReply2RpcMsg -> syncAppendEntriesReplyFromRpcMsg2 ", pMsg2);
syncAppendEntriesReplyDestroy(pMsg);
syncAppendEntriesReplyDestroy(pMsg2);
}
int main() {
// taosInitLog((char *)"syncTest.log", 100000, 10);
tsAsyncLog = 0;
sDebugFlag = 143 + 64;
logTest();
test1();
test2();
test3();
test4();
test5();
return 0;
}
#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");
}
SyncAppendEntries *createMsg() {
SyncAppendEntries *pMsg = syncAppendEntriesBuild(20);
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->prevLogIndex = 11;
pMsg->prevLogTerm = 22;
pMsg->commitIndex = 33;
strcpy(pMsg->data, "hello world");
return pMsg;
}
void test1() {
SyncAppendEntries *pMsg = createMsg();
syncAppendEntriesPrint2((char *)"test1:", pMsg);
syncAppendEntriesDestroy(pMsg);
}
void test2() {
SyncAppendEntries *pMsg = createMsg();
uint32_t len = pMsg->bytes;
char * serialized = (char *)malloc(len);
syncAppendEntriesSerialize(pMsg, serialized, len);
SyncAppendEntries *pMsg2 = syncAppendEntriesBuild(pMsg2->dataLen);
syncAppendEntriesDeserialize(serialized, len, pMsg2);
syncAppendEntriesPrint2((char *)"test2: syncAppendEntriesSerialize -> syncAppendEntriesDeserialize ", pMsg2);
free(serialized);
syncAppendEntriesDestroy(pMsg);
syncAppendEntriesDestroy(pMsg2);
}
void test3() {
SyncAppendEntries *pMsg = createMsg();
uint32_t len;
char * serialized = syncAppendEntriesSerialize2(pMsg, &len);
SyncAppendEntries *pMsg2 = syncAppendEntriesDeserialize2(serialized, len);
syncAppendEntriesPrint2((char *)"test3: syncAppendEntriesSerialize3 -> syncAppendEntriesDeserialize2 ", pMsg2);
free(serialized);
syncAppendEntriesDestroy(pMsg);
syncAppendEntriesDestroy(pMsg2);
}
void test4() {
SyncAppendEntries *pMsg = createMsg();
SRpcMsg rpcMsg;
syncAppendEntries2RpcMsg(pMsg, &rpcMsg);
SyncAppendEntries *pMsg2 = (SyncAppendEntries *)malloc(rpcMsg.contLen);
syncAppendEntriesFromRpcMsg(&rpcMsg, pMsg2);
syncAppendEntriesPrint2((char *)"test4: syncAppendEntries2RpcMsg -> syncAppendEntriesFromRpcMsg ", pMsg2);
syncAppendEntriesDestroy(pMsg);
syncAppendEntriesDestroy(pMsg2);
}
void test5() {
SyncAppendEntries *pMsg = createMsg();
SRpcMsg rpcMsg;
syncAppendEntries2RpcMsg(pMsg, &rpcMsg);
SyncAppendEntries *pMsg2 = syncAppendEntriesFromRpcMsg2(&rpcMsg);
syncAppendEntriesPrint2((char *)"test5: syncAppendEntries2RpcMsg -> syncAppendEntriesFromRpcMsg2 ", pMsg2);
syncAppendEntriesDestroy(pMsg);
syncAppendEntriesDestroy(pMsg2);
}
int main() {
// taosInitLog((char *)"syncTest.log", 100000, 10);
tsAsyncLog = 0;
sDebugFlag = 143 + 64;
logTest();
test1();
test2();
test3();
test4();
test5();
return 0;
}
...@@ -32,8 +32,8 @@ void test1() { ...@@ -32,8 +32,8 @@ void test1() {
void test2() { void test2() {
SyncRequestVoteReply *pMsg = createMsg(); SyncRequestVoteReply *pMsg = createMsg();
uint32_t len = pMsg->bytes; uint32_t len = pMsg->bytes;
char * serialized = (char *)malloc(len); char * serialized = (char *)malloc(len);
syncRequestVoteReplySerialize(pMsg, serialized, len); syncRequestVoteReplySerialize(pMsg, serialized, len);
SyncRequestVoteReply *pMsg2 = syncRequestVoteReplyBuild(); SyncRequestVoteReply *pMsg2 = syncRequestVoteReplyBuild();
syncRequestVoteReplyDeserialize(serialized, len, pMsg2); syncRequestVoteReplyDeserialize(serialized, len, pMsg2);
...@@ -46,10 +46,11 @@ void test2() { ...@@ -46,10 +46,11 @@ void test2() {
void test3() { void test3() {
SyncRequestVoteReply *pMsg = createMsg(); SyncRequestVoteReply *pMsg = createMsg();
uint32_t len; uint32_t len;
char * serialized = syncRequestVoteReplySerialize2(pMsg, &len); char * serialized = syncRequestVoteReplySerialize2(pMsg, &len);
SyncRequestVoteReply *pMsg2 = syncRequestVoteReplyDeserialize2(serialized, len); SyncRequestVoteReply *pMsg2 = syncRequestVoteReplyDeserialize2(serialized, len);
syncRequestVoteReplyPrint2((char *)"test3: syncRequestVoteReplySerialize3 -> syncRequestVoteReplyDeserialize2 ", pMsg2); syncRequestVoteReplyPrint2((char *)"test3: syncRequestVoteReplySerialize3 -> syncRequestVoteReplyDeserialize2 ",
pMsg2);
free(serialized); free(serialized);
syncRequestVoteReplyDestroy(pMsg); syncRequestVoteReplyDestroy(pMsg);
...@@ -58,7 +59,7 @@ void test3() { ...@@ -58,7 +59,7 @@ void test3() {
void test4() { void test4() {
SyncRequestVoteReply *pMsg = createMsg(); SyncRequestVoteReply *pMsg = createMsg();
SRpcMsg rpcMsg; SRpcMsg rpcMsg;
syncRequestVoteReply2RpcMsg(pMsg, &rpcMsg); syncRequestVoteReply2RpcMsg(pMsg, &rpcMsg);
SyncRequestVoteReply *pMsg2 = syncRequestVoteReplyBuild(); SyncRequestVoteReply *pMsg2 = syncRequestVoteReplyBuild();
syncRequestVoteReplyFromRpcMsg(&rpcMsg, pMsg2); syncRequestVoteReplyFromRpcMsg(&rpcMsg, pMsg2);
...@@ -70,7 +71,7 @@ void test4() { ...@@ -70,7 +71,7 @@ void test4() {
void test5() { void test5() {
SyncRequestVoteReply *pMsg = createMsg(); SyncRequestVoteReply *pMsg = createMsg();
SRpcMsg rpcMsg; SRpcMsg rpcMsg;
syncRequestVoteReply2RpcMsg(pMsg, &rpcMsg); syncRequestVoteReply2RpcMsg(pMsg, &rpcMsg);
SyncRequestVoteReply *pMsg2 = syncRequestVoteReplyFromRpcMsg2(&rpcMsg); SyncRequestVoteReply *pMsg2 = syncRequestVoteReplyFromRpcMsg2(&rpcMsg);
syncRequestVoteReplyPrint2((char *)"test5: syncRequestVoteReply2RpcMsg -> syncRequestVoteReplyFromRpcMsg2 ", pMsg2); syncRequestVoteReplyPrint2((char *)"test5: syncRequestVoteReply2RpcMsg -> syncRequestVoteReplyFromRpcMsg2 ", pMsg2);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册