]>
Commit | Line | Data |
---|---|---|
b2ea25d7 PB |
1 | /* |
2 | * AioContext tests | |
3 | * | |
4 | * Copyright Red Hat, Inc. 2012 | |
5 | * | |
6 | * Authors: | |
7 | * Paolo Bonzini <pbonzini@redhat.com> | |
8 | * | |
9 | * This work is licensed under the terms of the GNU LGPL, version 2 or later. | |
10 | * See the COPYING.LIB file in the top-level directory. | |
11 | */ | |
12 | ||
13 | #include <glib.h> | |
737e150e | 14 | #include "block/aio.h" |
dae21b98 | 15 | #include "qemu/timer.h" |
b2ea25d7 PB |
16 | |
17 | AioContext *ctx; | |
18 | ||
24d1a6d9 SH |
19 | typedef struct { |
20 | EventNotifier e; | |
21 | int n; | |
22 | int active; | |
23 | bool auto_set; | |
24 | } EventNotifierTestData; | |
25 | ||
9fe3781f SH |
26 | /* Wait until there are no more BHs or AIO requests */ |
27 | static void wait_for_aio(void) | |
28 | { | |
29 | while (aio_poll(ctx, true)) { | |
30 | /* Do nothing */ | |
31 | } | |
32 | } | |
33 | ||
24d1a6d9 SH |
34 | /* Wait until event notifier becomes inactive */ |
35 | static void wait_until_inactive(EventNotifierTestData *data) | |
36 | { | |
37 | while (data->active > 0) { | |
38 | aio_poll(ctx, true); | |
39 | } | |
40 | } | |
41 | ||
b2ea25d7 PB |
42 | /* Simple callbacks for testing. */ |
43 | ||
44 | typedef struct { | |
45 | QEMUBH *bh; | |
46 | int n; | |
47 | int max; | |
48 | } BHTestData; | |
49 | ||
50 | static void bh_test_cb(void *opaque) | |
51 | { | |
52 | BHTestData *data = opaque; | |
53 | if (++data->n < data->max) { | |
54 | qemu_bh_schedule(data->bh); | |
55 | } | |
56 | } | |
57 | ||
58 | static void bh_delete_cb(void *opaque) | |
59 | { | |
60 | BHTestData *data = opaque; | |
61 | if (++data->n < data->max) { | |
62 | qemu_bh_schedule(data->bh); | |
63 | } else { | |
64 | qemu_bh_delete(data->bh); | |
65 | data->bh = NULL; | |
66 | } | |
67 | } | |
68 | ||
b2ea25d7 PB |
69 | static void event_ready_cb(EventNotifier *e) |
70 | { | |
71 | EventNotifierTestData *data = container_of(e, EventNotifierTestData, e); | |
72 | g_assert(event_notifier_test_and_clear(e)); | |
73 | data->n++; | |
74 | if (data->active > 0) { | |
75 | data->active--; | |
76 | } | |
77 | if (data->auto_set && data->active) { | |
78 | event_notifier_set(e); | |
79 | } | |
80 | } | |
81 | ||
82 | /* Tests using aio_*. */ | |
83 | ||
84 | static void test_notify(void) | |
85 | { | |
86 | g_assert(!aio_poll(ctx, false)); | |
87 | aio_notify(ctx); | |
88 | g_assert(!aio_poll(ctx, true)); | |
89 | g_assert(!aio_poll(ctx, false)); | |
90 | } | |
91 | ||
b2ea25d7 PB |
92 | static void test_bh_schedule(void) |
93 | { | |
94 | BHTestData data = { .n = 0 }; | |
95 | data.bh = aio_bh_new(ctx, bh_test_cb, &data); | |
96 | ||
97 | qemu_bh_schedule(data.bh); | |
98 | g_assert_cmpint(data.n, ==, 0); | |
99 | ||
100 | g_assert(aio_poll(ctx, true)); | |
101 | g_assert_cmpint(data.n, ==, 1); | |
102 | ||
103 | g_assert(!aio_poll(ctx, false)); | |
104 | g_assert_cmpint(data.n, ==, 1); | |
105 | qemu_bh_delete(data.bh); | |
106 | } | |
107 | ||
108 | static void test_bh_schedule10(void) | |
109 | { | |
110 | BHTestData data = { .n = 0, .max = 10 }; | |
111 | data.bh = aio_bh_new(ctx, bh_test_cb, &data); | |
112 | ||
113 | qemu_bh_schedule(data.bh); | |
114 | g_assert_cmpint(data.n, ==, 0); | |
115 | ||
116 | g_assert(aio_poll(ctx, false)); | |
117 | g_assert_cmpint(data.n, ==, 1); | |
118 | ||
119 | g_assert(aio_poll(ctx, true)); | |
120 | g_assert_cmpint(data.n, ==, 2); | |
121 | ||
9fe3781f | 122 | wait_for_aio(); |
b2ea25d7 PB |
123 | g_assert_cmpint(data.n, ==, 10); |
124 | ||
125 | g_assert(!aio_poll(ctx, false)); | |
126 | g_assert_cmpint(data.n, ==, 10); | |
127 | qemu_bh_delete(data.bh); | |
128 | } | |
129 | ||
130 | static void test_bh_cancel(void) | |
131 | { | |
132 | BHTestData data = { .n = 0 }; | |
133 | data.bh = aio_bh_new(ctx, bh_test_cb, &data); | |
134 | ||
135 | qemu_bh_schedule(data.bh); | |
136 | g_assert_cmpint(data.n, ==, 0); | |
137 | ||
138 | qemu_bh_cancel(data.bh); | |
139 | g_assert_cmpint(data.n, ==, 0); | |
140 | ||
141 | g_assert(!aio_poll(ctx, false)); | |
142 | g_assert_cmpint(data.n, ==, 0); | |
143 | qemu_bh_delete(data.bh); | |
144 | } | |
145 | ||
146 | static void test_bh_delete(void) | |
147 | { | |
148 | BHTestData data = { .n = 0 }; | |
149 | data.bh = aio_bh_new(ctx, bh_test_cb, &data); | |
150 | ||
151 | qemu_bh_schedule(data.bh); | |
152 | g_assert_cmpint(data.n, ==, 0); | |
153 | ||
154 | qemu_bh_delete(data.bh); | |
155 | g_assert_cmpint(data.n, ==, 0); | |
156 | ||
157 | g_assert(!aio_poll(ctx, false)); | |
158 | g_assert_cmpint(data.n, ==, 0); | |
159 | } | |
160 | ||
161 | static void test_bh_delete_from_cb(void) | |
162 | { | |
163 | BHTestData data1 = { .n = 0, .max = 1 }; | |
164 | ||
165 | data1.bh = aio_bh_new(ctx, bh_delete_cb, &data1); | |
166 | ||
167 | qemu_bh_schedule(data1.bh); | |
168 | g_assert_cmpint(data1.n, ==, 0); | |
169 | ||
9fe3781f | 170 | wait_for_aio(); |
b2ea25d7 PB |
171 | g_assert_cmpint(data1.n, ==, data1.max); |
172 | g_assert(data1.bh == NULL); | |
173 | ||
174 | g_assert(!aio_poll(ctx, false)); | |
175 | g_assert(!aio_poll(ctx, true)); | |
176 | } | |
177 | ||
178 | static void test_bh_delete_from_cb_many(void) | |
179 | { | |
180 | BHTestData data1 = { .n = 0, .max = 1 }; | |
181 | BHTestData data2 = { .n = 0, .max = 3 }; | |
182 | BHTestData data3 = { .n = 0, .max = 2 }; | |
183 | BHTestData data4 = { .n = 0, .max = 4 }; | |
184 | ||
185 | data1.bh = aio_bh_new(ctx, bh_delete_cb, &data1); | |
186 | data2.bh = aio_bh_new(ctx, bh_delete_cb, &data2); | |
187 | data3.bh = aio_bh_new(ctx, bh_delete_cb, &data3); | |
188 | data4.bh = aio_bh_new(ctx, bh_delete_cb, &data4); | |
189 | ||
190 | qemu_bh_schedule(data1.bh); | |
191 | qemu_bh_schedule(data2.bh); | |
192 | qemu_bh_schedule(data3.bh); | |
193 | qemu_bh_schedule(data4.bh); | |
194 | g_assert_cmpint(data1.n, ==, 0); | |
195 | g_assert_cmpint(data2.n, ==, 0); | |
196 | g_assert_cmpint(data3.n, ==, 0); | |
197 | g_assert_cmpint(data4.n, ==, 0); | |
198 | ||
199 | g_assert(aio_poll(ctx, false)); | |
200 | g_assert_cmpint(data1.n, ==, 1); | |
201 | g_assert_cmpint(data2.n, ==, 1); | |
202 | g_assert_cmpint(data3.n, ==, 1); | |
203 | g_assert_cmpint(data4.n, ==, 1); | |
204 | g_assert(data1.bh == NULL); | |
205 | ||
9fe3781f | 206 | wait_for_aio(); |
b2ea25d7 PB |
207 | g_assert_cmpint(data1.n, ==, data1.max); |
208 | g_assert_cmpint(data2.n, ==, data2.max); | |
209 | g_assert_cmpint(data3.n, ==, data3.max); | |
210 | g_assert_cmpint(data4.n, ==, data4.max); | |
211 | g_assert(data1.bh == NULL); | |
212 | g_assert(data2.bh == NULL); | |
213 | g_assert(data3.bh == NULL); | |
214 | g_assert(data4.bh == NULL); | |
215 | } | |
216 | ||
217 | static void test_bh_flush(void) | |
218 | { | |
219 | BHTestData data = { .n = 0 }; | |
220 | data.bh = aio_bh_new(ctx, bh_test_cb, &data); | |
221 | ||
222 | qemu_bh_schedule(data.bh); | |
223 | g_assert_cmpint(data.n, ==, 0); | |
224 | ||
9fe3781f | 225 | wait_for_aio(); |
b2ea25d7 PB |
226 | g_assert_cmpint(data.n, ==, 1); |
227 | ||
228 | g_assert(!aio_poll(ctx, false)); | |
229 | g_assert_cmpint(data.n, ==, 1); | |
230 | qemu_bh_delete(data.bh); | |
231 | } | |
232 | ||
233 | static void test_set_event_notifier(void) | |
234 | { | |
235 | EventNotifierTestData data = { .n = 0, .active = 0 }; | |
236 | event_notifier_init(&data.e, false); | |
f2e5dca4 | 237 | aio_set_event_notifier(ctx, &data.e, event_ready_cb); |
b2ea25d7 PB |
238 | g_assert(!aio_poll(ctx, false)); |
239 | g_assert_cmpint(data.n, ==, 0); | |
240 | ||
f2e5dca4 | 241 | aio_set_event_notifier(ctx, &data.e, NULL); |
b2ea25d7 PB |
242 | g_assert(!aio_poll(ctx, false)); |
243 | g_assert_cmpint(data.n, ==, 0); | |
244 | event_notifier_cleanup(&data.e); | |
245 | } | |
246 | ||
247 | static void test_wait_event_notifier(void) | |
248 | { | |
249 | EventNotifierTestData data = { .n = 0, .active = 1 }; | |
250 | event_notifier_init(&data.e, false); | |
f2e5dca4 | 251 | aio_set_event_notifier(ctx, &data.e, event_ready_cb); |
164a101f | 252 | g_assert(!aio_poll(ctx, false)); |
b2ea25d7 PB |
253 | g_assert_cmpint(data.n, ==, 0); |
254 | g_assert_cmpint(data.active, ==, 1); | |
255 | ||
256 | event_notifier_set(&data.e); | |
257 | g_assert(aio_poll(ctx, false)); | |
258 | g_assert_cmpint(data.n, ==, 1); | |
259 | g_assert_cmpint(data.active, ==, 0); | |
260 | ||
261 | g_assert(!aio_poll(ctx, false)); | |
262 | g_assert_cmpint(data.n, ==, 1); | |
263 | g_assert_cmpint(data.active, ==, 0); | |
264 | ||
f2e5dca4 | 265 | aio_set_event_notifier(ctx, &data.e, NULL); |
b2ea25d7 PB |
266 | g_assert(!aio_poll(ctx, false)); |
267 | g_assert_cmpint(data.n, ==, 1); | |
268 | ||
269 | event_notifier_cleanup(&data.e); | |
270 | } | |
271 | ||
272 | static void test_flush_event_notifier(void) | |
273 | { | |
274 | EventNotifierTestData data = { .n = 0, .active = 10, .auto_set = true }; | |
275 | event_notifier_init(&data.e, false); | |
f2e5dca4 | 276 | aio_set_event_notifier(ctx, &data.e, event_ready_cb); |
164a101f | 277 | g_assert(!aio_poll(ctx, false)); |
b2ea25d7 PB |
278 | g_assert_cmpint(data.n, ==, 0); |
279 | g_assert_cmpint(data.active, ==, 10); | |
280 | ||
281 | event_notifier_set(&data.e); | |
282 | g_assert(aio_poll(ctx, false)); | |
283 | g_assert_cmpint(data.n, ==, 1); | |
284 | g_assert_cmpint(data.active, ==, 9); | |
285 | g_assert(aio_poll(ctx, false)); | |
286 | ||
24d1a6d9 | 287 | wait_until_inactive(&data); |
b2ea25d7 PB |
288 | g_assert_cmpint(data.n, ==, 10); |
289 | g_assert_cmpint(data.active, ==, 0); | |
290 | g_assert(!aio_poll(ctx, false)); | |
291 | ||
f2e5dca4 | 292 | aio_set_event_notifier(ctx, &data.e, NULL); |
b2ea25d7 PB |
293 | g_assert(!aio_poll(ctx, false)); |
294 | event_notifier_cleanup(&data.e); | |
295 | } | |
296 | ||
297 | static void test_wait_event_notifier_noflush(void) | |
298 | { | |
299 | EventNotifierTestData data = { .n = 0 }; | |
300 | EventNotifierTestData dummy = { .n = 0, .active = 1 }; | |
301 | ||
302 | event_notifier_init(&data.e, false); | |
f2e5dca4 | 303 | aio_set_event_notifier(ctx, &data.e, event_ready_cb); |
b2ea25d7 PB |
304 | |
305 | g_assert(!aio_poll(ctx, false)); | |
306 | g_assert_cmpint(data.n, ==, 0); | |
307 | ||
308 | /* Until there is an active descriptor, aio_poll may or may not call | |
309 | * event_ready_cb. Still, it must not block. */ | |
310 | event_notifier_set(&data.e); | |
164a101f | 311 | g_assert(aio_poll(ctx, true)); |
b2ea25d7 PB |
312 | data.n = 0; |
313 | ||
314 | /* An active event notifier forces aio_poll to look at EventNotifiers. */ | |
315 | event_notifier_init(&dummy.e, false); | |
f2e5dca4 | 316 | aio_set_event_notifier(ctx, &dummy.e, event_ready_cb); |
b2ea25d7 PB |
317 | |
318 | event_notifier_set(&data.e); | |
319 | g_assert(aio_poll(ctx, false)); | |
320 | g_assert_cmpint(data.n, ==, 1); | |
164a101f | 321 | g_assert(!aio_poll(ctx, false)); |
b2ea25d7 PB |
322 | g_assert_cmpint(data.n, ==, 1); |
323 | ||
324 | event_notifier_set(&data.e); | |
325 | g_assert(aio_poll(ctx, false)); | |
326 | g_assert_cmpint(data.n, ==, 2); | |
164a101f | 327 | g_assert(!aio_poll(ctx, false)); |
b2ea25d7 PB |
328 | g_assert_cmpint(data.n, ==, 2); |
329 | ||
330 | event_notifier_set(&dummy.e); | |
24d1a6d9 | 331 | wait_until_inactive(&dummy); |
b2ea25d7 PB |
332 | g_assert_cmpint(data.n, ==, 2); |
333 | g_assert_cmpint(dummy.n, ==, 1); | |
334 | g_assert_cmpint(dummy.active, ==, 0); | |
335 | ||
f2e5dca4 | 336 | aio_set_event_notifier(ctx, &dummy.e, NULL); |
b2ea25d7 PB |
337 | event_notifier_cleanup(&dummy.e); |
338 | ||
f2e5dca4 | 339 | aio_set_event_notifier(ctx, &data.e, NULL); |
b2ea25d7 PB |
340 | g_assert(!aio_poll(ctx, false)); |
341 | g_assert_cmpint(data.n, ==, 2); | |
342 | ||
343 | event_notifier_cleanup(&data.e); | |
344 | } | |
345 | ||
346 | /* Now the same tests, using the context as a GSource. They are | |
347 | * very similar to the ones above, with g_main_context_iteration | |
348 | * replacing aio_poll. However: | |
349 | * - sometimes both the AioContext and the glib main loop wake | |
350 | * themselves up. Hence, some "g_assert(!aio_poll(ctx, false));" | |
351 | * are replaced by "while (g_main_context_iteration(NULL, false));". | |
9fe3781f | 352 | * - there is no exact replacement for a blocking wait. |
b2ea25d7 PB |
353 | * "while (g_main_context_iteration(NULL, true)" seems to work, |
354 | * but it is not documented _why_ it works. For these tests a | |
355 | * non-blocking loop like "while (g_main_context_iteration(NULL, false)" | |
356 | * works well, and that's what I am using. | |
357 | */ | |
358 | ||
359 | static void test_source_notify(void) | |
360 | { | |
361 | while (g_main_context_iteration(NULL, false)); | |
362 | aio_notify(ctx); | |
363 | g_assert(g_main_context_iteration(NULL, true)); | |
364 | g_assert(!g_main_context_iteration(NULL, false)); | |
365 | } | |
366 | ||
367 | static void test_source_flush(void) | |
368 | { | |
369 | g_assert(!g_main_context_iteration(NULL, false)); | |
370 | aio_notify(ctx); | |
371 | while (g_main_context_iteration(NULL, false)); | |
372 | g_assert(!g_main_context_iteration(NULL, false)); | |
373 | } | |
374 | ||
375 | static void test_source_bh_schedule(void) | |
376 | { | |
377 | BHTestData data = { .n = 0 }; | |
378 | data.bh = aio_bh_new(ctx, bh_test_cb, &data); | |
379 | ||
380 | qemu_bh_schedule(data.bh); | |
381 | g_assert_cmpint(data.n, ==, 0); | |
382 | ||
383 | g_assert(g_main_context_iteration(NULL, true)); | |
384 | g_assert_cmpint(data.n, ==, 1); | |
385 | ||
386 | g_assert(!g_main_context_iteration(NULL, false)); | |
387 | g_assert_cmpint(data.n, ==, 1); | |
388 | qemu_bh_delete(data.bh); | |
389 | } | |
390 | ||
391 | static void test_source_bh_schedule10(void) | |
392 | { | |
393 | BHTestData data = { .n = 0, .max = 10 }; | |
394 | data.bh = aio_bh_new(ctx, bh_test_cb, &data); | |
395 | ||
396 | qemu_bh_schedule(data.bh); | |
397 | g_assert_cmpint(data.n, ==, 0); | |
398 | ||
399 | g_assert(g_main_context_iteration(NULL, false)); | |
400 | g_assert_cmpint(data.n, ==, 1); | |
401 | ||
402 | g_assert(g_main_context_iteration(NULL, true)); | |
403 | g_assert_cmpint(data.n, ==, 2); | |
404 | ||
405 | while (g_main_context_iteration(NULL, false)); | |
406 | g_assert_cmpint(data.n, ==, 10); | |
407 | ||
408 | g_assert(!g_main_context_iteration(NULL, false)); | |
409 | g_assert_cmpint(data.n, ==, 10); | |
410 | qemu_bh_delete(data.bh); | |
411 | } | |
412 | ||
413 | static void test_source_bh_cancel(void) | |
414 | { | |
415 | BHTestData data = { .n = 0 }; | |
416 | data.bh = aio_bh_new(ctx, bh_test_cb, &data); | |
417 | ||
418 | qemu_bh_schedule(data.bh); | |
419 | g_assert_cmpint(data.n, ==, 0); | |
420 | ||
421 | qemu_bh_cancel(data.bh); | |
422 | g_assert_cmpint(data.n, ==, 0); | |
423 | ||
424 | while (g_main_context_iteration(NULL, false)); | |
425 | g_assert_cmpint(data.n, ==, 0); | |
426 | qemu_bh_delete(data.bh); | |
427 | } | |
428 | ||
429 | static void test_source_bh_delete(void) | |
430 | { | |
431 | BHTestData data = { .n = 0 }; | |
432 | data.bh = aio_bh_new(ctx, bh_test_cb, &data); | |
433 | ||
434 | qemu_bh_schedule(data.bh); | |
435 | g_assert_cmpint(data.n, ==, 0); | |
436 | ||
437 | qemu_bh_delete(data.bh); | |
438 | g_assert_cmpint(data.n, ==, 0); | |
439 | ||
440 | while (g_main_context_iteration(NULL, false)); | |
441 | g_assert_cmpint(data.n, ==, 0); | |
442 | } | |
443 | ||
444 | static void test_source_bh_delete_from_cb(void) | |
445 | { | |
446 | BHTestData data1 = { .n = 0, .max = 1 }; | |
447 | ||
448 | data1.bh = aio_bh_new(ctx, bh_delete_cb, &data1); | |
449 | ||
450 | qemu_bh_schedule(data1.bh); | |
451 | g_assert_cmpint(data1.n, ==, 0); | |
452 | ||
453 | g_main_context_iteration(NULL, true); | |
454 | g_assert_cmpint(data1.n, ==, data1.max); | |
455 | g_assert(data1.bh == NULL); | |
456 | ||
457 | g_assert(!g_main_context_iteration(NULL, false)); | |
458 | } | |
459 | ||
460 | static void test_source_bh_delete_from_cb_many(void) | |
461 | { | |
462 | BHTestData data1 = { .n = 0, .max = 1 }; | |
463 | BHTestData data2 = { .n = 0, .max = 3 }; | |
464 | BHTestData data3 = { .n = 0, .max = 2 }; | |
465 | BHTestData data4 = { .n = 0, .max = 4 }; | |
466 | ||
467 | data1.bh = aio_bh_new(ctx, bh_delete_cb, &data1); | |
468 | data2.bh = aio_bh_new(ctx, bh_delete_cb, &data2); | |
469 | data3.bh = aio_bh_new(ctx, bh_delete_cb, &data3); | |
470 | data4.bh = aio_bh_new(ctx, bh_delete_cb, &data4); | |
471 | ||
472 | qemu_bh_schedule(data1.bh); | |
473 | qemu_bh_schedule(data2.bh); | |
474 | qemu_bh_schedule(data3.bh); | |
475 | qemu_bh_schedule(data4.bh); | |
476 | g_assert_cmpint(data1.n, ==, 0); | |
477 | g_assert_cmpint(data2.n, ==, 0); | |
478 | g_assert_cmpint(data3.n, ==, 0); | |
479 | g_assert_cmpint(data4.n, ==, 0); | |
480 | ||
481 | g_assert(g_main_context_iteration(NULL, false)); | |
482 | g_assert_cmpint(data1.n, ==, 1); | |
483 | g_assert_cmpint(data2.n, ==, 1); | |
484 | g_assert_cmpint(data3.n, ==, 1); | |
485 | g_assert_cmpint(data4.n, ==, 1); | |
486 | g_assert(data1.bh == NULL); | |
487 | ||
488 | while (g_main_context_iteration(NULL, false)); | |
489 | g_assert_cmpint(data1.n, ==, data1.max); | |
490 | g_assert_cmpint(data2.n, ==, data2.max); | |
491 | g_assert_cmpint(data3.n, ==, data3.max); | |
492 | g_assert_cmpint(data4.n, ==, data4.max); | |
493 | g_assert(data1.bh == NULL); | |
494 | g_assert(data2.bh == NULL); | |
495 | g_assert(data3.bh == NULL); | |
496 | g_assert(data4.bh == NULL); | |
497 | } | |
498 | ||
499 | static void test_source_bh_flush(void) | |
500 | { | |
501 | BHTestData data = { .n = 0 }; | |
502 | data.bh = aio_bh_new(ctx, bh_test_cb, &data); | |
503 | ||
504 | qemu_bh_schedule(data.bh); | |
505 | g_assert_cmpint(data.n, ==, 0); | |
506 | ||
507 | g_assert(g_main_context_iteration(NULL, true)); | |
508 | g_assert_cmpint(data.n, ==, 1); | |
509 | ||
510 | g_assert(!g_main_context_iteration(NULL, false)); | |
511 | g_assert_cmpint(data.n, ==, 1); | |
512 | qemu_bh_delete(data.bh); | |
513 | } | |
514 | ||
515 | static void test_source_set_event_notifier(void) | |
516 | { | |
517 | EventNotifierTestData data = { .n = 0, .active = 0 }; | |
518 | event_notifier_init(&data.e, false); | |
f2e5dca4 | 519 | aio_set_event_notifier(ctx, &data.e, event_ready_cb); |
b2ea25d7 PB |
520 | while (g_main_context_iteration(NULL, false)); |
521 | g_assert_cmpint(data.n, ==, 0); | |
522 | ||
f2e5dca4 | 523 | aio_set_event_notifier(ctx, &data.e, NULL); |
b2ea25d7 PB |
524 | while (g_main_context_iteration(NULL, false)); |
525 | g_assert_cmpint(data.n, ==, 0); | |
526 | event_notifier_cleanup(&data.e); | |
527 | } | |
528 | ||
529 | static void test_source_wait_event_notifier(void) | |
530 | { | |
531 | EventNotifierTestData data = { .n = 0, .active = 1 }; | |
532 | event_notifier_init(&data.e, false); | |
f2e5dca4 | 533 | aio_set_event_notifier(ctx, &data.e, event_ready_cb); |
b2ea25d7 PB |
534 | g_assert(g_main_context_iteration(NULL, false)); |
535 | g_assert_cmpint(data.n, ==, 0); | |
536 | g_assert_cmpint(data.active, ==, 1); | |
537 | ||
538 | event_notifier_set(&data.e); | |
539 | g_assert(g_main_context_iteration(NULL, false)); | |
540 | g_assert_cmpint(data.n, ==, 1); | |
541 | g_assert_cmpint(data.active, ==, 0); | |
542 | ||
543 | while (g_main_context_iteration(NULL, false)); | |
544 | g_assert_cmpint(data.n, ==, 1); | |
545 | g_assert_cmpint(data.active, ==, 0); | |
546 | ||
f2e5dca4 | 547 | aio_set_event_notifier(ctx, &data.e, NULL); |
b2ea25d7 PB |
548 | while (g_main_context_iteration(NULL, false)); |
549 | g_assert_cmpint(data.n, ==, 1); | |
550 | ||
551 | event_notifier_cleanup(&data.e); | |
552 | } | |
553 | ||
554 | static void test_source_flush_event_notifier(void) | |
555 | { | |
556 | EventNotifierTestData data = { .n = 0, .active = 10, .auto_set = true }; | |
557 | event_notifier_init(&data.e, false); | |
f2e5dca4 | 558 | aio_set_event_notifier(ctx, &data.e, event_ready_cb); |
b2ea25d7 PB |
559 | g_assert(g_main_context_iteration(NULL, false)); |
560 | g_assert_cmpint(data.n, ==, 0); | |
561 | g_assert_cmpint(data.active, ==, 10); | |
562 | ||
563 | event_notifier_set(&data.e); | |
564 | g_assert(g_main_context_iteration(NULL, false)); | |
565 | g_assert_cmpint(data.n, ==, 1); | |
566 | g_assert_cmpint(data.active, ==, 9); | |
567 | g_assert(g_main_context_iteration(NULL, false)); | |
568 | ||
569 | while (g_main_context_iteration(NULL, false)); | |
570 | g_assert_cmpint(data.n, ==, 10); | |
571 | g_assert_cmpint(data.active, ==, 0); | |
572 | g_assert(!g_main_context_iteration(NULL, false)); | |
573 | ||
f2e5dca4 | 574 | aio_set_event_notifier(ctx, &data.e, NULL); |
b2ea25d7 PB |
575 | while (g_main_context_iteration(NULL, false)); |
576 | event_notifier_cleanup(&data.e); | |
577 | } | |
578 | ||
579 | static void test_source_wait_event_notifier_noflush(void) | |
580 | { | |
581 | EventNotifierTestData data = { .n = 0 }; | |
582 | EventNotifierTestData dummy = { .n = 0, .active = 1 }; | |
583 | ||
584 | event_notifier_init(&data.e, false); | |
f2e5dca4 | 585 | aio_set_event_notifier(ctx, &data.e, event_ready_cb); |
b2ea25d7 PB |
586 | |
587 | while (g_main_context_iteration(NULL, false)); | |
588 | g_assert_cmpint(data.n, ==, 0); | |
589 | ||
590 | /* Until there is an active descriptor, glib may or may not call | |
591 | * event_ready_cb. Still, it must not block. */ | |
592 | event_notifier_set(&data.e); | |
593 | g_main_context_iteration(NULL, true); | |
594 | data.n = 0; | |
595 | ||
596 | /* An active event notifier forces aio_poll to look at EventNotifiers. */ | |
597 | event_notifier_init(&dummy.e, false); | |
f2e5dca4 | 598 | aio_set_event_notifier(ctx, &dummy.e, event_ready_cb); |
b2ea25d7 PB |
599 | |
600 | event_notifier_set(&data.e); | |
601 | g_assert(g_main_context_iteration(NULL, false)); | |
602 | g_assert_cmpint(data.n, ==, 1); | |
603 | g_assert(!g_main_context_iteration(NULL, false)); | |
604 | g_assert_cmpint(data.n, ==, 1); | |
605 | ||
606 | event_notifier_set(&data.e); | |
607 | g_assert(g_main_context_iteration(NULL, false)); | |
608 | g_assert_cmpint(data.n, ==, 2); | |
609 | g_assert(!g_main_context_iteration(NULL, false)); | |
610 | g_assert_cmpint(data.n, ==, 2); | |
611 | ||
612 | event_notifier_set(&dummy.e); | |
613 | while (g_main_context_iteration(NULL, false)); | |
614 | g_assert_cmpint(data.n, ==, 2); | |
615 | g_assert_cmpint(dummy.n, ==, 1); | |
616 | g_assert_cmpint(dummy.active, ==, 0); | |
617 | ||
f2e5dca4 | 618 | aio_set_event_notifier(ctx, &dummy.e, NULL); |
b2ea25d7 PB |
619 | event_notifier_cleanup(&dummy.e); |
620 | ||
f2e5dca4 | 621 | aio_set_event_notifier(ctx, &data.e, NULL); |
b2ea25d7 PB |
622 | while (g_main_context_iteration(NULL, false)); |
623 | g_assert_cmpint(data.n, ==, 2); | |
624 | ||
625 | event_notifier_cleanup(&data.e); | |
626 | } | |
627 | ||
628 | /* End of tests. */ | |
629 | ||
630 | int main(int argc, char **argv) | |
631 | { | |
632 | GSource *src; | |
633 | ||
dae21b98 AB |
634 | init_clocks(); |
635 | ||
b2ea25d7 PB |
636 | ctx = aio_context_new(); |
637 | src = aio_get_g_source(ctx); | |
638 | g_source_attach(src, NULL); | |
639 | g_source_unref(src); | |
640 | ||
641 | while (g_main_context_iteration(NULL, false)); | |
642 | ||
643 | g_test_init(&argc, &argv, NULL); | |
644 | g_test_add_func("/aio/notify", test_notify); | |
b2ea25d7 PB |
645 | g_test_add_func("/aio/bh/schedule", test_bh_schedule); |
646 | g_test_add_func("/aio/bh/schedule10", test_bh_schedule10); | |
647 | g_test_add_func("/aio/bh/cancel", test_bh_cancel); | |
648 | g_test_add_func("/aio/bh/delete", test_bh_delete); | |
649 | g_test_add_func("/aio/bh/callback-delete/one", test_bh_delete_from_cb); | |
650 | g_test_add_func("/aio/bh/callback-delete/many", test_bh_delete_from_cb_many); | |
651 | g_test_add_func("/aio/bh/flush", test_bh_flush); | |
652 | g_test_add_func("/aio/event/add-remove", test_set_event_notifier); | |
653 | g_test_add_func("/aio/event/wait", test_wait_event_notifier); | |
654 | g_test_add_func("/aio/event/wait/no-flush-cb", test_wait_event_notifier_noflush); | |
655 | g_test_add_func("/aio/event/flush", test_flush_event_notifier); | |
656 | ||
657 | g_test_add_func("/aio-gsource/notify", test_source_notify); | |
658 | g_test_add_func("/aio-gsource/flush", test_source_flush); | |
659 | g_test_add_func("/aio-gsource/bh/schedule", test_source_bh_schedule); | |
660 | g_test_add_func("/aio-gsource/bh/schedule10", test_source_bh_schedule10); | |
661 | g_test_add_func("/aio-gsource/bh/cancel", test_source_bh_cancel); | |
662 | g_test_add_func("/aio-gsource/bh/delete", test_source_bh_delete); | |
663 | g_test_add_func("/aio-gsource/bh/callback-delete/one", test_source_bh_delete_from_cb); | |
664 | g_test_add_func("/aio-gsource/bh/callback-delete/many", test_source_bh_delete_from_cb_many); | |
665 | g_test_add_func("/aio-gsource/bh/flush", test_source_bh_flush); | |
666 | g_test_add_func("/aio-gsource/event/add-remove", test_source_set_event_notifier); | |
667 | g_test_add_func("/aio-gsource/event/wait", test_source_wait_event_notifier); | |
668 | g_test_add_func("/aio-gsource/event/wait/no-flush-cb", test_source_wait_event_notifier_noflush); | |
669 | g_test_add_func("/aio-gsource/event/flush", test_source_flush_event_notifier); | |
670 | return g_test_run(); | |
671 | } |