syncPingTest.cpp 2.8 KB
Newer Older
M
Minghao Li 已提交
1
#include <gtest/gtest.h>
M
Minghao Li 已提交
2 3 4
#include <stdio.h>
#include "syncIO.h"
#include "syncInt.h"
M
Minghao Li 已提交
5
#include "syncMessage.h"
M
Minghao Li 已提交
6
#include "syncUtil.h"
M
Minghao Li 已提交
7 8 9 10 11 12 13 14 15 16

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 已提交
17 18 19 20 21 22
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 已提交
23
  SyncPing *pMsg = syncPingBuild3(&srcId, &destId, 1000);
M
Minghao Li 已提交
24 25
  return pMsg;
}
M
Minghao Li 已提交
26

M
Minghao Li 已提交
27 28
void test1() {
  SyncPing *pMsg = createMsg();
M
Minghao Li 已提交
29
  syncPingLog2((char *)"test1:", pMsg);
M
Minghao Li 已提交
30 31
  syncPingDestroy(pMsg);
}
M
Minghao Li 已提交
32

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

wafwerar's avatar
wafwerar 已提交
42
  taosMemoryFree(serialized);
M
Minghao Li 已提交
43 44 45
  syncPingDestroy(pMsg);
  syncPingDestroy(pMsg2);
}
M
Minghao Li 已提交
46

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

wafwerar's avatar
wafwerar 已提交
54
  taosMemoryFree(serialized);
M
Minghao Li 已提交
55 56 57
  syncPingDestroy(pMsg);
  syncPingDestroy(pMsg2);
}
M
Minghao Li 已提交
58

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

M
Minghao Li 已提交
67 68
  syncPingDestroy(pMsg);
  syncPingDestroy(pMsg2);
M
Minghao Li 已提交
69
  rpcFreeCont(rpcMsg.pCont);
M
Minghao Li 已提交
70 71
}

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

M
Minghao Li 已提交
79 80
  syncPingDestroy(pMsg);
  syncPingDestroy(pMsg2);
M
Minghao Li 已提交
81 82 83 84 85 86
  rpcFreeCont(rpcMsg.pCont);
}

void test6() {
  SyncPing *pMsg = createMsg();
  int32_t   bufLen = syncPingSerialize3(pMsg, NULL, 0);
H
Hongze Cheng 已提交
87
  char     *serialized = (char *)taosMemoryMalloc(bufLen);
M
Minghao Li 已提交
88 89 90 91 92 93 94 95
  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 已提交
96 97
}

M
Minghao Li 已提交
98
int main() {
M
Minghao Li 已提交
99
  tsAsyncLog = 0;
M
Minghao Li 已提交
100
  sDebugFlag = DEBUG_TRACE + DEBUG_SCREEN + DEBUG_FILE;
M
Minghao Li 已提交
101
  logTest();
M
Minghao Li 已提交
102

M
Minghao Li 已提交
103 104 105 106 107
  test1();
  test2();
  test3();
  test4();
  test5();
M
Minghao Li 已提交
108
  test6();
M
Minghao Li 已提交
109 110 111

  return 0;
}