syncSnapshotSendTest.cpp 2.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#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");
}

SyncSnapshotSend *createMsg() {
  SyncSnapshotSend *pMsg = syncSnapshotSendBuild(20, 1000);
  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->term = 11;
24
  pMsg->privateTerm = 99;
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
  pMsg->lastIndex = 22;
  pMsg->lastTerm = 33;
  pMsg->seq = 44;
  strcpy(pMsg->data, "hello world");
  return pMsg;
}

void test1() {
  SyncSnapshotSend *pMsg = createMsg();
  syncSnapshotSendLog2((char *)"test1:", pMsg);
  syncSnapshotSendDestroy(pMsg);
}

void test2() {
  SyncSnapshotSend *pMsg = createMsg();
M
Minghao Li 已提交
40 41
  uint32_t          len = pMsg->bytes;
  char *            serialized = (char *)taosMemoryMalloc(len);
42 43 44 45 46 47 48 49 50 51 52 53
  syncSnapshotSendSerialize(pMsg, serialized, len);
  SyncSnapshotSend *pMsg2 = syncSnapshotSendBuild(pMsg->dataLen, 1000);
  syncSnapshotSendDeserialize(serialized, len, pMsg2);
  syncSnapshotSendLog2((char *)"test2: syncSnapshotSendSerialize -> syncSnapshotSendDeserialize ", pMsg2);

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

void test3() {
  SyncSnapshotSend *pMsg = createMsg();
M
Minghao Li 已提交
54 55
  uint32_t          len;
  char *            serialized = syncSnapshotSendSerialize2(pMsg, &len);
56 57 58 59 60 61 62 63 64 65
  SyncSnapshotSend *pMsg2 = syncSnapshotSendDeserialize2(serialized, len);
  syncSnapshotSendLog2((char *)"test3: syncSnapshotSendSerialize2 -> syncSnapshotSendDeserialize2 ", pMsg2);

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

void test4() {
  SyncSnapshotSend *pMsg = createMsg();
M
Minghao Li 已提交
66
  SRpcMsg           rpcMsg;
67 68 69 70 71 72 73 74 75 76 77 78
  syncSnapshotSend2RpcMsg(pMsg, &rpcMsg);
  SyncSnapshotSend *pMsg2 = (SyncSnapshotSend *)taosMemoryMalloc(rpcMsg.contLen);
  syncSnapshotSendFromRpcMsg(&rpcMsg, pMsg2);
  syncSnapshotSendLog2((char *)"test4: syncSnapshotSend2RpcMsg -> syncSnapshotSendFromRpcMsg ", pMsg2);

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

void test5() {
  SyncSnapshotSend *pMsg = createMsg();
M
Minghao Li 已提交
79
  SRpcMsg           rpcMsg;
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
  syncSnapshotSend2RpcMsg(pMsg, &rpcMsg);
  SyncSnapshotSend *pMsg2 = syncSnapshotSendFromRpcMsg2(&rpcMsg);
  syncSnapshotSendLog2((char *)"test5: syncSnapshotSend2RpcMsg -> syncSnapshotSendFromRpcMsg2 ", pMsg2);

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

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

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

  return 0;
}