syncEntryCacheTest.cpp 3.7 KB
Newer Older
M
Minghao Li 已提交
1 2 3 4 5 6 7
#include <stdio.h>
#include "syncEnv.h"
#include "syncIO.h"
#include "syncInt.h"
#include "syncRaftLog.h"
#include "syncRaftStore.h"
#include "syncUtil.h"
8
#include "tskiplist.h"
M
Minghao Li 已提交
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

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");
}

SSyncRaftEntry* createEntry(int i) {
  int32_t         dataLen = 20;
  SSyncRaftEntry* pEntry = syncEntryBuild(dataLen);
  assert(pEntry != NULL);
  pEntry->msgType = 88;
  pEntry->originalRpcType = 99;
  pEntry->seqNum = 3;
  pEntry->isWeak = true;
  pEntry->term = 100 + i;
  pEntry->index = i;
  snprintf(pEntry->data, dataLen, "value%d", i);

  return pEntry;
}

SSyncNode* createFakeNode() {
  SSyncNode* pSyncNode = (SSyncNode*)taosMemoryMalloc(sizeof(SSyncNode));
  ASSERT(pSyncNode != NULL);
  memset(pSyncNode, 0, sizeof(SSyncNode));

  return pSyncNode;
}

SRaftEntryCache* createCache(int maxCount) {
  SSyncNode* pSyncNode = createFakeNode();
  ASSERT(pSyncNode != NULL);

46
  SRaftEntryCache* pCache = raftEntryCacheCreate(pSyncNode, maxCount);
M
Minghao Li 已提交
47 48 49 50 51 52
  ASSERT(pCache != NULL);

  return pCache;
}

void test1() {
53
  int32_t              code = 0;
M
Minghao Li 已提交
54
  SRaftEntryCache* pCache = createCache(5);
55
  for (int i = 0; i < 10; ++i) {
M
Minghao Li 已提交
56
    SSyncRaftEntry* pEntry = createEntry(i);
57 58
    code = raftEntryCachePutEntry(pCache, pEntry);
    sTrace("put entry code:%d, pEntry:%p", code, pEntry);
M
Minghao Li 已提交
59
  }
60
  raftEntryCacheLog2((char*)"==test1 write 5 entries==", pCache);
M
Minghao Li 已提交
61

62 63
  raftEntryCacheClear(pCache, 3);
  raftEntryCacheLog2((char*)"==test1 evict 3 entries==", pCache);
M
Minghao Li 已提交
64

65 66
  raftEntryCacheClear(pCache, -1);
  raftEntryCacheLog2((char*)"==test1 evict -1(all) entries==", pCache);
M
Minghao Li 已提交
67 68 69
}

void test2() {
70
  int32_t              code = 0;
M
Minghao Li 已提交
71
  SRaftEntryCache* pCache = createCache(5);
72
  for (int i = 0; i < 10; ++i) {
M
Minghao Li 已提交
73
    SSyncRaftEntry* pEntry = createEntry(i);
74 75
    code = raftEntryCachePutEntry(pCache, pEntry);
    sTrace("put entry code:%d, pEntry:%p", code, pEntry);
M
Minghao Li 已提交
76
  }
77
  raftEntryCacheLog2((char*)"==test1 write 5 entries==", pCache);
M
Minghao Li 已提交
78

79 80
  SyncIndex index = 2;
  SSyncRaftEntry* pEntry = NULL;
M
Minghao Li 已提交
81

82 83 84
  code = raftEntryCacheGetEntryP(pCache, index, &pEntry);
  ASSERT(code == 1 && index == pEntry->index);
  sTrace("get entry:%p for %ld", pEntry, index);
M
Minghao Li 已提交
85 86
  syncEntryLog2((char*)"==test2 get entry pointer 2==", pEntry);

87 88 89 90 91 92
  code = raftEntryCacheGetEntry(pCache, index, &pEntry);
  ASSERT(code == 1 && index == pEntry->index);
  sTrace("get entry:%p for %ld", pEntry, index);
  syncEntryLog2((char*)"==test2 get entry 2==", pEntry);
  syncEntryDestory(pEntry);

M
Minghao Li 已提交
93 94
  // not found
  index = 8;
95 96 97
  code = raftEntryCacheGetEntry(pCache, index, &pEntry);
  ASSERT(code == 0);
  sTrace("get entry:%p for %ld", pEntry, index);
M
Minghao Li 已提交
98 99 100 101
  sTrace("==test2 get entry 8 not found==");

  // not found
  index = 9;
102 103 104 105
  code = raftEntryCacheGetEntry(pCache, index, &pEntry);
  ASSERT(code == 0);
  sTrace("get entry:%p for %ld", pEntry, index);
  sTrace("==test2 get entry 9 not found==");
M
Minghao Li 已提交
106 107 108
}

void test3() {
109 110 111
  int32_t              code = 0;
  SRaftEntryCache* pCache = createCache(20);
  for (int i = 0; i <= 4; ++i) {
M
Minghao Li 已提交
112
    SSyncRaftEntry* pEntry = createEntry(i);
113 114
    code = raftEntryCachePutEntry(pCache, pEntry);
    sTrace("put entry code:%d, pEntry:%p", code, pEntry);
115 116
  }
  for (int i = 9; i >= 5; --i) {
117
    SSyncRaftEntry* pEntry = createEntry(i);
118 119
    code = raftEntryCachePutEntry(pCache, pEntry);
    sTrace("put entry code:%d, pEntry:%p", code, pEntry);
120
  }
121
  raftEntryCacheLog2((char*)"==test3 write 10 entries==", pCache);
122 123
}

M
Minghao Li 已提交
124 125 126 127 128
int main(int argc, char** argv) {
  gRaftDetailLog = true;
  tsAsyncLog = 0;
  sDebugFlag = DEBUG_TRACE + DEBUG_SCREEN + DEBUG_FILE + DEBUG_DEBUG;

129 130 131
  test1();
  test2();
  test3();
M
Minghao Li 已提交
132 133 134

  return 0;
}