d13977ca8ae7bc1addb7a439aa575f6a95c9274b
[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/tdb/hash.c>
13 #include <ccan/tap/tap.h>
14 #include <stdlib.h>
15 #include <stdbool.h>
16 #include <err.h>
17 #include "logging.h"
18
19 int main(int argc, char *argv[])
20 {
21         struct tdb_context *tdb;
22         TDB_DATA key, data;
23
24         plan_tests(27);
25         key.dsize = strlen("hi");
26         key.dptr = (void *)"hi";
27
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);
31         ok1(tdb);
32
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);
41         free(data.dptr);
42         ok1(tdb_transaction_start(tdb) != 0);
43         ok1(tdb_error(tdb) == TDB_ERR_NESTING);
44
45         data = tdb_fetch(tdb, key);
46         ok1(data.dsize == strlen("world"));
47         ok1(memcmp(data.dptr, "world", strlen("world")) == 0);
48         free(data.dptr);
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);
53         free(data.dptr);
54         tdb_close(tdb);
55
56         tdb = tdb_open_ex("run-nested-transactions.tdb",
57                           1024, TDB_ALLOW_NESTING, O_RDWR, 0, &taplogctx, NULL);
58         ok1(tdb);
59
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));
68
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));
76         tdb_close(tdb);
77
78         return exit_status();
79 }