syncReconfigFinishTest.cpp 3.7 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 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
#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");
}

SSyncCfg* createSyncOldCfg() {
  SSyncCfg* pCfg = (SSyncCfg*)taosMemoryMalloc(sizeof(SSyncCfg));
  memset(pCfg, 0, sizeof(SSyncCfg));

  pCfg->replicaNum = 3;
  pCfg->myIndex = 1;
  for (int i = 0; i < pCfg->replicaNum; ++i) {
    ((pCfg->nodeInfo)[i]).nodePort = i * 100;
    snprintf(((pCfg->nodeInfo)[i]).nodeFqdn, sizeof(((pCfg->nodeInfo)[i]).nodeFqdn), "100.200.300.%d", i);
  }

  return pCfg;
}

SSyncCfg* createSyncNewCfg() {
  SSyncCfg* pCfg = (SSyncCfg*)taosMemoryMalloc(sizeof(SSyncCfg));
  memset(pCfg, 0, sizeof(SSyncCfg));

  pCfg->replicaNum = 3;
  pCfg->myIndex = 1;
  for (int i = 0; i < pCfg->replicaNum; ++i) {
    ((pCfg->nodeInfo)[i]).nodePort = i * 100;
    snprintf(((pCfg->nodeInfo)[i]).nodeFqdn, sizeof(((pCfg->nodeInfo)[i]).nodeFqdn), "500.600.700.%d", i);
  }

  return pCfg;
}

SyncReconfigFinish *createMsg() {
  SyncReconfigFinish *pMsg = syncReconfigFinishBuild(1234);
 
  SSyncCfg* pOld = createSyncOldCfg();
  SSyncCfg* pNew = createSyncNewCfg();
  pMsg->oldCfg = *pOld;
  pMsg->newCfg = *pNew;

  pMsg->newCfgIndex = 11;
  pMsg->newCfgTerm = 22;
  pMsg->newCfgSeqNum = 33;

  taosMemoryFree(pOld);
  taosMemoryFree(pNew);

  return pMsg;
}


void test1() {
  SyncReconfigFinish *pMsg = createMsg();
  syncReconfigFinishLog2((char *)"test1:", pMsg);
  syncReconfigFinishDestroy(pMsg);
}


void test2() {
  SyncReconfigFinish *pMsg = createMsg();
  uint32_t         len = pMsg->bytes;
  char *           serialized = (char *)taosMemoryMalloc(len);
  syncReconfigFinishSerialize(pMsg, serialized, len);
  SyncReconfigFinish *pMsg2 = syncReconfigFinishBuild(1000);
  syncReconfigFinishDeserialize(serialized, len, pMsg2);
  syncReconfigFinishLog2((char *)"test2: syncReconfigFinishSerialize -> syncReconfigFinishDeserialize ", pMsg2);

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

void test3() {
  SyncReconfigFinish *pMsg = createMsg();
  uint32_t         len;
  char *           serialized = syncReconfigFinishSerialize2(pMsg, &len);
  SyncReconfigFinish *pMsg2 = syncReconfigFinishDeserialize2(serialized, len);
  syncReconfigFinishLog2((char *)"test3: SyncReconfigFinishSerialize2 -> syncReconfigFinishDeserialize2 ", pMsg2);

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

void test4() {
  SyncReconfigFinish *pMsg = createMsg();
  SRpcMsg          rpcMsg;
  syncReconfigFinish2RpcMsg(pMsg, &rpcMsg);
  SyncReconfigFinish *pMsg2 = (SyncReconfigFinish *)taosMemoryMalloc(rpcMsg.contLen);
  syncReconfigFinishFromRpcMsg(&rpcMsg, pMsg2);
  syncReconfigFinishLog2((char *)"test4: syncReconfigFinish2RpcMsg -> syncReconfigFinishFromRpcMsg ", pMsg2);

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

void test5() {
  SyncReconfigFinish *pMsg = createMsg();
  SRpcMsg          rpcMsg;
  syncReconfigFinish2RpcMsg(pMsg, &rpcMsg);
  SyncReconfigFinish *pMsg2 = syncReconfigFinishFromRpcMsg2(&rpcMsg);
  syncReconfigFinishLog2((char *)"test5: syncReconfigFinish2RpcMsg -> syncReconfigFinishFromRpcMsg2 ", pMsg2);

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

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

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

  return 0;
}