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

M
Minghao Li 已提交
19 20 21 22 23 24 25 26 27
SSyncNode*     pSyncNode;
SWal*          pWal;
SSyncLogStore* pLogStore;
const char*    pWalPath = "./syncLogStoreTest_wal";

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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