提交 39d0b652 编写于 作者: M Minghao Li

refactor(sync): add raft log test

上级 687caac2
......@@ -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) {
......
......@@ -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);
}
......
......@@ -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();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册