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