]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/test/api-open-multiple-times.c
failtest: add --trace to replace --tracepath
[ccan] / ccan / tdb2 / test / api-open-multiple-times.c
1 #include <ccan/tdb2/tdb2.h>
2 #include <ccan/tap/tap.h>
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <fcntl.h>
6 #include <stdlib.h>
7 #include "logging.h"
8
9 int main(int argc, char *argv[])
10 {
11         unsigned int i, extra_messages;
12         struct tdb_context *tdb, *tdb2;
13         struct tdb_data key = { (unsigned char *)&i, sizeof(i) };
14         struct tdb_data data = { (unsigned char *)&i, sizeof(i) };
15         struct tdb_data d = { NULL, 0 }; /* Bogus GCC warning */
16         int flags[] = { TDB_DEFAULT, TDB_NOMMAP,
17                         TDB_CONVERT, TDB_NOMMAP|TDB_CONVERT,
18                         TDB_VERSION1, TDB_NOMMAP|TDB_VERSION1,
19                         TDB_CONVERT|TDB_VERSION1,
20                         TDB_NOMMAP|TDB_CONVERT|TDB_VERSION1 };
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
30                 if (flags[i] & TDB_VERSION1) {
31                         extra_messages = 1;
32                 } else {
33                         extra_messages = 0;
34                 }
35                 tdb2 = tdb_open("run-open-multiple-times.tdb", flags[i],
36                                 O_RDWR|O_CREAT, 0600, &tap_log_attr);
37                 ok1(tdb_check(tdb, NULL, NULL) == 0);
38                 ok1(tdb_check(tdb2, NULL, NULL) == 0);
39
40                 /* Store in one, fetch in the other. */
41                 ok1(tdb_store(tdb, key, data, TDB_REPLACE) == 0);
42                 ok1(tdb_fetch(tdb2, key, &d) == TDB_SUCCESS);
43                 ok1(tdb_deq(d, data));
44                 free(d.dptr);
45
46                 /* Vice versa, with delete. */
47                 ok1(tdb_delete(tdb2, key) == 0);
48                 ok1(tdb_fetch(tdb, key, &d) == TDB_ERR_NOEXIST);
49
50                 /* OK, now close first one, check second still good. */
51                 ok1(tdb_close(tdb) == 0);
52
53                 ok1(tdb_store(tdb2, key, data, TDB_REPLACE) == 0);
54                 ok1(tdb_fetch(tdb2, key, &d) == TDB_SUCCESS);
55                 ok1(tdb_deq(d, data));
56                 free(d.dptr);
57
58                 /* Reopen */
59                 tdb = tdb_open("run-open-multiple-times.tdb", flags[i],
60                                O_RDWR|O_CREAT, 0600, &tap_log_attr);
61                 ok1(tdb);
62
63                 ok1(tdb_transaction_start(tdb2) == 0);
64
65                 /* Anything in the other one should fail. */
66                 ok1(tdb_fetch(tdb, key, &d) == TDB_ERR_LOCK);
67                 tap_log_messages -= extra_messages;
68                 ok1(tap_log_messages == 1);
69                 ok1(tdb_store(tdb, key, data, TDB_REPLACE) == TDB_ERR_LOCK);
70                 tap_log_messages -= extra_messages;
71                 ok1(tap_log_messages == 2);
72                 ok1(tdb_transaction_start(tdb) == TDB_ERR_LOCK);
73                 ok1(tap_log_messages == 3);
74                 ok1(tdb_chainlock(tdb, key) == TDB_ERR_LOCK);
75                 tap_log_messages -= extra_messages;
76                 ok1(tap_log_messages == 4);
77
78                 /* Transaciton should work as normal. */
79                 ok1(tdb_store(tdb2, key, data, TDB_REPLACE) == TDB_SUCCESS);
80
81                 /* Now... try closing with locks held. */
82                 ok1(tdb_close(tdb2) == 0);
83
84                 ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
85                 ok1(tdb_deq(d, data));
86                 free(d.dptr);
87                 ok1(tdb_close(tdb) == 0);
88                 ok1(tap_log_messages == 4);
89                 tap_log_messages = 0;
90         }
91
92         return exit_status();
93 }