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 35 36 37 38 39 40 41 42 43 44 45 46 47 48
#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() {
  SyncClientRequest* pSyncMsg = syncClientRequestBuild(10);
  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() {
M
Minghao Li 已提交
49 50 51 52 53 54
  SyncClientRequest* pSyncMsg = syncClientRequestBuild(10);
  pSyncMsg->originalRpcType = 33;
  pSyncMsg->seqNum = 11;
  pSyncMsg->isWeak = 1;
  strcpy(pSyncMsg->data, "test3");

M
Minghao Li 已提交
55
  SSyncRaftEntry* pEntry = syncEntryBuild3(pSyncMsg, 100, 200, SYNC_RAFT_ENTRY_NOOP);
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 72
  pEntry->entryType = SYNC_RAFT_ENTRY_CONFIG;
  strcpy(pEntry->data, "test4");
M
Minghao Li 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
  syncEntryPrint(pEntry);

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

  free(serialized);
  syncEntryDestory(pEntry2);
  syncEntryDestory(pEntry);
}

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

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

  return 0;
}