syncPingReplyTest.cpp 3.1 KB
Newer Older
M
Minghao Li 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#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");
}

SyncPingReply *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
  SyncPingReply *pMsg = syncPingReplyBuild3(&srcId, &destId, 1000);
M
Minghao Li 已提交
24 25 26 27 28
  return pMsg;
}

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

void test2() {
  SyncPingReply *pMsg = createMsg();
M
Minghao Li 已提交
35
  uint32_t       len = pMsg->bytes;
wafwerar's avatar
wafwerar 已提交
36
  char *         serialized = (char *)taosMemoryMalloc(len);
M
Minghao Li 已提交
37 38 39
  syncPingReplySerialize(pMsg, serialized, len);
  SyncPingReply *pMsg2 = syncPingReplyBuild(pMsg->dataLen);
  syncPingReplyDeserialize(serialized, len, pMsg2);
M
Minghao Li 已提交
40
  syncPingReplyLog2((char *)"test2: syncPingReplySerialize -> syncPingReplyDeserialize ", pMsg2);
M
Minghao Li 已提交
41

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

void test3() {
  SyncPingReply *pMsg = createMsg();
M
Minghao Li 已提交
49 50
  uint32_t       len;
  char *         serialized = syncPingReplySerialize2(pMsg, &len);
M
Minghao Li 已提交
51
  SyncPingReply *pMsg2 = syncPingReplyDeserialize2(serialized, len);
M
Minghao Li 已提交
52
  syncPingReplyLog2((char *)"test3: syncPingReplySerialize2 -> syncPingReplyDeserialize2 ", pMsg2);
M
Minghao Li 已提交
53

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

void test4() {
  SyncPingReply *pMsg = createMsg();
M
Minghao Li 已提交
61
  SRpcMsg        rpcMsg;
M
Minghao Li 已提交
62
  syncPingReply2RpcMsg(pMsg, &rpcMsg);
wafwerar's avatar
wafwerar 已提交
63
  SyncPingReply *pMsg2 = (SyncPingReply *)taosMemoryMalloc(rpcMsg.contLen);
M
Minghao Li 已提交
64
  syncPingReplyFromRpcMsg(&rpcMsg, pMsg2);
M
Minghao Li 已提交
65
  syncPingReplyLog2((char *)"test4: syncPingReply2RpcMsg -> syncPingReplyFromRpcMsg ", pMsg2);
M
Minghao Li 已提交
66

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

void test5() {
  SyncPingReply *pMsg = createMsg();
M
Minghao Li 已提交
74
  SRpcMsg        rpcMsg;
M
Minghao Li 已提交
75 76
  syncPingReply2RpcMsg(pMsg, &rpcMsg);
  SyncPingReply *pMsg2 = syncPingReplyFromRpcMsg2(&rpcMsg);
M
Minghao Li 已提交
77
  syncPingReplyLog2((char *)"test5: syncPingReply2RpcMsg -> syncPingReplyFromRpcMsg2 ", pMsg2);
M
Minghao Li 已提交
78

M
Minghao Li 已提交
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
  rpcFreeCont(rpcMsg.pCont);
  syncPingReplyDestroy(pMsg);
  syncPingReplyDestroy(pMsg2);
}

void test6() {
  SyncPingReply *pMsg = createMsg();
  int32_t        bufLen = syncPingReplySerialize3(pMsg, NULL, 0);
  char *         serialized = (char *)taosMemoryMalloc(bufLen);
  syncPingReplySerialize3(pMsg, serialized, bufLen);
  SyncPingReply *pMsg2 = syncPingReplyDeserialize3(serialized, bufLen);
  assert(pMsg2 != NULL);
  syncPingReplyLog2((char *)"test6: syncPingReplySerialize3 -> syncPingReplyDeserialize3 ", pMsg2);

  taosMemoryFree(serialized);
M
Minghao Li 已提交
94 95 96 97 98 99
  syncPingReplyDestroy(pMsg);
  syncPingReplyDestroy(pMsg2);
}

int main() {
  tsAsyncLog = 0;
M
Minghao Li 已提交
100
  sDebugFlag = DEBUG_TRACE + DEBUG_SCREEN + DEBUG_FILE;
M
Minghao Li 已提交
101 102 103 104 105 106 107
  logTest();

  test1();
  test2();
  test3();
  test4();
  test5();
M
Minghao Li 已提交
108
  test6();
M
Minghao Li 已提交
109 110 111

  return 0;
}