tdb: unify logging code in test dir.
[ccan] / ccan / tdb / test / run-nested-transactions.c
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/tap/tap.h>
13 #include <stdlib.h>
14 #include <stdbool.h>
15 #include <err.h>
16 #include "logging.h"
17
18 int main(int argc, char *argv[])
19 {
20         struct tdb_context *tdb;
21         TDB_DATA key, data;
22
23         plan_tests(27);
24         key.dsize = strlen("hi");
25         key.dptr = (void *)"hi";
26
27         tdb = tdb_open_ex("run-nested-transactions.tdb",
28                           1024, TDB_CLEAR_IF_FIRST,
29                           O_CREAT|O_TRUNC|O_RDWR, 0600, &taplogctx, NULL);
30         ok1(tdb);
31
32         /* No nesting by default. */
33         ok1(tdb_transaction_start(tdb) == 0);
34         data.dptr = (void *)"world";
35         data.dsize = strlen("world");
36         ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
37         data = tdb_fetch(tdb, key);
38         ok1(data.dsize == strlen("world"));
39         ok1(memcmp(data.dptr, "world", strlen("world")) == 0);
40         free(data.dptr);
41         ok1(tdb_transaction_start(tdb) != 0);
42         ok1(tdb_error(tdb) == TDB_ERR_NESTING);
43
44         data = tdb_fetch(tdb, key);
45         ok1(data.dsize == strlen("world"));
46         ok1(memcmp(data.dptr, "world", strlen("world")) == 0);
47         free(data.dptr);
48         ok1(tdb_transaction_commit(tdb) == 0);
49         data = tdb_fetch(tdb, key);
50         ok1(data.dsize == strlen("world"));
51         ok1(memcmp(data.dptr, "world", strlen("world")) == 0);
52         free(data.dptr);
53         tdb_close(tdb);
54
55         tdb = tdb_open_ex("run-nested-transactions.tdb",
56                           1024, TDB_ALLOW_NESTING, O_RDWR, 0, &taplogctx, NULL);
57         ok1(tdb);
58
59         ok1(tdb_transaction_start(tdb) == 0);
60         ok1(tdb_transaction_start(tdb) == 0);
61         ok1(tdb_delete(tdb, key) == 0);
62         ok1(tdb_transaction_commit(tdb) == 0);
63         ok1(!tdb_exists(tdb, key));
64         ok1(tdb_transaction_cancel(tdb) == 0);
65         /* Surprise! Kills inner "committed" transaction. */
66         ok1(tdb_exists(tdb, key));
67
68         ok1(tdb_transaction_start(tdb) == 0);
69         ok1(tdb_transaction_start(tdb) == 0);
70         ok1(tdb_delete(tdb, key) == 0);
71         ok1(tdb_transaction_commit(tdb) == 0);
72         ok1(!tdb_exists(tdb, key));
73         ok1(tdb_transaction_commit(tdb) == 0);
74         ok1(!tdb_exists(tdb, key));
75         tdb_close(tdb);
76
77         return exit_status();
78 }