tdb2: stricter ordering on expansion lock
[ccan] / ccan / tdb2 / test / run-02-expand.c
1 #include <ccan/tdb2/tdb.c>
2 #include <ccan/tdb2/free.c>
3 #include <ccan/tdb2/lock.c>
4 #include <ccan/tdb2/io.c>
5 #include <ccan/tdb2/check.c>
6 #include <ccan/tdb2/transaction.c>
7 #include <ccan/tdb2/hash.c>
8 #include <ccan/tap/tap.h>
9 #include "logging.h"
10
11 int main(int argc, char *argv[])
12 {
13         unsigned int i;
14         uint64_t val;
15         struct tdb_context *tdb;
16         int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP,
17                         TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT,
18                         TDB_NOMMAP|TDB_CONVERT };
19
20         plan_tests(sizeof(flags) / sizeof(flags[0]) * 11 + 1);
21
22         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
23                 tdb = tdb_open("run-expand.tdb", flags[i],
24                                O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
25                 ok1(tdb);
26                 if (!tdb)
27                         continue;
28
29                 val = tdb->map_size;
30                 /* Need some hash lock for expand. */
31                 ok1(tdb_lock_hashes(tdb, 0, 1, F_WRLCK, TDB_LOCK_WAIT) == 0);
32                 ok1(tdb_expand(tdb, 1) == 0);
33                 ok1(tdb->map_size >= val + 1 * TDB_EXTENSION_FACTOR);
34                 ok1(tdb_unlock_hashes(tdb, 0, 1, F_WRLCK) == 0);
35                 ok1(tdb_check(tdb, NULL, NULL) == 0);
36
37                 val = tdb->map_size;
38                 ok1(tdb_lock_hashes(tdb, 0, 1, F_WRLCK, TDB_LOCK_WAIT) == 0);
39                 ok1(tdb_expand(tdb, 1024) == 0);
40                 ok1(tdb_unlock_hashes(tdb, 0, 1, F_WRLCK) == 0);
41                 ok1(tdb->map_size >= val + 1024 * TDB_EXTENSION_FACTOR);
42                 ok1(tdb_check(tdb, NULL, NULL) == 0);
43                 tdb_close(tdb);
44         }
45
46         ok1(tap_log_messages == 0);
47         return exit_status();
48 }