syncRpcMsgTest.cpp 4.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
#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");
}

int          gg = 0;
SyncTimeout *createSyncTimeout() {
  SyncTimeout *pMsg = syncTimeoutBuild2(SYNC_TIMEOUT_PING, 999, 333, &gg);
  return pMsg;
}

SyncPing *createSyncPing() {
  SRaftId srcId, destId;
  srcId.addr = syncUtilAddr2U64("127.0.0.1", 1234);
  srcId.vgId = 100;
  destId.addr = syncUtilAddr2U64("127.0.0.1", 5678);
  destId.vgId = 100;
  SyncPing *pMsg = syncPingBuild3(&srcId, &destId);
  return pMsg;
}

SyncPingReply *createSyncPingReply() {
  SRaftId srcId, destId;
  srcId.addr = syncUtilAddr2U64("127.0.0.1", 1234);
  srcId.vgId = 100;
  destId.addr = syncUtilAddr2U64("127.0.0.1", 5678);
  destId.vgId = 100;
  SyncPingReply *pMsg = syncPingReplyBuild3(&srcId, &destId);
  return pMsg;
}

SyncClientRequest *createSyncClientRequest() {
  SRpcMsg rpcMsg;
  memset(&rpcMsg, 0, sizeof(rpcMsg));
  rpcMsg.msgType = 12345;
  rpcMsg.contLen = 20;
  rpcMsg.pCont = rpcMallocCont(rpcMsg.contLen);
  strcpy((char *)rpcMsg.pCont, "hello rpc");
  SyncClientRequest *pMsg = syncClientRequestBuild2(&rpcMsg, 123, true);
  return pMsg;
}

SyncRequestVote *createSyncRequestVote() {
  SyncRequestVote *pMsg = syncRequestVoteBuild();
  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;
M
Minghao Li 已提交
60
  pMsg->term = 11;
M
Minghao Li 已提交
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
  pMsg->lastLogIndex = 22;
  pMsg->lastLogTerm = 33;
  return pMsg;
}

SyncRequestVoteReply *createSyncRequestVoteReply() {
  SyncRequestVoteReply *pMsg = syncRequestVoteReplyBuild();
  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 = 77;
  pMsg->voteGranted = true;
  return pMsg;
}

SyncAppendEntries *createSyncAppendEntries() {
  SyncAppendEntries *pMsg = syncAppendEntriesBuild(20);
  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->prevLogIndex = 11;
  pMsg->prevLogTerm = 22;
  pMsg->commitIndex = 33;
  strcpy(pMsg->data, "hello world");
  return pMsg;
}

SyncAppendEntriesReply *createSyncAppendEntriesReply() {
  SyncAppendEntriesReply *pMsg = syncAppendEntriesReplyBuild();
  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->success = true;
  pMsg->matchIndex = 77;
  return pMsg;
}

void test1() {
  SyncTimeout *pMsg = createSyncTimeout();
M
Minghao Li 已提交
103
  SRpcMsg      rpcMsg;
M
Minghao Li 已提交
104 105 106 107 108 109 110
  syncTimeout2RpcMsg(pMsg, &rpcMsg);
  syncRpcMsgPrint2((char *)"test1", &rpcMsg);
  syncTimeoutDestroy(pMsg);
}

void test2() {
  SyncPing *pMsg = createSyncPing();
M
Minghao Li 已提交
111
  SRpcMsg   rpcMsg;
M
Minghao Li 已提交
112 113 114 115 116 117 118
  syncPing2RpcMsg(pMsg, &rpcMsg);
  syncRpcMsgPrint2((char *)"test2", &rpcMsg);
  syncPingDestroy(pMsg);
}

void test3() {
  SyncPingReply *pMsg = createSyncPingReply();
M
Minghao Li 已提交
119
  SRpcMsg        rpcMsg;
M
Minghao Li 已提交
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
  syncPingReply2RpcMsg(pMsg, &rpcMsg);
  syncRpcMsgPrint2((char *)"test3", &rpcMsg);
  syncPingReplyDestroy(pMsg);
}

void test4() {
  SyncRequestVote *pMsg = createSyncRequestVote();
  SRpcMsg          rpcMsg;
  syncRequestVote2RpcMsg(pMsg, &rpcMsg);
  syncRpcMsgPrint2((char *)"test4", &rpcMsg);
  syncRequestVoteDestroy(pMsg);
}

void test5() {
  SyncRequestVoteReply *pMsg = createSyncRequestVoteReply();
M
Minghao Li 已提交
135
  SRpcMsg               rpcMsg;
M
Minghao Li 已提交
136 137 138 139 140 141 142
  syncRequestVoteReply2RpcMsg(pMsg, &rpcMsg);
  syncRpcMsgPrint2((char *)"test5", &rpcMsg);
  syncRequestVoteReplyDestroy(pMsg);
}

void test6() {
  SyncAppendEntries *pMsg = createSyncAppendEntries();
M
Minghao Li 已提交
143
  SRpcMsg            rpcMsg;
M
Minghao Li 已提交
144 145 146 147 148 149 150
  syncAppendEntries2RpcMsg(pMsg, &rpcMsg);
  syncRpcMsgPrint2((char *)"test6", &rpcMsg);
  syncAppendEntriesDestroy(pMsg);
}

void test7() {
  SyncAppendEntriesReply *pMsg = createSyncAppendEntriesReply();
M
Minghao Li 已提交
151
  SRpcMsg                 rpcMsg;
M
Minghao Li 已提交
152 153 154 155 156 157 158
  syncAppendEntriesReply2RpcMsg(pMsg, &rpcMsg);
  syncRpcMsgPrint2((char *)"test7", &rpcMsg);
  syncAppendEntriesReplyDestroy(pMsg);
}

void test8() {
  SyncClientRequest *pMsg = createSyncClientRequest();
M
Minghao Li 已提交
159
  SRpcMsg            rpcMsg;
M
Minghao Li 已提交
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
  syncClientRequest2RpcMsg(pMsg, &rpcMsg);
  syncRpcMsgPrint2((char *)"test8", &rpcMsg);
  syncClientRequestDestroy(pMsg);
}

int main() {
  // taosInitLog((char *)"syncTest.log", 100000, 10);
  tsAsyncLog = 0;
  sDebugFlag = 143 + 64;
  logTest();

  test1();
  test2();
  test3();
  test4();
  test5();
  test6();
  test7();
  test8();

  return 0;
}