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