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 26 27 28 29 30 31
  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();
M
Minghao Li 已提交
32
  syncAppendEntriesLog2((char *)"test1:", pMsg);
M
Minghao Li 已提交
33 34 35 36 37 38
  syncAppendEntriesDestroy(pMsg);
}

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

wafwerar's avatar
wafwerar 已提交
45
  taosMemoryFree(serialized);
M
Minghao Li 已提交
46 47 48 49 50 51 52 53 54
  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 已提交
55
  syncAppendEntriesLog2((char *)"test3: syncAppendEntriesSerialize3 -> syncAppendEntriesDeserialize2 ", pMsg2);
M
Minghao Li 已提交
56

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

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

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

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

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

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

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

  return 0;
}