]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/test/run-tdb1-nested-transactions.c
tdb2: Make tdb1 share tdb_store flags, struct tdb_data and TDB_MAGIC_FOOD.
[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, TDB1_CLEAR_IF_FIRST|TDB1_DISALLOW_NESTING,
19                           O_CREAT|O_TRUNC|O_RDWR, 0600, &taplogctx, NULL);
20         ok1(tdb);
21
22         ok1(tdb1_transaction_start(tdb) == 0);
23         data.dptr = (void *)"world";
24         data.dsize = strlen("world");
25         ok1(tdb1_store(tdb, key, data, TDB_INSERT) == 0);
26         data = tdb1_fetch(tdb, key);
27         ok1(data.dsize == strlen("world"));
28         ok1(memcmp(data.dptr, "world", strlen("world")) == 0);
29         free(data.dptr);
30         ok1(tdb1_transaction_start(tdb) != 0);
31         ok1(tdb_error(tdb) == TDB_ERR_EINVAL);
32
33         data = tdb1_fetch(tdb, key);
34         ok1(data.dsize == strlen("world"));
35         ok1(memcmp(data.dptr, "world", strlen("world")) == 0);
36         free(data.dptr);
37         ok1(tdb1_transaction_commit(tdb) == 0);
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         tdb1_close(tdb);
43
44         /* Allow nesting by default. */
45         tdb = tdb1_open_ex("run-nested-transactions.tdb",
46                           1024, TDB1_DEFAULT, 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 }