syncPingTest.cpp 2.7 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

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

M
Minghao Li 已提交
13 14 15 16 17 18
SyncPing *createMsg() {
  SRaftId srcId, destId;
  srcId.addr = syncUtilAddr2U64("127.0.0.1", 1234);
  srcId.vgId = 100;
  destId.addr = syncUtilAddr2U64("127.0.0.1", 5678);
  destId.vgId = 100;
M
Minghao Li 已提交
19
  SyncPing *pMsg = syncPingBuild3(&srcId, &destId, 1000);
M
Minghao Li 已提交
20 21
  return pMsg;
}
M
Minghao Li 已提交
22

M
Minghao Li 已提交
23 24
void test1() {
  SyncPing *pMsg = createMsg();
M
Minghao Li 已提交
25
  syncPingLog2((char *)"test1:", pMsg);
M
Minghao Li 已提交
26 27
  syncPingDestroy(pMsg);
}
M
Minghao Li 已提交
28

M
Minghao Li 已提交
29 30
void test2() {
  SyncPing *pMsg = createMsg();
M
Minghao Li 已提交
31
  uint32_t  len = pMsg->bytes;
H
Hongze Cheng 已提交
32
  char     *serialized = (char *)taosMemoryMalloc(len);
M
Minghao Li 已提交
33 34 35
  syncPingSerialize(pMsg, serialized, len);
  SyncPing *pMsg2 = syncPingBuild(pMsg->dataLen);
  syncPingDeserialize(serialized, len, pMsg2);
M
Minghao Li 已提交
36
  syncPingLog2((char *)"test2: syncPingSerialize -> syncPingDeserialize ", pMsg2);
M
Minghao Li 已提交
37

wafwerar's avatar
wafwerar 已提交
38
  taosMemoryFree(serialized);
M
Minghao Li 已提交
39 40 41
  syncPingDestroy(pMsg);
  syncPingDestroy(pMsg2);
}
M
Minghao Li 已提交
42

M
Minghao Li 已提交
43 44
void test3() {
  SyncPing *pMsg = createMsg();
M
Minghao Li 已提交
45
  uint32_t  len;
H
Hongze Cheng 已提交
46
  char     *serialized = syncPingSerialize2(pMsg, &len);
M
Minghao Li 已提交
47
  SyncPing *pMsg2 = syncPingDeserialize2(serialized, len);
M
Minghao Li 已提交
48
  syncPingLog2((char *)"test3: syncPingSerialize2 -> syncPingDeserialize2 ", pMsg2);
M
Minghao Li 已提交
49

wafwerar's avatar
wafwerar 已提交
50
  taosMemoryFree(serialized);
M
Minghao Li 已提交
51 52 53
  syncPingDestroy(pMsg);
  syncPingDestroy(pMsg2);
}
M
Minghao Li 已提交
54

M
Minghao Li 已提交
55 56
void test4() {
  SyncPing *pMsg = createMsg();
M
Minghao Li 已提交
57
  SRpcMsg   rpcMsg;
M
Minghao Li 已提交
58
  syncPing2RpcMsg(pMsg, &rpcMsg);
wafwerar's avatar
wafwerar 已提交
59
  SyncPing *pMsg2 = (SyncPing *)taosMemoryMalloc(rpcMsg.contLen);
M
Minghao Li 已提交
60
  syncPingFromRpcMsg(&rpcMsg, pMsg2);
M
Minghao Li 已提交
61
  syncPingLog2((char *)"test4: syncPing2RpcMsg -> syncPingFromRpcMsg ", pMsg2);
M
Minghao Li 已提交
62

M
Minghao Li 已提交
63 64
  syncPingDestroy(pMsg);
  syncPingDestroy(pMsg2);
M
Minghao Li 已提交
65
  rpcFreeCont(rpcMsg.pCont);
M
Minghao Li 已提交
66 67
}

M
Minghao Li 已提交
68 69
void test5() {
  SyncPing *pMsg = createMsg();
M
Minghao Li 已提交
70
  SRpcMsg   rpcMsg;
M
Minghao Li 已提交
71 72
  syncPing2RpcMsg(pMsg, &rpcMsg);
  SyncPing *pMsg2 = syncPingFromRpcMsg2(&rpcMsg);
M
Minghao Li 已提交
73
  syncPingLog2((char *)"test5: syncPing2RpcMsg -> syncPingFromRpcMsg2 ", pMsg2);
M
Minghao Li 已提交
74

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

void test6() {
  SyncPing *pMsg = createMsg();
  int32_t   bufLen = syncPingSerialize3(pMsg, NULL, 0);
H
Hongze Cheng 已提交
83
  char     *serialized = (char *)taosMemoryMalloc(bufLen);
M
Minghao Li 已提交
84 85 86 87 88 89 90 91
  syncPingSerialize3(pMsg, serialized, bufLen);
  SyncPing *pMsg2 = syncPingDeserialize3(serialized, bufLen);
  assert(pMsg2 != NULL);
  syncPingLog2((char *)"test6: syncPingSerialize3 -> syncPingDeserialize3 ", pMsg2);

  taosMemoryFree(serialized);
  syncPingDestroy(pMsg);
  syncPingDestroy(pMsg2);
M
Minghao Li 已提交
92 93
}

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

M
Minghao Li 已提交
99 100 101 102 103
  test1();
  test2();
  test3();
  test4();
  test5();
M
Minghao Li 已提交
104
  test6();
M
Minghao Li 已提交
105 106 107

  return 0;
}