]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/test/run-open-multiple-times.c
tdb2: tdb_deq: inline helper for comparing two struct tdb_data
[ccan] / ccan / tdb2 / test / run-open-multiple-times.c
1 #include <ccan/tdb2/tdb.c>
2 #include <ccan/tdb2/open.c>
3 #include <ccan/tdb2/free.c>
4 #include <ccan/tdb2/lock.c>
5 #include <ccan/tdb2/io.c>
6 #include <ccan/tdb2/hash.c>
7 #include <ccan/tdb2/check.c>
8 #include <ccan/tdb2/transaction.c>
9 #include <ccan/tap/tap.h>
10 #include "logging.h"
11
12 int main(int argc, char *argv[])
13 {
14         unsigned int i;
15         struct tdb_context *tdb, *tdb2;
16         struct tdb_data key = { (unsigned char *)&i, sizeof(i) };
17         struct tdb_data data = { (unsigned char *)&i, sizeof(i) };
18         struct tdb_data d = { NULL, 0 }; /* Bogus GCC warning */
19         int flags[] = { TDB_DEFAULT, TDB_NOMMAP,
20                         TDB_CONVERT, TDB_NOMMAP|TDB_CONVERT };
21
22         plan_tests(sizeof(flags) / sizeof(flags[0]) * 28);
23         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
24                 tdb = tdb_open("run-open-multiple-times.tdb", flags[i],
25                                O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
26                 ok1(tdb);
27                 if (!tdb)
28                         continue;
29                 tdb2 = tdb_open("run-open-multiple-times.tdb", flags[i],
30                                 O_RDWR|O_CREAT, 0600, &tap_log_attr);
31                 ok1(tdb_check(tdb, NULL, NULL) == 0);
32                 ok1(tdb_check(tdb2, NULL, NULL) == 0);
33
34                 /* Store in one, fetch in the other. */
35                 ok1(tdb_store(tdb, key, data, TDB_REPLACE) == 0);
36                 ok1(tdb_fetch(tdb2, key, &d) == TDB_SUCCESS);
37                 ok1(tdb_deq(d, data));
38                 free(d.dptr);
39
40                 /* Vice versa, with delete. */
41                 ok1(tdb_delete(tdb2, key) == 0);
42                 ok1(tdb_fetch(tdb, key, &d) == TDB_ERR_NOEXIST);
43
44                 /* OK, now close first one, check second still good. */
45                 ok1(tdb_close(tdb) == 0);
46
47                 ok1(tdb_store(tdb2, key, data, TDB_REPLACE) == 0);
48                 ok1(tdb_fetch(tdb2, key, &d) == TDB_SUCCESS);
49                 ok1(tdb_deq(d, data));
50                 free(d.dptr);
51
52                 /* Reopen */
53                 tdb = tdb_open("run-open-multiple-times.tdb", flags[i],
54                                O_RDWR|O_CREAT, 0600, &tap_log_attr);
55                 ok1(tdb);
56
57                 ok1(tdb_transaction_start(tdb2) == 0);
58
59                 /* Anything in the other one should fail. */
60                 ok1(tdb_fetch(tdb, key, &d) == TDB_ERR_LOCK);
61                 ok1(tap_log_messages == 1);
62                 ok1(tdb_store(tdb, key, data, TDB_REPLACE) == TDB_ERR_LOCK);
63                 ok1(tap_log_messages == 2);
64                 ok1(tdb_transaction_start(tdb) == TDB_ERR_LOCK);
65                 ok1(tap_log_messages == 3);
66                 ok1(tdb_chainlock(tdb, key) == TDB_ERR_LOCK);
67                 ok1(tap_log_messages == 4);
68
69                 /* Transaciton should work as normal. */
70                 ok1(tdb_store(tdb2, key, data, TDB_REPLACE) == TDB_SUCCESS);
71
72                 /* Now... try closing with locks held. */
73                 ok1(tdb_close(tdb2) == 0);
74
75                 ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
76                 ok1(tdb_deq(d, data));
77                 free(d.dptr);
78                 ok1(tdb_close(tdb) == 0);
79                 ok1(tap_log_messages == 4);
80                 tap_log_messages = 0;
81         }
82
83         return exit_status();
84 }