From 39d0b652baef3380678991d4bd9bec60a577c290 Mon Sep 17 00:00:00 2001 From: Minghao Li Date: Wed, 8 Jun 2022 17:18:26 +0800 Subject: [PATCH] refactor(sync): add raft log test --- source/libs/sync/src/syncRaftLog.c | 2 +- source/libs/sync/test/syncRaftLogTest2.cpp | 30 ++++++------- source/libs/sync/test/syncRaftLogTest3.cpp | 51 ++++++++++++++++++++++ 3 files changed, 67 insertions(+), 16 deletions(-) diff --git a/source/libs/sync/src/syncRaftLog.c b/source/libs/sync/src/syncRaftLog.c index e03f8ba65f..7feb7d293f 100644 --- a/source/libs/sync/src/syncRaftLog.c +++ b/source/libs/sync/src/syncRaftLog.c @@ -67,7 +67,7 @@ static SyncIndex raftLogEndIndex(struct SSyncLogStore* pLogStore) { return raftL static bool raftLogIsEmpty(struct SSyncLogStore* pLogStore) { SyncIndex beginIndex = raftLogBeginIndex(pLogStore); SyncIndex endIndex = raftLogEndIndex(pLogStore); - return (endIndex >= beginIndex); + return (endIndex < beginIndex); } static int32_t raftLogEntryCount(struct SSyncLogStore* pLogStore) { diff --git a/source/libs/sync/test/syncRaftLogTest2.cpp b/source/libs/sync/test/syncRaftLogTest2.cpp index d05401b0f3..64e1da51a1 100644 --- a/source/libs/sync/test/syncRaftLogTest2.cpp +++ b/source/libs/sync/test/syncRaftLogTest2.cpp @@ -76,7 +76,7 @@ void test1() { assert(pLogStore->syncLogEndIndex(pLogStore) == -1); assert(pLogStore->syncLogEntryCount(pLogStore) == 0); assert(pLogStore->syncLogWriteIndex(pLogStore) == 0); - assert(pLogStore->syncLogIsEmpty(pLogStore) == 0); + assert(pLogStore->syncLogIsEmpty(pLogStore) == 1); assert(pLogStore->syncLogLastIndex(pLogStore) == -1); assert(pLogStore->syncLogLastTerm(pLogStore) == 0); } @@ -95,7 +95,7 @@ void test1() { assert(pLogStore->syncLogEndIndex(pLogStore) == -1); assert(pLogStore->syncLogEntryCount(pLogStore) == 0); assert(pLogStore->syncLogWriteIndex(pLogStore) == 0); - assert(pLogStore->syncLogIsEmpty(pLogStore) == 0); + assert(pLogStore->syncLogIsEmpty(pLogStore) == 1); assert(pLogStore->syncLogLastIndex(pLogStore) == -1); assert(pLogStore->syncLogLastTerm(pLogStore) == 0); } @@ -118,7 +118,7 @@ void test2() { assert(pLogStore->syncLogEndIndex(pLogStore) == -1); assert(pLogStore->syncLogEntryCount(pLogStore) == 0); assert(pLogStore->syncLogWriteIndex(pLogStore) == 5); - assert(pLogStore->syncLogIsEmpty(pLogStore) == 0); + assert(pLogStore->syncLogIsEmpty(pLogStore) == 1); assert(pLogStore->syncLogLastIndex(pLogStore) == -1); assert(pLogStore->syncLogLastTerm(pLogStore) == 0); } @@ -137,7 +137,7 @@ void test2() { assert(pLogStore->syncLogEndIndex(pLogStore) == -1); assert(pLogStore->syncLogEntryCount(pLogStore) == 0); assert(pLogStore->syncLogWriteIndex(pLogStore) == 5); - assert(pLogStore->syncLogIsEmpty(pLogStore) == 0); + assert(pLogStore->syncLogIsEmpty(pLogStore) == 1); assert(pLogStore->syncLogLastIndex(pLogStore) == -1); assert(pLogStore->syncLogLastTerm(pLogStore) == 0); } @@ -159,7 +159,7 @@ void test3() { assert(pLogStore->syncLogEndIndex(pLogStore) == -1); assert(pLogStore->syncLogEntryCount(pLogStore) == 0); assert(pLogStore->syncLogWriteIndex(pLogStore) == 0); - assert(pLogStore->syncLogIsEmpty(pLogStore) == 0); + assert(pLogStore->syncLogIsEmpty(pLogStore) == 1); assert(pLogStore->syncLogLastIndex(pLogStore) == -1); assert(pLogStore->syncLogLastTerm(pLogStore) == 0); } @@ -186,7 +186,7 @@ void test3() { assert(pLogStore->syncLogEndIndex(pLogStore) == 4); assert(pLogStore->syncLogEntryCount(pLogStore) == 5); assert(pLogStore->syncLogWriteIndex(pLogStore) == 5); - assert(pLogStore->syncLogIsEmpty(pLogStore) == 1); + assert(pLogStore->syncLogIsEmpty(pLogStore) == 0); assert(pLogStore->syncLogLastIndex(pLogStore) == 4); assert(pLogStore->syncLogLastTerm(pLogStore) == 104); } @@ -205,7 +205,7 @@ void test3() { assert(pLogStore->syncLogEndIndex(pLogStore) == 4); assert(pLogStore->syncLogEntryCount(pLogStore) == 5); assert(pLogStore->syncLogWriteIndex(pLogStore) == 5); - assert(pLogStore->syncLogIsEmpty(pLogStore) == 1); + assert(pLogStore->syncLogIsEmpty(pLogStore) == 0); assert(pLogStore->syncLogLastIndex(pLogStore) == 4); assert(pLogStore->syncLogLastTerm(pLogStore) == 104); } @@ -245,7 +245,7 @@ void test4() { assert(pLogStore->syncLogEndIndex(pLogStore) == 9); assert(pLogStore->syncLogEntryCount(pLogStore) == 5); assert(pLogStore->syncLogWriteIndex(pLogStore) == 10); - assert(pLogStore->syncLogIsEmpty(pLogStore) == 1); + assert(pLogStore->syncLogIsEmpty(pLogStore) == 0); assert(pLogStore->syncLogLastIndex(pLogStore) == 9); assert(pLogStore->syncLogLastTerm(pLogStore) == 109); } @@ -264,7 +264,7 @@ void test4() { assert(pLogStore->syncLogEndIndex(pLogStore) == 9); assert(pLogStore->syncLogEntryCount(pLogStore) == 5); assert(pLogStore->syncLogWriteIndex(pLogStore) == 10); - assert(pLogStore->syncLogIsEmpty(pLogStore) == 1); + assert(pLogStore->syncLogIsEmpty(pLogStore) == 0); assert(pLogStore->syncLogLastIndex(pLogStore) == 9); assert(pLogStore->syncLogLastTerm(pLogStore) == 109); } @@ -304,7 +304,7 @@ void test5() { assert(pLogStore->syncLogEndIndex(pLogStore) == 9); assert(pLogStore->syncLogEntryCount(pLogStore) == 5); assert(pLogStore->syncLogWriteIndex(pLogStore) == 10); - assert(pLogStore->syncLogIsEmpty(pLogStore) == 1); + assert(pLogStore->syncLogIsEmpty(pLogStore) == 0); assert(pLogStore->syncLogLastIndex(pLogStore) == 9); assert(pLogStore->syncLogLastTerm(pLogStore) == 109); } @@ -317,7 +317,7 @@ void test5() { assert(pLogStore->syncLogEndIndex(pLogStore) == 6); assert(pLogStore->syncLogEntryCount(pLogStore) == 2); assert(pLogStore->syncLogWriteIndex(pLogStore) == 7); - assert(pLogStore->syncLogIsEmpty(pLogStore) == 1); + assert(pLogStore->syncLogIsEmpty(pLogStore) == 0); assert(pLogStore->syncLogLastIndex(pLogStore) == 6); assert(pLogStore->syncLogLastTerm(pLogStore) == 106); } @@ -336,7 +336,7 @@ void test5() { assert(pLogStore->syncLogEndIndex(pLogStore) == 6); assert(pLogStore->syncLogEntryCount(pLogStore) == 2); assert(pLogStore->syncLogWriteIndex(pLogStore) == 7); - assert(pLogStore->syncLogIsEmpty(pLogStore) == 1); + assert(pLogStore->syncLogIsEmpty(pLogStore) == 0); assert(pLogStore->syncLogLastIndex(pLogStore) == 6); assert(pLogStore->syncLogLastTerm(pLogStore) == 106); } @@ -376,7 +376,7 @@ void test6() { assert(pLogStore->syncLogEndIndex(pLogStore) == 9); assert(pLogStore->syncLogEntryCount(pLogStore) == 5); assert(pLogStore->syncLogWriteIndex(pLogStore) == 10); - assert(pLogStore->syncLogIsEmpty(pLogStore) == 1); + assert(pLogStore->syncLogIsEmpty(pLogStore) == 0); assert(pLogStore->syncLogLastIndex(pLogStore) == 9); assert(pLogStore->syncLogLastTerm(pLogStore) == 109); } @@ -389,7 +389,7 @@ void test6() { assert(pLogStore->syncLogEndIndex(pLogStore) == -1); assert(pLogStore->syncLogEntryCount(pLogStore) == 0); assert(pLogStore->syncLogWriteIndex(pLogStore) == 5); - assert(pLogStore->syncLogIsEmpty(pLogStore) == 0); + assert(pLogStore->syncLogIsEmpty(pLogStore) == 1); assert(pLogStore->syncLogLastIndex(pLogStore) == -1); assert(pLogStore->syncLogLastTerm(pLogStore) == 0); } @@ -408,7 +408,7 @@ void test6() { assert(pLogStore->syncLogEndIndex(pLogStore) == -1); assert(pLogStore->syncLogEntryCount(pLogStore) == 0); assert(pLogStore->syncLogWriteIndex(pLogStore) == 5); - assert(pLogStore->syncLogIsEmpty(pLogStore) == 0); + assert(pLogStore->syncLogIsEmpty(pLogStore) == 1); assert(pLogStore->syncLogLastIndex(pLogStore) == -1); assert(pLogStore->syncLogLastTerm(pLogStore) == 0); } diff --git a/source/libs/sync/test/syncRaftLogTest3.cpp b/source/libs/sync/test/syncRaftLogTest3.cpp index 9dbc201966..b47f8c96c5 100644 --- a/source/libs/sync/test/syncRaftLogTest3.cpp +++ b/source/libs/sync/test/syncRaftLogTest3.cpp @@ -17,6 +17,8 @@ void logTest() { sFatal("--- sync log test: fatal"); } +bool gAssert = true; + SSyncNode* pSyncNode; SWal* pWal; SSyncLogStore* pLogStore; @@ -98,6 +100,14 @@ void test1() { sTrace("%ld's preIndex: %ld", testIndex, preIndex); sTrace("%ld's preTerm: %lu", testIndex, preTerm); + if (gAssert) { + assert(lastIndex == -1); + assert(lastTerm == 0); + assert(syncStartIndex == 0); + assert(preIndex == -1); + assert(preTerm == 0); + } + logStoreDestory(pLogStore); cleanup(); } @@ -150,12 +160,26 @@ void test2() { sTrace("lastTerm: %lu", lastTerm); sTrace("syncStartIndex: %ld", syncStartIndex); + if (gAssert) { + assert(lastIndex == 10); + assert(lastTerm == 110); + assert(syncStartIndex == 11); + } + for (SyncIndex i = 11; i >= 0; --i) { SyncIndex preIndex = syncNodeGetPreIndex(pSyncNode, i); SyncTerm preTerm = syncNodeGetPreTerm(pSyncNode, i); sTrace("%ld's preIndex: %ld", i, preIndex); sTrace("%ld's preTerm: %lu", i, preTerm); + + if (gAssert) { + SyncIndex preIndexArr[12] = {-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; + SyncTerm preTermArr[12] = {0, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110}; + + assert(preIndex == preIndexArr[i]); + assert(preTerm == preTermArr[i]); + } } logStoreDestory(pLogStore); @@ -198,6 +222,14 @@ void test3() { sTrace("%d's preIndex: %ld", 6, preIndex); sTrace("%d's preTerm: %lu", 6, preTerm); + if (gAssert) { + assert(lastIndex == 5); + assert(lastTerm == 100); + assert(syncStartIndex == 6); + assert(preIndex == 5); + assert(preTerm == 100); + } + logStoreDestory(pLogStore); cleanup(); } @@ -250,6 +282,12 @@ void test4() { sTrace("lastTerm: %lu", lastTerm); sTrace("syncStartIndex: %ld", syncStartIndex); + if (gAssert) { + assert(lastIndex == 10); + assert(lastTerm == 110); + assert(syncStartIndex == 11); + } + for (SyncIndex i = 11; i >= 6; --i) { SyncIndex preIndex = syncNodeGetPreIndex(pSyncNode, i); SyncTerm preTerm = syncNodeGetPreTerm(pSyncNode, i); @@ -317,6 +355,14 @@ void test5() { sTrace("%ld's preIndex: %ld", i, preIndex); sTrace("%ld's preTerm: %lu", i, preTerm); + + if (gAssert) { + SyncIndex preIndexArr[12] = {9999, 9999, 9999, 9999, 9999, 9999, 5, 6, 7, 8, 9, 10}; + SyncTerm preTermArr[12] = {9999, 9999, 9999, 9999, 9999, 9999, 100, 106, 107, 108, 109, 110}; + + assert(preIndex == preIndexArr[i]); + assert(preTerm == preTermArr[i]); + } } logStoreDestory(pLogStore); @@ -327,6 +373,11 @@ int main(int argc, char** argv) { tsAsyncLog = 0; sDebugFlag = DEBUG_TRACE + DEBUG_INFO + DEBUG_SCREEN + DEBUG_FILE; + if (argc == 2) { + gAssert = atoi(argv[1]); + } + sTrace("gAssert : %d", gAssert); + test1(); test2(); test3();