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

SyncAppendEntriesReply *createMsg() {
M
Minghao Li 已提交
18
  SyncAppendEntriesReply *pMsg = syncAppendEntriesReplyBuild(1000);
M
Minghao Li 已提交
19 20 21 22 23 24
  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;
25 26
  pMsg->term = 33;
  pMsg->privateTerm = 44;
27
  pMsg->startTime = taosGetTimestampMs();
M
Minghao Li 已提交
28
  return pMsg;
M
Minghao Li 已提交
29 30 31 32
}

void test1() {
  SyncAppendEntriesReply *pMsg = createMsg();
M
Minghao Li 已提交
33
  syncAppendEntriesReplyLog2((char *)"test1:", pMsg);
M
Minghao Li 已提交
34 35 36 37 38
  syncAppendEntriesReplyDestroy(pMsg);
}

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

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

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

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

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

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

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

M
Minghao Li 已提交
87
  rpcFreeCont(rpcMsg.pCont);
M
Minghao Li 已提交
88 89 90 91 92
  syncAppendEntriesReplyDestroy(pMsg);
  syncAppendEntriesReplyDestroy(pMsg2);
}

int main() {
93 94
  gRaftDetailLog = true;

M
Minghao Li 已提交
95
  tsAsyncLog = 0;
M
Minghao Li 已提交
96
  sDebugFlag = DEBUG_TRACE + DEBUG_SCREEN + DEBUG_FILE;
M
Minghao Li 已提交
97 98 99 100 101 102 103 104 105 106
  logTest();

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

  return 0;
}