syncRpcMsgTest.cpp 4.9 KB
Newer Older
M
Minghao Li 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#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() {
M
Minghao Li 已提交
19
  SyncTimeout *pMsg = syncTimeoutBuild2(SYNC_TIMEOUT_PING, 999, 333, 1000, &gg);
M
Minghao Li 已提交
20 21 22 23 24 25 26 27 28
  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;
M
Minghao Li 已提交
29
  SyncPing *pMsg = syncPingBuild3(&srcId, &destId, 1000);
M
Minghao Li 已提交
30 31 32 33 34 35 36 37 38
  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;
M
Minghao Li 已提交
39
  SyncPingReply *pMsg = syncPingReplyBuild3(&srcId, &destId, 1000);
M
Minghao Li 已提交
40 41 42 43 44 45 46 47 48 49
  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");
50 51 52 53 54

  SRpcMsg clientRequestMsg;
  syncClientRequestBuildFromRpcMsg(&clientRequestMsg, &rpcMsg, 123, true, 1000);
  SyncClientRequest *pMsg = (SyncClientRequest *)taosMemoryMalloc(clientRequestMsg.contLen);
  memcpy(pMsg->data, clientRequestMsg.pCont, clientRequestMsg.contLen);
M
Minghao Li 已提交
55 56 57 58
  return pMsg;
}

SyncRequestVote *createSyncRequestVote() {
M
Minghao Li 已提交
59
  SyncRequestVote *pMsg = syncRequestVoteBuild(1000);
M
Minghao Li 已提交
60 61 62 63
  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 已提交
64
  pMsg->term = 11;
M
Minghao Li 已提交
65 66 67 68 69 70
  pMsg->lastLogIndex = 22;
  pMsg->lastLogTerm = 33;
  return pMsg;
}

SyncRequestVoteReply *createSyncRequestVoteReply() {
M
Minghao Li 已提交
71
  SyncRequestVoteReply *pMsg = syncRequestVoteReplyBuild(1000);
M
Minghao Li 已提交
72 73 74 75 76 77 78 79 80 81
  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() {
M
Minghao Li 已提交
82
  SyncAppendEntries *pMsg = syncAppendEntriesBuild(20, 1000);
M
Minghao Li 已提交
83 84 85 86 87 88 89 90 91 92 93 94
  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() {
M
Minghao Li 已提交
95
  SyncAppendEntriesReply *pMsg = syncAppendEntriesReplyBuild(1000);
M
Minghao Li 已提交
96 97 98 99 100 101 102 103 104 105 106
  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 已提交
107
  SRpcMsg      rpcMsg;
M
Minghao Li 已提交
108
  syncTimeout2RpcMsg(pMsg, &rpcMsg);
M
Minghao Li 已提交
109
  syncRpcMsgLog2((char *)"test1", &rpcMsg);
M
Minghao Li 已提交
110 111 112 113 114
  syncTimeoutDestroy(pMsg);
}

void test2() {
  SyncPing *pMsg = createSyncPing();
M
Minghao Li 已提交
115
  SRpcMsg   rpcMsg;
M
Minghao Li 已提交
116
  syncPing2RpcMsg(pMsg, &rpcMsg);
M
Minghao Li 已提交
117
  syncRpcMsgLog2((char *)"test2", &rpcMsg);
M
Minghao Li 已提交
118 119 120 121 122
  syncPingDestroy(pMsg);
}

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

void test4() {
  SyncRequestVote *pMsg = createSyncRequestVote();
  SRpcMsg          rpcMsg;
  syncRequestVote2RpcMsg(pMsg, &rpcMsg);
M
Minghao Li 已提交
133
  syncRpcMsgLog2((char *)"test4", &rpcMsg);
M
Minghao Li 已提交
134 135 136 137 138
  syncRequestVoteDestroy(pMsg);
}

void test5() {
  SyncRequestVoteReply *pMsg = createSyncRequestVoteReply();
M
Minghao Li 已提交
139
  SRpcMsg               rpcMsg;
M
Minghao Li 已提交
140
  syncRequestVoteReply2RpcMsg(pMsg, &rpcMsg);
M
Minghao Li 已提交
141
  syncRpcMsgLog2((char *)"test5", &rpcMsg);
M
Minghao Li 已提交
142 143 144 145 146
  syncRequestVoteReplyDestroy(pMsg);
}

void test6() {
  SyncAppendEntries *pMsg = createSyncAppendEntries();
M
Minghao Li 已提交
147
  SRpcMsg            rpcMsg;
M
Minghao Li 已提交
148
  syncAppendEntries2RpcMsg(pMsg, &rpcMsg);
M
Minghao Li 已提交
149
  syncRpcMsgLog2((char *)"test6", &rpcMsg);
M
Minghao Li 已提交
150 151 152 153 154
  syncAppendEntriesDestroy(pMsg);
}

void test7() {
  SyncAppendEntriesReply *pMsg = createSyncAppendEntriesReply();
M
Minghao Li 已提交
155
  SRpcMsg                 rpcMsg;
M
Minghao Li 已提交
156
  syncAppendEntriesReply2RpcMsg(pMsg, &rpcMsg);
M
Minghao Li 已提交
157
  syncRpcMsgLog2((char *)"test7", &rpcMsg);
M
Minghao Li 已提交
158 159 160 161
  syncAppendEntriesReplyDestroy(pMsg);
}

void test8() {
162
#if 0
M
Minghao Li 已提交
163
  SyncClientRequest *pMsg = createSyncClientRequest();
S
Shengliang Guan 已提交
164
  SRpcMsg            rpcMsg = {0};
M
Minghao Li 已提交
165
  syncClientRequest2RpcMsg(pMsg, &rpcMsg);
M
Minghao Li 已提交
166
  syncRpcMsgLog2((char *)"test8", &rpcMsg);
167 168
  taosMemoryFree(pMsg);
#endif
M
Minghao Li 已提交
169 170 171 172
}

int main() {
  tsAsyncLog = 0;
M
Minghao Li 已提交
173
  sDebugFlag = DEBUG_TRACE + DEBUG_SCREEN + DEBUG_FILE;
M
Minghao Li 已提交
174 175 176 177 178 179 180 181 182 183 184 185 186
  logTest();

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

  return 0;
}