tdb2: unify tdb1_transaction_start etc. into tdb_transaction_start.
[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(29);
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(tdb_transaction_start(tdb) == TDB_SUCCESS);
28         data.dptr = (void *)"world";
29         data.dsize = strlen("world");
30         ok1(tdb_store(tdb, key, data, TDB_INSERT) == TDB_SUCCESS);
31         ok1(tdb_fetch(tdb, key, &data) == TDB_SUCCESS);
32         ok1(data.dsize == strlen("world"));
33         ok1(memcmp(data.dptr, "world", strlen("world")) == 0);
34         free(data.dptr);
35         ok1(tdb_transaction_start(tdb) == TDB_ERR_EINVAL);
36
37         ok1(tdb_fetch(tdb, key, &data) == TDB_SUCCESS);
38         ok1(data.dsize == strlen("world"));
39         ok1(memcmp(data.dptr, "world", strlen("world")) == 0);
40         free(data.dptr);
41         ok1(tdb_transaction_commit(tdb) == TDB_SUCCESS);
42         ok1(tdb_fetch(tdb, key, &data) == TDB_SUCCESS);
43         ok1(data.dsize == strlen("world"));
44         ok1(memcmp(data.dptr, "world", strlen("world")) == 0);
45         free(data.dptr);
46         tdb_close(tdb);
47
48         tdb = tdb_open("run-nested-transactions.tdb1",
49                        TDB_ALLOW_NESTING, O_RDWR, 0, &tap_log_attr);
50         ok1(tdb);
51
52         ok1(tdb_transaction_start(tdb) == TDB_SUCCESS);
53         ok1(tdb_transaction_start(tdb) == TDB_SUCCESS);
54         ok1(tdb_delete(tdb, key) == TDB_SUCCESS);
55         ok1(tdb_transaction_commit(tdb) == TDB_SUCCESS);
56         ok1(!tdb_exists(tdb, key));
57         tdb_transaction_cancel(tdb);
58         ok1(tap_log_messages == 0);
59         /* Surprise! Kills inner "committed" transaction. */
60         ok1(tdb_exists(tdb, key));
61
62         ok1(tdb_transaction_start(tdb) == TDB_SUCCESS);
63         ok1(tdb_transaction_start(tdb) == TDB_SUCCESS);
64         ok1(tdb_delete(tdb, key) == TDB_SUCCESS);
65         ok1(tdb_transaction_commit(tdb) == TDB_SUCCESS);
66         ok1(!tdb_exists(tdb, key));
67         ok1(tdb_transaction_commit(tdb) == TDB_SUCCESS);
68         ok1(!tdb_exists(tdb, key));
69         tdb_close(tdb);
70
71         return exit_status();
72 }