syncAppendEntriesReplyTest.cpp 3.2 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");
}

SyncAppendEntriesReply *createMsg() {
M
Minghao Li 已提交
14
  SyncAppendEntriesReply *pMsg = syncAppendEntriesReplyBuild(1000);
M
Minghao Li 已提交
15 16 17 18 19 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->success = true;
  pMsg->matchIndex = 77;
21 22
  pMsg->term = 33;
  pMsg->privateTerm = 44;
23
  pMsg->startTime = taosGetTimestampMs();
M
Minghao Li 已提交
24
  return pMsg;
M
Minghao Li 已提交
25 26 27 28
}

void test1() {
  SyncAppendEntriesReply *pMsg = createMsg();
M
Minghao Li 已提交
29
  syncAppendEntriesReplyLog2((char *)"test1:", pMsg);
M
Minghao Li 已提交
30 31 32 33 34
  syncAppendEntriesReplyDestroy(pMsg);
}

void test2() {
  SyncAppendEntriesReply *pMsg = createMsg();
M
Minghao Li 已提交
35
  uint32_t                len = pMsg->bytes;
H
Hongze Cheng 已提交
36
  char                   *serialized = (char *)taosMemoryMalloc(len);
M
Minghao Li 已提交
37
  syncAppendEntriesReplySerialize(pMsg, serialized, len);
M
Minghao Li 已提交
38
  SyncAppendEntriesReply *pMsg2 = syncAppendEntriesReplyBuild(1000);
M
Minghao Li 已提交
39
  syncAppendEntriesReplyDeserialize(serialized, len, pMsg2);
M
Minghao Li 已提交
40 41
  syncAppendEntriesReplyLog2((char *)"test2: syncAppendEntriesReplySerialize -> syncAppendEntriesReplyDeserialize ",
                             pMsg2);
M
Minghao Li 已提交
42

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

void test3() {
  SyncAppendEntriesReply *pMsg = createMsg();
M
Minghao Li 已提交
50
  uint32_t                len;
H
Hongze Cheng 已提交
51
  char                   *serialized = syncAppendEntriesReplySerialize2(pMsg, &len);
M
Minghao Li 已提交
52
  SyncAppendEntriesReply *pMsg2 = syncAppendEntriesReplyDeserialize2(serialized, len);
M
Minghao Li 已提交
53 54
  syncAppendEntriesReplyLog2((char *)"test3: syncAppendEntriesReplySerialize3 -> syncAppendEntriesReplyDeserialize2 ",
                             pMsg2);
M
Minghao Li 已提交
55

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

void test4() {
  SyncAppendEntriesReply *pMsg = createMsg();
M
Minghao Li 已提交
63
  SRpcMsg                 rpcMsg;
M
Minghao Li 已提交
64
  syncAppendEntriesReply2RpcMsg(pMsg, &rpcMsg);
M
Minghao Li 已提交
65
  SyncAppendEntriesReply *pMsg2 = syncAppendEntriesReplyBuild(1000);
M
Minghao Li 已提交
66
  syncAppendEntriesReplyFromRpcMsg(&rpcMsg, pMsg2);
M
Minghao Li 已提交
67 68
  syncAppendEntriesReplyLog2((char *)"test4: syncAppendEntriesReply2RpcMsg -> syncAppendEntriesReplyFromRpcMsg ",
                             pMsg2);
M
Minghao Li 已提交
69

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

void test5() {
  SyncAppendEntriesReply *pMsg = createMsg();
M
Minghao Li 已提交
77
  SRpcMsg                 rpcMsg;
M
Minghao Li 已提交
78 79
  syncAppendEntriesReply2RpcMsg(pMsg, &rpcMsg);
  SyncAppendEntriesReply *pMsg2 = syncAppendEntriesReplyFromRpcMsg2(&rpcMsg);
M
Minghao Li 已提交
80 81
  syncAppendEntriesReplyLog2((char *)"test5: syncAppendEntriesReply2RpcMsg -> syncAppendEntriesReplyFromRpcMsg2 ",
                             pMsg2);
M
Minghao Li 已提交
82

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

int main() {
89 90
  gRaftDetailLog = true;

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

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

  return 0;
}