syncEntryTest.cpp 2.2 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
#include <gtest/gtest.h>
#include <stdio.h>
#include "syncEnv.h"
#include "syncIO.h"
#include "syncInt.h"
#include "syncRaftLog.h"
#include "syncRaftStore.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");
}

void test1() {
  SSyncRaftEntry* pEntry = syncEntryBuild(10);
  assert(pEntry != NULL);
  pEntry->msgType = 1;
  pEntry->originalRpcType = 2;
  pEntry->seqNum = 3;
  pEntry->isWeak = true;
  pEntry->term = 100;
  pEntry->index = 200;
  strcpy(pEntry->data, "test1");

  syncEntryPrint(pEntry);
  syncEntryDestory(pEntry);
}

void test2() {
S
Shengliang Guan 已提交
35
  SyncClientRequest* pSyncMsg = syncClientRequestAlloc(10);
M
Minghao Li 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48
  pSyncMsg->originalRpcType = 33;
  pSyncMsg->seqNum = 11;
  pSyncMsg->isWeak = 1;
  strcpy(pSyncMsg->data, "test2");

  SSyncRaftEntry* pEntry = syncEntryBuild2(pSyncMsg, 100, 200);
  syncEntryPrint(pEntry);

  syncClientRequestDestroy(pSyncMsg);
  syncEntryDestory(pEntry);
}

void test3() {
S
Shengliang Guan 已提交
49
  SyncClientRequest* pSyncMsg = syncClientRequestAlloc(10);
M
Minghao Li 已提交
50 51 52 53 54
  pSyncMsg->originalRpcType = 33;
  pSyncMsg->seqNum = 11;
  pSyncMsg->isWeak = 1;
  strcpy(pSyncMsg->data, "test3");

M
Minghao Li 已提交
55
  SSyncRaftEntry* pEntry = syncEntryBuild3(pSyncMsg, 100, 200);
M
Minghao Li 已提交
56 57 58 59 60 61 62
  syncEntryPrint(pEntry);

  syncClientRequestDestroy(pSyncMsg);
  syncEntryDestory(pEntry);
}

void test4() {
M
Minghao Li 已提交
63 64 65 66 67 68 69 70
  SSyncRaftEntry* pEntry = syncEntryBuild(10);
  assert(pEntry != NULL);
  pEntry->msgType = 11;
  pEntry->originalRpcType = 22;
  pEntry->seqNum = 33;
  pEntry->isWeak = true;
  pEntry->term = 44;
  pEntry->index = 55;
M
Minghao Li 已提交
71
  strcpy(pEntry->data, "test4");
M
Minghao Li 已提交
72 73 74 75 76 77 78 79
  syncEntryPrint(pEntry);

  uint32_t len;
  char*    serialized = syncEntrySerialize(pEntry, &len);
  assert(serialized != NULL);
  SSyncRaftEntry* pEntry2 = syncEntryDeserialize(serialized, len);
  syncEntryPrint(pEntry2);

wafwerar's avatar
wafwerar 已提交
80
  taosMemoryFree(serialized);
M
Minghao Li 已提交
81 82 83 84 85 86
  syncEntryDestory(pEntry2);
  syncEntryDestory(pEntry);
}

int main(int argc, char** argv) {
  tsAsyncLog = 0;
M
Minghao Li 已提交
87
  sDebugFlag = DEBUG_TRACE + DEBUG_SCREEN + DEBUG_FILE;
M
Minghao Li 已提交
88 89 90

  test1();
  test2();
M
Minghao Li 已提交
91
  test3();
M
Minghao Li 已提交
92
  test4();
M
Minghao Li 已提交
93 94 95

  return 0;
}