syncHeartbeatTest.cpp 2.6 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 14 15 16 17

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");
}

SyncHeartbeat *createMsg() {
  SyncHeartbeat *pMsg = syncHeartbeatBuild(789);
  pMsg->srcId.addr = syncUtilAddr2U64("127.0.0.1", 1234);
  pMsg->srcId.vgId = 100;
  pMsg->destId.addr = syncUtilAddr2U64("127.0.0.1", 5678);
18
  pMsg->destId.vgId = 100;
M
Minghao Li 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32
  pMsg->term = 8;
  pMsg->commitIndex = 33;
  pMsg->privateTerm = 44;
  return pMsg;
}

void test1() {
  SyncHeartbeat *pMsg = createMsg();
  syncHeartbeatLog2((char *)"test1:", pMsg);
  syncHeartbeatDestroy(pMsg);
}

void test2() {
  SyncHeartbeat *pMsg = createMsg();
33 34
  uint32_t       len = pMsg->bytes;
  char *         serialized = (char *)taosMemoryMalloc(len);
M
Minghao Li 已提交
35 36 37 38 39 40 41 42 43 44 45 46
  syncHeartbeatSerialize(pMsg, serialized, len);
  SyncHeartbeat *pMsg2 = syncHeartbeatBuild(789);
  syncHeartbeatDeserialize(serialized, len, pMsg2);
  syncHeartbeatLog2((char *)"test2: syncHeartbeatSerialize -> syncHeartbeatDeserialize ", pMsg2);

  taosMemoryFree(serialized);
  syncHeartbeatDestroy(pMsg);
  syncHeartbeatDestroy(pMsg2);
}

void test3() {
  SyncHeartbeat *pMsg = createMsg();
47 48
  uint32_t       len;
  char *         serialized = syncHeartbeatSerialize2(pMsg, &len);
M
Minghao Li 已提交
49 50 51 52 53 54 55 56 57 58
  SyncHeartbeat *pMsg2 = syncHeartbeatDeserialize2(serialized, len);
  syncHeartbeatLog2((char *)"test3: syncHeartbeatSerialize2 -> syncHeartbeatDeserialize2 ", pMsg2);

  taosMemoryFree(serialized);
  syncHeartbeatDestroy(pMsg);
  syncHeartbeatDestroy(pMsg2);
}

void test4() {
  SyncHeartbeat *pMsg = createMsg();
59
  SRpcMsg        rpcMsg;
M
Minghao Li 已提交
60 61 62 63 64 65 66 67 68 69 70 71
  syncHeartbeat2RpcMsg(pMsg, &rpcMsg);
  SyncHeartbeat *pMsg2 = (SyncHeartbeat *)taosMemoryMalloc(rpcMsg.contLen);
  syncHeartbeatFromRpcMsg(&rpcMsg, pMsg2);
  syncHeartbeatLog2((char *)"test4: syncHeartbeat2RpcMsg -> syncHeartbeatFromRpcMsg ", pMsg2);

  rpcFreeCont(rpcMsg.pCont);
  syncHeartbeatDestroy(pMsg);
  syncHeartbeatDestroy(pMsg2);
}

void test5() {
  SyncHeartbeat *pMsg = createMsg();
72
  SRpcMsg        rpcMsg;
M
Minghao Li 已提交
73
  syncHeartbeat2RpcMsg(pMsg, &rpcMsg);
74
  SyncHeartbeat *pMsg2 = syncHeartbeatFromRpcMsg2(&rpcMsg);
M
Minghao Li 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
  syncHeartbeatLog2((char *)"test5: syncHeartbeat2RpcMsg -> syncHeartbeatFromRpcMsg2 ", pMsg2);

  rpcFreeCont(rpcMsg.pCont);
  syncHeartbeatDestroy(pMsg);
  syncHeartbeatDestroy(pMsg2);
}

int main() {
  tsAsyncLog = 0;
  sDebugFlag = DEBUG_TRACE + DEBUG_SCREEN + DEBUG_FILE;
  gRaftDetailLog = true;
  logTest();

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

  return 0;
}