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