]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/test/run-tdb1-nested-transactions.c
6a13c13dedfdfa1f512b3fd8d2892717fb52e9d4
[ccan] / ccan / tdb2 / test / run-tdb1-nested-transactions.c
1 #include "tdb2-source.h"
2 #include <ccan/tap/tap.h>
3 #include <stdlib.h>
4 #include <stdbool.h>
5 #include <err.h>
6 #include "tdb1-logging.h"
7
8 int main(int argc, char *argv[])
9 {
10         struct tdb1_context *tdb;
11         TDB_DATA key, data;
12
13         plan_tests(27);
14         key.dsize = strlen("hi");
15         key.dptr = (void *)"hi";
16
17         tdb = tdb1_open_ex("run-nested-transactions.tdb",
18                           1024, TDB_DEFAULT,
19                           O_CREAT|O_TRUNC|O_RDWR, 0600, &taplogctx, NULL);
20         ok1(tdb);
21
22         /* No nesting by default. */
23         ok1(tdb1_transaction_start(tdb) == 0);
24         data.dptr = (void *)"world";
25         data.dsize = strlen("world");
26         ok1(tdb1_store(tdb, key, data, TDB_INSERT) == 0);
27         data = tdb1_fetch(tdb, key);
28         ok1(data.dsize == strlen("world"));
29         ok1(memcmp(data.dptr, "world", strlen("world")) == 0);
30         free(data.dptr);
31         ok1(tdb1_transaction_start(tdb) != 0);
32         ok1(tdb_error(tdb) == TDB_ERR_EINVAL);
33
34         data = tdb1_fetch(tdb, key);
35         ok1(data.dsize == strlen("world"));
36         ok1(memcmp(data.dptr, "world", strlen("world")) == 0);
37         free(data.dptr);
38         ok1(tdb1_transaction_commit(tdb) == 0);
39         data = tdb1_fetch(tdb, key);
40         ok1(data.dsize == strlen("world"));
41         ok1(memcmp(data.dptr, "world", strlen("world")) == 0);
42         free(data.dptr);
43         tdb1_close(tdb);
44
45         tdb = tdb1_open_ex("run-nested-transactions.tdb",
46                           1024, TDB_ALLOW_NESTING, O_RDWR, 0, &taplogctx, NULL);
47         ok1(tdb);
48
49         ok1(tdb1_transaction_start(tdb) == 0);
50         ok1(tdb1_transaction_start(tdb) == 0);
51         ok1(tdb1_delete(tdb, key) == 0);
52         ok1(tdb1_transaction_commit(tdb) == 0);
53         ok1(!tdb1_exists(tdb, key));
54         ok1(tdb1_transaction_cancel(tdb) == 0);
55         /* Surprise! Kills inner "committed" transaction. */
56         ok1(tdb1_exists(tdb, key));
57
58         ok1(tdb1_transaction_start(tdb) == 0);
59         ok1(tdb1_transaction_start(tdb) == 0);
60         ok1(tdb1_delete(tdb, key) == 0);
61         ok1(tdb1_transaction_commit(tdb) == 0);
62         ok1(!tdb1_exists(tdb, key));
63         ok1(tdb1_transaction_commit(tdb) == 0);
64         ok1(!tdb1_exists(tdb, key));
65         tdb1_close(tdb);
66
67         return exit_status();
68 }