-
Notifications
You must be signed in to change notification settings - Fork 238
Expand file tree
/
Copy pathtest_store_pragmas.c
More file actions
97 lines (85 loc) · 2.84 KB
/
test_store_pragmas.c
File metadata and controls
97 lines (85 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
* test_store_pragmas.c — Tests for SQLite pragma resolution.
*
* Validates that the CBM_SQLITE_MMAP_SIZE env var controls the mmap_size
* pragma applied to on-disk stores. Default behavior (env unset) must
* remain 64 MB. Setting the env to 0 disables memory-mapped I/O so
* concurrent processes that truncate the DB file under a sibling's live
* mapping return SQLITE_IOERR instead of crashing the process with SIGBUS.
*/
#include "../src/foundation/compat.h"
#include "test_framework.h"
#include "test_helpers.h"
#include <store/store.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
static void clear_mmap_env(void) {
unsetenv("CBM_SQLITE_MMAP_SIZE");
}
TEST(mmap_size_default_when_unset) {
clear_mmap_env();
ASSERT_EQ(cbm_store_resolve_mmap_size(), 67108864LL);
PASS();
}
TEST(mmap_size_zero_disables_mmap) {
setenv("CBM_SQLITE_MMAP_SIZE", "0", 1);
ASSERT_EQ(cbm_store_resolve_mmap_size(), 0LL);
clear_mmap_env();
PASS();
}
TEST(mmap_size_explicit_value) {
setenv("CBM_SQLITE_MMAP_SIZE", "1048576", 1);
ASSERT_EQ(cbm_store_resolve_mmap_size(), 1048576LL);
clear_mmap_env();
PASS();
}
TEST(mmap_size_negative_clamped_to_zero) {
setenv("CBM_SQLITE_MMAP_SIZE", "-1", 1);
ASSERT_EQ(cbm_store_resolve_mmap_size(), 0LL);
clear_mmap_env();
PASS();
}
TEST(mmap_size_garbage_falls_back_to_default) {
setenv("CBM_SQLITE_MMAP_SIZE", "not-a-number", 1);
ASSERT_EQ(cbm_store_resolve_mmap_size(), 67108864LL);
clear_mmap_env();
PASS();
}
TEST(mmap_size_partial_garbage_falls_back_to_default) {
setenv("CBM_SQLITE_MMAP_SIZE", "123abc", 1);
ASSERT_EQ(cbm_store_resolve_mmap_size(), 67108864LL);
clear_mmap_env();
PASS();
}
/* Integration smoke: opening a file-backed store with mmap_size=0 must
* succeed. Proves the resolver is wired through configure_pragmas(). */
TEST(store_open_with_mmap_disabled) {
setenv("CBM_SQLITE_MMAP_SIZE", "0", 1);
char tmp_path[256];
snprintf(tmp_path, sizeof(tmp_path), "/tmp/cbm_test_pragmas_%d.db", (int)getpid());
unlink(tmp_path);
cbm_store_t *s = cbm_store_open_path(tmp_path);
ASSERT(s != NULL);
cbm_store_close(s);
unlink(tmp_path);
/* WAL/SHM siblings created by the open */
char tmp_wal[300];
char tmp_shm[300];
snprintf(tmp_wal, sizeof(tmp_wal), "%s-wal", tmp_path);
snprintf(tmp_shm, sizeof(tmp_shm), "%s-shm", tmp_path);
unlink(tmp_wal);
unlink(tmp_shm);
clear_mmap_env();
PASS();
}
SUITE(store_pragmas) {
RUN_TEST(mmap_size_default_when_unset);
RUN_TEST(mmap_size_zero_disables_mmap);
RUN_TEST(mmap_size_explicit_value);
RUN_TEST(mmap_size_negative_clamped_to_zero);
RUN_TEST(mmap_size_garbage_falls_back_to_default);
RUN_TEST(mmap_size_partial_garbage_falls_back_to_default);
RUN_TEST(store_open_with_mmap_disabled);
}