tdb: unify logging code in test dir.
[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 #include "logging.h"
21
22 static struct agent *agent;
23
24 static bool correct_key(TDB_DATA key)
25 {
26         return key.dsize == strlen("hi")
27                 && memcmp(key.dptr, "hi", key.dsize) == 0;
28 }
29
30 static bool correct_data(TDB_DATA data)
31 {
32         return data.dsize == strlen("world")
33                 && memcmp(data.dptr, "world", data.dsize) == 0;
34 }
35
36 static int traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data,
37                      void *p)
38 {
39         ok1(correct_key(key));
40         ok1(correct_data(data));
41         return 0;
42 }
43
44 int main(int argc, char *argv[])
45 {
46         struct tdb_context *tdb;
47         TDB_DATA key, data;
48
49         plan_tests(13);
50         agent = prepare_external_agent();
51         if (!agent)
52                 err(1, "preparing agent");
53
54         tdb = tdb_open_ex("run-traverse-in-transaction.tdb",
55                           1024, TDB_CLEAR_IF_FIRST, O_CREAT|O_TRUNC|O_RDWR,
56                           0600, &taplogctx, NULL);
57         ok1(tdb);
58
59         key.dsize = strlen("hi");
60         key.dptr = (void *)"hi";
61         data.dptr = (void *)"world";
62         data.dsize = strlen("world");
63
64         ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
65
66         ok1(external_agent_operation(agent, OPEN, tdb_name(tdb)) == SUCCESS);
67
68         ok1(tdb_transaction_start(tdb) == 0);
69         ok1(external_agent_operation(agent, TRANSACTION_START, tdb_name(tdb))
70             == WOULD_HAVE_BLOCKED);
71         tdb_traverse(tdb, traverse, NULL);
72
73         /* That should *not* release the transaction lock! */
74         ok1(external_agent_operation(agent, TRANSACTION_START, tdb_name(tdb))
75             == WOULD_HAVE_BLOCKED);
76         tdb_traverse_read(tdb, traverse, NULL);
77
78         /* That should *not* release the transaction lock! */
79         ok1(external_agent_operation(agent, TRANSACTION_START, tdb_name(tdb))
80             == WOULD_HAVE_BLOCKED);
81         ok1(tdb_transaction_commit(tdb) == 0);
82         /* Now we should be fine. */
83         ok1(external_agent_operation(agent, TRANSACTION_START, tdb_name(tdb))
84             == SUCCESS);
85
86         tdb_close(tdb);
87
88         return exit_status();
89 }