|
| 1 | +/* |
| 2 | + * test_store_checkpoint.c — Tests for WAL checkpoint behavior. |
| 3 | + * |
| 4 | + * Verifies that cbm_store_checkpoint() does not truncate the on-disk |
| 5 | + * WAL file. SQLITE_CHECKPOINT_TRUNCATE shrinks the WAL via ftruncate(fd, 0) |
| 6 | + * on success; on macOS this can raise SIGBUS in a sibling process that |
| 7 | + * has the DB mmap'd through SQLite when it next faults a page in the |
| 8 | + * now-shorter region. SQLITE_CHECKPOINT_PASSIVE marks frames as |
| 9 | + * checkpointed in the WAL header without changing the file size — disk |
| 10 | + * space is reclaimed on the next write cycle, not on every checkpoint. |
| 11 | + */ |
| 12 | +#include "../src/foundation/compat.h" |
| 13 | +#include "test_framework.h" |
| 14 | +#include "test_helpers.h" |
| 15 | +#include <store/store.h> |
| 16 | +#include <stdio.h> |
| 17 | +#include <stdlib.h> |
| 18 | +#include <string.h> |
| 19 | +#include <sys/stat.h> |
| 20 | +#include <unistd.h> |
| 21 | + |
| 22 | +TEST(checkpoint_does_not_truncate_wal) { |
| 23 | + enum { N_ROWS = 100, PATH_BUF = 256, PATH_BUF_EXT = 300 }; |
| 24 | + char db_path[PATH_BUF]; |
| 25 | + snprintf(db_path, sizeof(db_path), "/tmp/cbm_test_ckpt_%d.db", (int)getpid()); |
| 26 | + char wal_path[PATH_BUF_EXT]; |
| 27 | + snprintf(wal_path, sizeof(wal_path), "%s-wal", db_path); |
| 28 | + char shm_path[PATH_BUF_EXT]; |
| 29 | + snprintf(shm_path, sizeof(shm_path), "%s-shm", db_path); |
| 30 | + unlink(db_path); |
| 31 | + unlink(wal_path); |
| 32 | + unlink(shm_path); |
| 33 | + |
| 34 | + cbm_store_t *s = cbm_store_open_path(db_path); |
| 35 | + ASSERT(s != NULL); |
| 36 | + |
| 37 | + /* Grow WAL beyond zero bytes via direct SQL. */ |
| 38 | + int rc_sql = cbm_store_exec( |
| 39 | + s, |
| 40 | + "INSERT OR IGNORE INTO projects(name, indexed_at, root_path) " |
| 41 | + "VALUES('p', '2026-01-01', '/tmp/p');"); |
| 42 | + ASSERT_EQ(rc_sql, 0); |
| 43 | + for (int i = 0; i < N_ROWS; i++) { |
| 44 | + char sql[256]; |
| 45 | + snprintf(sql, sizeof(sql), |
| 46 | + "INSERT INTO nodes(project, label, name, qualified_name, file_path) " |
| 47 | + "VALUES('p', 'Function', 'fn', 'p.module.fn_%d', 'f.c');", |
| 48 | + i); |
| 49 | + rc_sql = cbm_store_exec(s, sql); |
| 50 | + ASSERT_EQ(rc_sql, 0); |
| 51 | + } |
| 52 | + |
| 53 | + /* WAL must exist and be non-empty before the checkpoint call. */ |
| 54 | + struct stat st_before; |
| 55 | + int rc_stat = stat(wal_path, &st_before); |
| 56 | + ASSERT_EQ(rc_stat, 0); |
| 57 | + ASSERT(st_before.st_size > 0); |
| 58 | + |
| 59 | + /* Under SQLITE_CHECKPOINT_TRUNCATE the WAL would be ftruncate()d to 0 |
| 60 | + * bytes on success. Under SQLITE_CHECKPOINT_PASSIVE the file size is |
| 61 | + * preserved (frames marked, not removed). */ |
| 62 | + int rc_ckpt = cbm_store_checkpoint(s); |
| 63 | + ASSERT_EQ(rc_ckpt, 0); /* CBM_STORE_OK */ |
| 64 | + |
| 65 | + struct stat st_after; |
| 66 | + rc_stat = stat(wal_path, &st_after); |
| 67 | + ASSERT_EQ(rc_stat, 0); |
| 68 | + ASSERT(st_after.st_size > 0); |
| 69 | + |
| 70 | + cbm_store_close(s); |
| 71 | + unlink(db_path); |
| 72 | + unlink(wal_path); |
| 73 | + unlink(shm_path); |
| 74 | + PASS(); |
| 75 | +} |
| 76 | + |
| 77 | +SUITE(store_checkpoint) { |
| 78 | + RUN_TEST(checkpoint_does_not_truncate_wal); |
| 79 | +} |
0 commit comments