feat: add schedule_checkpoint() with WAL record-count trigger and idempotency CAS guard

This commit is contained in:
Rongsong Shen 2026-05-13 17:56:46 +00:00
parent 8f4db308da
commit a95476be4e
2 changed files with 120 additions and 0 deletions

View file

@ -126,6 +126,48 @@ static void test_wal_checkpoint_type_rejected(void)
printf("test_wal_checkpoint_type_rejected: PASS\n");
}
/* ------------------------------------------------------------------ */
/* Schedule-checkpoint and WAL record-count threshold tests */
/* ------------------------------------------------------------------ */
/* test: schedule_checkpoint CAS guard is idempotent */
static void test_schedule_checkpoint_cas_idempotent(void)
{
/* Simulate what schedule_checkpoint does: CAS 0→1 twice.
* Second must fail (idempotency guard). */
atomic_int flag = 0;
int expected = 0;
int swapped = atomic_compare_exchange_strong(&flag, &expected, 1);
TEST_ASSERT_EQUAL_INT(1, swapped); /* first CAS succeeds */
TEST_ASSERT_EQUAL_INT(1, atomic_load(&flag));
expected = 0;
swapped = atomic_compare_exchange_strong(&flag, &expected, 1);
TEST_ASSERT_EQUAL_INT(0, swapped); /* second CAS fails — idempotent */
atomic_store(&flag, 0); /* cleanup */
}
/* test: wal_record_count increments and triggers at threshold */
static void test_wal_record_count_threshold(void)
{
persist_config_t cfg = persist_config_defaults();
cfg.max_wal_records = 5;
atomic_uint count = 0;
for (int i = 0; i < 5; i++)
atomic_fetch_add(&count, 1);
TEST_ASSERT_TRUE(atomic_load(&count) >= cfg.max_wal_records);
}
/* test: persist_config_defaults has max_wal_records set */
static void test_persist_config_defaults_max_wal_records(void)
{
persist_config_t cfg = persist_config_defaults();
TEST_ASSERT_EQUAL_UINT(DEFAULT_MAX_WAL_RECORDS, cfg.max_wal_records);
TEST_ASSERT_TRUE(cfg.max_wal_records >= 100);
}
/* ------------------------------------------------------------------ */
/* Suite entry point (called from controller_test.c) */
/* ------------------------------------------------------------------ */
@ -137,4 +179,7 @@ void test_persist_wal(void)
RUN_TEST(test_persist_init_creates_directories);
RUN_TEST(test_persist_wal_async_writes_entry);
RUN_TEST(test_wal_checkpoint_type_rejected);
RUN_TEST(test_schedule_checkpoint_cas_idempotent);
RUN_TEST(test_wal_record_count_threshold);
RUN_TEST(test_persist_config_defaults_max_wal_records);
}