]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/test/run-lockall.c
tdb2: test: fix run-57-die-during-transaction.c to be more efficient.
[ccan] / ccan / tdb2 / test / run-lockall.c
1 #include <ccan/tdb2/private.h>
2 #include <unistd.h>
3 #include "lock-tracking.h"
4
5 #define fcntl fcntl_with_lockcheck
6 #include "tdb2-source.h"
7
8 #include <ccan/tap/tap.h>
9 #include <stdlib.h>
10 #include <stdbool.h>
11 #include <stdarg.h>
12 #include <err.h>
13 #include "external-agent.h"
14 #include "logging.h"
15
16 #define TEST_DBNAME "run-lockall.tdb"
17
18 #undef fcntl
19
20 int main(int argc, char *argv[])
21 {
22         struct agent *agent;
23         int flags[] = { TDB_DEFAULT, TDB_NOMMAP,
24                         TDB_CONVERT, TDB_NOMMAP|TDB_CONVERT,
25                         TDB_VERSION1, TDB_NOMMAP|TDB_VERSION1,
26                         TDB_CONVERT|TDB_VERSION1,
27                         TDB_NOMMAP|TDB_CONVERT|TDB_VERSION1 };
28         int i;
29
30         plan_tests(13 * sizeof(flags)/sizeof(flags[0]) + 1);
31         agent = prepare_external_agent();
32         if (!agent)
33                 err(1, "preparing agent");
34
35         for (i = 0; i < sizeof(flags)/sizeof(flags[0]); i++) {
36                 enum agent_return ret;
37                 struct tdb_context *tdb;
38
39                 tdb = tdb_open(TEST_DBNAME, flags[i],
40                                O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
41                 ok1(tdb);
42
43                 ret = external_agent_operation(agent, OPEN, TEST_DBNAME);
44                 ok1(ret == SUCCESS);
45
46                 ok1(tdb_lockall(tdb) == TDB_SUCCESS);
47                 ok1(external_agent_operation(agent, STORE, "key")
48                     == WOULD_HAVE_BLOCKED);
49                 ok1(external_agent_operation(agent, FETCH, "key")
50                     == WOULD_HAVE_BLOCKED);
51                 /* Test nesting. */
52                 ok1(tdb_lockall(tdb) == TDB_SUCCESS);
53                 tdb_unlockall(tdb);
54                 tdb_unlockall(tdb);
55
56                 ok1(external_agent_operation(agent, STORE, "key") == SUCCESS);
57
58                 ok1(tdb_lockall_read(tdb) == TDB_SUCCESS);
59                 ok1(external_agent_operation(agent, STORE, "key")
60                     == WOULD_HAVE_BLOCKED);
61                 ok1(external_agent_operation(agent, FETCH, "key") == SUCCESS);
62                 ok1(tdb_lockall_read(tdb) == TDB_SUCCESS);
63                 tdb_unlockall_read(tdb);
64                 tdb_unlockall_read(tdb);
65
66                 ok1(external_agent_operation(agent, STORE, "key") == SUCCESS);
67                 ok1(external_agent_operation(agent, CLOSE, NULL) == SUCCESS);
68                 tdb_close(tdb);
69         }
70
71         free_external_agent(agent);
72         ok1(tap_log_messages == 0);
73         return exit_status();
74 }