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

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
uint16_t ports[] = {7010, 7110, 7210, 7310, 7410};
int32_t  replicaNum = 5;
int32_t  myIndex = 0;
M
Minghao Li 已提交
21

M
Minghao Li 已提交
22 23 24
SRaftId   ids[TSDB_MAX_REPLICA];
SSyncInfo syncInfo;
SSyncFSM* pFsm;
M
Minghao Li 已提交
25

M
Minghao Li 已提交
26 27
SSyncNode* syncNodeInit() {
  syncInfo.vgId = 1234;
S
Shengliang Guan 已提交
28
  syncInfo.msgcb = &gSyncIO->msgcb;
M
Minghao Li 已提交
29 30 31
  syncInfo.FpSendMsg = syncIOSendMsg;
  syncInfo.FpEqMsg = syncIOEqMsg;
  syncInfo.pFsm = pFsm;
M
Minghao Li 已提交
32
  snprintf(syncInfo.path, sizeof(syncInfo.path), "%s", "./sync_init_test");
M
Minghao Li 已提交
33 34

  SSyncCfg* pCfg = &syncInfo.syncCfg;
M
Minghao Li 已提交
35 36
  pCfg->myIndex = myIndex;
  pCfg->replicaNum = replicaNum;
M
Minghao Li 已提交
37

M
Minghao Li 已提交
38 39 40 41 42
  for (int i = 0; i < replicaNum; ++i) {
    pCfg->nodeInfo[i].nodePort = ports[i];
    snprintf(pCfg->nodeInfo[i].nodeFqdn, sizeof(pCfg->nodeInfo[i].nodeFqdn), "%s", "127.0.0.1");
    // taosGetFqdn(pCfg->nodeInfo[0].nodeFqdn);
  }
M
Minghao Li 已提交
43 44 45 46 47

  SSyncNode* pSyncNode = syncNodeOpen(&syncInfo);
  assert(pSyncNode != NULL);

  gSyncIO->FpOnSyncPing = pSyncNode->FpOnPing;
M
Minghao Li 已提交
48
  gSyncIO->FpOnSyncPingReply = pSyncNode->FpOnPingReply;
M
Minghao Li 已提交
49
  gSyncIO->FpOnSyncClientRequest = pSyncNode->FpOnClientRequest;
M
Minghao Li 已提交
50 51 52 53
  gSyncIO->FpOnSyncRequestVote = pSyncNode->FpOnRequestVote;
  gSyncIO->FpOnSyncRequestVoteReply = pSyncNode->FpOnRequestVoteReply;
  gSyncIO->FpOnSyncAppendEntries = pSyncNode->FpOnAppendEntries;
  gSyncIO->FpOnSyncAppendEntriesReply = pSyncNode->FpOnAppendEntriesReply;
M
Minghao Li 已提交
54
  gSyncIO->FpOnSyncTimeout = pSyncNode->FpOnTimeout;
M
Minghao Li 已提交
55 56 57 58 59
  gSyncIO->pSyncNode = pSyncNode;

  return pSyncNode;
}

M
Minghao Li 已提交
60 61 62 63 64 65
SSyncNode* syncInitTest() { return syncNodeInit(); }

void initRaftId(SSyncNode* pSyncNode) {
  for (int i = 0; i < replicaNum; ++i) {
    ids[i] = pSyncNode->replicasId[i];
    char* s = syncUtilRaftId2Str(&ids[i]);
M
Minghao Li 已提交
66
    sTrace("raftId[%d] : %s\n", i, s);
wafwerar's avatar
wafwerar 已提交
67
    taosMemoryFree(s);
M
Minghao Li 已提交
68 69 70 71
  }
}

int main(int argc, char** argv) {
M
Minghao Li 已提交
72
  // taosInitLog((char *)"tmp/syncInitTest.log", 100);
M
Minghao Li 已提交
73
  tsAsyncLog = 0;
M
Minghao Li 已提交
74
  sDebugFlag = DEBUG_TRACE + DEBUG_SCREEN + DEBUG_FILE;
M
Minghao Li 已提交
75

M
Minghao Li 已提交
76 77 78 79 80 81 82 83
  myIndex = 0;
  if (argc >= 2) {
    myIndex = atoi(argv[1]);
  }

  int32_t ret = syncIOStart((char*)"127.0.0.1", ports[myIndex]);
  assert(ret == 0);

S
Shengliang Guan 已提交
84
  ret = syncInit();
M
Minghao Li 已提交
85 86 87 88 89
  assert(ret == 0);

  SSyncNode* pSyncNode = syncInitTest();
  assert(pSyncNode != NULL);

M
Minghao Li 已提交
90
  syncNodeLog2((char*)"syncInitTest", pSyncNode);
M
Minghao Li 已提交
91
  initRaftId(pSyncNode);
M
Minghao Li 已提交
92

M
Minghao Li 已提交
93
  syncNodeClose(pSyncNode);
S
Shengliang Guan 已提交
94
  syncCleanUp();
M
Minghao Li 已提交
95
  // syncIOStop();
M
Minghao Li 已提交
96

M
Minghao Li 已提交
97
  // taosCloseLog();
M
Minghao Li 已提交
98
  return 0;
M
Minghao Li 已提交
99
}