]> git.ozlabs.org Git - ccan/blob - ccan/tdb/test/run-nested-traverse.c
tdb: unify logging code in test dir.
[ccan] / ccan / tdb / test / run-nested-traverse.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
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 traverse2(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 static int traverse1(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data,
45                      void *p)
46 {
47         ok1(correct_key(key));
48         ok1(correct_data(data));
49         ok1(external_agent_operation(agent, TRANSACTION_START, tdb_name(tdb))
50             == WOULD_HAVE_BLOCKED);
51         tdb_traverse(tdb, traverse2, NULL);
52
53         /* That should *not* release the transaction lock! */
54         ok1(external_agent_operation(agent, TRANSACTION_START, tdb_name(tdb))
55             == WOULD_HAVE_BLOCKED);
56         return 0;
57 }
58
59 int main(int argc, char *argv[])
60 {
61         struct tdb_context *tdb;
62         TDB_DATA key, data;
63
64         plan_tests(17);
65         agent = prepare_external_agent();
66         if (!agent)
67                 err(1, "preparing agent");
68
69         tdb = tdb_open_ex("run-nested-traverse.tdb", 1024, TDB_CLEAR_IF_FIRST,
70                           O_CREAT|O_TRUNC|O_RDWR, 0600, &taplogctx, NULL);
71         ok1(tdb);
72
73         ok1(external_agent_operation(agent, OPEN, tdb_name(tdb)) == SUCCESS);
74         ok1(external_agent_operation(agent, TRANSACTION_START, tdb_name(tdb))
75             == SUCCESS);
76         ok1(external_agent_operation(agent, TRANSACTION_COMMIT, tdb_name(tdb))
77             == SUCCESS);
78
79         key.dsize = strlen("hi");
80         key.dptr = (void *)"hi";
81         data.dptr = (void *)"world";
82         data.dsize = strlen("world");
83
84         ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
85         tdb_traverse(tdb, traverse1, NULL);
86         tdb_traverse_read(tdb, traverse1, NULL);
87         tdb_close(tdb);
88
89         return exit_status();
90 }