c7079cf6828eb39deb3665950c5913684f6a0b2c
[ccan] / ccan / tdb / test / run-traverse-in-transaction.c
1 #define _XOPEN_SOURCE 500
2 #include "lock-tracking.h"
3 #define fcntl fcntl_with_lockcheck
4 #include <ccan/tdb/tdb.h>
5 #include <ccan/tdb/io.c>
6 #include <ccan/tdb/tdb.c>
7 #include <ccan/tdb/lock.c>
8 #include <ccan/tdb/freelist.c>
9 #include <ccan/tdb/traverse.c>
10 #include <ccan/tdb/transaction.c>
11 #include <ccan/tdb/error.c>
12 #include <ccan/tdb/open.c>
13 #include <ccan/tdb/check.c>
14 #include <ccan/tap/tap.h>
15 #undef fcntl_with_lockcheck
16 #include <stdlib.h>
17 #include <stdbool.h>
18 #include <err.h>
19 #include "external-agent.h"
20
21 static struct agent *agent;
22
23 static bool correct_key(TDB_DATA key)
24 {
25         return key.dsize == strlen("hi")
26                 && memcmp(key.dptr, "hi", key.dsize) == 0;
27 }
28
29 static bool correct_data(TDB_DATA data)
30 {
31         return data.dsize == strlen("world")
32                 && memcmp(data.dptr, "world", data.dsize) == 0;
33 }
34
35 static int traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data,
36                      void *p)
37 {
38         ok1(correct_key(key));
39         ok1(correct_data(data));
40         return 0;
41 }
42
43 int main(int argc, char *argv[])
44 {
45         struct tdb_context *tdb;
46         TDB_DATA key, data;
47
48         plan_tests(13);
49         agent = prepare_external_agent();
50         if (!agent)
51                 err(1, "preparing agent");
52
53         tdb = tdb_open("run-traverse-in-transaction.tdb",
54                        1024, TDB_CLEAR_IF_FIRST, O_CREAT|O_TRUNC|O_RDWR, 0600);
55         ok1(tdb);
56
57         key.dsize = strlen("hi");
58         key.dptr = (void *)"hi";
59         data.dptr = (void *)"world";
60         data.dsize = strlen("world");
61
62         ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
63
64         ok1(external_agent_operation(agent, OPEN, tdb_name(tdb)) == SUCCESS);
65
66         ok1(tdb_transaction_start(tdb) == 0);
67         ok1(external_agent_operation(agent, TRANSACTION_START, tdb_name(tdb))
68             == WOULD_HAVE_BLOCKED);
69         tdb_traverse(tdb, traverse, NULL);
70
71         /* That should *not* release the transaction lock! */
72         ok1(external_agent_operation(agent, TRANSACTION_START, tdb_name(tdb))
73             == WOULD_HAVE_BLOCKED);
74         tdb_traverse_read(tdb, traverse, NULL);
75
76         /* That should *not* release the transaction lock! */
77         ok1(external_agent_operation(agent, TRANSACTION_START, tdb_name(tdb))
78             == WOULD_HAVE_BLOCKED);
79         ok1(tdb_transaction_commit(tdb) == 0);
80         /* Now we should be fine. */
81         ok1(external_agent_operation(agent, TRANSACTION_START, tdb_name(tdb))
82             == SUCCESS);
83
84         tdb_close(tdb);
85
86         return exit_status();
87 }