]> git.ozlabs.org Git - ccan/blob - ccan/tdb/test/run-nested-transactions.c
tdb: locking cleanup
[ccan] / ccan / tdb / test / run-nested-transactions.c
1 #define _XOPEN_SOURCE 500
2 #include "tdb/tdb.h"
3 #include "tdb/io.c"
4 #include "tdb/tdb.c"
5 #include "tdb/lock.c"
6 #include "tdb/freelist.c"
7 #include "tdb/traverse.c"
8 #include "tdb/transaction.c"
9 #include "tdb/error.c"
10 #include "tdb/open.c"
11 #include "tap/tap.h"
12 #include <stdlib.h>
13 #include <stdbool.h>
14 #include <err.h>
15 int main(int argc, char *argv[])
16 {
17         struct tdb_context *tdb;
18         TDB_DATA key, data;
19
20         plan_tests(27);
21         key.dsize = strlen("hi");
22         key.dptr = (void *)"hi";
23
24         tdb = tdb_open("/tmp/test4.tdb", 1024, TDB_CLEAR_IF_FIRST,
25                        O_CREAT|O_TRUNC|O_RDWR, 0600);
26         ok1(tdb);
27
28         /* No nesting by default. */
29         ok1(tdb_transaction_start(tdb) == 0);
30         data.dptr = (void *)"world";
31         data.dsize = strlen("world");
32         ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
33         data = tdb_fetch(tdb, key);
34         ok1(data.dsize == strlen("world"));
35         ok1(memcmp(data.dptr, "world", strlen("world")) == 0);
36         free(data.dptr);
37         ok1(tdb_transaction_start(tdb) != 0);
38         ok1(tdb_error(tdb) == TDB_ERR_NESTING);
39
40         data = tdb_fetch(tdb, key);
41         ok1(data.dsize == strlen("world"));
42         ok1(memcmp(data.dptr, "world", strlen("world")) == 0);
43         free(data.dptr);
44         ok1(tdb_transaction_commit(tdb) == 0);
45         data = tdb_fetch(tdb, key);
46         ok1(data.dsize == strlen("world"));
47         ok1(memcmp(data.dptr, "world", strlen("world")) == 0);
48         free(data.dptr);
49         tdb_close(tdb);
50
51         tdb = tdb_open("/tmp/test4.tdb", 1024, TDB_ALLOW_NESTING, O_RDWR, 0);
52         ok1(tdb);
53
54         ok1(tdb_transaction_start(tdb) == 0);
55         ok1(tdb_transaction_start(tdb) == 0);
56         ok1(tdb_delete(tdb, key) == 0);
57         ok1(tdb_transaction_commit(tdb) == 0);
58         ok1(!tdb_exists(tdb, key));
59         ok1(tdb_transaction_cancel(tdb) == 0);
60         /* Surprise! Kills inner "committed" transaction. */
61         ok1(tdb_exists(tdb, key));
62
63         ok1(tdb_transaction_start(tdb) == 0);
64         ok1(tdb_transaction_start(tdb) == 0);
65         ok1(tdb_delete(tdb, key) == 0);
66         ok1(tdb_transaction_commit(tdb) == 0);
67         ok1(!tdb_exists(tdb, key));
68         ok1(tdb_transaction_commit(tdb) == 0);
69         ok1(!tdb_exists(tdb, key));
70         tdb_close(tdb);
71
72         return exit_status();
73 }