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