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