pseries: Fix stalls on hypervisor virtual console
[qemu/ar7.git] / tests / test-aio.c
blob07a1f61f87f746b2e12fe8f68b3ecc9181dff9f2
1 /*
2 * AioContext tests
4 * Copyright Red Hat, Inc. 2012
6 * Authors:
7 * Paolo Bonzini <pbonzini@redhat.com>
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.
13 #include <glib.h>
14 #include "block/aio.h"
15 #include "qemu/timer.h"
17 AioContext *ctx;
19 typedef struct {
20 EventNotifier e;
21 int n;
22 int active;
23 bool auto_set;
24 } EventNotifierTestData;
26 /* Wait until there are no more BHs or AIO requests */
27 static void wait_for_aio(void)
29 while (aio_poll(ctx, true)) {
30 /* Do nothing */
34 /* Wait until event notifier becomes inactive */
35 static void wait_until_inactive(EventNotifierTestData *data)
37 while (data->active > 0) {
38 aio_poll(ctx, true);
42 /* Simple callbacks for testing. */
44 typedef struct {
45 QEMUBH *bh;
46 int n;
47 int max;
48 } BHTestData;
50 typedef struct {
51 QEMUTimer timer;
52 QEMUClockType clock_type;
53 int n;
54 int max;
55 int64_t ns;
56 AioContext *ctx;
57 } TimerTestData;
59 static void bh_test_cb(void *opaque)
61 BHTestData *data = opaque;
62 if (++data->n < data->max) {
63 qemu_bh_schedule(data->bh);
67 static void timer_test_cb(void *opaque)
69 TimerTestData *data = opaque;
70 if (++data->n < data->max) {
71 timer_mod(&data->timer,
72 qemu_clock_get_ns(data->clock_type) + data->ns);
76 static void dummy_io_handler_read(void *opaque)
80 static void bh_delete_cb(void *opaque)
82 BHTestData *data = opaque;
83 if (++data->n < data->max) {
84 qemu_bh_schedule(data->bh);
85 } else {
86 qemu_bh_delete(data->bh);
87 data->bh = NULL;
91 static void event_ready_cb(EventNotifier *e)
93 EventNotifierTestData *data = container_of(e, EventNotifierTestData, e);
94 g_assert(event_notifier_test_and_clear(e));
95 data->n++;
96 if (data->active > 0) {
97 data->active--;
99 if (data->auto_set && data->active) {
100 event_notifier_set(e);
104 /* Tests using aio_*. */
106 static void test_notify(void)
108 g_assert(!aio_poll(ctx, false));
109 aio_notify(ctx);
110 g_assert(!aio_poll(ctx, true));
111 g_assert(!aio_poll(ctx, false));
114 static void test_bh_schedule(void)
116 BHTestData data = { .n = 0 };
117 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
119 qemu_bh_schedule(data.bh);
120 g_assert_cmpint(data.n, ==, 0);
122 g_assert(aio_poll(ctx, true));
123 g_assert_cmpint(data.n, ==, 1);
125 g_assert(!aio_poll(ctx, false));
126 g_assert_cmpint(data.n, ==, 1);
127 qemu_bh_delete(data.bh);
130 static void test_bh_schedule10(void)
132 BHTestData data = { .n = 0, .max = 10 };
133 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
135 qemu_bh_schedule(data.bh);
136 g_assert_cmpint(data.n, ==, 0);
138 g_assert(aio_poll(ctx, false));
139 g_assert_cmpint(data.n, ==, 1);
141 g_assert(aio_poll(ctx, true));
142 g_assert_cmpint(data.n, ==, 2);
144 wait_for_aio();
145 g_assert_cmpint(data.n, ==, 10);
147 g_assert(!aio_poll(ctx, false));
148 g_assert_cmpint(data.n, ==, 10);
149 qemu_bh_delete(data.bh);
152 static void test_bh_cancel(void)
154 BHTestData data = { .n = 0 };
155 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
157 qemu_bh_schedule(data.bh);
158 g_assert_cmpint(data.n, ==, 0);
160 qemu_bh_cancel(data.bh);
161 g_assert_cmpint(data.n, ==, 0);
163 g_assert(!aio_poll(ctx, false));
164 g_assert_cmpint(data.n, ==, 0);
165 qemu_bh_delete(data.bh);
168 static void test_bh_delete(void)
170 BHTestData data = { .n = 0 };
171 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
173 qemu_bh_schedule(data.bh);
174 g_assert_cmpint(data.n, ==, 0);
176 qemu_bh_delete(data.bh);
177 g_assert_cmpint(data.n, ==, 0);
179 g_assert(!aio_poll(ctx, false));
180 g_assert_cmpint(data.n, ==, 0);
183 static void test_bh_delete_from_cb(void)
185 BHTestData data1 = { .n = 0, .max = 1 };
187 data1.bh = aio_bh_new(ctx, bh_delete_cb, &data1);
189 qemu_bh_schedule(data1.bh);
190 g_assert_cmpint(data1.n, ==, 0);
192 wait_for_aio();
193 g_assert_cmpint(data1.n, ==, data1.max);
194 g_assert(data1.bh == NULL);
196 g_assert(!aio_poll(ctx, false));
197 g_assert(!aio_poll(ctx, true));
200 static void test_bh_delete_from_cb_many(void)
202 BHTestData data1 = { .n = 0, .max = 1 };
203 BHTestData data2 = { .n = 0, .max = 3 };
204 BHTestData data3 = { .n = 0, .max = 2 };
205 BHTestData data4 = { .n = 0, .max = 4 };
207 data1.bh = aio_bh_new(ctx, bh_delete_cb, &data1);
208 data2.bh = aio_bh_new(ctx, bh_delete_cb, &data2);
209 data3.bh = aio_bh_new(ctx, bh_delete_cb, &data3);
210 data4.bh = aio_bh_new(ctx, bh_delete_cb, &data4);
212 qemu_bh_schedule(data1.bh);
213 qemu_bh_schedule(data2.bh);
214 qemu_bh_schedule(data3.bh);
215 qemu_bh_schedule(data4.bh);
216 g_assert_cmpint(data1.n, ==, 0);
217 g_assert_cmpint(data2.n, ==, 0);
218 g_assert_cmpint(data3.n, ==, 0);
219 g_assert_cmpint(data4.n, ==, 0);
221 g_assert(aio_poll(ctx, false));
222 g_assert_cmpint(data1.n, ==, 1);
223 g_assert_cmpint(data2.n, ==, 1);
224 g_assert_cmpint(data3.n, ==, 1);
225 g_assert_cmpint(data4.n, ==, 1);
226 g_assert(data1.bh == NULL);
228 wait_for_aio();
229 g_assert_cmpint(data1.n, ==, data1.max);
230 g_assert_cmpint(data2.n, ==, data2.max);
231 g_assert_cmpint(data3.n, ==, data3.max);
232 g_assert_cmpint(data4.n, ==, data4.max);
233 g_assert(data1.bh == NULL);
234 g_assert(data2.bh == NULL);
235 g_assert(data3.bh == NULL);
236 g_assert(data4.bh == NULL);
239 static void test_bh_flush(void)
241 BHTestData data = { .n = 0 };
242 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
244 qemu_bh_schedule(data.bh);
245 g_assert_cmpint(data.n, ==, 0);
247 wait_for_aio();
248 g_assert_cmpint(data.n, ==, 1);
250 g_assert(!aio_poll(ctx, false));
251 g_assert_cmpint(data.n, ==, 1);
252 qemu_bh_delete(data.bh);
255 static void test_set_event_notifier(void)
257 EventNotifierTestData data = { .n = 0, .active = 0 };
258 event_notifier_init(&data.e, false);
259 aio_set_event_notifier(ctx, &data.e, event_ready_cb);
260 g_assert(!aio_poll(ctx, false));
261 g_assert_cmpint(data.n, ==, 0);
263 aio_set_event_notifier(ctx, &data.e, NULL);
264 g_assert(!aio_poll(ctx, false));
265 g_assert_cmpint(data.n, ==, 0);
266 event_notifier_cleanup(&data.e);
269 static void test_wait_event_notifier(void)
271 EventNotifierTestData data = { .n = 0, .active = 1 };
272 event_notifier_init(&data.e, false);
273 aio_set_event_notifier(ctx, &data.e, event_ready_cb);
274 g_assert(!aio_poll(ctx, false));
275 g_assert_cmpint(data.n, ==, 0);
276 g_assert_cmpint(data.active, ==, 1);
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, ==, 0);
283 g_assert(!aio_poll(ctx, false));
284 g_assert_cmpint(data.n, ==, 1);
285 g_assert_cmpint(data.active, ==, 0);
287 aio_set_event_notifier(ctx, &data.e, NULL);
288 g_assert(!aio_poll(ctx, false));
289 g_assert_cmpint(data.n, ==, 1);
291 event_notifier_cleanup(&data.e);
294 static void test_flush_event_notifier(void)
296 EventNotifierTestData data = { .n = 0, .active = 10, .auto_set = true };
297 event_notifier_init(&data.e, false);
298 aio_set_event_notifier(ctx, &data.e, event_ready_cb);
299 g_assert(!aio_poll(ctx, false));
300 g_assert_cmpint(data.n, ==, 0);
301 g_assert_cmpint(data.active, ==, 10);
303 event_notifier_set(&data.e);
304 g_assert(aio_poll(ctx, false));
305 g_assert_cmpint(data.n, ==, 1);
306 g_assert_cmpint(data.active, ==, 9);
307 g_assert(aio_poll(ctx, false));
309 wait_until_inactive(&data);
310 g_assert_cmpint(data.n, ==, 10);
311 g_assert_cmpint(data.active, ==, 0);
312 g_assert(!aio_poll(ctx, false));
314 aio_set_event_notifier(ctx, &data.e, NULL);
315 g_assert(!aio_poll(ctx, false));
316 event_notifier_cleanup(&data.e);
319 static void test_wait_event_notifier_noflush(void)
321 EventNotifierTestData data = { .n = 0 };
322 EventNotifierTestData dummy = { .n = 0, .active = 1 };
324 event_notifier_init(&data.e, false);
325 aio_set_event_notifier(ctx, &data.e, event_ready_cb);
327 g_assert(!aio_poll(ctx, false));
328 g_assert_cmpint(data.n, ==, 0);
330 /* Until there is an active descriptor, aio_poll may or may not call
331 * event_ready_cb. Still, it must not block. */
332 event_notifier_set(&data.e);
333 g_assert(aio_poll(ctx, true));
334 data.n = 0;
336 /* An active event notifier forces aio_poll to look at EventNotifiers. */
337 event_notifier_init(&dummy.e, false);
338 aio_set_event_notifier(ctx, &dummy.e, event_ready_cb);
340 event_notifier_set(&data.e);
341 g_assert(aio_poll(ctx, false));
342 g_assert_cmpint(data.n, ==, 1);
343 g_assert(!aio_poll(ctx, false));
344 g_assert_cmpint(data.n, ==, 1);
346 event_notifier_set(&data.e);
347 g_assert(aio_poll(ctx, false));
348 g_assert_cmpint(data.n, ==, 2);
349 g_assert(!aio_poll(ctx, false));
350 g_assert_cmpint(data.n, ==, 2);
352 event_notifier_set(&dummy.e);
353 wait_until_inactive(&dummy);
354 g_assert_cmpint(data.n, ==, 2);
355 g_assert_cmpint(dummy.n, ==, 1);
356 g_assert_cmpint(dummy.active, ==, 0);
358 aio_set_event_notifier(ctx, &dummy.e, NULL);
359 event_notifier_cleanup(&dummy.e);
361 aio_set_event_notifier(ctx, &data.e, NULL);
362 g_assert(!aio_poll(ctx, false));
363 g_assert_cmpint(data.n, ==, 2);
365 event_notifier_cleanup(&data.e);
368 static void test_timer_schedule(void)
370 TimerTestData data = { .n = 0, .ctx = ctx, .ns = SCALE_MS * 750LL,
371 .max = 2,
372 .clock_type = QEMU_CLOCK_VIRTUAL };
373 int pipefd[2];
375 /* aio_poll will not block to wait for timers to complete unless it has
376 * an fd to wait on. Fixing this breaks other tests. So create a dummy one.
378 g_assert(!pipe2(pipefd, O_NONBLOCK));
379 aio_set_fd_handler(ctx, pipefd[0],
380 dummy_io_handler_read, NULL, NULL);
381 aio_poll(ctx, false);
383 aio_timer_init(ctx, &data.timer, data.clock_type,
384 SCALE_NS, timer_test_cb, &data);
385 timer_mod(&data.timer,
386 qemu_clock_get_ns(data.clock_type) +
387 data.ns);
389 g_assert_cmpint(data.n, ==, 0);
391 /* timer_mod may well cause an event notifer to have gone off,
392 * so clear that
394 do {} while (aio_poll(ctx, false));
396 g_assert(!aio_poll(ctx, false));
397 g_assert_cmpint(data.n, ==, 0);
399 sleep(1);
400 g_assert_cmpint(data.n, ==, 0);
402 g_assert(aio_poll(ctx, false));
403 g_assert_cmpint(data.n, ==, 1);
405 /* timer_mod called by our callback */
406 do {} while (aio_poll(ctx, false));
408 g_assert(!aio_poll(ctx, false));
409 g_assert_cmpint(data.n, ==, 1);
411 g_assert(aio_poll(ctx, true));
412 g_assert_cmpint(data.n, ==, 2);
414 /* As max is now 2, an event notifier should not have gone off */
416 g_assert(!aio_poll(ctx, false));
417 g_assert_cmpint(data.n, ==, 2);
419 aio_set_fd_handler(ctx, pipefd[0], NULL, NULL, NULL);
420 close(pipefd[0]);
421 close(pipefd[1]);
423 timer_del(&data.timer);
426 /* Now the same tests, using the context as a GSource. They are
427 * very similar to the ones above, with g_main_context_iteration
428 * replacing aio_poll. However:
429 * - sometimes both the AioContext and the glib main loop wake
430 * themselves up. Hence, some "g_assert(!aio_poll(ctx, false));"
431 * are replaced by "while (g_main_context_iteration(NULL, false));".
432 * - there is no exact replacement for a blocking wait.
433 * "while (g_main_context_iteration(NULL, true)" seems to work,
434 * but it is not documented _why_ it works. For these tests a
435 * non-blocking loop like "while (g_main_context_iteration(NULL, false)"
436 * works well, and that's what I am using.
439 static void test_source_notify(void)
441 while (g_main_context_iteration(NULL, false));
442 aio_notify(ctx);
443 g_assert(g_main_context_iteration(NULL, true));
444 g_assert(!g_main_context_iteration(NULL, false));
447 static void test_source_flush(void)
449 g_assert(!g_main_context_iteration(NULL, false));
450 aio_notify(ctx);
451 while (g_main_context_iteration(NULL, false));
452 g_assert(!g_main_context_iteration(NULL, false));
455 static void test_source_bh_schedule(void)
457 BHTestData data = { .n = 0 };
458 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
460 qemu_bh_schedule(data.bh);
461 g_assert_cmpint(data.n, ==, 0);
463 g_assert(g_main_context_iteration(NULL, true));
464 g_assert_cmpint(data.n, ==, 1);
466 g_assert(!g_main_context_iteration(NULL, false));
467 g_assert_cmpint(data.n, ==, 1);
468 qemu_bh_delete(data.bh);
471 static void test_source_bh_schedule10(void)
473 BHTestData data = { .n = 0, .max = 10 };
474 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
476 qemu_bh_schedule(data.bh);
477 g_assert_cmpint(data.n, ==, 0);
479 g_assert(g_main_context_iteration(NULL, false));
480 g_assert_cmpint(data.n, ==, 1);
482 g_assert(g_main_context_iteration(NULL, true));
483 g_assert_cmpint(data.n, ==, 2);
485 while (g_main_context_iteration(NULL, false));
486 g_assert_cmpint(data.n, ==, 10);
488 g_assert(!g_main_context_iteration(NULL, false));
489 g_assert_cmpint(data.n, ==, 10);
490 qemu_bh_delete(data.bh);
493 static void test_source_bh_cancel(void)
495 BHTestData data = { .n = 0 };
496 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
498 qemu_bh_schedule(data.bh);
499 g_assert_cmpint(data.n, ==, 0);
501 qemu_bh_cancel(data.bh);
502 g_assert_cmpint(data.n, ==, 0);
504 while (g_main_context_iteration(NULL, false));
505 g_assert_cmpint(data.n, ==, 0);
506 qemu_bh_delete(data.bh);
509 static void test_source_bh_delete(void)
511 BHTestData data = { .n = 0 };
512 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
514 qemu_bh_schedule(data.bh);
515 g_assert_cmpint(data.n, ==, 0);
517 qemu_bh_delete(data.bh);
518 g_assert_cmpint(data.n, ==, 0);
520 while (g_main_context_iteration(NULL, false));
521 g_assert_cmpint(data.n, ==, 0);
524 static void test_source_bh_delete_from_cb(void)
526 BHTestData data1 = { .n = 0, .max = 1 };
528 data1.bh = aio_bh_new(ctx, bh_delete_cb, &data1);
530 qemu_bh_schedule(data1.bh);
531 g_assert_cmpint(data1.n, ==, 0);
533 g_main_context_iteration(NULL, true);
534 g_assert_cmpint(data1.n, ==, data1.max);
535 g_assert(data1.bh == NULL);
537 g_assert(!g_main_context_iteration(NULL, false));
540 static void test_source_bh_delete_from_cb_many(void)
542 BHTestData data1 = { .n = 0, .max = 1 };
543 BHTestData data2 = { .n = 0, .max = 3 };
544 BHTestData data3 = { .n = 0, .max = 2 };
545 BHTestData data4 = { .n = 0, .max = 4 };
547 data1.bh = aio_bh_new(ctx, bh_delete_cb, &data1);
548 data2.bh = aio_bh_new(ctx, bh_delete_cb, &data2);
549 data3.bh = aio_bh_new(ctx, bh_delete_cb, &data3);
550 data4.bh = aio_bh_new(ctx, bh_delete_cb, &data4);
552 qemu_bh_schedule(data1.bh);
553 qemu_bh_schedule(data2.bh);
554 qemu_bh_schedule(data3.bh);
555 qemu_bh_schedule(data4.bh);
556 g_assert_cmpint(data1.n, ==, 0);
557 g_assert_cmpint(data2.n, ==, 0);
558 g_assert_cmpint(data3.n, ==, 0);
559 g_assert_cmpint(data4.n, ==, 0);
561 g_assert(g_main_context_iteration(NULL, false));
562 g_assert_cmpint(data1.n, ==, 1);
563 g_assert_cmpint(data2.n, ==, 1);
564 g_assert_cmpint(data3.n, ==, 1);
565 g_assert_cmpint(data4.n, ==, 1);
566 g_assert(data1.bh == NULL);
568 while (g_main_context_iteration(NULL, false));
569 g_assert_cmpint(data1.n, ==, data1.max);
570 g_assert_cmpint(data2.n, ==, data2.max);
571 g_assert_cmpint(data3.n, ==, data3.max);
572 g_assert_cmpint(data4.n, ==, data4.max);
573 g_assert(data1.bh == NULL);
574 g_assert(data2.bh == NULL);
575 g_assert(data3.bh == NULL);
576 g_assert(data4.bh == NULL);
579 static void test_source_bh_flush(void)
581 BHTestData data = { .n = 0 };
582 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
584 qemu_bh_schedule(data.bh);
585 g_assert_cmpint(data.n, ==, 0);
587 g_assert(g_main_context_iteration(NULL, true));
588 g_assert_cmpint(data.n, ==, 1);
590 g_assert(!g_main_context_iteration(NULL, false));
591 g_assert_cmpint(data.n, ==, 1);
592 qemu_bh_delete(data.bh);
595 static void test_source_set_event_notifier(void)
597 EventNotifierTestData data = { .n = 0, .active = 0 };
598 event_notifier_init(&data.e, false);
599 aio_set_event_notifier(ctx, &data.e, event_ready_cb);
600 while (g_main_context_iteration(NULL, false));
601 g_assert_cmpint(data.n, ==, 0);
603 aio_set_event_notifier(ctx, &data.e, NULL);
604 while (g_main_context_iteration(NULL, false));
605 g_assert_cmpint(data.n, ==, 0);
606 event_notifier_cleanup(&data.e);
609 static void test_source_wait_event_notifier(void)
611 EventNotifierTestData data = { .n = 0, .active = 1 };
612 event_notifier_init(&data.e, false);
613 aio_set_event_notifier(ctx, &data.e, event_ready_cb);
614 g_assert(g_main_context_iteration(NULL, false));
615 g_assert_cmpint(data.n, ==, 0);
616 g_assert_cmpint(data.active, ==, 1);
618 event_notifier_set(&data.e);
619 g_assert(g_main_context_iteration(NULL, false));
620 g_assert_cmpint(data.n, ==, 1);
621 g_assert_cmpint(data.active, ==, 0);
623 while (g_main_context_iteration(NULL, false));
624 g_assert_cmpint(data.n, ==, 1);
625 g_assert_cmpint(data.active, ==, 0);
627 aio_set_event_notifier(ctx, &data.e, NULL);
628 while (g_main_context_iteration(NULL, false));
629 g_assert_cmpint(data.n, ==, 1);
631 event_notifier_cleanup(&data.e);
634 static void test_source_flush_event_notifier(void)
636 EventNotifierTestData data = { .n = 0, .active = 10, .auto_set = true };
637 event_notifier_init(&data.e, false);
638 aio_set_event_notifier(ctx, &data.e, event_ready_cb);
639 g_assert(g_main_context_iteration(NULL, false));
640 g_assert_cmpint(data.n, ==, 0);
641 g_assert_cmpint(data.active, ==, 10);
643 event_notifier_set(&data.e);
644 g_assert(g_main_context_iteration(NULL, false));
645 g_assert_cmpint(data.n, ==, 1);
646 g_assert_cmpint(data.active, ==, 9);
647 g_assert(g_main_context_iteration(NULL, false));
649 while (g_main_context_iteration(NULL, false));
650 g_assert_cmpint(data.n, ==, 10);
651 g_assert_cmpint(data.active, ==, 0);
652 g_assert(!g_main_context_iteration(NULL, false));
654 aio_set_event_notifier(ctx, &data.e, NULL);
655 while (g_main_context_iteration(NULL, false));
656 event_notifier_cleanup(&data.e);
659 static void test_source_wait_event_notifier_noflush(void)
661 EventNotifierTestData data = { .n = 0 };
662 EventNotifierTestData dummy = { .n = 0, .active = 1 };
664 event_notifier_init(&data.e, false);
665 aio_set_event_notifier(ctx, &data.e, event_ready_cb);
667 while (g_main_context_iteration(NULL, false));
668 g_assert_cmpint(data.n, ==, 0);
670 /* Until there is an active descriptor, glib may or may not call
671 * event_ready_cb. Still, it must not block. */
672 event_notifier_set(&data.e);
673 g_main_context_iteration(NULL, true);
674 data.n = 0;
676 /* An active event notifier forces aio_poll to look at EventNotifiers. */
677 event_notifier_init(&dummy.e, false);
678 aio_set_event_notifier(ctx, &dummy.e, event_ready_cb);
680 event_notifier_set(&data.e);
681 g_assert(g_main_context_iteration(NULL, false));
682 g_assert_cmpint(data.n, ==, 1);
683 g_assert(!g_main_context_iteration(NULL, false));
684 g_assert_cmpint(data.n, ==, 1);
686 event_notifier_set(&data.e);
687 g_assert(g_main_context_iteration(NULL, false));
688 g_assert_cmpint(data.n, ==, 2);
689 g_assert(!g_main_context_iteration(NULL, false));
690 g_assert_cmpint(data.n, ==, 2);
692 event_notifier_set(&dummy.e);
693 while (g_main_context_iteration(NULL, false));
694 g_assert_cmpint(data.n, ==, 2);
695 g_assert_cmpint(dummy.n, ==, 1);
696 g_assert_cmpint(dummy.active, ==, 0);
698 aio_set_event_notifier(ctx, &dummy.e, NULL);
699 event_notifier_cleanup(&dummy.e);
701 aio_set_event_notifier(ctx, &data.e, NULL);
702 while (g_main_context_iteration(NULL, false));
703 g_assert_cmpint(data.n, ==, 2);
705 event_notifier_cleanup(&data.e);
708 static void test_source_timer_schedule(void)
710 TimerTestData data = { .n = 0, .ctx = ctx, .ns = SCALE_MS * 750LL,
711 .max = 2,
712 .clock_type = QEMU_CLOCK_VIRTUAL };
713 int pipefd[2];
714 int64_t expiry;
716 /* aio_poll will not block to wait for timers to complete unless it has
717 * an fd to wait on. Fixing this breaks other tests. So create a dummy one.
719 g_assert(!pipe2(pipefd, O_NONBLOCK));
720 aio_set_fd_handler(ctx, pipefd[0],
721 dummy_io_handler_read, NULL, NULL);
722 do {} while (g_main_context_iteration(NULL, false));
724 aio_timer_init(ctx, &data.timer, data.clock_type,
725 SCALE_NS, timer_test_cb, &data);
726 expiry = qemu_clock_get_ns(data.clock_type) +
727 data.ns;
728 timer_mod(&data.timer, expiry);
730 g_assert_cmpint(data.n, ==, 0);
732 sleep(1);
733 g_assert_cmpint(data.n, ==, 0);
735 g_assert(g_main_context_iteration(NULL, false));
736 g_assert_cmpint(data.n, ==, 1);
738 /* The comment above was not kidding when it said this wakes up itself */
739 do {
740 g_assert(g_main_context_iteration(NULL, true));
741 } while (qemu_clock_get_ns(data.clock_type) <= expiry);
742 sleep(1);
743 g_main_context_iteration(NULL, false);
745 g_assert_cmpint(data.n, ==, 2);
747 aio_set_fd_handler(ctx, pipefd[0], NULL, NULL, NULL);
748 close(pipefd[0]);
749 close(pipefd[1]);
751 timer_del(&data.timer);
755 /* End of tests. */
757 int main(int argc, char **argv)
759 GSource *src;
761 init_clocks();
763 ctx = aio_context_new();
764 src = aio_get_g_source(ctx);
765 g_source_attach(src, NULL);
766 g_source_unref(src);
768 while (g_main_context_iteration(NULL, false));
770 g_test_init(&argc, &argv, NULL);
771 g_test_add_func("/aio/notify", test_notify);
772 g_test_add_func("/aio/bh/schedule", test_bh_schedule);
773 g_test_add_func("/aio/bh/schedule10", test_bh_schedule10);
774 g_test_add_func("/aio/bh/cancel", test_bh_cancel);
775 g_test_add_func("/aio/bh/delete", test_bh_delete);
776 g_test_add_func("/aio/bh/callback-delete/one", test_bh_delete_from_cb);
777 g_test_add_func("/aio/bh/callback-delete/many", test_bh_delete_from_cb_many);
778 g_test_add_func("/aio/bh/flush", test_bh_flush);
779 g_test_add_func("/aio/event/add-remove", test_set_event_notifier);
780 g_test_add_func("/aio/event/wait", test_wait_event_notifier);
781 g_test_add_func("/aio/event/wait/no-flush-cb", test_wait_event_notifier_noflush);
782 g_test_add_func("/aio/event/flush", test_flush_event_notifier);
783 g_test_add_func("/aio/timer/schedule", test_timer_schedule);
785 g_test_add_func("/aio-gsource/notify", test_source_notify);
786 g_test_add_func("/aio-gsource/flush", test_source_flush);
787 g_test_add_func("/aio-gsource/bh/schedule", test_source_bh_schedule);
788 g_test_add_func("/aio-gsource/bh/schedule10", test_source_bh_schedule10);
789 g_test_add_func("/aio-gsource/bh/cancel", test_source_bh_cancel);
790 g_test_add_func("/aio-gsource/bh/delete", test_source_bh_delete);
791 g_test_add_func("/aio-gsource/bh/callback-delete/one", test_source_bh_delete_from_cb);
792 g_test_add_func("/aio-gsource/bh/callback-delete/many", test_source_bh_delete_from_cb_many);
793 g_test_add_func("/aio-gsource/bh/flush", test_source_bh_flush);
794 g_test_add_func("/aio-gsource/event/add-remove", test_source_set_event_notifier);
795 g_test_add_func("/aio-gsource/event/wait", test_source_wait_event_notifier);
796 g_test_add_func("/aio-gsource/event/wait/no-flush-cb", test_source_wait_event_notifier_noflush);
797 g_test_add_func("/aio-gsource/event/flush", test_source_flush_event_notifier);
798 g_test_add_func("/aio-gsource/timer/schedule", test_source_timer_schedule);
799 return g_test_run();