AioContext: introduce aio_prepare
[qemu/ar7.git] / tests / test-aio.c
blobc6a8713e83064f10765461f28d9278ce5419b07b
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"
16 #include "qemu/sockets.h"
18 static AioContext *ctx;
20 typedef struct {
21 EventNotifier e;
22 int n;
23 int active;
24 bool auto_set;
25 } EventNotifierTestData;
27 /* Wait until event notifier becomes inactive */
28 static void wait_until_inactive(EventNotifierTestData *data)
30 while (data->active > 0) {
31 aio_poll(ctx, true);
35 /* Simple callbacks for testing. */
37 typedef struct {
38 QEMUBH *bh;
39 int n;
40 int max;
41 } BHTestData;
43 typedef struct {
44 QEMUTimer timer;
45 QEMUClockType clock_type;
46 int n;
47 int max;
48 int64_t ns;
49 AioContext *ctx;
50 } TimerTestData;
52 static void bh_test_cb(void *opaque)
54 BHTestData *data = opaque;
55 if (++data->n < data->max) {
56 qemu_bh_schedule(data->bh);
60 static void timer_test_cb(void *opaque)
62 TimerTestData *data = opaque;
63 if (++data->n < data->max) {
64 timer_mod(&data->timer,
65 qemu_clock_get_ns(data->clock_type) + data->ns);
69 static void dummy_io_handler_read(EventNotifier *e)
73 static void bh_delete_cb(void *opaque)
75 BHTestData *data = opaque;
76 if (++data->n < data->max) {
77 qemu_bh_schedule(data->bh);
78 } else {
79 qemu_bh_delete(data->bh);
80 data->bh = NULL;
84 static void event_ready_cb(EventNotifier *e)
86 EventNotifierTestData *data = container_of(e, EventNotifierTestData, e);
87 g_assert(event_notifier_test_and_clear(e));
88 data->n++;
89 if (data->active > 0) {
90 data->active--;
92 if (data->auto_set && data->active) {
93 event_notifier_set(e);
97 /* Tests using aio_*. */
99 static void test_notify(void)
101 g_assert(!aio_poll(ctx, false));
102 aio_notify(ctx);
103 g_assert(!aio_poll(ctx, true));
104 g_assert(!aio_poll(ctx, false));
107 typedef struct {
108 QemuMutex start_lock;
109 bool thread_acquired;
110 } AcquireTestData;
112 static void *test_acquire_thread(void *opaque)
114 AcquireTestData *data = opaque;
116 /* Wait for other thread to let us start */
117 qemu_mutex_lock(&data->start_lock);
118 qemu_mutex_unlock(&data->start_lock);
120 aio_context_acquire(ctx);
121 aio_context_release(ctx);
123 data->thread_acquired = true; /* success, we got here */
125 return NULL;
128 static void dummy_notifier_read(EventNotifier *unused)
130 g_assert(false); /* should never be invoked */
133 static void test_acquire(void)
135 QemuThread thread;
136 EventNotifier notifier;
137 AcquireTestData data;
139 /* Dummy event notifier ensures aio_poll() will block */
140 event_notifier_init(&notifier, false);
141 aio_set_event_notifier(ctx, &notifier, dummy_notifier_read);
142 g_assert(!aio_poll(ctx, false)); /* consume aio_notify() */
144 qemu_mutex_init(&data.start_lock);
145 qemu_mutex_lock(&data.start_lock);
146 data.thread_acquired = false;
148 qemu_thread_create(&thread, "test_acquire_thread",
149 test_acquire_thread,
150 &data, QEMU_THREAD_JOINABLE);
152 /* Block in aio_poll(), let other thread kick us and acquire context */
153 aio_context_acquire(ctx);
154 qemu_mutex_unlock(&data.start_lock); /* let the thread run */
155 g_assert(!aio_poll(ctx, true));
156 aio_context_release(ctx);
158 qemu_thread_join(&thread);
159 aio_set_event_notifier(ctx, &notifier, NULL);
160 event_notifier_cleanup(&notifier);
162 g_assert(data.thread_acquired);
165 static void test_bh_schedule(void)
167 BHTestData data = { .n = 0 };
168 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
170 qemu_bh_schedule(data.bh);
171 g_assert_cmpint(data.n, ==, 0);
173 g_assert(aio_poll(ctx, true));
174 g_assert_cmpint(data.n, ==, 1);
176 g_assert(!aio_poll(ctx, false));
177 g_assert_cmpint(data.n, ==, 1);
178 qemu_bh_delete(data.bh);
181 static void test_bh_schedule10(void)
183 BHTestData data = { .n = 0, .max = 10 };
184 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
186 qemu_bh_schedule(data.bh);
187 g_assert_cmpint(data.n, ==, 0);
189 g_assert(aio_poll(ctx, false));
190 g_assert_cmpint(data.n, ==, 1);
192 g_assert(aio_poll(ctx, true));
193 g_assert_cmpint(data.n, ==, 2);
195 while (data.n < 10) {
196 aio_poll(ctx, true);
198 g_assert_cmpint(data.n, ==, 10);
200 g_assert(!aio_poll(ctx, false));
201 g_assert_cmpint(data.n, ==, 10);
202 qemu_bh_delete(data.bh);
205 static void test_bh_cancel(void)
207 BHTestData data = { .n = 0 };
208 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
210 qemu_bh_schedule(data.bh);
211 g_assert_cmpint(data.n, ==, 0);
213 qemu_bh_cancel(data.bh);
214 g_assert_cmpint(data.n, ==, 0);
216 g_assert(!aio_poll(ctx, false));
217 g_assert_cmpint(data.n, ==, 0);
218 qemu_bh_delete(data.bh);
221 static void test_bh_delete(void)
223 BHTestData data = { .n = 0 };
224 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
226 qemu_bh_schedule(data.bh);
227 g_assert_cmpint(data.n, ==, 0);
229 qemu_bh_delete(data.bh);
230 g_assert_cmpint(data.n, ==, 0);
232 g_assert(!aio_poll(ctx, false));
233 g_assert_cmpint(data.n, ==, 0);
236 static void test_bh_delete_from_cb(void)
238 BHTestData data1 = { .n = 0, .max = 1 };
240 data1.bh = aio_bh_new(ctx, bh_delete_cb, &data1);
242 qemu_bh_schedule(data1.bh);
243 g_assert_cmpint(data1.n, ==, 0);
245 while (data1.n < data1.max) {
246 aio_poll(ctx, true);
248 g_assert_cmpint(data1.n, ==, data1.max);
249 g_assert(data1.bh == NULL);
251 g_assert(!aio_poll(ctx, false));
254 static void test_bh_delete_from_cb_many(void)
256 BHTestData data1 = { .n = 0, .max = 1 };
257 BHTestData data2 = { .n = 0, .max = 3 };
258 BHTestData data3 = { .n = 0, .max = 2 };
259 BHTestData data4 = { .n = 0, .max = 4 };
261 data1.bh = aio_bh_new(ctx, bh_delete_cb, &data1);
262 data2.bh = aio_bh_new(ctx, bh_delete_cb, &data2);
263 data3.bh = aio_bh_new(ctx, bh_delete_cb, &data3);
264 data4.bh = aio_bh_new(ctx, bh_delete_cb, &data4);
266 qemu_bh_schedule(data1.bh);
267 qemu_bh_schedule(data2.bh);
268 qemu_bh_schedule(data3.bh);
269 qemu_bh_schedule(data4.bh);
270 g_assert_cmpint(data1.n, ==, 0);
271 g_assert_cmpint(data2.n, ==, 0);
272 g_assert_cmpint(data3.n, ==, 0);
273 g_assert_cmpint(data4.n, ==, 0);
275 g_assert(aio_poll(ctx, false));
276 g_assert_cmpint(data1.n, ==, 1);
277 g_assert_cmpint(data2.n, ==, 1);
278 g_assert_cmpint(data3.n, ==, 1);
279 g_assert_cmpint(data4.n, ==, 1);
280 g_assert(data1.bh == NULL);
282 while (data1.n < data1.max ||
283 data2.n < data2.max ||
284 data3.n < data3.max ||
285 data4.n < data4.max) {
286 aio_poll(ctx, true);
288 g_assert_cmpint(data1.n, ==, data1.max);
289 g_assert_cmpint(data2.n, ==, data2.max);
290 g_assert_cmpint(data3.n, ==, data3.max);
291 g_assert_cmpint(data4.n, ==, data4.max);
292 g_assert(data1.bh == NULL);
293 g_assert(data2.bh == NULL);
294 g_assert(data3.bh == NULL);
295 g_assert(data4.bh == NULL);
298 static void test_bh_flush(void)
300 BHTestData data = { .n = 0 };
301 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
303 qemu_bh_schedule(data.bh);
304 g_assert_cmpint(data.n, ==, 0);
306 g_assert(aio_poll(ctx, true));
307 g_assert_cmpint(data.n, ==, 1);
309 g_assert(!aio_poll(ctx, false));
310 g_assert_cmpint(data.n, ==, 1);
311 qemu_bh_delete(data.bh);
314 static void test_set_event_notifier(void)
316 EventNotifierTestData data = { .n = 0, .active = 0 };
317 event_notifier_init(&data.e, false);
318 aio_set_event_notifier(ctx, &data.e, event_ready_cb);
319 g_assert(!aio_poll(ctx, false));
320 g_assert_cmpint(data.n, ==, 0);
322 aio_set_event_notifier(ctx, &data.e, NULL);
323 g_assert(!aio_poll(ctx, false));
324 g_assert_cmpint(data.n, ==, 0);
325 event_notifier_cleanup(&data.e);
328 static void test_wait_event_notifier(void)
330 EventNotifierTestData data = { .n = 0, .active = 1 };
331 event_notifier_init(&data.e, false);
332 aio_set_event_notifier(ctx, &data.e, event_ready_cb);
333 g_assert(!aio_poll(ctx, false));
334 g_assert_cmpint(data.n, ==, 0);
335 g_assert_cmpint(data.active, ==, 1);
337 event_notifier_set(&data.e);
338 g_assert(aio_poll(ctx, false));
339 g_assert_cmpint(data.n, ==, 1);
340 g_assert_cmpint(data.active, ==, 0);
342 g_assert(!aio_poll(ctx, false));
343 g_assert_cmpint(data.n, ==, 1);
344 g_assert_cmpint(data.active, ==, 0);
346 aio_set_event_notifier(ctx, &data.e, NULL);
347 g_assert(!aio_poll(ctx, false));
348 g_assert_cmpint(data.n, ==, 1);
350 event_notifier_cleanup(&data.e);
353 static void test_flush_event_notifier(void)
355 EventNotifierTestData data = { .n = 0, .active = 10, .auto_set = true };
356 event_notifier_init(&data.e, false);
357 aio_set_event_notifier(ctx, &data.e, event_ready_cb);
358 g_assert(!aio_poll(ctx, false));
359 g_assert_cmpint(data.n, ==, 0);
360 g_assert_cmpint(data.active, ==, 10);
362 event_notifier_set(&data.e);
363 g_assert(aio_poll(ctx, false));
364 g_assert_cmpint(data.n, ==, 1);
365 g_assert_cmpint(data.active, ==, 9);
366 g_assert(aio_poll(ctx, false));
368 wait_until_inactive(&data);
369 g_assert_cmpint(data.n, ==, 10);
370 g_assert_cmpint(data.active, ==, 0);
371 g_assert(!aio_poll(ctx, false));
373 aio_set_event_notifier(ctx, &data.e, NULL);
374 g_assert(!aio_poll(ctx, false));
375 event_notifier_cleanup(&data.e);
378 static void test_wait_event_notifier_noflush(void)
380 EventNotifierTestData data = { .n = 0 };
381 EventNotifierTestData dummy = { .n = 0, .active = 1 };
383 event_notifier_init(&data.e, false);
384 aio_set_event_notifier(ctx, &data.e, event_ready_cb);
386 g_assert(!aio_poll(ctx, false));
387 g_assert_cmpint(data.n, ==, 0);
389 /* Until there is an active descriptor, aio_poll may or may not call
390 * event_ready_cb. Still, it must not block. */
391 event_notifier_set(&data.e);
392 g_assert(aio_poll(ctx, true));
393 data.n = 0;
395 /* An active event notifier forces aio_poll to look at EventNotifiers. */
396 event_notifier_init(&dummy.e, false);
397 aio_set_event_notifier(ctx, &dummy.e, event_ready_cb);
399 event_notifier_set(&data.e);
400 g_assert(aio_poll(ctx, false));
401 g_assert_cmpint(data.n, ==, 1);
402 g_assert(!aio_poll(ctx, false));
403 g_assert_cmpint(data.n, ==, 1);
405 event_notifier_set(&data.e);
406 g_assert(aio_poll(ctx, false));
407 g_assert_cmpint(data.n, ==, 2);
408 g_assert(!aio_poll(ctx, false));
409 g_assert_cmpint(data.n, ==, 2);
411 event_notifier_set(&dummy.e);
412 wait_until_inactive(&dummy);
413 g_assert_cmpint(data.n, ==, 2);
414 g_assert_cmpint(dummy.n, ==, 1);
415 g_assert_cmpint(dummy.active, ==, 0);
417 aio_set_event_notifier(ctx, &dummy.e, NULL);
418 event_notifier_cleanup(&dummy.e);
420 aio_set_event_notifier(ctx, &data.e, NULL);
421 g_assert(!aio_poll(ctx, false));
422 g_assert_cmpint(data.n, ==, 2);
424 event_notifier_cleanup(&data.e);
427 static void test_timer_schedule(void)
429 TimerTestData data = { .n = 0, .ctx = ctx, .ns = SCALE_MS * 750LL,
430 .max = 2,
431 .clock_type = QEMU_CLOCK_VIRTUAL };
432 EventNotifier e;
434 /* aio_poll will not block to wait for timers to complete unless it has
435 * an fd to wait on. Fixing this breaks other tests. So create a dummy one.
437 event_notifier_init(&e, false);
438 aio_set_event_notifier(ctx, &e, dummy_io_handler_read);
439 aio_poll(ctx, false);
441 aio_timer_init(ctx, &data.timer, data.clock_type,
442 SCALE_NS, timer_test_cb, &data);
443 timer_mod(&data.timer,
444 qemu_clock_get_ns(data.clock_type) +
445 data.ns);
447 g_assert_cmpint(data.n, ==, 0);
449 /* timer_mod may well cause an event notifer to have gone off,
450 * so clear that
452 do {} while (aio_poll(ctx, false));
454 g_assert(!aio_poll(ctx, false));
455 g_assert_cmpint(data.n, ==, 0);
457 g_usleep(1 * G_USEC_PER_SEC);
458 g_assert_cmpint(data.n, ==, 0);
460 g_assert(aio_poll(ctx, false));
461 g_assert_cmpint(data.n, ==, 1);
463 /* timer_mod called by our callback */
464 do {} while (aio_poll(ctx, false));
466 g_assert(!aio_poll(ctx, false));
467 g_assert_cmpint(data.n, ==, 1);
469 g_assert(aio_poll(ctx, true));
470 g_assert_cmpint(data.n, ==, 2);
472 /* As max is now 2, an event notifier should not have gone off */
474 g_assert(!aio_poll(ctx, false));
475 g_assert_cmpint(data.n, ==, 2);
477 aio_set_event_notifier(ctx, &e, NULL);
478 event_notifier_cleanup(&e);
480 timer_del(&data.timer);
483 /* Now the same tests, using the context as a GSource. They are
484 * very similar to the ones above, with g_main_context_iteration
485 * replacing aio_poll. However:
486 * - sometimes both the AioContext and the glib main loop wake
487 * themselves up. Hence, some "g_assert(!aio_poll(ctx, false));"
488 * are replaced by "while (g_main_context_iteration(NULL, false));".
489 * - there is no exact replacement for a blocking wait.
490 * "while (g_main_context_iteration(NULL, true)" seems to work,
491 * but it is not documented _why_ it works. For these tests a
492 * non-blocking loop like "while (g_main_context_iteration(NULL, false)"
493 * works well, and that's what I am using.
496 static void test_source_notify(void)
498 while (g_main_context_iteration(NULL, false));
499 aio_notify(ctx);
500 g_assert(g_main_context_iteration(NULL, true));
501 g_assert(!g_main_context_iteration(NULL, false));
504 static void test_source_flush(void)
506 g_assert(!g_main_context_iteration(NULL, false));
507 aio_notify(ctx);
508 while (g_main_context_iteration(NULL, false));
509 g_assert(!g_main_context_iteration(NULL, false));
512 static void test_source_bh_schedule(void)
514 BHTestData data = { .n = 0 };
515 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
517 qemu_bh_schedule(data.bh);
518 g_assert_cmpint(data.n, ==, 0);
520 g_assert(g_main_context_iteration(NULL, true));
521 g_assert_cmpint(data.n, ==, 1);
523 g_assert(!g_main_context_iteration(NULL, false));
524 g_assert_cmpint(data.n, ==, 1);
525 qemu_bh_delete(data.bh);
528 static void test_source_bh_schedule10(void)
530 BHTestData data = { .n = 0, .max = 10 };
531 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
533 qemu_bh_schedule(data.bh);
534 g_assert_cmpint(data.n, ==, 0);
536 g_assert(g_main_context_iteration(NULL, false));
537 g_assert_cmpint(data.n, ==, 1);
539 g_assert(g_main_context_iteration(NULL, true));
540 g_assert_cmpint(data.n, ==, 2);
542 while (g_main_context_iteration(NULL, false));
543 g_assert_cmpint(data.n, ==, 10);
545 g_assert(!g_main_context_iteration(NULL, false));
546 g_assert_cmpint(data.n, ==, 10);
547 qemu_bh_delete(data.bh);
550 static void test_source_bh_cancel(void)
552 BHTestData data = { .n = 0 };
553 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
555 qemu_bh_schedule(data.bh);
556 g_assert_cmpint(data.n, ==, 0);
558 qemu_bh_cancel(data.bh);
559 g_assert_cmpint(data.n, ==, 0);
561 while (g_main_context_iteration(NULL, false));
562 g_assert_cmpint(data.n, ==, 0);
563 qemu_bh_delete(data.bh);
566 static void test_source_bh_delete(void)
568 BHTestData data = { .n = 0 };
569 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
571 qemu_bh_schedule(data.bh);
572 g_assert_cmpint(data.n, ==, 0);
574 qemu_bh_delete(data.bh);
575 g_assert_cmpint(data.n, ==, 0);
577 while (g_main_context_iteration(NULL, false));
578 g_assert_cmpint(data.n, ==, 0);
581 static void test_source_bh_delete_from_cb(void)
583 BHTestData data1 = { .n = 0, .max = 1 };
585 data1.bh = aio_bh_new(ctx, bh_delete_cb, &data1);
587 qemu_bh_schedule(data1.bh);
588 g_assert_cmpint(data1.n, ==, 0);
590 g_main_context_iteration(NULL, true);
591 g_assert_cmpint(data1.n, ==, data1.max);
592 g_assert(data1.bh == NULL);
594 g_assert(!g_main_context_iteration(NULL, false));
597 static void test_source_bh_delete_from_cb_many(void)
599 BHTestData data1 = { .n = 0, .max = 1 };
600 BHTestData data2 = { .n = 0, .max = 3 };
601 BHTestData data3 = { .n = 0, .max = 2 };
602 BHTestData data4 = { .n = 0, .max = 4 };
604 data1.bh = aio_bh_new(ctx, bh_delete_cb, &data1);
605 data2.bh = aio_bh_new(ctx, bh_delete_cb, &data2);
606 data3.bh = aio_bh_new(ctx, bh_delete_cb, &data3);
607 data4.bh = aio_bh_new(ctx, bh_delete_cb, &data4);
609 qemu_bh_schedule(data1.bh);
610 qemu_bh_schedule(data2.bh);
611 qemu_bh_schedule(data3.bh);
612 qemu_bh_schedule(data4.bh);
613 g_assert_cmpint(data1.n, ==, 0);
614 g_assert_cmpint(data2.n, ==, 0);
615 g_assert_cmpint(data3.n, ==, 0);
616 g_assert_cmpint(data4.n, ==, 0);
618 g_assert(g_main_context_iteration(NULL, false));
619 g_assert_cmpint(data1.n, ==, 1);
620 g_assert_cmpint(data2.n, ==, 1);
621 g_assert_cmpint(data3.n, ==, 1);
622 g_assert_cmpint(data4.n, ==, 1);
623 g_assert(data1.bh == NULL);
625 while (g_main_context_iteration(NULL, false));
626 g_assert_cmpint(data1.n, ==, data1.max);
627 g_assert_cmpint(data2.n, ==, data2.max);
628 g_assert_cmpint(data3.n, ==, data3.max);
629 g_assert_cmpint(data4.n, ==, data4.max);
630 g_assert(data1.bh == NULL);
631 g_assert(data2.bh == NULL);
632 g_assert(data3.bh == NULL);
633 g_assert(data4.bh == NULL);
636 static void test_source_bh_flush(void)
638 BHTestData data = { .n = 0 };
639 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
641 qemu_bh_schedule(data.bh);
642 g_assert_cmpint(data.n, ==, 0);
644 g_assert(g_main_context_iteration(NULL, true));
645 g_assert_cmpint(data.n, ==, 1);
647 g_assert(!g_main_context_iteration(NULL, false));
648 g_assert_cmpint(data.n, ==, 1);
649 qemu_bh_delete(data.bh);
652 static void test_source_set_event_notifier(void)
654 EventNotifierTestData data = { .n = 0, .active = 0 };
655 event_notifier_init(&data.e, false);
656 aio_set_event_notifier(ctx, &data.e, event_ready_cb);
657 while (g_main_context_iteration(NULL, false));
658 g_assert_cmpint(data.n, ==, 0);
660 aio_set_event_notifier(ctx, &data.e, NULL);
661 while (g_main_context_iteration(NULL, false));
662 g_assert_cmpint(data.n, ==, 0);
663 event_notifier_cleanup(&data.e);
666 static void test_source_wait_event_notifier(void)
668 EventNotifierTestData data = { .n = 0, .active = 1 };
669 event_notifier_init(&data.e, false);
670 aio_set_event_notifier(ctx, &data.e, event_ready_cb);
671 g_assert(g_main_context_iteration(NULL, false));
672 g_assert_cmpint(data.n, ==, 0);
673 g_assert_cmpint(data.active, ==, 1);
675 event_notifier_set(&data.e);
676 g_assert(g_main_context_iteration(NULL, false));
677 g_assert_cmpint(data.n, ==, 1);
678 g_assert_cmpint(data.active, ==, 0);
680 while (g_main_context_iteration(NULL, false));
681 g_assert_cmpint(data.n, ==, 1);
682 g_assert_cmpint(data.active, ==, 0);
684 aio_set_event_notifier(ctx, &data.e, NULL);
685 while (g_main_context_iteration(NULL, false));
686 g_assert_cmpint(data.n, ==, 1);
688 event_notifier_cleanup(&data.e);
691 static void test_source_flush_event_notifier(void)
693 EventNotifierTestData data = { .n = 0, .active = 10, .auto_set = true };
694 event_notifier_init(&data.e, false);
695 aio_set_event_notifier(ctx, &data.e, event_ready_cb);
696 g_assert(g_main_context_iteration(NULL, false));
697 g_assert_cmpint(data.n, ==, 0);
698 g_assert_cmpint(data.active, ==, 10);
700 event_notifier_set(&data.e);
701 g_assert(g_main_context_iteration(NULL, false));
702 g_assert_cmpint(data.n, ==, 1);
703 g_assert_cmpint(data.active, ==, 9);
704 g_assert(g_main_context_iteration(NULL, false));
706 while (g_main_context_iteration(NULL, false));
707 g_assert_cmpint(data.n, ==, 10);
708 g_assert_cmpint(data.active, ==, 0);
709 g_assert(!g_main_context_iteration(NULL, false));
711 aio_set_event_notifier(ctx, &data.e, NULL);
712 while (g_main_context_iteration(NULL, false));
713 event_notifier_cleanup(&data.e);
716 static void test_source_wait_event_notifier_noflush(void)
718 EventNotifierTestData data = { .n = 0 };
719 EventNotifierTestData dummy = { .n = 0, .active = 1 };
721 event_notifier_init(&data.e, false);
722 aio_set_event_notifier(ctx, &data.e, event_ready_cb);
724 while (g_main_context_iteration(NULL, false));
725 g_assert_cmpint(data.n, ==, 0);
727 /* Until there is an active descriptor, glib may or may not call
728 * event_ready_cb. Still, it must not block. */
729 event_notifier_set(&data.e);
730 g_main_context_iteration(NULL, true);
731 data.n = 0;
733 /* An active event notifier forces aio_poll to look at EventNotifiers. */
734 event_notifier_init(&dummy.e, false);
735 aio_set_event_notifier(ctx, &dummy.e, event_ready_cb);
737 event_notifier_set(&data.e);
738 g_assert(g_main_context_iteration(NULL, false));
739 g_assert_cmpint(data.n, ==, 1);
740 g_assert(!g_main_context_iteration(NULL, false));
741 g_assert_cmpint(data.n, ==, 1);
743 event_notifier_set(&data.e);
744 g_assert(g_main_context_iteration(NULL, false));
745 g_assert_cmpint(data.n, ==, 2);
746 g_assert(!g_main_context_iteration(NULL, false));
747 g_assert_cmpint(data.n, ==, 2);
749 event_notifier_set(&dummy.e);
750 while (g_main_context_iteration(NULL, false));
751 g_assert_cmpint(data.n, ==, 2);
752 g_assert_cmpint(dummy.n, ==, 1);
753 g_assert_cmpint(dummy.active, ==, 0);
755 aio_set_event_notifier(ctx, &dummy.e, NULL);
756 event_notifier_cleanup(&dummy.e);
758 aio_set_event_notifier(ctx, &data.e, NULL);
759 while (g_main_context_iteration(NULL, false));
760 g_assert_cmpint(data.n, ==, 2);
762 event_notifier_cleanup(&data.e);
765 static void test_source_timer_schedule(void)
767 TimerTestData data = { .n = 0, .ctx = ctx, .ns = SCALE_MS * 750LL,
768 .max = 2,
769 .clock_type = QEMU_CLOCK_VIRTUAL };
770 EventNotifier e;
771 int64_t expiry;
773 /* aio_poll will not block to wait for timers to complete unless it has
774 * an fd to wait on. Fixing this breaks other tests. So create a dummy one.
776 event_notifier_init(&e, false);
777 aio_set_event_notifier(ctx, &e, dummy_io_handler_read);
778 do {} while (g_main_context_iteration(NULL, false));
780 aio_timer_init(ctx, &data.timer, data.clock_type,
781 SCALE_NS, timer_test_cb, &data);
782 expiry = qemu_clock_get_ns(data.clock_type) +
783 data.ns;
784 timer_mod(&data.timer, expiry);
786 g_assert_cmpint(data.n, ==, 0);
788 g_usleep(1 * G_USEC_PER_SEC);
789 g_assert_cmpint(data.n, ==, 0);
791 g_assert(g_main_context_iteration(NULL, true));
792 g_assert_cmpint(data.n, ==, 1);
793 expiry += data.ns;
795 while (data.n < 2) {
796 g_main_context_iteration(NULL, true);
799 g_assert_cmpint(data.n, ==, 2);
800 g_assert(qemu_clock_get_ns(data.clock_type) > expiry);
802 aio_set_event_notifier(ctx, &e, NULL);
803 event_notifier_cleanup(&e);
805 timer_del(&data.timer);
809 /* End of tests. */
811 int main(int argc, char **argv)
813 GSource *src;
815 init_clocks();
817 ctx = aio_context_new();
818 src = aio_get_g_source(ctx);
819 g_source_attach(src, NULL);
820 g_source_unref(src);
822 while (g_main_context_iteration(NULL, false));
824 g_test_init(&argc, &argv, NULL);
825 g_test_add_func("/aio/notify", test_notify);
826 g_test_add_func("/aio/acquire", test_acquire);
827 g_test_add_func("/aio/bh/schedule", test_bh_schedule);
828 g_test_add_func("/aio/bh/schedule10", test_bh_schedule10);
829 g_test_add_func("/aio/bh/cancel", test_bh_cancel);
830 g_test_add_func("/aio/bh/delete", test_bh_delete);
831 g_test_add_func("/aio/bh/callback-delete/one", test_bh_delete_from_cb);
832 g_test_add_func("/aio/bh/callback-delete/many", test_bh_delete_from_cb_many);
833 g_test_add_func("/aio/bh/flush", test_bh_flush);
834 g_test_add_func("/aio/event/add-remove", test_set_event_notifier);
835 g_test_add_func("/aio/event/wait", test_wait_event_notifier);
836 g_test_add_func("/aio/event/wait/no-flush-cb", test_wait_event_notifier_noflush);
837 g_test_add_func("/aio/event/flush", test_flush_event_notifier);
838 g_test_add_func("/aio/timer/schedule", test_timer_schedule);
840 g_test_add_func("/aio-gsource/notify", test_source_notify);
841 g_test_add_func("/aio-gsource/flush", test_source_flush);
842 g_test_add_func("/aio-gsource/bh/schedule", test_source_bh_schedule);
843 g_test_add_func("/aio-gsource/bh/schedule10", test_source_bh_schedule10);
844 g_test_add_func("/aio-gsource/bh/cancel", test_source_bh_cancel);
845 g_test_add_func("/aio-gsource/bh/delete", test_source_bh_delete);
846 g_test_add_func("/aio-gsource/bh/callback-delete/one", test_source_bh_delete_from_cb);
847 g_test_add_func("/aio-gsource/bh/callback-delete/many", test_source_bh_delete_from_cb_many);
848 g_test_add_func("/aio-gsource/bh/flush", test_source_bh_flush);
849 g_test_add_func("/aio-gsource/event/add-remove", test_source_set_event_notifier);
850 g_test_add_func("/aio-gsource/event/wait", test_source_wait_event_notifier);
851 g_test_add_func("/aio-gsource/event/wait/no-flush-cb", test_source_wait_event_notifier_noflush);
852 g_test_add_func("/aio-gsource/event/flush", test_source_flush_event_notifier);
853 g_test_add_func("/aio-gsource/timer/schedule", test_source_timer_schedule);
854 return g_test_run();