tdb2: Make TDB1 code use TDB2's open flags.
[ccan] / ccan / tdb2 / test / run-tdb1-traverse-in-transaction.c
1 #include "config.h"
2 #include "tdb1-lock-tracking.h"
3 #define fcntl fcntl_with_lockcheck1
4 #include "tdb2-source.h"
5 #include <ccan/tap/tap.h>
6 #undef fcntl_with_lockcheck
7 #include <stdlib.h>
8 #include <stdbool.h>
9 #include <err.h>
10 #include "tdb1-external-agent.h"
11 #include "tdb1-logging.h"
12
13 static struct agent *agent;
14
15 static bool correct_key(TDB_DATA key)
16 {
17         return key.dsize == strlen("hi")
18                 && memcmp(key.dptr, "hi", key.dsize) == 0;
19 }
20
21 static bool correct_data(TDB_DATA data)
22 {
23         return data.dsize == strlen("world")
24                 && memcmp(data.dptr, "world", data.dsize) == 0;
25 }
26
27 static int traverse(struct tdb1_context *tdb, TDB_DATA key, TDB_DATA data,
28                      void *p)
29 {
30         ok1(correct_key(key));
31         ok1(correct_data(data));
32         return 0;
33 }
34
35 int main(int argc, char *argv[])
36 {
37         struct tdb1_context *tdb;
38         TDB_DATA key, data;
39
40         plan_tests(13);
41         agent = prepare_external_agent1();
42         if (!agent)
43                 err(1, "preparing agent");
44
45         tdb = tdb1_open_ex("run-traverse-in-transaction.tdb",
46                           1024, TDB_DEFAULT, O_CREAT|O_TRUNC|O_RDWR,
47                           0600, &taplogctx, NULL);
48         ok1(tdb);
49
50         key.dsize = strlen("hi");
51         key.dptr = (void *)"hi";
52         data.dptr = (void *)"world";
53         data.dsize = strlen("world");
54
55         ok1(tdb1_store(tdb, key, data, TDB_INSERT) == 0);
56
57         ok1(external_agent_operation1(agent, OPEN, tdb->name) == SUCCESS);
58
59         ok1(tdb1_transaction_start(tdb) == 0);
60         ok1(external_agent_operation1(agent, TRANSACTION_START, tdb->name)
61             == WOULD_HAVE_BLOCKED);
62         tdb1_traverse(tdb, traverse, NULL);
63
64         /* That should *not* release the transaction lock! */
65         ok1(external_agent_operation1(agent, TRANSACTION_START, tdb->name)
66             == WOULD_HAVE_BLOCKED);
67         tdb1_traverse_read(tdb, traverse, NULL);
68
69         /* That should *not* release the transaction lock! */
70         ok1(external_agent_operation1(agent, TRANSACTION_START, tdb->name)
71             == WOULD_HAVE_BLOCKED);
72         ok1(tdb1_transaction_commit(tdb) == 0);
73         /* Now we should be fine. */
74         ok1(external_agent_operation1(agent, TRANSACTION_START, tdb->name)
75             == SUCCESS);
76
77         tdb1_close(tdb);
78
79         return exit_status();
80 }