syncAppendEntriesTest.cpp 2.9 KB
Newer Older
M
Minghao Li 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#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() {
M
Minghao Li 已提交
18
  SyncAppendEntries *pMsg = syncAppendEntriesBuild(20, 1000);
M
Minghao Li 已提交
19 20 21 22 23 24 25
  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;
C
Cary Xu 已提交
26
  pMsg->privateTerm = 44;
M
Minghao Li 已提交
27 28 29 30 31 32
  strcpy(pMsg->data, "hello world");
  return pMsg;
}

void test1() {
  SyncAppendEntries *pMsg = createMsg();
M
Minghao Li 已提交
33
  syncAppendEntriesLog2((char *)"test1:", pMsg);
M
Minghao Li 已提交
34 35 36 37 38 39
  syncAppendEntriesDestroy(pMsg);
}

void test2() {
  SyncAppendEntries *pMsg = createMsg();
  uint32_t           len = pMsg->bytes;
wafwerar's avatar
wafwerar 已提交
40
  char *             serialized = (char *)taosMemoryMalloc(len);
M
Minghao Li 已提交
41
  syncAppendEntriesSerialize(pMsg, serialized, len);
M
Minghao Li 已提交
42
  SyncAppendEntries *pMsg2 = syncAppendEntriesBuild(pMsg->dataLen, 1000);
M
Minghao Li 已提交
43
  syncAppendEntriesDeserialize(serialized, len, pMsg2);
M
Minghao Li 已提交
44
  syncAppendEntriesLog2((char *)"test2: syncAppendEntriesSerialize -> syncAppendEntriesDeserialize ", pMsg2);
M
Minghao Li 已提交
45

wafwerar's avatar
wafwerar 已提交
46
  taosMemoryFree(serialized);
M
Minghao Li 已提交
47 48 49 50 51 52 53 54 55
  syncAppendEntriesDestroy(pMsg);
  syncAppendEntriesDestroy(pMsg2);
}

void test3() {
  SyncAppendEntries *pMsg = createMsg();
  uint32_t           len;
  char *             serialized = syncAppendEntriesSerialize2(pMsg, &len);
  SyncAppendEntries *pMsg2 = syncAppendEntriesDeserialize2(serialized, len);
M
Minghao Li 已提交
56
  syncAppendEntriesLog2((char *)"test3: syncAppendEntriesSerialize3 -> syncAppendEntriesDeserialize2 ", pMsg2);
M
Minghao Li 已提交
57

wafwerar's avatar
wafwerar 已提交
58
  taosMemoryFree(serialized);
M
Minghao Li 已提交
59 60 61 62 63 64 65 66
  syncAppendEntriesDestroy(pMsg);
  syncAppendEntriesDestroy(pMsg2);
}

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

M
Minghao Li 已提交
71
  rpcFreeCont(rpcMsg.pCont);
M
Minghao Li 已提交
72 73 74 75 76 77 78 79 80
  syncAppendEntriesDestroy(pMsg);
  syncAppendEntriesDestroy(pMsg2);
}

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

M
Minghao Li 已提交
83
  rpcFreeCont(rpcMsg.pCont);
M
Minghao Li 已提交
84 85 86 87 88 89
  syncAppendEntriesDestroy(pMsg);
  syncAppendEntriesDestroy(pMsg2);
}

int main() {
  tsAsyncLog = 0;
M
Minghao Li 已提交
90
  sDebugFlag = DEBUG_TRACE + DEBUG_SCREEN + DEBUG_FILE;
M
Minghao Li 已提交
91 92 93 94 95 96 97 98 99 100
  logTest();

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

  return 0;
}