tdb2: make tests work in parallel.
[ccan] / ccan / tdb2 / test / api-81-seqnum.c
1 #include <ccan/tdb2/tdb2.h>
2 #include <ccan/tap/tap.h>
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <fcntl.h>
6 #include <stdlib.h>
7 #include "logging.h"
8
9 int main(int argc, char *argv[])
10 {
11         unsigned int i, seq;
12         struct tdb_context *tdb;
13         struct tdb_data d = { NULL, 0 }; /* Bogus GCC warning */
14         struct tdb_data key = tdb_mkdata("key", 3);
15         struct tdb_data data = tdb_mkdata("data", 4);
16         int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP,
17                         TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT,
18                         TDB_NOMMAP|TDB_CONVERT,
19                         TDB_INTERNAL|TDB_VERSION1, TDB_VERSION1,
20                         TDB_NOMMAP|TDB_VERSION1,
21                         TDB_INTERNAL|TDB_CONVERT|TDB_VERSION1,
22                         TDB_CONVERT|TDB_VERSION1,
23                         TDB_NOMMAP|TDB_CONVERT|TDB_VERSION1 };
24
25         plan_tests(sizeof(flags) / sizeof(flags[0]) * 15 + 8 * 13);
26         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
27                 tdb = tdb_open("api-81-seqnum.tdb", flags[i]|TDB_SEQNUM,
28                                O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
29                 if (!ok1(tdb))
30                         continue;
31
32                 seq = 0;
33                 ok1(tdb_get_seqnum(tdb) == seq);
34                 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
35                 ok1(tdb_get_seqnum(tdb) == ++seq);
36                 /* Fetch doesn't change seqnum */
37                 if (ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS))
38                         free(d.dptr);
39                 ok1(tdb_get_seqnum(tdb) == seq);
40                 ok1(tdb_append(tdb, key, data) == TDB_SUCCESS);
41                 /* Append in tdb1 (or store over value) bumps twice! */
42                 if (flags[i] & TDB_VERSION1)
43                         seq++;
44                 ok1(tdb_get_seqnum(tdb) == ++seq);
45
46                 ok1(tdb_delete(tdb, key) == TDB_SUCCESS);
47                 ok1(tdb_get_seqnum(tdb) == ++seq);
48                 /* Empty append works */
49                 ok1(tdb_append(tdb, key, data) == TDB_SUCCESS);
50                 ok1(tdb_get_seqnum(tdb) == ++seq);
51
52                 ok1(tdb_wipe_all(tdb) == TDB_SUCCESS);
53                 ok1(tdb_get_seqnum(tdb) == ++seq);
54
55                 if (!(flags[i] & TDB_INTERNAL)) {
56                         ok1(tdb_transaction_start(tdb) == TDB_SUCCESS);
57                         ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
58                         ok1(tdb_get_seqnum(tdb) == ++seq);
59                         /* Append in tdb1 (or store over value) bumps twice! */
60                         if (flags[i] & TDB_VERSION1)
61                                 seq++;
62                         ok1(tdb_append(tdb, key, data) == TDB_SUCCESS);
63                         ok1(tdb_get_seqnum(tdb) == ++seq);
64                         ok1(tdb_delete(tdb, key) == TDB_SUCCESS);
65                         ok1(tdb_get_seqnum(tdb) == ++seq);
66                         ok1(tdb_transaction_commit(tdb) == TDB_SUCCESS);
67                         ok1(tdb_get_seqnum(tdb) == seq);
68
69                         ok1(tdb_transaction_start(tdb) == TDB_SUCCESS);
70                         ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
71                         ok1(tdb_get_seqnum(tdb) == seq + 1);
72                         tdb_transaction_cancel(tdb);
73                         ok1(tdb_get_seqnum(tdb) == seq);
74                 }
75                 tdb_close(tdb);
76                 ok1(tap_log_messages == 0);
77         }
78         return exit_status();
79 }