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
#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");
M
Minghao Li 已提交
50
  SyncClientRequest *pMsg = syncClientRequestBuild2(&rpcMsg, 123, true, 1000);
M
Minghao Li 已提交
51 52 53 54
  return pMsg;
}

SyncRequestVote *createSyncRequestVote() {
M
Minghao Li 已提交
55
  SyncRequestVote *pMsg = syncRequestVoteBuild(1000);
M
Minghao Li 已提交
56 57 58 59
  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
  pMsg->lastLogIndex = 22;
  pMsg->lastLogTerm = 33;
  return pMsg;
}

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

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

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

void test4() {
  SyncRequestVote *pMsg = createSyncRequestVote();
  SRpcMsg          rpcMsg;
  syncRequestVote2RpcMsg(pMsg, &rpcMsg);
M
Minghao Li 已提交
129
  syncRpcMsgLog2((char *)"test4", &rpcMsg);
M
Minghao Li 已提交
130 131 132 133 134
  syncRequestVoteDestroy(pMsg);
}

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

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

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

void test8() {
  SyncClientRequest *pMsg = createSyncClientRequest();
M
Minghao Li 已提交
159
  SRpcMsg            rpcMsg;
M
Minghao Li 已提交
160
  syncClientRequest2RpcMsg(pMsg, &rpcMsg);
M
Minghao Li 已提交
161
  syncRpcMsgLog2((char *)"test8", &rpcMsg);
M
Minghao Li 已提交
162 163 164 165 166
  syncClientRequestDestroy(pMsg);
}

int main() {
  tsAsyncLog = 0;
M
Minghao Li 已提交
167
  sDebugFlag = DEBUG_TRACE + DEBUG_SCREEN + DEBUG_FILE;
M
Minghao Li 已提交
168 169 170 171 172 173 174 175 176 177 178 179 180
  logTest();

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

  return 0;
}