syncLogStoreTest.cpp 2.2 KB
Newer Older
M
Minghao Li 已提交
1
#include <gtest/gtest.h>
2
#include "syncTest.h"
M
Minghao Li 已提交
3 4 5 6 7 8 9 10 11 12

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

M
Minghao Li 已提交
13 14 15 16 17 18 19 20 21
SSyncNode*     pSyncNode;
SWal*          pWal;
SSyncLogStore* pLogStore;
const char*    pWalPath = "./syncLogStoreTest_wal";

void init() {
  walInit();
  taosRemoveDir(pWalPath);

M
Minghao Li 已提交
22 23
  SWalCfg walCfg;
  memset(&walCfg, 0, sizeof(SWalCfg));
M
Minghao Li 已提交
24
  walCfg.vgId = 1000;
M
Minghao Li 已提交
25 26 27
  walCfg.fsyncPeriod = 1000;
  walCfg.retentionPeriod = 1000;
  walCfg.rollPeriod = 1000;
M
Minghao Li 已提交
28 29 30
  walCfg.retentionSize = 1000;
  walCfg.segSize = 1000;
  walCfg.level = TAOS_WAL_FSYNC;
M
Minghao Li 已提交
31
  pWal = walOpen(pWalPath, &walCfg);
M
Minghao Li 已提交
32
  assert(pWal != NULL);
M
Minghao Li 已提交
33

M
Minghao Li 已提交
34 35 36
  pSyncNode = (SSyncNode*)taosMemoryMalloc(sizeof(SSyncNode));
  memset(pSyncNode, 0, sizeof(SSyncNode));
  pSyncNode->pWal = pWal;
M
Minghao Li 已提交
37 38
}

M
Minghao Li 已提交
39 40 41 42 43
void cleanup() {
  walClose(pWal);
  walCleanUp();
  taosMemoryFree(pSyncNode);
}
M
Minghao Li 已提交
44 45

void logStoreTest() {
M
Minghao Li 已提交
46 47
  pLogStore = logStoreCreate(pSyncNode);
  assert(pLogStore);
M
Minghao Li 已提交
48
  assert(pLogStore->syncLogLastIndex(pLogStore) == SYNC_INDEX_INVALID);
M
Minghao Li 已提交
49

M
Minghao Li 已提交
50
  logStoreLog2((char*)"logStoreTest", pLogStore);
M
Minghao Li 已提交
51

M
Minghao Li 已提交
52
  for (int i = 0; i < 5; ++i) {
M
Minghao Li 已提交
53
    int32_t         dataLen = 10;
M
Minghao Li 已提交
54 55 56 57 58 59
    SSyncRaftEntry* pEntry = syncEntryBuild(dataLen);
    assert(pEntry != NULL);
    pEntry->msgType = 1;
    pEntry->originalRpcType = 2;
    pEntry->seqNum = 3;
    pEntry->isWeak = true;
M
Minghao Li 已提交
60
    pEntry->term = 100 + i;
M
Minghao Li 已提交
61
    pEntry->index = pLogStore->syncLogLastIndex(pLogStore) + 1;
M
Minghao Li 已提交
62 63
    snprintf(pEntry->data, dataLen, "value%d", i);

M
Minghao Li 已提交
64
    syncEntryLog2((char*)"==write entry== :", pEntry);
M
Minghao Li 已提交
65
    pLogStore->syncLogAppendEntry(pLogStore, pEntry);
M
Minghao Li 已提交
66
    syncEntryDestory(pEntry);
M
Minghao Li 已提交
67 68

    if (i == 0) {
M
Minghao Li 已提交
69
      assert(pLogStore->syncLogLastIndex(pLogStore) == SYNC_INDEX_BEGIN);
M
Minghao Li 已提交
70
    }
M
Minghao Li 已提交
71
  }
M
Minghao Li 已提交
72
  logStoreLog2((char*)"after appendEntry", pLogStore);
M
Minghao Li 已提交
73
  pLogStore->syncLogTruncate(pLogStore, 3);
M
Minghao Li 已提交
74 75
  logStoreLog2((char*)"after truncate 3", pLogStore);
  logStoreDestory(pLogStore);
M
Minghao Li 已提交
76 77 78
}

int main(int argc, char** argv) {
M
Minghao Li 已提交
79
  gRaftDetailLog = true;
M
Minghao Li 已提交
80
  tsAsyncLog = 0;
M
Minghao Li 已提交
81
  sDebugFlag = DEBUG_TRACE + DEBUG_SCREEN + DEBUG_FILE;
M
Minghao Li 已提交
82

M
Minghao Li 已提交
83
  init();
M
Minghao Li 已提交
84 85
  logStoreTest();

M
Minghao Li 已提交
86 87 88
  taosMsleep(2000);
  cleanup();

M
Minghao Li 已提交
89 90
  return 0;
}