tdb2: allow multiple chain locks.
[ccan] / ccan / tdb2 / test / run-02-expand.c
1 #include <ccan/failtest/failtest_override.h>
2 #include <ccan/tdb2/tdb.c>
3 #include <ccan/tdb2/open.c>
4 #include <ccan/tdb2/free.c>
5 #include <ccan/tdb2/lock.c>
6 #include <ccan/tdb2/io.c>
7 #include <ccan/tdb2/check.c>
8 #include <ccan/tdb2/transaction.c>
9 #include <ccan/tdb2/hash.c>
10 #include <ccan/tap/tap.h>
11 #include <ccan/failtest/failtest.h>
12 #include "logging.h"
13 #include "failtest_helper.h"
14
15 static bool failtest_suppress = false;
16
17 /* Don't need to test everything here, just want expand testing. */
18 static enum failtest_result
19 suppress_failure(struct failtest_call *history, unsigned num)
20 {
21         if (failtest_suppress)
22                 return FAIL_DONT_FAIL;
23         return block_repeat_failures(history, num);
24 }
25
26 int main(int argc, char *argv[])
27 {
28         unsigned int i;
29         uint64_t val;
30         struct tdb_context *tdb;
31         int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP,
32                         TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT,
33                         TDB_NOMMAP|TDB_CONVERT };
34
35         plan_tests(sizeof(flags) / sizeof(flags[0]) * 11 + 1);
36
37         failtest_init(argc, argv);
38         failtest_hook = suppress_failure;
39         failtest_exit_check = exit_check_log;
40
41         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
42                 failtest_suppress = true;
43                 tdb = tdb_open("run-expand.tdb", flags[i],
44                                O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
45                 if (!ok1(tdb))
46                         break;
47
48                 val = tdb->file->map_size;
49                 /* Need some hash lock for expand. */
50                 ok1(tdb_lock_hashes(tdb, 0, 1, F_WRLCK, TDB_LOCK_WAIT) == 0);
51                 failtest_suppress = false;
52                 if (!ok1(tdb_expand(tdb, 1) == 0)) {
53                         failtest_suppress = true;
54                         tdb_close(tdb);
55                         break;
56                 }
57                 failtest_suppress = true;
58                         
59                 ok1(tdb->file->map_size >= val + 1 * TDB_EXTENSION_FACTOR);
60                 ok1(tdb_unlock_hashes(tdb, 0, 1, F_WRLCK) == 0);
61                 ok1(tdb_check(tdb, NULL, NULL) == 0);
62
63                 val = tdb->file->map_size;
64                 ok1(tdb_lock_hashes(tdb, 0, 1, F_WRLCK, TDB_LOCK_WAIT) == 0);
65                 failtest_suppress = false;
66                 if (!ok1(tdb_expand(tdb, 1024) == 0)) {
67                         failtest_suppress = true;
68                         tdb_close(tdb);
69                         break;
70                 }
71                 failtest_suppress = true;
72                 ok1(tdb_unlock_hashes(tdb, 0, 1, F_WRLCK) == 0);
73                 ok1(tdb->file->map_size >= val + 1024 * TDB_EXTENSION_FACTOR);
74                 ok1(tdb_check(tdb, NULL, NULL) == 0);
75                 tdb_close(tdb);
76         }
77
78         ok1(tap_log_messages == 0);
79         failtest_exit(exit_status());
80 }