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