qxl: QOMify
[qemu/ar7.git] / tests / test-aio.c
blob4b0cb45d31b56399d8592cc97a30d2c4b1a0f7eb
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"
17 #include "qemu/error-report.h"
19 static AioContext *ctx;
21 typedef struct {
22 EventNotifier e;
23 int n;
24 int active;
25 bool auto_set;
26 } EventNotifierTestData;
28 /* Wait until event notifier becomes inactive */
29 static void wait_until_inactive(EventNotifierTestData *data)
31 while (data->active > 0) {
32 aio_poll(ctx, true);
36 /* Simple callbacks for testing. */
38 typedef struct {
39 QEMUBH *bh;
40 int n;
41 int max;
42 } BHTestData;
44 typedef struct {
45 QEMUTimer timer;
46 QEMUClockType clock_type;
47 int n;
48 int max;
49 int64_t ns;
50 AioContext *ctx;
51 } TimerTestData;
53 static void bh_test_cb(void *opaque)
55 BHTestData *data = opaque;
56 if (++data->n < data->max) {
57 qemu_bh_schedule(data->bh);
61 static void timer_test_cb(void *opaque)
63 TimerTestData *data = opaque;
64 if (++data->n < data->max) {
65 timer_mod(&data->timer,
66 qemu_clock_get_ns(data->clock_type) + data->ns);
70 static void dummy_io_handler_read(EventNotifier *e)
74 static void bh_delete_cb(void *opaque)
76 BHTestData *data = opaque;
77 if (++data->n < data->max) {
78 qemu_bh_schedule(data->bh);
79 } else {
80 qemu_bh_delete(data->bh);
81 data->bh = NULL;
85 static void event_ready_cb(EventNotifier *e)
87 EventNotifierTestData *data = container_of(e, EventNotifierTestData, e);
88 g_assert(event_notifier_test_and_clear(e));
89 data->n++;
90 if (data->active > 0) {
91 data->active--;
93 if (data->auto_set && data->active) {
94 event_notifier_set(e);
98 /* Tests using aio_*. */
100 static void test_notify(void)
102 g_assert(!aio_poll(ctx, false));
103 aio_notify(ctx);
104 g_assert(!aio_poll(ctx, true));
105 g_assert(!aio_poll(ctx, false));
108 typedef struct {
109 QemuMutex start_lock;
110 EventNotifier notifier;
111 bool thread_acquired;
112 } AcquireTestData;
114 static void *test_acquire_thread(void *opaque)
116 AcquireTestData *data = opaque;
118 /* Wait for other thread to let us start */
119 qemu_mutex_lock(&data->start_lock);
120 qemu_mutex_unlock(&data->start_lock);
122 g_usleep(500000);
123 event_notifier_set(&data->notifier);
124 aio_context_acquire(ctx);
125 aio_context_release(ctx);
127 data->thread_acquired = true; /* success, we got here */
129 return NULL;
132 static void dummy_notifier_read(EventNotifier *n)
134 event_notifier_test_and_clear(n);
137 static void test_acquire(void)
139 QemuThread thread;
140 AcquireTestData data;
142 /* Dummy event notifier ensures aio_poll() will block */
143 event_notifier_init(&data.notifier, false);
144 aio_set_event_notifier(ctx, &data.notifier, dummy_notifier_read);
145 g_assert(!aio_poll(ctx, false)); /* consume aio_notify() */
147 qemu_mutex_init(&data.start_lock);
148 qemu_mutex_lock(&data.start_lock);
149 data.thread_acquired = false;
151 qemu_thread_create(&thread, "test_acquire_thread",
152 test_acquire_thread,
153 &data, QEMU_THREAD_JOINABLE);
155 /* Block in aio_poll(), let other thread kick us and acquire context */
156 aio_context_acquire(ctx);
157 qemu_mutex_unlock(&data.start_lock); /* let the thread run */
158 g_assert(aio_poll(ctx, true));
159 g_assert(!data.thread_acquired);
160 aio_context_release(ctx);
162 qemu_thread_join(&thread);
163 aio_set_event_notifier(ctx, &data.notifier, NULL);
164 event_notifier_cleanup(&data.notifier);
166 g_assert(data.thread_acquired);
169 static void test_bh_schedule(void)
171 BHTestData data = { .n = 0 };
172 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
174 qemu_bh_schedule(data.bh);
175 g_assert_cmpint(data.n, ==, 0);
177 g_assert(aio_poll(ctx, true));
178 g_assert_cmpint(data.n, ==, 1);
180 g_assert(!aio_poll(ctx, false));
181 g_assert_cmpint(data.n, ==, 1);
182 qemu_bh_delete(data.bh);
185 static void test_bh_schedule10(void)
187 BHTestData data = { .n = 0, .max = 10 };
188 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
190 qemu_bh_schedule(data.bh);
191 g_assert_cmpint(data.n, ==, 0);
193 g_assert(aio_poll(ctx, false));
194 g_assert_cmpint(data.n, ==, 1);
196 g_assert(aio_poll(ctx, true));
197 g_assert_cmpint(data.n, ==, 2);
199 while (data.n < 10) {
200 aio_poll(ctx, true);
202 g_assert_cmpint(data.n, ==, 10);
204 g_assert(!aio_poll(ctx, false));
205 g_assert_cmpint(data.n, ==, 10);
206 qemu_bh_delete(data.bh);
209 static void test_bh_cancel(void)
211 BHTestData data = { .n = 0 };
212 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
214 qemu_bh_schedule(data.bh);
215 g_assert_cmpint(data.n, ==, 0);
217 qemu_bh_cancel(data.bh);
218 g_assert_cmpint(data.n, ==, 0);
220 g_assert(!aio_poll(ctx, false));
221 g_assert_cmpint(data.n, ==, 0);
222 qemu_bh_delete(data.bh);
225 static void test_bh_delete(void)
227 BHTestData data = { .n = 0 };
228 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
230 qemu_bh_schedule(data.bh);
231 g_assert_cmpint(data.n, ==, 0);
233 qemu_bh_delete(data.bh);
234 g_assert_cmpint(data.n, ==, 0);
236 g_assert(!aio_poll(ctx, false));
237 g_assert_cmpint(data.n, ==, 0);
240 static void test_bh_delete_from_cb(void)
242 BHTestData data1 = { .n = 0, .max = 1 };
244 data1.bh = aio_bh_new(ctx, bh_delete_cb, &data1);
246 qemu_bh_schedule(data1.bh);
247 g_assert_cmpint(data1.n, ==, 0);
249 while (data1.n < data1.max) {
250 aio_poll(ctx, true);
252 g_assert_cmpint(data1.n, ==, data1.max);
253 g_assert(data1.bh == NULL);
255 g_assert(!aio_poll(ctx, false));
258 static void test_bh_delete_from_cb_many(void)
260 BHTestData data1 = { .n = 0, .max = 1 };
261 BHTestData data2 = { .n = 0, .max = 3 };
262 BHTestData data3 = { .n = 0, .max = 2 };
263 BHTestData data4 = { .n = 0, .max = 4 };
265 data1.bh = aio_bh_new(ctx, bh_delete_cb, &data1);
266 data2.bh = aio_bh_new(ctx, bh_delete_cb, &data2);
267 data3.bh = aio_bh_new(ctx, bh_delete_cb, &data3);
268 data4.bh = aio_bh_new(ctx, bh_delete_cb, &data4);
270 qemu_bh_schedule(data1.bh);
271 qemu_bh_schedule(data2.bh);
272 qemu_bh_schedule(data3.bh);
273 qemu_bh_schedule(data4.bh);
274 g_assert_cmpint(data1.n, ==, 0);
275 g_assert_cmpint(data2.n, ==, 0);
276 g_assert_cmpint(data3.n, ==, 0);
277 g_assert_cmpint(data4.n, ==, 0);
279 g_assert(aio_poll(ctx, false));
280 g_assert_cmpint(data1.n, ==, 1);
281 g_assert_cmpint(data2.n, ==, 1);
282 g_assert_cmpint(data3.n, ==, 1);
283 g_assert_cmpint(data4.n, ==, 1);
284 g_assert(data1.bh == NULL);
286 while (data1.n < data1.max ||
287 data2.n < data2.max ||
288 data3.n < data3.max ||
289 data4.n < data4.max) {
290 aio_poll(ctx, true);
292 g_assert_cmpint(data1.n, ==, data1.max);
293 g_assert_cmpint(data2.n, ==, data2.max);
294 g_assert_cmpint(data3.n, ==, data3.max);
295 g_assert_cmpint(data4.n, ==, data4.max);
296 g_assert(data1.bh == NULL);
297 g_assert(data2.bh == NULL);
298 g_assert(data3.bh == NULL);
299 g_assert(data4.bh == NULL);
302 static void test_bh_flush(void)
304 BHTestData data = { .n = 0 };
305 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
307 qemu_bh_schedule(data.bh);
308 g_assert_cmpint(data.n, ==, 0);
310 g_assert(aio_poll(ctx, true));
311 g_assert_cmpint(data.n, ==, 1);
313 g_assert(!aio_poll(ctx, false));
314 g_assert_cmpint(data.n, ==, 1);
315 qemu_bh_delete(data.bh);
318 static void test_set_event_notifier(void)
320 EventNotifierTestData data = { .n = 0, .active = 0 };
321 event_notifier_init(&data.e, false);
322 aio_set_event_notifier(ctx, &data.e, event_ready_cb);
323 g_assert(!aio_poll(ctx, false));
324 g_assert_cmpint(data.n, ==, 0);
326 aio_set_event_notifier(ctx, &data.e, NULL);
327 g_assert(!aio_poll(ctx, false));
328 g_assert_cmpint(data.n, ==, 0);
329 event_notifier_cleanup(&data.e);
332 static void test_wait_event_notifier(void)
334 EventNotifierTestData data = { .n = 0, .active = 1 };
335 event_notifier_init(&data.e, false);
336 aio_set_event_notifier(ctx, &data.e, event_ready_cb);
337 g_assert(!aio_poll(ctx, false));
338 g_assert_cmpint(data.n, ==, 0);
339 g_assert_cmpint(data.active, ==, 1);
341 event_notifier_set(&data.e);
342 g_assert(aio_poll(ctx, false));
343 g_assert_cmpint(data.n, ==, 1);
344 g_assert_cmpint(data.active, ==, 0);
346 g_assert(!aio_poll(ctx, false));
347 g_assert_cmpint(data.n, ==, 1);
348 g_assert_cmpint(data.active, ==, 0);
350 aio_set_event_notifier(ctx, &data.e, NULL);
351 g_assert(!aio_poll(ctx, false));
352 g_assert_cmpint(data.n, ==, 1);
354 event_notifier_cleanup(&data.e);
357 static void test_flush_event_notifier(void)
359 EventNotifierTestData data = { .n = 0, .active = 10, .auto_set = true };
360 event_notifier_init(&data.e, false);
361 aio_set_event_notifier(ctx, &data.e, event_ready_cb);
362 g_assert(!aio_poll(ctx, false));
363 g_assert_cmpint(data.n, ==, 0);
364 g_assert_cmpint(data.active, ==, 10);
366 event_notifier_set(&data.e);
367 g_assert(aio_poll(ctx, false));
368 g_assert_cmpint(data.n, ==, 1);
369 g_assert_cmpint(data.active, ==, 9);
370 g_assert(aio_poll(ctx, false));
372 wait_until_inactive(&data);
373 g_assert_cmpint(data.n, ==, 10);
374 g_assert_cmpint(data.active, ==, 0);
375 g_assert(!aio_poll(ctx, false));
377 aio_set_event_notifier(ctx, &data.e, NULL);
378 g_assert(!aio_poll(ctx, false));
379 event_notifier_cleanup(&data.e);
382 static void test_wait_event_notifier_noflush(void)
384 EventNotifierTestData data = { .n = 0 };
385 EventNotifierTestData dummy = { .n = 0, .active = 1 };
387 event_notifier_init(&data.e, false);
388 aio_set_event_notifier(ctx, &data.e, event_ready_cb);
390 g_assert(!aio_poll(ctx, false));
391 g_assert_cmpint(data.n, ==, 0);
393 /* Until there is an active descriptor, aio_poll may or may not call
394 * event_ready_cb. Still, it must not block. */
395 event_notifier_set(&data.e);
396 g_assert(aio_poll(ctx, true));
397 data.n = 0;
399 /* An active event notifier forces aio_poll to look at EventNotifiers. */
400 event_notifier_init(&dummy.e, false);
401 aio_set_event_notifier(ctx, &dummy.e, event_ready_cb);
403 event_notifier_set(&data.e);
404 g_assert(aio_poll(ctx, false));
405 g_assert_cmpint(data.n, ==, 1);
406 g_assert(!aio_poll(ctx, false));
407 g_assert_cmpint(data.n, ==, 1);
409 event_notifier_set(&data.e);
410 g_assert(aio_poll(ctx, false));
411 g_assert_cmpint(data.n, ==, 2);
412 g_assert(!aio_poll(ctx, false));
413 g_assert_cmpint(data.n, ==, 2);
415 event_notifier_set(&dummy.e);
416 wait_until_inactive(&dummy);
417 g_assert_cmpint(data.n, ==, 2);
418 g_assert_cmpint(dummy.n, ==, 1);
419 g_assert_cmpint(dummy.active, ==, 0);
421 aio_set_event_notifier(ctx, &dummy.e, NULL);
422 event_notifier_cleanup(&dummy.e);
424 aio_set_event_notifier(ctx, &data.e, NULL);
425 g_assert(!aio_poll(ctx, false));
426 g_assert_cmpint(data.n, ==, 2);
428 event_notifier_cleanup(&data.e);
431 static void test_timer_schedule(void)
433 TimerTestData data = { .n = 0, .ctx = ctx, .ns = SCALE_MS * 750LL,
434 .max = 2,
435 .clock_type = QEMU_CLOCK_VIRTUAL };
436 EventNotifier e;
438 /* aio_poll will not block to wait for timers to complete unless it has
439 * an fd to wait on. Fixing this breaks other tests. So create a dummy one.
441 event_notifier_init(&e, false);
442 aio_set_event_notifier(ctx, &e, dummy_io_handler_read);
443 aio_poll(ctx, false);
445 aio_timer_init(ctx, &data.timer, data.clock_type,
446 SCALE_NS, timer_test_cb, &data);
447 timer_mod(&data.timer,
448 qemu_clock_get_ns(data.clock_type) +
449 data.ns);
451 g_assert_cmpint(data.n, ==, 0);
453 /* timer_mod may well cause an event notifer to have gone off,
454 * so clear that
456 do {} while (aio_poll(ctx, false));
458 g_assert(!aio_poll(ctx, false));
459 g_assert_cmpint(data.n, ==, 0);
461 g_usleep(1 * G_USEC_PER_SEC);
462 g_assert_cmpint(data.n, ==, 0);
464 g_assert(aio_poll(ctx, false));
465 g_assert_cmpint(data.n, ==, 1);
467 /* timer_mod called by our callback */
468 do {} while (aio_poll(ctx, false));
470 g_assert(!aio_poll(ctx, false));
471 g_assert_cmpint(data.n, ==, 1);
473 g_assert(aio_poll(ctx, true));
474 g_assert_cmpint(data.n, ==, 2);
476 /* As max is now 2, an event notifier should not have gone off */
478 g_assert(!aio_poll(ctx, false));
479 g_assert_cmpint(data.n, ==, 2);
481 aio_set_event_notifier(ctx, &e, NULL);
482 event_notifier_cleanup(&e);
484 timer_del(&data.timer);
487 /* Now the same tests, using the context as a GSource. They are
488 * very similar to the ones above, with g_main_context_iteration
489 * replacing aio_poll. However:
490 * - sometimes both the AioContext and the glib main loop wake
491 * themselves up. Hence, some "g_assert(!aio_poll(ctx, false));"
492 * are replaced by "while (g_main_context_iteration(NULL, false));".
493 * - there is no exact replacement for a blocking wait.
494 * "while (g_main_context_iteration(NULL, true)" seems to work,
495 * but it is not documented _why_ it works. For these tests a
496 * non-blocking loop like "while (g_main_context_iteration(NULL, false)"
497 * works well, and that's what I am using.
500 static void test_source_notify(void)
502 while (g_main_context_iteration(NULL, false));
503 aio_notify(ctx);
504 g_assert(g_main_context_iteration(NULL, true));
505 g_assert(!g_main_context_iteration(NULL, false));
508 static void test_source_flush(void)
510 g_assert(!g_main_context_iteration(NULL, false));
511 aio_notify(ctx);
512 while (g_main_context_iteration(NULL, false));
513 g_assert(!g_main_context_iteration(NULL, false));
516 static void test_source_bh_schedule(void)
518 BHTestData data = { .n = 0 };
519 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
521 qemu_bh_schedule(data.bh);
522 g_assert_cmpint(data.n, ==, 0);
524 g_assert(g_main_context_iteration(NULL, true));
525 g_assert_cmpint(data.n, ==, 1);
527 g_assert(!g_main_context_iteration(NULL, false));
528 g_assert_cmpint(data.n, ==, 1);
529 qemu_bh_delete(data.bh);
532 static void test_source_bh_schedule10(void)
534 BHTestData data = { .n = 0, .max = 10 };
535 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
537 qemu_bh_schedule(data.bh);
538 g_assert_cmpint(data.n, ==, 0);
540 g_assert(g_main_context_iteration(NULL, false));
541 g_assert_cmpint(data.n, ==, 1);
543 g_assert(g_main_context_iteration(NULL, true));
544 g_assert_cmpint(data.n, ==, 2);
546 while (g_main_context_iteration(NULL, false));
547 g_assert_cmpint(data.n, ==, 10);
549 g_assert(!g_main_context_iteration(NULL, false));
550 g_assert_cmpint(data.n, ==, 10);
551 qemu_bh_delete(data.bh);
554 static void test_source_bh_cancel(void)
556 BHTestData data = { .n = 0 };
557 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
559 qemu_bh_schedule(data.bh);
560 g_assert_cmpint(data.n, ==, 0);
562 qemu_bh_cancel(data.bh);
563 g_assert_cmpint(data.n, ==, 0);
565 while (g_main_context_iteration(NULL, false));
566 g_assert_cmpint(data.n, ==, 0);
567 qemu_bh_delete(data.bh);
570 static void test_source_bh_delete(void)
572 BHTestData data = { .n = 0 };
573 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
575 qemu_bh_schedule(data.bh);
576 g_assert_cmpint(data.n, ==, 0);
578 qemu_bh_delete(data.bh);
579 g_assert_cmpint(data.n, ==, 0);
581 while (g_main_context_iteration(NULL, false));
582 g_assert_cmpint(data.n, ==, 0);
585 static void test_source_bh_delete_from_cb(void)
587 BHTestData data1 = { .n = 0, .max = 1 };
589 data1.bh = aio_bh_new(ctx, bh_delete_cb, &data1);
591 qemu_bh_schedule(data1.bh);
592 g_assert_cmpint(data1.n, ==, 0);
594 g_main_context_iteration(NULL, true);
595 g_assert_cmpint(data1.n, ==, data1.max);
596 g_assert(data1.bh == NULL);
598 g_assert(!g_main_context_iteration(NULL, false));
601 static void test_source_bh_delete_from_cb_many(void)
603 BHTestData data1 = { .n = 0, .max = 1 };
604 BHTestData data2 = { .n = 0, .max = 3 };
605 BHTestData data3 = { .n = 0, .max = 2 };
606 BHTestData data4 = { .n = 0, .max = 4 };
608 data1.bh = aio_bh_new(ctx, bh_delete_cb, &data1);
609 data2.bh = aio_bh_new(ctx, bh_delete_cb, &data2);
610 data3.bh = aio_bh_new(ctx, bh_delete_cb, &data3);
611 data4.bh = aio_bh_new(ctx, bh_delete_cb, &data4);
613 qemu_bh_schedule(data1.bh);
614 qemu_bh_schedule(data2.bh);
615 qemu_bh_schedule(data3.bh);
616 qemu_bh_schedule(data4.bh);
617 g_assert_cmpint(data1.n, ==, 0);
618 g_assert_cmpint(data2.n, ==, 0);
619 g_assert_cmpint(data3.n, ==, 0);
620 g_assert_cmpint(data4.n, ==, 0);
622 g_assert(g_main_context_iteration(NULL, false));
623 g_assert_cmpint(data1.n, ==, 1);
624 g_assert_cmpint(data2.n, ==, 1);
625 g_assert_cmpint(data3.n, ==, 1);
626 g_assert_cmpint(data4.n, ==, 1);
627 g_assert(data1.bh == NULL);
629 while (g_main_context_iteration(NULL, false));
630 g_assert_cmpint(data1.n, ==, data1.max);
631 g_assert_cmpint(data2.n, ==, data2.max);
632 g_assert_cmpint(data3.n, ==, data3.max);
633 g_assert_cmpint(data4.n, ==, data4.max);
634 g_assert(data1.bh == NULL);
635 g_assert(data2.bh == NULL);
636 g_assert(data3.bh == NULL);
637 g_assert(data4.bh == NULL);
640 static void test_source_bh_flush(void)
642 BHTestData data = { .n = 0 };
643 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
645 qemu_bh_schedule(data.bh);
646 g_assert_cmpint(data.n, ==, 0);
648 g_assert(g_main_context_iteration(NULL, true));
649 g_assert_cmpint(data.n, ==, 1);
651 g_assert(!g_main_context_iteration(NULL, false));
652 g_assert_cmpint(data.n, ==, 1);
653 qemu_bh_delete(data.bh);
656 static void test_source_set_event_notifier(void)
658 EventNotifierTestData data = { .n = 0, .active = 0 };
659 event_notifier_init(&data.e, false);
660 aio_set_event_notifier(ctx, &data.e, event_ready_cb);
661 while (g_main_context_iteration(NULL, false));
662 g_assert_cmpint(data.n, ==, 0);
664 aio_set_event_notifier(ctx, &data.e, NULL);
665 while (g_main_context_iteration(NULL, false));
666 g_assert_cmpint(data.n, ==, 0);
667 event_notifier_cleanup(&data.e);
670 static void test_source_wait_event_notifier(void)
672 EventNotifierTestData data = { .n = 0, .active = 1 };
673 event_notifier_init(&data.e, false);
674 aio_set_event_notifier(ctx, &data.e, event_ready_cb);
675 g_assert(g_main_context_iteration(NULL, false));
676 g_assert_cmpint(data.n, ==, 0);
677 g_assert_cmpint(data.active, ==, 1);
679 event_notifier_set(&data.e);
680 g_assert(g_main_context_iteration(NULL, false));
681 g_assert_cmpint(data.n, ==, 1);
682 g_assert_cmpint(data.active, ==, 0);
684 while (g_main_context_iteration(NULL, false));
685 g_assert_cmpint(data.n, ==, 1);
686 g_assert_cmpint(data.active, ==, 0);
688 aio_set_event_notifier(ctx, &data.e, NULL);
689 while (g_main_context_iteration(NULL, false));
690 g_assert_cmpint(data.n, ==, 1);
692 event_notifier_cleanup(&data.e);
695 static void test_source_flush_event_notifier(void)
697 EventNotifierTestData data = { .n = 0, .active = 10, .auto_set = true };
698 event_notifier_init(&data.e, false);
699 aio_set_event_notifier(ctx, &data.e, event_ready_cb);
700 g_assert(g_main_context_iteration(NULL, false));
701 g_assert_cmpint(data.n, ==, 0);
702 g_assert_cmpint(data.active, ==, 10);
704 event_notifier_set(&data.e);
705 g_assert(g_main_context_iteration(NULL, false));
706 g_assert_cmpint(data.n, ==, 1);
707 g_assert_cmpint(data.active, ==, 9);
708 g_assert(g_main_context_iteration(NULL, false));
710 while (g_main_context_iteration(NULL, false));
711 g_assert_cmpint(data.n, ==, 10);
712 g_assert_cmpint(data.active, ==, 0);
713 g_assert(!g_main_context_iteration(NULL, false));
715 aio_set_event_notifier(ctx, &data.e, NULL);
716 while (g_main_context_iteration(NULL, false));
717 event_notifier_cleanup(&data.e);
720 static void test_source_wait_event_notifier_noflush(void)
722 EventNotifierTestData data = { .n = 0 };
723 EventNotifierTestData dummy = { .n = 0, .active = 1 };
725 event_notifier_init(&data.e, false);
726 aio_set_event_notifier(ctx, &data.e, event_ready_cb);
728 while (g_main_context_iteration(NULL, false));
729 g_assert_cmpint(data.n, ==, 0);
731 /* Until there is an active descriptor, glib may or may not call
732 * event_ready_cb. Still, it must not block. */
733 event_notifier_set(&data.e);
734 g_main_context_iteration(NULL, true);
735 data.n = 0;
737 /* An active event notifier forces aio_poll to look at EventNotifiers. */
738 event_notifier_init(&dummy.e, false);
739 aio_set_event_notifier(ctx, &dummy.e, event_ready_cb);
741 event_notifier_set(&data.e);
742 g_assert(g_main_context_iteration(NULL, false));
743 g_assert_cmpint(data.n, ==, 1);
744 g_assert(!g_main_context_iteration(NULL, false));
745 g_assert_cmpint(data.n, ==, 1);
747 event_notifier_set(&data.e);
748 g_assert(g_main_context_iteration(NULL, false));
749 g_assert_cmpint(data.n, ==, 2);
750 g_assert(!g_main_context_iteration(NULL, false));
751 g_assert_cmpint(data.n, ==, 2);
753 event_notifier_set(&dummy.e);
754 while (g_main_context_iteration(NULL, false));
755 g_assert_cmpint(data.n, ==, 2);
756 g_assert_cmpint(dummy.n, ==, 1);
757 g_assert_cmpint(dummy.active, ==, 0);
759 aio_set_event_notifier(ctx, &dummy.e, NULL);
760 event_notifier_cleanup(&dummy.e);
762 aio_set_event_notifier(ctx, &data.e, NULL);
763 while (g_main_context_iteration(NULL, false));
764 g_assert_cmpint(data.n, ==, 2);
766 event_notifier_cleanup(&data.e);
769 static void test_source_timer_schedule(void)
771 TimerTestData data = { .n = 0, .ctx = ctx, .ns = SCALE_MS * 750LL,
772 .max = 2,
773 .clock_type = QEMU_CLOCK_VIRTUAL };
774 EventNotifier e;
775 int64_t expiry;
777 /* aio_poll will not block to wait for timers to complete unless it has
778 * an fd to wait on. Fixing this breaks other tests. So create a dummy one.
780 event_notifier_init(&e, false);
781 aio_set_event_notifier(ctx, &e, dummy_io_handler_read);
782 do {} while (g_main_context_iteration(NULL, false));
784 aio_timer_init(ctx, &data.timer, data.clock_type,
785 SCALE_NS, timer_test_cb, &data);
786 expiry = qemu_clock_get_ns(data.clock_type) +
787 data.ns;
788 timer_mod(&data.timer, expiry);
790 g_assert_cmpint(data.n, ==, 0);
792 g_usleep(1 * G_USEC_PER_SEC);
793 g_assert_cmpint(data.n, ==, 0);
795 g_assert(g_main_context_iteration(NULL, true));
796 g_assert_cmpint(data.n, ==, 1);
797 expiry += data.ns;
799 while (data.n < 2) {
800 g_main_context_iteration(NULL, true);
803 g_assert_cmpint(data.n, ==, 2);
804 g_assert(qemu_clock_get_ns(data.clock_type) > expiry);
806 aio_set_event_notifier(ctx, &e, NULL);
807 event_notifier_cleanup(&e);
809 timer_del(&data.timer);
813 /* End of tests. */
815 int main(int argc, char **argv)
817 Error *local_error = NULL;
818 GSource *src;
820 init_clocks();
822 ctx = aio_context_new(&local_error);
823 if (!ctx) {
824 error_report("Failed to create AIO Context: '%s'",
825 error_get_pretty(local_error));
826 error_free(local_error);
827 exit(1);
829 src = aio_get_g_source(ctx);
830 g_source_attach(src, NULL);
831 g_source_unref(src);
833 while (g_main_context_iteration(NULL, false));
835 g_test_init(&argc, &argv, NULL);
836 g_test_add_func("/aio/notify", test_notify);
837 g_test_add_func("/aio/acquire", test_acquire);
838 g_test_add_func("/aio/bh/schedule", test_bh_schedule);
839 g_test_add_func("/aio/bh/schedule10", test_bh_schedule10);
840 g_test_add_func("/aio/bh/cancel", test_bh_cancel);
841 g_test_add_func("/aio/bh/delete", test_bh_delete);
842 g_test_add_func("/aio/bh/callback-delete/one", test_bh_delete_from_cb);
843 g_test_add_func("/aio/bh/callback-delete/many", test_bh_delete_from_cb_many);
844 g_test_add_func("/aio/bh/flush", test_bh_flush);
845 g_test_add_func("/aio/event/add-remove", test_set_event_notifier);
846 g_test_add_func("/aio/event/wait", test_wait_event_notifier);
847 g_test_add_func("/aio/event/wait/no-flush-cb", test_wait_event_notifier_noflush);
848 g_test_add_func("/aio/event/flush", test_flush_event_notifier);
849 g_test_add_func("/aio/timer/schedule", test_timer_schedule);
851 g_test_add_func("/aio-gsource/notify", test_source_notify);
852 g_test_add_func("/aio-gsource/flush", test_source_flush);
853 g_test_add_func("/aio-gsource/bh/schedule", test_source_bh_schedule);
854 g_test_add_func("/aio-gsource/bh/schedule10", test_source_bh_schedule10);
855 g_test_add_func("/aio-gsource/bh/cancel", test_source_bh_cancel);
856 g_test_add_func("/aio-gsource/bh/delete", test_source_bh_delete);
857 g_test_add_func("/aio-gsource/bh/callback-delete/one", test_source_bh_delete_from_cb);
858 g_test_add_func("/aio-gsource/bh/callback-delete/many", test_source_bh_delete_from_cb_many);
859 g_test_add_func("/aio-gsource/bh/flush", test_source_bh_flush);
860 g_test_add_func("/aio-gsource/event/add-remove", test_source_set_event_notifier);
861 g_test_add_func("/aio-gsource/event/wait", test_source_wait_event_notifier);
862 g_test_add_func("/aio-gsource/event/wait/no-flush-cb", test_source_wait_event_notifier_noflush);
863 g_test_add_func("/aio-gsource/event/flush", test_source_flush_event_notifier);
864 g_test_add_func("/aio-gsource/timer/schedule", test_source_timer_schedule);
865 return g_test_run();