syncLogStoreTest.cpp 2.3 KB
Newer Older
M
Minghao Li 已提交
1 2 3 4 5 6 7 8
#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"
M
Minghao Li 已提交
9
#include "wal.h"
M
Minghao Li 已提交
10 11 12 13 14 15 16 17 18 19

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 已提交
20 21 22 23 24 25 26 27 28
SSyncNode*     pSyncNode;
SWal*          pWal;
SSyncLogStore* pLogStore;
const char*    pWalPath = "./syncLogStoreTest_wal";

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

M
Minghao Li 已提交
29 30
  SWalCfg walCfg;
  memset(&walCfg, 0, sizeof(SWalCfg));
M
Minghao Li 已提交
31
  walCfg.vgId = 1000;
M
Minghao Li 已提交
32 33 34
  walCfg.fsyncPeriod = 1000;
  walCfg.retentionPeriod = 1000;
  walCfg.rollPeriod = 1000;
M
Minghao Li 已提交
35 36 37
  walCfg.retentionSize = 1000;
  walCfg.segSize = 1000;
  walCfg.level = TAOS_WAL_FSYNC;
M
Minghao Li 已提交
38
  pWal = walOpen(pWalPath, &walCfg);
M
Minghao Li 已提交
39
  assert(pWal != NULL);
M
Minghao Li 已提交
40

M
Minghao Li 已提交
41 42 43
  pSyncNode = (SSyncNode*)taosMemoryMalloc(sizeof(SSyncNode));
  memset(pSyncNode, 0, sizeof(SSyncNode));
  pSyncNode->pWal = pWal;
M
Minghao Li 已提交
44 45
}

M
Minghao Li 已提交
46 47 48 49 50
void cleanup() {
  walClose(pWal);
  walCleanUp();
  taosMemoryFree(pSyncNode);
}
M
Minghao Li 已提交
51 52

void logStoreTest() {
M
Minghao Li 已提交
53 54 55
  pLogStore = logStoreCreate(pSyncNode);
  assert(pLogStore);
  assert(pLogStore->getLastIndex(pLogStore) == SYNC_INDEX_INVALID);
M
Minghao Li 已提交
56

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

M
Minghao Li 已提交
59
  for (int i = 0; i < 5; ++i) {
M
Minghao Li 已提交
60
    int32_t         dataLen = 10;
M
Minghao Li 已提交
61 62 63 64 65 66
    SSyncRaftEntry* pEntry = syncEntryBuild(dataLen);
    assert(pEntry != NULL);
    pEntry->msgType = 1;
    pEntry->originalRpcType = 2;
    pEntry->seqNum = 3;
    pEntry->isWeak = true;
M
Minghao Li 已提交
67 68
    pEntry->term = 100 + i;
    pEntry->index = pLogStore->getLastIndex(pLogStore) + 1;
M
Minghao Li 已提交
69 70
    snprintf(pEntry->data, dataLen, "value%d", i);

M
Minghao Li 已提交
71 72
    syncEntryLog2((char*)"==write entry== :", pEntry);
    pLogStore->appendEntry(pLogStore, pEntry);
M
Minghao Li 已提交
73
    syncEntryDestory(pEntry);
M
Minghao Li 已提交
74 75

    if (i == 0) {
M
Minghao Li 已提交
76
      assert(pLogStore->getLastIndex(pLogStore) == SYNC_INDEX_BEGIN);
M
Minghao Li 已提交
77
    }
M
Minghao Li 已提交
78
  }
M
Minghao Li 已提交
79
  logStoreLog2((char*)"after appendEntry", pLogStore);
M
Minghao Li 已提交
80

M
Minghao Li 已提交
81 82
  pLogStore->truncate(pLogStore, 3);
  logStoreLog2((char*)"after truncate 3", pLogStore);
M
Minghao Li 已提交
83

M
Minghao Li 已提交
84
  logStoreDestory(pLogStore);
M
Minghao Li 已提交
85 86 87 88
}

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

M
Minghao Li 已提交
91
  init();
M
Minghao Li 已提交
92 93
  logStoreTest();

M
Minghao Li 已提交
94 95 96
  taosMsleep(2000);
  cleanup();

M
Minghao Li 已提交
97 98
  return 0;
}