tdb: more testcase fixup.
[ccan] / ccan / tdb / test / run-die-during-transaction.c
1 #define _XOPEN_SOURCE 500
2 #include <unistd.h>
3 #include "lock-tracking.h"
4 static ssize_t pwrite_check(int fd, const void *buf, size_t count, off_t offset);
5 static ssize_t write_check(int fd, const void *buf, size_t count);
6 static int ftruncate_check(int fd, off_t length);
7
8 #define pwrite pwrite_check
9 #define write write_check
10 #define fcntl fcntl_with_lockcheck
11 #define ftruncate ftruncate_check
12
13 #include <ccan/tdb/tdb.h>
14 #include <ccan/tdb/io.c>
15 #include <ccan/tdb/tdb.c>
16 #include <ccan/tdb/lock.c>
17 #include <ccan/tdb/freelist.c>
18 #include <ccan/tdb/traverse.c>
19 #include <ccan/tdb/transaction.c>
20 #include <ccan/tdb/error.c>
21 #include <ccan/tdb/open.c>
22 #include <ccan/tdb/check.c>
23 #include <ccan/tap/tap.h>
24 #include <stdlib.h>
25 #include <stdbool.h>
26 #include <stdarg.h>
27 #include <err.h>
28 #include <setjmp.h>
29 #include "external-transaction.h"
30
31 #undef write
32 #undef pwrite
33 #undef fcntl
34 #undef ftruncate
35
36 static bool in_transaction;
37 static bool suppress_logging;
38 static int target, current;
39 static jmp_buf jmpbuf;
40 #define TEST_DBNAME "/tmp/test7.tdb"
41
42 static void taplog(struct tdb_context *tdb,
43                    enum tdb_debug_level level,
44                    const char *fmt, ...)
45 {
46         va_list ap;
47         char line[200];
48
49         if (suppress_logging)
50                 return;
51
52         va_start(ap, fmt);
53         vsprintf(line, fmt, ap);
54         va_end(ap);
55
56         diag("%s", line);
57 }
58
59 static void maybe_die(int fd)
60 {
61         if (in_transaction && current++ == target) {
62                 longjmp(jmpbuf, 1);
63         }
64 }
65
66 static ssize_t pwrite_check(int fd,
67                             const void *buf, size_t count, off_t offset)
68 {
69         ssize_t ret;
70
71         maybe_die(fd);
72
73         ret = pwrite(fd, buf, count, offset);
74         if (ret != count)
75                 return ret;
76
77         maybe_die(fd);
78         return ret;
79 }
80
81 static ssize_t write_check(int fd, const void *buf, size_t count)
82 {
83         ssize_t ret;
84
85         maybe_die(fd);
86
87         ret = write(fd, buf, count);
88         if (ret != count)
89                 return ret;
90
91         maybe_die(fd);
92         return ret;
93 }
94
95 static int ftruncate_check(int fd, off_t length)
96 {
97         int ret;
98
99         maybe_die(fd);
100
101         ret = ftruncate(fd, length);
102
103         maybe_die(fd);
104         return ret;
105 }
106
107 static bool test_death(enum operation op, struct agent *agent)
108 {
109         struct tdb_context *tdb = NULL;
110         TDB_DATA key, data;
111         struct tdb_logging_context logctx = { taplog, NULL };
112         int needed_recovery = 0;
113
114         current = target = 0;
115 reset:
116         if (setjmp(jmpbuf) != 0) {
117                 /* We're partway through.  Simulate our death. */
118                 close(tdb->fd);
119                 forget_locking();
120                 in_transaction = false;
121
122                 if (external_agent_operation(agent, NEEDS_RECOVERY_KEEP_OPENED,
123                                              ""))
124                         needed_recovery++;
125
126                 if (external_agent_operation(agent, op, "") != 1) {
127                         diag("Step %u op failed", current);
128                         return false;
129                 }
130
131                 if (external_agent_operation(agent, NEEDS_RECOVERY_KEEP_OPENED,
132                                              "")) {
133                         diag("Still needs recovery after step %u", current);
134                         return false;
135                 }
136
137                 if (external_agent_operation(agent, CHECK_KEEP_OPENED, "")
138                     != 1) {
139                         diag("Step %u check failed", current);
140 #if 0
141                         return false;
142 #endif
143                 }
144
145                 external_agent_operation(agent, CLOSE, "");
146                 /* Suppress logging as this tries to use closed fd. */
147                 suppress_logging = true;
148                 suppress_lockcheck = true;
149                 tdb_close(tdb);
150                 suppress_logging = false;
151                 suppress_lockcheck = false;
152                 target++;
153                 current = 0;
154                 goto reset;
155         }
156
157         unlink(TEST_DBNAME);
158         tdb = tdb_open_ex(TEST_DBNAME, 1024, TDB_NOMMAP,
159                           O_CREAT|O_TRUNC|O_RDWR, 0600, &logctx, NULL);
160
161         if (external_agent_operation(agent, KEEP_OPENED, TEST_DBNAME) != 0)
162                 errx(1, "Agent failed to open?");
163
164         if (tdb_transaction_start(tdb) != 0)
165                 return false;
166
167         in_transaction = true;
168         key.dsize = strlen("hi");
169         key.dptr = (void *)"hi";
170         data.dptr = (void *)"world";
171         data.dsize = strlen("world");
172
173         if (tdb_store(tdb, key, data, TDB_INSERT) != 0)
174                 return false;
175         if (tdb_transaction_commit(tdb) != 0)
176                 return false;
177
178         in_transaction = false;
179
180         /* We made it! */
181         diag("Completed %u runs", current);
182         tdb_close(tdb);
183         external_agent_operation(agent, CLOSE, "");
184
185         ok1(needed_recovery);
186         ok1(locking_errors == 0);
187         ok1(forget_locking() == 0);
188         locking_errors = 0;
189         return true;
190 }
191
192 int main(int argc, char *argv[])
193 {
194         enum operation ops[] = { FETCH_KEEP_OPENED,
195                                  STORE_KEEP_OPENED,
196                                  TRANSACTION_KEEP_OPENED };
197         struct agent *agent;
198         int i;
199
200         plan_tests(12);
201         unlock_callback = maybe_die;
202
203         agent = prepare_external_agent();
204         if (!agent)
205                 err(1, "preparing agent");
206
207         /* Nice ourselves down: we can't tell the difference between agent
208          * blocking on lock, and agent not scheduled. */
209         nice(15);
210
211         for (i = 0; i < sizeof(ops)/sizeof(ops[0]); i++) {
212                 diag("Testing %s after death",
213                      ops[i] == TRANSACTION_KEEP_OPENED ? "transaction"
214                      : ops[i] == FETCH_KEEP_OPENED ? "fetch"
215                      : ops[i] == STORE_KEEP_OPENED ? "store"
216                      : NULL);
217
218                 ok1(test_death(ops[i], agent));
219         }
220
221         return exit_status();
222 }