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