syncAppendEntriesTest.cpp 2.8 KB
Newer Older
M
Minghao Li 已提交
1
#include <gtest/gtest.h>
2
#include "syncTest.h"
M
Minghao Li 已提交
3 4 5 6 7 8 9 10 11 12 13

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() {
M
Minghao Li 已提交
14
  SyncAppendEntries *pMsg = syncAppendEntriesBuild(20, 1000);
M
Minghao Li 已提交
15 16 17 18 19 20 21
  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;
22
  pMsg->privateTerm = 44;
M
Minghao Li 已提交
23 24 25 26 27 28
  strcpy(pMsg->data, "hello world");
  return pMsg;
}

void test1() {
  SyncAppendEntries *pMsg = createMsg();
M
Minghao Li 已提交
29
  syncAppendEntriesLog2((char *)"test1:", pMsg);
M
Minghao Li 已提交
30 31 32 33 34 35
  syncAppendEntriesDestroy(pMsg);
}

void test2() {
  SyncAppendEntries *pMsg = createMsg();
  uint32_t           len = pMsg->bytes;
H
Hongze Cheng 已提交
36
  char              *serialized = (char *)taosMemoryMalloc(len);
M
Minghao Li 已提交
37
  syncAppendEntriesSerialize(pMsg, serialized, len);
M
Minghao Li 已提交
38
  SyncAppendEntries *pMsg2 = syncAppendEntriesBuild(pMsg->dataLen, 1000);
M
Minghao Li 已提交
39
  syncAppendEntriesDeserialize(serialized, len, pMsg2);
M
Minghao Li 已提交
40
  syncAppendEntriesLog2((char *)"test2: syncAppendEntriesSerialize -> syncAppendEntriesDeserialize ", pMsg2);
M
Minghao Li 已提交
41

wafwerar's avatar
wafwerar 已提交
42
  taosMemoryFree(serialized);
M
Minghao Li 已提交
43 44 45 46 47 48 49
  syncAppendEntriesDestroy(pMsg);
  syncAppendEntriesDestroy(pMsg2);
}

void test3() {
  SyncAppendEntries *pMsg = createMsg();
  uint32_t           len;
H
Hongze Cheng 已提交
50
  char              *serialized = syncAppendEntriesSerialize2(pMsg, &len);
M
Minghao Li 已提交
51
  SyncAppendEntries *pMsg2 = syncAppendEntriesDeserialize2(serialized, len);
M
Minghao Li 已提交
52
  syncAppendEntriesLog2((char *)"test3: syncAppendEntriesSerialize3 -> syncAppendEntriesDeserialize2 ", pMsg2);
M
Minghao Li 已提交
53

wafwerar's avatar
wafwerar 已提交
54
  taosMemoryFree(serialized);
M
Minghao Li 已提交
55 56 57 58 59 60 61 62
  syncAppendEntriesDestroy(pMsg);
  syncAppendEntriesDestroy(pMsg2);
}

void test4() {
  SyncAppendEntries *pMsg = createMsg();
  SRpcMsg            rpcMsg;
  syncAppendEntries2RpcMsg(pMsg, &rpcMsg);
wafwerar's avatar
wafwerar 已提交
63
  SyncAppendEntries *pMsg2 = (SyncAppendEntries *)taosMemoryMalloc(rpcMsg.contLen);
M
Minghao Li 已提交
64
  syncAppendEntriesFromRpcMsg(&rpcMsg, pMsg2);
M
Minghao Li 已提交
65
  syncAppendEntriesLog2((char *)"test4: syncAppendEntries2RpcMsg -> syncAppendEntriesFromRpcMsg ", pMsg2);
M
Minghao Li 已提交
66

M
Minghao Li 已提交
67
  rpcFreeCont(rpcMsg.pCont);
M
Minghao Li 已提交
68 69 70 71 72 73 74 75 76
  syncAppendEntriesDestroy(pMsg);
  syncAppendEntriesDestroy(pMsg2);
}

void test5() {
  SyncAppendEntries *pMsg = createMsg();
  SRpcMsg            rpcMsg;
  syncAppendEntries2RpcMsg(pMsg, &rpcMsg);
  SyncAppendEntries *pMsg2 = syncAppendEntriesFromRpcMsg2(&rpcMsg);
M
Minghao Li 已提交
77
  syncAppendEntriesLog2((char *)"test5: syncAppendEntries2RpcMsg -> syncAppendEntriesFromRpcMsg2 ", pMsg2);
M
Minghao Li 已提交
78

M
Minghao Li 已提交
79
  rpcFreeCont(rpcMsg.pCont);
M
Minghao Li 已提交
80 81 82 83 84 85
  syncAppendEntriesDestroy(pMsg);
  syncAppendEntriesDestroy(pMsg2);
}

int main() {
  tsAsyncLog = 0;
M
Minghao Li 已提交
86
  sDebugFlag = DEBUG_TRACE + DEBUG_SCREEN + DEBUG_FILE;
M
Minghao Li 已提交
87 88 89 90 91 92 93 94 95 96
  logTest();

  test1();
  test2();
  test3();
  test4();
  test5();

  return 0;
}