syncPingTest.cpp 3.3 KB
Newer Older
M
Minghao Li 已提交
1
#include <gtest/gtest.h>
M
Minghao Li 已提交
2 3 4 5 6
#include <stdio.h>
#include "syncEnv.h"
#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 = 3;
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;
M
Minghao Li 已提交
28
  syncInfo.rpcClient = gSyncIO->clientRpc;
M
Minghao Li 已提交
29
  syncInfo.FpSendMsg = syncIOSendMsg;
M
Minghao Li 已提交
30 31
  syncInfo.queue = gSyncIO->pMsgQ;
  syncInfo.FpEqMsg = syncIOEqMsg;
M
Minghao Li 已提交
32
  syncInfo.pFsm = pFsm;
M
Minghao Li 已提交
33
  snprintf(syncInfo.path, sizeof(syncInfo.path), "%s", "./");
M
Minghao Li 已提交
34 35

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

M
Minghao Li 已提交
39 40 41 42 43
  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 已提交
44 45 46

  SSyncNode* pSyncNode = syncNodeOpen(&syncInfo);
  assert(pSyncNode != NULL);
M
Minghao Li 已提交
47

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

  return pSyncNode;
}

M
Minghao Li 已提交
60 61 62 63 64 65 66 67 68
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]);
    printf("raftId[%d] : %s\n", i, s);
    free(s);
  }
M
Minghao Li 已提交
69 70
}

M
Minghao Li 已提交
71
int main(int argc, char** argv) {
M
Minghao Li 已提交
72
  // taosInitLog((char *)"syncTest.log", 100000, 10);
M
Minghao Li 已提交
73 74 75
  tsAsyncLog = 0;
  sDebugFlag = 143 + 64;

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

  int32_t ret = syncIOStart((char*)"127.0.0.1", ports[myIndex]);
M
Minghao Li 已提交
82 83 84 85 86
  assert(ret == 0);

  ret = syncEnvStart();
  assert(ret == 0);

M
Minghao Li 已提交
87 88 89
  SSyncNode* pSyncNode = syncInitTest();
  assert(pSyncNode != NULL);
  syncNodePrint((char*)"----1", pSyncNode);
M
Minghao Li 已提交
90

M
Minghao Li 已提交
91 92 93 94 95
  initRaftId(pSyncNode);

  //---------------------------

  sTrace("syncNodeStartPingTimer ...");
M
sync io  
Minghao Li 已提交
96 97
  ret = syncNodeStartPingTimer(pSyncNode);
  assert(ret == 0);
M
Minghao Li 已提交
98
  syncNodePrint((char*)"----2", pSyncNode);
M
Minghao Li 已提交
99

M
Minghao Li 已提交
100
  sTrace("sleep ...");
M
Minghao Li 已提交
101
  taosMsleep(10000);
M
Minghao Li 已提交
102

M
Minghao Li 已提交
103
  sTrace("syncNodeStopPingTimer ...");
M
Minghao Li 已提交
104 105
  ret = syncNodeStopPingTimer(pSyncNode);
  assert(ret == 0);
M
Minghao Li 已提交
106
  syncNodePrint((char*)"----3", pSyncNode);
M
Minghao Li 已提交
107

M
Minghao Li 已提交
108 109
  sTrace("sleep ...");
  taosMsleep(5000);
M
Minghao Li 已提交
110

M
Minghao Li 已提交
111
  sTrace("syncNodeStartPingTimer ...");
M
Minghao Li 已提交
112 113
  ret = syncNodeStartPingTimer(pSyncNode);
  assert(ret == 0);
M
Minghao Li 已提交
114
  syncNodePrint((char*)"----4", pSyncNode);
M
Minghao Li 已提交
115

M
Minghao Li 已提交
116
  sTrace("sleep ...");
M
Minghao Li 已提交
117 118
  taosMsleep(10000);

M
Minghao Li 已提交
119
  sTrace("syncNodeStopPingTimer ...");
M
Minghao Li 已提交
120 121
  ret = syncNodeStopPingTimer(pSyncNode);
  assert(ret == 0);
M
Minghao Li 已提交
122
  syncNodePrint((char*)"----5", pSyncNode);
M
Minghao Li 已提交
123 124

  while (1) {
M
Minghao Li 已提交
125
    sTrace("while 1 sleep ...");
M
Minghao Li 已提交
126 127 128 129 130
    taosMsleep(1000);
  }

  return 0;
}