]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/test/run-tdb1-nested-transactions.c
6a10b46b1b64886d5a1b4ff2f122d9112d022a16
[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 "logging.h"
7
8 int main(int argc, char *argv[])
9 {
10         struct tdb_context *tdb;
11         TDB_DATA key, data;
12         union tdb_attribute hsize;
13
14         hsize.base.attr = TDB_ATTRIBUTE_TDB1_HASHSIZE;
15         hsize.base.next = &tap_log_attr;
16         hsize.tdb1_hashsize.hsize = 1024;
17
18         plan_tests(27);
19         key.dsize = strlen("hi");
20         key.dptr = (void *)"hi";
21
22         tdb = tdb_open("run-nested-transactions.tdb1",
23                        TDB_VERSION1, O_CREAT|O_TRUNC|O_RDWR, 0600, &hsize);
24         ok1(tdb);
25
26         /* No nesting by default. */
27         ok1(tdb1_transaction_start(tdb) == 0);
28         data.dptr = (void *)"world";
29         data.dsize = strlen("world");
30         ok1(tdb_store(tdb, key, data, TDB_INSERT) == TDB_SUCCESS);
31         data = tdb1_fetch(tdb, key);
32         ok1(data.dsize == strlen("world"));
33         ok1(memcmp(data.dptr, "world", strlen("world")) == 0);
34         free(data.dptr);
35         ok1(tdb1_transaction_start(tdb) != 0);
36         ok1(tdb_error(tdb) == TDB_ERR_EINVAL);
37
38         data = tdb1_fetch(tdb, key);
39         ok1(data.dsize == strlen("world"));
40         ok1(memcmp(data.dptr, "world", strlen("world")) == 0);
41         free(data.dptr);
42         ok1(tdb1_transaction_commit(tdb) == 0);
43         data = tdb1_fetch(tdb, key);
44         ok1(data.dsize == strlen("world"));
45         ok1(memcmp(data.dptr, "world", strlen("world")) == 0);
46         free(data.dptr);
47         tdb_close(tdb);
48
49         tdb = tdb_open("run-nested-transactions.tdb1",
50                        TDB_ALLOW_NESTING, O_RDWR, 0, &tap_log_attr);
51         ok1(tdb);
52
53         ok1(tdb1_transaction_start(tdb) == 0);
54         ok1(tdb1_transaction_start(tdb) == 0);
55         ok1(tdb1_delete(tdb, key) == 0);
56         ok1(tdb1_transaction_commit(tdb) == 0);
57         ok1(!tdb1_exists(tdb, key));
58         ok1(tdb1_transaction_cancel(tdb) == 0);
59         /* Surprise! Kills inner "committed" transaction. */
60         ok1(tdb1_exists(tdb, key));
61
62         ok1(tdb1_transaction_start(tdb) == 0);
63         ok1(tdb1_transaction_start(tdb) == 0);
64         ok1(tdb1_delete(tdb, key) == 0);
65         ok1(tdb1_transaction_commit(tdb) == 0);
66         ok1(!tdb1_exists(tdb, key));
67         ok1(tdb1_transaction_commit(tdb) == 0);
68         ok1(!tdb1_exists(tdb, key));
69         tdb_close(tdb);
70
71         return exit_status();
72 }