1 #define _XOPEN_SOURCE 500
2 #include <ccan/tdb/tdb.h>
3 #include <ccan/tdb/io.c>
4 #include <ccan/tdb/tdb.c>
5 #include <ccan/tdb/lock.c>
6 #include <ccan/tdb/freelist.c>
7 #include <ccan/tdb/traverse.c>
8 #include <ccan/tdb/transaction.c>
9 #include <ccan/tdb/error.c>
10 #include <ccan/tdb/open.c>
11 #include <ccan/tdb/check.c>
12 #include <ccan/tdb/hash.c>
13 #include <ccan/tap/tap.h>
19 int main(int argc, char *argv[])
21 struct tdb_context *tdb;
25 key.dsize = strlen("hi");
26 key.dptr = (void *)"hi";
28 tdb = tdb_open_ex("run-nested-transactions.tdb",
29 1024, TDB_CLEAR_IF_FIRST,
30 O_CREAT|O_TRUNC|O_RDWR, 0600, &taplogctx, NULL);
33 /* No nesting by default. */
34 ok1(tdb_transaction_start(tdb) == 0);
35 data.dptr = (void *)"world";
36 data.dsize = strlen("world");
37 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
38 data = tdb_fetch(tdb, key);
39 ok1(data.dsize == strlen("world"));
40 ok1(memcmp(data.dptr, "world", strlen("world")) == 0);
42 ok1(tdb_transaction_start(tdb) != 0);
43 ok1(tdb_error(tdb) == TDB_ERR_NESTING);
45 data = tdb_fetch(tdb, key);
46 ok1(data.dsize == strlen("world"));
47 ok1(memcmp(data.dptr, "world", strlen("world")) == 0);
49 ok1(tdb_transaction_commit(tdb) == 0);
50 data = tdb_fetch(tdb, key);
51 ok1(data.dsize == strlen("world"));
52 ok1(memcmp(data.dptr, "world", strlen("world")) == 0);
56 tdb = tdb_open_ex("run-nested-transactions.tdb",
57 1024, TDB_ALLOW_NESTING, O_RDWR, 0, &taplogctx, NULL);
60 ok1(tdb_transaction_start(tdb) == 0);
61 ok1(tdb_transaction_start(tdb) == 0);
62 ok1(tdb_delete(tdb, key) == 0);
63 ok1(tdb_transaction_commit(tdb) == 0);
64 ok1(!tdb_exists(tdb, key));
65 ok1(tdb_transaction_cancel(tdb) == 0);
66 /* Surprise! Kills inner "committed" transaction. */
67 ok1(tdb_exists(tdb, key));
69 ok1(tdb_transaction_start(tdb) == 0);
70 ok1(tdb_transaction_start(tdb) == 0);
71 ok1(tdb_delete(tdb, key) == 0);
72 ok1(tdb_transaction_commit(tdb) == 0);
73 ok1(!tdb_exists(tdb, key));
74 ok1(tdb_transaction_commit(tdb) == 0);
75 ok1(!tdb_exists(tdb, key));