syncRaftCfgTest.cpp 3.2 KB
Newer Older
M
Minghao Li 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include "syncRaftStore.h"
//#include <gtest/gtest.h>
#include <stdio.h>
#include "syncIO.h"
#include "syncInt.h"
#include "syncRaftCfg.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");
}

M
Minghao Li 已提交
18 19 20 21 22 23 24 25 26 27 28
SRaftCfg* createRaftCfg() {
  SRaftCfg* pCfg = (SRaftCfg*)taosMemoryMalloc(sizeof(SRaftCfg));
  memset(pCfg, 0, sizeof(SRaftCfg));

  pCfg->cfg.replicaNum = 3;
  pCfg->cfg.myIndex = 1;
  for (int i = 0; i < pCfg->cfg.replicaNum; ++i) {
    ((pCfg->cfg.nodeInfo)[i]).nodePort = i * 100;
    snprintf(((pCfg->cfg.nodeInfo)[i]).nodeFqdn, sizeof(((pCfg->cfg.nodeInfo)[i]).nodeFqdn), "100.200.300.%d", i);
  }
  pCfg->isStandBy = taosGetTimestampSec() % 100;
M
Minghao Li 已提交
29
  pCfg->batchSize = taosGetTimestampSec() % 100;
M
Minghao Li 已提交
30

31 32 33 34 35 36 37 38
  pCfg->configIndexCount = 5;
  for (int i = 0; i < MAX_CONFIG_INDEX_COUNT; ++i) {
    (pCfg->configIndexArr)[i] = -1;
  }
  for (int i = 0; i < pCfg->configIndexCount; ++i) {
    (pCfg->configIndexArr)[i] = i * 100;
  }

M
Minghao Li 已提交
39 40 41
  return pCfg;
}

M
Minghao Li 已提交
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
SSyncCfg* createSyncCfg() {
  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;
}

void test2() {
  SSyncCfg* pCfg = createSyncCfg();
  char*     s = syncCfg2Str(pCfg);

  SSyncCfg* pCfg2 = (SSyncCfg*)taosMemoryMalloc(sizeof(SSyncCfg));
  int32_t   ret = syncCfgFromStr(s, pCfg2);
  assert(ret == 0);

  taosMemoryFree(pCfg);
  taosMemoryFree(s);
  taosMemoryFree(pCfg2);
}

void test3() {
  SSyncCfg* pCfg = createSyncCfg();
  char*     s = (char*)"./test3_raft_cfg.json";

  if (taosCheckExistFile(s)) {
    printf("%s file: %s already exist! \n", (char*)__FUNCTION__, s);
  } else {
M
Minghao Li 已提交
76 77
    SRaftCfgMeta meta;
    meta.isStandBy = 7;
M
Minghao Li 已提交
78
    meta.snapshotStrategy = 9;
M
Minghao Li 已提交
79
    meta.batchSize = 10;
80
    meta.lastConfigIndex = 789;
M
Minghao Li 已提交
81
    raftCfgCreateFile(pCfg, meta, s);
M
Minghao Li 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
    printf("%s create json file: %s \n", (char*)__FUNCTION__, s);
  }

  taosMemoryFree(pCfg);
}

void test4() {
  SRaftCfg* pCfg = raftCfgOpen("./test3_raft_cfg.json");
  assert(pCfg != NULL);

  int32_t ret = raftCfgClose(pCfg);
  assert(ret == 0);
}

void test5() {
  SRaftCfg* pCfg = raftCfgOpen("./test3_raft_cfg.json");
  assert(pCfg != NULL);

  pCfg->cfg.myIndex = taosGetTimestampSec();
M
Minghao Li 已提交
101
  pCfg->isStandBy += 2;
M
Minghao Li 已提交
102
  pCfg->snapshotStrategy += 3;
M
Minghao Li 已提交
103
  pCfg->batchSize += 4;
104
  pCfg->lastConfigIndex += 1000;
105 106 107 108 109 110 111 112 113

  pCfg->configIndexCount = 5;
  for (int i = 0; i < MAX_CONFIG_INDEX_COUNT; ++i) {
    (pCfg->configIndexArr)[i] = -1;
  }
  for (int i = 0; i < pCfg->configIndexCount; ++i) {
    (pCfg->configIndexArr)[i] = i * 100;
  }

M
Minghao Li 已提交
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
  raftCfgPersist(pCfg);

  printf("%s update json file: %s myIndex->%d \n", (char*)__FUNCTION__, "./test3_raft_cfg.json", pCfg->cfg.myIndex);

  int32_t ret = raftCfgClose(pCfg);
  assert(ret == 0);
}

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

  logTest();
  test2();
  test3();
  test4();
  test5();
M
Minghao Li 已提交
131

M
Minghao Li 已提交
132 133
  return 0;
}