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