tdb2: allow multiple chain locks.
[ccan] / ccan / tdb2 / test / run-55-transaction.c
1 #include <ccan/tdb2/tdb.c>
2 #include <ccan/tdb2/open.c>
3 #include <ccan/tdb2/free.c>
4 #include <ccan/tdb2/lock.c>
5 #include <ccan/tdb2/io.c>
6 #include <ccan/tdb2/hash.c>
7 #include <ccan/tdb2/check.c>
8 #include <ccan/tdb2/transaction.c>
9 #include <ccan/tap/tap.h>
10 #include "logging.h"
11
12 int main(int argc, char *argv[])
13 {
14         unsigned int i;
15         struct tdb_context *tdb;
16         unsigned char *buffer;
17         int flags[] = { TDB_DEFAULT, TDB_NOMMAP,
18                         TDB_CONVERT, TDB_NOMMAP|TDB_CONVERT };
19         struct tdb_data key = tdb_mkdata("key", 3);
20         struct tdb_data data;
21
22         buffer = malloc(1000);
23         for (i = 0; i < 1000; i++)
24                 buffer[i] = i;
25
26         plan_tests(sizeof(flags) / sizeof(flags[0]) * 20 + 1);
27
28         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
29                 tdb = tdb_open("run-55-transaction.tdb", flags[i],
30                                O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
31                 ok1(tdb);
32                 if (!tdb)
33                         continue;
34
35                 ok1(tdb_transaction_start(tdb) == 0);
36                 data.dptr = buffer;
37                 data.dsize = 1000;
38                 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
39                 ok1(tdb_fetch(tdb, key, &data) == TDB_SUCCESS);
40                 ok1(data.dsize == 1000);
41                 ok1(memcmp(data.dptr, buffer, data.dsize) == 0);
42                 free(data.dptr);
43
44                 /* Cancelling a transaction means no store */
45                 tdb_transaction_cancel(tdb);
46                 ok1(tdb->file->allrecord_lock.count == 0
47                     && tdb->file->num_lockrecs == 0);
48                 ok1(tdb_check(tdb, NULL, NULL) == 0);
49                 ok1(tdb_fetch(tdb, key, &data) == TDB_ERR_NOEXIST);
50
51                 /* Commit the transaction. */
52                 ok1(tdb_transaction_start(tdb) == 0);
53                 data.dptr = buffer;
54                 data.dsize = 1000;
55                 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
56                 ok1(tdb_fetch(tdb, key, &data) == TDB_SUCCESS);
57                 ok1(data.dsize == 1000);
58                 ok1(memcmp(data.dptr, buffer, data.dsize) == 0);
59                 free(data.dptr);
60                 ok1(tdb_transaction_commit(tdb) == 0);
61                 ok1(tdb->file->allrecord_lock.count == 0
62                     && tdb->file->num_lockrecs == 0);
63                 ok1(tdb_check(tdb, NULL, NULL) == 0);
64                 ok1(tdb_fetch(tdb, key, &data) == TDB_SUCCESS);
65                 ok1(data.dsize == 1000);
66                 ok1(memcmp(data.dptr, buffer, data.dsize) == 0);
67                 free(data.dptr);
68
69                 tdb_close(tdb);
70         }
71
72         ok1(tap_log_messages == 0);
73         free(buffer);
74         return exit_status();
75 }