Debian Codegraph @

test/unit/test_concurrency.c

raw
1 #include "../lib/cluster.h"
2 #include "../lib/runner.h"
3
4 #include "../../src/gateway.h"
5 #include "../../src/protocol.h"
6 #include "../../src/request.h"
7 #include "../../src/response.h"
8
9 TEST_MODULE(concurrency);
10
11 /******************************************************************************
12 *
13 * Fixture.
14 *
15 ******************************************************************************/
16
17 #define N_GATEWAYS 2
18
19 /* Context for a gateway handle request */
20 struct context
21 {
22 bool invoked;
23 int status;
24 uint8_t type;
25 uint8_t schema;
26 };
27
28 /* Standalone leader database connection */
29 struct connection
30 {
31 struct gateway gateway;
32 struct buffer request; /* Request payload */
33 struct buffer response; /* Response payload */
34 struct handle handle; /* Async handle request */
35 struct context context;
36 };
37
38 #define FIXTURE \
39 FIXTURE_CLUSTER; \
40 struct connection connections[N_GATEWAYS]
41
42 #define SETUP \
43 unsigned i; \
44 int rc; \
45 SETUP_CLUSTER(V2); \
46 CLUSTER_ELECT(0); \
47 for (i = 0; i < N_GATEWAYS; i++) { \
48 struct connection *c = &f->connections[i]; \
49 struct request_open open; \
50 struct response_db db; \
51 struct id_state seed = {{1}}; \
52 gateway__init(&c->gateway, CLUSTER_CONFIG(0), \
53 CLUSTER_REGISTRY(0), CLUSTER_RAFT(0), seed); \
54 c->handle.data = &c->context; \
55 rc = buffer__init(&c->request); \
56 munit_assert_int(rc, ==, 0); \
57 rc = buffer__init(&c->response); \
58 munit_assert_int(rc, ==, 0); \
59 open.filename = "test"; \
60 open.vfs = ""; \
61 ENCODE(c, &open, open); \
62 HANDLE(c, OPEN); \
63 ASSERT_CALLBACK(c, 0, DB); \
64 DECODE(c, &db, db); \
65 munit_assert_int(db.id, ==, 0); \
66 }
67
68 #define TEAR_DOWN \
69 unsigned i; \
70 for (i = 0; i < N_GATEWAYS; i++) { \
71 struct connection *c = &f->connections[i]; \
72 buffer__close(&c->request); \
73 buffer__close(&c->response); \
74 gateway__close(&c->gateway); \
75 } \
76 TEAR_DOWN_CLUSTER;
77
78 static void fixture_handle_cb(struct handle *req,
79 int status,
80 uint8_t type,
81 uint8_t schema)
82 {
83 struct context *c = req->data;
84 c->invoked = true;
85 c->status = status;
86 c->type = type;
87 c->schema = schema;
88 }
89
90 /******************************************************************************
91 *
92 * Helper macros.
93 *
94 ******************************************************************************/
95
96 /* Reset the request buffer of the given connection and encode a request of the
97 * given lower case name. */
98 #define ENCODE(C, REQUEST, LOWER) \
99 { \
100 size_t n2 = request_##LOWER##__sizeof(REQUEST); \
101 void *cursor; \
102 buffer__reset(&C->request); \
103 cursor = buffer__advance(&C->request, n2); \
104 munit_assert_ptr_not_null(cursor); \
105 request_##LOWER##__encode(REQUEST, &cursor); \
106 }
107
108 /* Decode a response of the given lower/upper case name using the response
109 * buffer of the given connection. */
110 #define DECODE(C, RESPONSE, LOWER) \
111 { \
112 struct cursor cursor; \
113 int rc2; \
114 cursor.p = buffer__cursor(&C->response, 0); \
115 cursor.cap = buffer__offset(&C->response); \
116 rc2 = response_##LOWER##__decode(&cursor, RESPONSE); \
117 munit_assert_int(rc2, ==, 0); \
118 }
119
120 /* Submit a request of the given type to the given connection and check that no
121 * error occurs. */
122 #define HANDLE(C, TYPE) \
123 { \
124 int rc2; \
125 C->handle.cursor.p = buffer__cursor(&C->request, 0); \
126 C->handle.cursor.cap = buffer__offset(&C->request); \
127 buffer__reset(&C->response); \
128 rc2 = gateway__handle(&C->gateway, &C->handle, \
129 COWSQL_REQUEST_##TYPE, 0, &C->response, \
130 fixture_handle_cb); \
131 munit_assert_int(rc2, ==, 0); \
132 }
133
134 /* Prepare a statement on the given connection. The ID will be saved in
135 * the STMT_ID pointer. */
136 #define PREPARE(C, SQL, STMT_ID) \
137 { \
138 struct request_prepare prepare; \
139 struct response_stmt stmt; \
140 prepare.db_id = 0; \
141 prepare.sql = SQL; \
142 ENCODE(C, &prepare, prepare); \
143 HANDLE(C, PREPARE); \
144 WAIT(C); \
145 ASSERT_CALLBACK(C, 0, STMT); \
146 DECODE(C, &stmt, stmt); \
147 *(STMT_ID) = stmt.id; \
148 }
149
150 /* Submit a request to exec a statement. */
151 #define EXEC(C, STMT_ID) \
152 { \
153 struct request_exec exec; \
154 exec.db_id = 0; \
155 exec.stmt_id = STMT_ID; \
156 ENCODE(C, &exec, exec); \
157 HANDLE(C, EXEC); \
158 }
159
160 /* Submit a query request. */
161 #define QUERY(C, STMT_ID) \
162 { \
163 struct request_query query; \
164 query.db_id = 0; \
165 query.stmt_id = STMT_ID; \
166 ENCODE(C, &query, query); \
167 HANDLE(C, QUERY); \
168 }
169
170 /* Wait for the gateway of the given connection to finish handling a request. */
171 #define WAIT(C) \
172 { \
173 unsigned _i; \
174 for (_i = 0; _i < 50; _i++) { \
175 CLUSTER_STEP; \
176 if (C->context.invoked) { \
177 break; \
178 } \
179 } \
180 munit_assert_true(C->context.invoked); \
181 }
182
183 /******************************************************************************
184 *
185 * Assertions.
186 *
187 ******************************************************************************/
188
189 /* Assert that the handle callback of the given connection has been invoked with
190 * the given status and response type.. */
191 #define ASSERT_CALLBACK(C, STATUS, UPPER) \
192 munit_assert_true(C->context.invoked); \
193 munit_assert_int(C->context.status, ==, STATUS); \
194 munit_assert_int(C->context.type, ==, COWSQL_RESPONSE_##UPPER); \
195 C->context.invoked = false
196
197 /* Assert that the failure response generated by the gateway of the given
198 * connection matches the given details. */
199 #define ASSERT_FAILURE(C, CODE, MESSAGE) \
200 { \
201 struct response_failure failure; \
202 DECODE(C, &failure, failure); \
203 munit_assert_int(failure.code, ==, CODE); \
204 munit_assert_string_equal(failure.message, MESSAGE); \
205 }
206
207 /******************************************************************************
208 *
209 * Concurrent exec requests
210 *
211 ******************************************************************************/
212
213 struct exec_fixture
214 {
215 FIXTURE;
216 struct connection *c1;
217 struct connection *c2;
218 unsigned stmt_id1;
219 unsigned stmt_id2;
220 };
221
222 TEST_SUITE(exec);
223 TEST_SETUP(exec)
224 {
225 struct exec_fixture *f = munit_malloc(sizeof *f);
226 SETUP;
227 f->c1 = &f->connections[0];
228 f->c2 = &f->connections[1];
229 return f;
230 }
231 TEST_TEAR_DOWN(exec)
232 {
233 struct exec_fixture *f = data;
234 TEAR_DOWN;
235 free(f);
236 }
237
238 /* If another leader connection has submitted an Open request and is waiting for
239 * it to complete, SQLITE_BUSY is returned. */
240 TEST_CASE(exec, open, NULL)
241 {
242 struct exec_fixture *f = data;
243 (void)params;
244
245 PREPARE(f->c1, "CREATE TABLE test1 (n INT)", &f->stmt_id1);
246 PREPARE(f->c2, "CREATE TABLE test2 (n INT)", &f->stmt_id2);
247
248 EXEC(f->c1, f->stmt_id1);
249 EXEC(f->c2, f->stmt_id2);
250 WAIT(f->c2);
251 ASSERT_CALLBACK(f->c2, 0, FAILURE);
252 ASSERT_FAILURE(f->c2, SQLITE_BUSY, "database is locked");
253 WAIT(f->c1);
254 ASSERT_CALLBACK(f->c1, 0, RESULT);
255
256 return MUNIT_OK;
257 }
258
259 /* If an exec request is already in progress on another leader connection,
260 * SQLITE_BUSY is returned. */
261 TEST_CASE(exec, tx, NULL)
262 {
263 struct exec_fixture *f = data;
264 (void)params;
265
266 /* Create a test table using connection 0 */
267 PREPARE(f->c1, "CREATE TABLE test (n INT)", &f->stmt_id1);
268 EXEC(f->c1, f->stmt_id1);
269 WAIT(f->c1);
270 ASSERT_CALLBACK(f->c1, 0, RESULT);
271 PREPARE(f->c1, "INSERT INTO test(n) VALUES(1)", &f->stmt_id1);
272 PREPARE(f->c2, "INSERT INTO test(n) VALUES(1)", &f->stmt_id2);
273
274 EXEC(f->c1, f->stmt_id1);
275 EXEC(f->c2, f->stmt_id2);
276 WAIT(f->c2);
277 ASSERT_CALLBACK(f->c2, 0, FAILURE);
278 ASSERT_FAILURE(f->c2, SQLITE_BUSY, "database is locked");
279 WAIT(f->c1);
280 ASSERT_CALLBACK(f->c1, 0, RESULT);
281 return MUNIT_OK;
282 }
283
284 /******************************************************************************
285 *
286 * Concurrent query requests
287 *
288 ******************************************************************************/
289
290 struct query_fixture
291 {
292 FIXTURE;
293 struct connection *c1;
294 struct connection *c2;
295 unsigned stmt_id1;
296 unsigned stmt_id2;
297 };
298
299 TEST_SUITE(query);
300 TEST_SETUP(query)
301 {
302 struct exec_fixture *f = munit_malloc(sizeof *f);
303 SETUP;
304 f->c1 = &f->connections[0];
305 f->c2 = &f->connections[1];
306 PREPARE(f->c1, "CREATE TABLE test (n INT)", &f->stmt_id1);
307 EXEC(f->c1, f->stmt_id1);
308 WAIT(f->c1);
309 return f;
310 }
311 TEST_TEAR_DOWN(query)
312 {
313 struct exec_fixture *f = data;
314 TEAR_DOWN;
315 free(f);
316 }
317
318 /* Handle a query request while there is a transaction in progress. */
319 TEST_CASE(query, tx, NULL)
320 {
321 struct exec_fixture *f = data;
322 (void)params;
323 PREPARE(f->c1, "INSERT INTO test VALUES(1)", &f->stmt_id1);
324 PREPARE(f->c2, "SELECT n FROM test", &f->stmt_id2);
325 EXEC(f->c1, f->stmt_id1);
326 QUERY(f->c2, f->stmt_id2);
327 WAIT(f->c1);
328 WAIT(f->c2);
329 ASSERT_CALLBACK(f->c1, 0, RESULT);
330 ASSERT_CALLBACK(f->c2, 0, ROWS);
331 return MUNIT_OK;
332 }