]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/test/run-tdb1-nested-transactions.c
tdb2: add TDB_ATTRIBUTE_TDB1_HASHSIZE
[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 = tdb1_open("run-nested-transactions.tdb",
23                         TDB_DEFAULT,
24                         O_CREAT|O_TRUNC|O_RDWR, 0600, &hsize);
25         ok1(tdb);
26
27         /* No nesting by default. */
28         ok1(tdb1_transaction_start(tdb) == 0);
29         data.dptr = (void *)"world";
30         data.dsize = strlen("world");
31         ok1(tdb1_store(tdb, key, data, TDB_INSERT) == 0);
32         data = tdb1_fetch(tdb, key);
33         ok1(data.dsize == strlen("world"));
34         ok1(memcmp(data.dptr, "world", strlen("world")) == 0);
35         free(data.dptr);
36         ok1(tdb1_transaction_start(tdb) != 0);
37         ok1(tdb_error(tdb) == TDB_ERR_EINVAL);
38
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         ok1(tdb1_transaction_commit(tdb) == 0);
44         data = tdb1_fetch(tdb, key);
45         ok1(data.dsize == strlen("world"));
46         ok1(memcmp(data.dptr, "world", strlen("world")) == 0);
47         free(data.dptr);
48         tdb1_close(tdb);
49
50         tdb = tdb1_open("run-nested-transactions.tdb",
51                         TDB_ALLOW_NESTING, O_RDWR, 0, &tap_log_attr);
52         ok1(tdb);
53
54         ok1(tdb1_transaction_start(tdb) == 0);
55         ok1(tdb1_transaction_start(tdb) == 0);
56         ok1(tdb1_delete(tdb, key) == 0);
57         ok1(tdb1_transaction_commit(tdb) == 0);
58         ok1(!tdb1_exists(tdb, key));
59         ok1(tdb1_transaction_cancel(tdb) == 0);
60         /* Surprise! Kills inner "committed" transaction. */
61         ok1(tdb1_exists(tdb, key));
62
63         ok1(tdb1_transaction_start(tdb) == 0);
64         ok1(tdb1_transaction_start(tdb) == 0);
65         ok1(tdb1_delete(tdb, key) == 0);
66         ok1(tdb1_transaction_commit(tdb) == 0);
67         ok1(!tdb1_exists(tdb, key));
68         ok1(tdb1_transaction_commit(tdb) == 0);
69         ok1(!tdb1_exists(tdb, key));
70         tdb1_close(tdb);
71
72         return exit_status();
73 }