Merge remote-tracking branch 'remotes/kraxel/tags/pull-vnc-20150312-1' into staging
[qemu/qmp-unstable.git] / tests / test-aio.c
bloba7cb5c99152233b1d94480083f874faf8554eb68
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 bool thread_acquired;
111 } AcquireTestData;
113 static void *test_acquire_thread(void *opaque)
115 AcquireTestData *data = opaque;
117 /* Wait for other thread to let us start */
118 qemu_mutex_lock(&data->start_lock);
119 qemu_mutex_unlock(&data->start_lock);
121 aio_context_acquire(ctx);
122 aio_context_release(ctx);
124 data->thread_acquired = true; /* success, we got here */
126 return NULL;
129 static void dummy_notifier_read(EventNotifier *unused)
131 g_assert(false); /* should never be invoked */
134 static void test_acquire(void)
136 QemuThread thread;
137 EventNotifier notifier;
138 AcquireTestData data;
140 /* Dummy event notifier ensures aio_poll() will block */
141 event_notifier_init(&notifier, false);
142 aio_set_event_notifier(ctx, &notifier, dummy_notifier_read);
143 g_assert(!aio_poll(ctx, false)); /* consume aio_notify() */
145 qemu_mutex_init(&data.start_lock);
146 qemu_mutex_lock(&data.start_lock);
147 data.thread_acquired = false;
149 qemu_thread_create(&thread, "test_acquire_thread",
150 test_acquire_thread,
151 &data, QEMU_THREAD_JOINABLE);
153 /* Block in aio_poll(), let other thread kick us and acquire context */
154 aio_context_acquire(ctx);
155 qemu_mutex_unlock(&data.start_lock); /* let the thread run */
156 g_assert(!aio_poll(ctx, true));
157 aio_context_release(ctx);
159 qemu_thread_join(&thread);
160 aio_set_event_notifier(ctx, &notifier, NULL);
161 event_notifier_cleanup(&notifier);
163 g_assert(data.thread_acquired);
166 static void test_bh_schedule(void)
168 BHTestData data = { .n = 0 };
169 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
171 qemu_bh_schedule(data.bh);
172 g_assert_cmpint(data.n, ==, 0);
174 g_assert(aio_poll(ctx, true));
175 g_assert_cmpint(data.n, ==, 1);
177 g_assert(!aio_poll(ctx, false));
178 g_assert_cmpint(data.n, ==, 1);
179 qemu_bh_delete(data.bh);
182 static void test_bh_schedule10(void)
184 BHTestData data = { .n = 0, .max = 10 };
185 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
187 qemu_bh_schedule(data.bh);
188 g_assert_cmpint(data.n, ==, 0);
190 g_assert(aio_poll(ctx, false));
191 g_assert_cmpint(data.n, ==, 1);
193 g_assert(aio_poll(ctx, true));
194 g_assert_cmpint(data.n, ==, 2);
196 while (data.n < 10) {
197 aio_poll(ctx, true);
199 g_assert_cmpint(data.n, ==, 10);
201 g_assert(!aio_poll(ctx, false));
202 g_assert_cmpint(data.n, ==, 10);
203 qemu_bh_delete(data.bh);
206 static void test_bh_cancel(void)
208 BHTestData data = { .n = 0 };
209 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
211 qemu_bh_schedule(data.bh);
212 g_assert_cmpint(data.n, ==, 0);
214 qemu_bh_cancel(data.bh);
215 g_assert_cmpint(data.n, ==, 0);
217 g_assert(!aio_poll(ctx, false));
218 g_assert_cmpint(data.n, ==, 0);
219 qemu_bh_delete(data.bh);
222 static void test_bh_delete(void)
224 BHTestData data = { .n = 0 };
225 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
227 qemu_bh_schedule(data.bh);
228 g_assert_cmpint(data.n, ==, 0);
230 qemu_bh_delete(data.bh);
231 g_assert_cmpint(data.n, ==, 0);
233 g_assert(!aio_poll(ctx, false));
234 g_assert_cmpint(data.n, ==, 0);
237 static void test_bh_delete_from_cb(void)
239 BHTestData data1 = { .n = 0, .max = 1 };
241 data1.bh = aio_bh_new(ctx, bh_delete_cb, &data1);
243 qemu_bh_schedule(data1.bh);
244 g_assert_cmpint(data1.n, ==, 0);
246 while (data1.n < data1.max) {
247 aio_poll(ctx, true);
249 g_assert_cmpint(data1.n, ==, data1.max);
250 g_assert(data1.bh == NULL);
252 g_assert(!aio_poll(ctx, false));
255 static void test_bh_delete_from_cb_many(void)
257 BHTestData data1 = { .n = 0, .max = 1 };
258 BHTestData data2 = { .n = 0, .max = 3 };
259 BHTestData data3 = { .n = 0, .max = 2 };
260 BHTestData data4 = { .n = 0, .max = 4 };
262 data1.bh = aio_bh_new(ctx, bh_delete_cb, &data1);
263 data2.bh = aio_bh_new(ctx, bh_delete_cb, &data2);
264 data3.bh = aio_bh_new(ctx, bh_delete_cb, &data3);
265 data4.bh = aio_bh_new(ctx, bh_delete_cb, &data4);
267 qemu_bh_schedule(data1.bh);
268 qemu_bh_schedule(data2.bh);
269 qemu_bh_schedule(data3.bh);
270 qemu_bh_schedule(data4.bh);
271 g_assert_cmpint(data1.n, ==, 0);
272 g_assert_cmpint(data2.n, ==, 0);
273 g_assert_cmpint(data3.n, ==, 0);
274 g_assert_cmpint(data4.n, ==, 0);
276 g_assert(aio_poll(ctx, false));
277 g_assert_cmpint(data1.n, ==, 1);
278 g_assert_cmpint(data2.n, ==, 1);
279 g_assert_cmpint(data3.n, ==, 1);
280 g_assert_cmpint(data4.n, ==, 1);
281 g_assert(data1.bh == NULL);
283 while (data1.n < data1.max ||
284 data2.n < data2.max ||
285 data3.n < data3.max ||
286 data4.n < data4.max) {
287 aio_poll(ctx, true);
289 g_assert_cmpint(data1.n, ==, data1.max);
290 g_assert_cmpint(data2.n, ==, data2.max);
291 g_assert_cmpint(data3.n, ==, data3.max);
292 g_assert_cmpint(data4.n, ==, data4.max);
293 g_assert(data1.bh == NULL);
294 g_assert(data2.bh == NULL);
295 g_assert(data3.bh == NULL);
296 g_assert(data4.bh == NULL);
299 static void test_bh_flush(void)
301 BHTestData data = { .n = 0 };
302 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
304 qemu_bh_schedule(data.bh);
305 g_assert_cmpint(data.n, ==, 0);
307 g_assert(aio_poll(ctx, true));
308 g_assert_cmpint(data.n, ==, 1);
310 g_assert(!aio_poll(ctx, false));
311 g_assert_cmpint(data.n, ==, 1);
312 qemu_bh_delete(data.bh);
315 static void test_set_event_notifier(void)
317 EventNotifierTestData data = { .n = 0, .active = 0 };
318 event_notifier_init(&data.e, false);
319 aio_set_event_notifier(ctx, &data.e, event_ready_cb);
320 g_assert(!aio_poll(ctx, false));
321 g_assert_cmpint(data.n, ==, 0);
323 aio_set_event_notifier(ctx, &data.e, NULL);
324 g_assert(!aio_poll(ctx, false));
325 g_assert_cmpint(data.n, ==, 0);
326 event_notifier_cleanup(&data.e);
329 static void test_wait_event_notifier(void)
331 EventNotifierTestData data = { .n = 0, .active = 1 };
332 event_notifier_init(&data.e, false);
333 aio_set_event_notifier(ctx, &data.e, event_ready_cb);
334 g_assert(!aio_poll(ctx, false));
335 g_assert_cmpint(data.n, ==, 0);
336 g_assert_cmpint(data.active, ==, 1);
338 event_notifier_set(&data.e);
339 g_assert(aio_poll(ctx, false));
340 g_assert_cmpint(data.n, ==, 1);
341 g_assert_cmpint(data.active, ==, 0);
343 g_assert(!aio_poll(ctx, false));
344 g_assert_cmpint(data.n, ==, 1);
345 g_assert_cmpint(data.active, ==, 0);
347 aio_set_event_notifier(ctx, &data.e, NULL);
348 g_assert(!aio_poll(ctx, false));
349 g_assert_cmpint(data.n, ==, 1);
351 event_notifier_cleanup(&data.e);
354 static void test_flush_event_notifier(void)
356 EventNotifierTestData data = { .n = 0, .active = 10, .auto_set = true };
357 event_notifier_init(&data.e, false);
358 aio_set_event_notifier(ctx, &data.e, event_ready_cb);
359 g_assert(!aio_poll(ctx, false));
360 g_assert_cmpint(data.n, ==, 0);
361 g_assert_cmpint(data.active, ==, 10);
363 event_notifier_set(&data.e);
364 g_assert(aio_poll(ctx, false));
365 g_assert_cmpint(data.n, ==, 1);
366 g_assert_cmpint(data.active, ==, 9);
367 g_assert(aio_poll(ctx, false));
369 wait_until_inactive(&data);
370 g_assert_cmpint(data.n, ==, 10);
371 g_assert_cmpint(data.active, ==, 0);
372 g_assert(!aio_poll(ctx, false));
374 aio_set_event_notifier(ctx, &data.e, NULL);
375 g_assert(!aio_poll(ctx, false));
376 event_notifier_cleanup(&data.e);
379 static void test_wait_event_notifier_noflush(void)
381 EventNotifierTestData data = { .n = 0 };
382 EventNotifierTestData dummy = { .n = 0, .active = 1 };
384 event_notifier_init(&data.e, false);
385 aio_set_event_notifier(ctx, &data.e, event_ready_cb);
387 g_assert(!aio_poll(ctx, false));
388 g_assert_cmpint(data.n, ==, 0);
390 /* Until there is an active descriptor, aio_poll may or may not call
391 * event_ready_cb. Still, it must not block. */
392 event_notifier_set(&data.e);
393 g_assert(aio_poll(ctx, true));
394 data.n = 0;
396 /* An active event notifier forces aio_poll to look at EventNotifiers. */
397 event_notifier_init(&dummy.e, false);
398 aio_set_event_notifier(ctx, &dummy.e, event_ready_cb);
400 event_notifier_set(&data.e);
401 g_assert(aio_poll(ctx, false));
402 g_assert_cmpint(data.n, ==, 1);
403 g_assert(!aio_poll(ctx, false));
404 g_assert_cmpint(data.n, ==, 1);
406 event_notifier_set(&data.e);
407 g_assert(aio_poll(ctx, false));
408 g_assert_cmpint(data.n, ==, 2);
409 g_assert(!aio_poll(ctx, false));
410 g_assert_cmpint(data.n, ==, 2);
412 event_notifier_set(&dummy.e);
413 wait_until_inactive(&dummy);
414 g_assert_cmpint(data.n, ==, 2);
415 g_assert_cmpint(dummy.n, ==, 1);
416 g_assert_cmpint(dummy.active, ==, 0);
418 aio_set_event_notifier(ctx, &dummy.e, NULL);
419 event_notifier_cleanup(&dummy.e);
421 aio_set_event_notifier(ctx, &data.e, NULL);
422 g_assert(!aio_poll(ctx, false));
423 g_assert_cmpint(data.n, ==, 2);
425 event_notifier_cleanup(&data.e);
428 static void test_timer_schedule(void)
430 TimerTestData data = { .n = 0, .ctx = ctx, .ns = SCALE_MS * 750LL,
431 .max = 2,
432 .clock_type = QEMU_CLOCK_VIRTUAL };
433 EventNotifier e;
435 /* aio_poll will not block to wait for timers to complete unless it has
436 * an fd to wait on. Fixing this breaks other tests. So create a dummy one.
438 event_notifier_init(&e, false);
439 aio_set_event_notifier(ctx, &e, dummy_io_handler_read);
440 aio_poll(ctx, false);
442 aio_timer_init(ctx, &data.timer, data.clock_type,
443 SCALE_NS, timer_test_cb, &data);
444 timer_mod(&data.timer,
445 qemu_clock_get_ns(data.clock_type) +
446 data.ns);
448 g_assert_cmpint(data.n, ==, 0);
450 /* timer_mod may well cause an event notifer to have gone off,
451 * so clear that
453 do {} while (aio_poll(ctx, false));
455 g_assert(!aio_poll(ctx, false));
456 g_assert_cmpint(data.n, ==, 0);
458 g_usleep(1 * G_USEC_PER_SEC);
459 g_assert_cmpint(data.n, ==, 0);
461 g_assert(aio_poll(ctx, false));
462 g_assert_cmpint(data.n, ==, 1);
464 /* timer_mod called by our callback */
465 do {} while (aio_poll(ctx, false));
467 g_assert(!aio_poll(ctx, false));
468 g_assert_cmpint(data.n, ==, 1);
470 g_assert(aio_poll(ctx, true));
471 g_assert_cmpint(data.n, ==, 2);
473 /* As max is now 2, an event notifier should not have gone off */
475 g_assert(!aio_poll(ctx, false));
476 g_assert_cmpint(data.n, ==, 2);
478 aio_set_event_notifier(ctx, &e, NULL);
479 event_notifier_cleanup(&e);
481 timer_del(&data.timer);
484 /* Now the same tests, using the context as a GSource. They are
485 * very similar to the ones above, with g_main_context_iteration
486 * replacing aio_poll. However:
487 * - sometimes both the AioContext and the glib main loop wake
488 * themselves up. Hence, some "g_assert(!aio_poll(ctx, false));"
489 * are replaced by "while (g_main_context_iteration(NULL, false));".
490 * - there is no exact replacement for a blocking wait.
491 * "while (g_main_context_iteration(NULL, true)" seems to work,
492 * but it is not documented _why_ it works. For these tests a
493 * non-blocking loop like "while (g_main_context_iteration(NULL, false)"
494 * works well, and that's what I am using.
497 static void test_source_notify(void)
499 while (g_main_context_iteration(NULL, false));
500 aio_notify(ctx);
501 g_assert(g_main_context_iteration(NULL, true));
502 g_assert(!g_main_context_iteration(NULL, false));
505 static void test_source_flush(void)
507 g_assert(!g_main_context_iteration(NULL, false));
508 aio_notify(ctx);
509 while (g_main_context_iteration(NULL, false));
510 g_assert(!g_main_context_iteration(NULL, false));
513 static void test_source_bh_schedule(void)
515 BHTestData data = { .n = 0 };
516 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
518 qemu_bh_schedule(data.bh);
519 g_assert_cmpint(data.n, ==, 0);
521 g_assert(g_main_context_iteration(NULL, true));
522 g_assert_cmpint(data.n, ==, 1);
524 g_assert(!g_main_context_iteration(NULL, false));
525 g_assert_cmpint(data.n, ==, 1);
526 qemu_bh_delete(data.bh);
529 static void test_source_bh_schedule10(void)
531 BHTestData data = { .n = 0, .max = 10 };
532 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
534 qemu_bh_schedule(data.bh);
535 g_assert_cmpint(data.n, ==, 0);
537 g_assert(g_main_context_iteration(NULL, false));
538 g_assert_cmpint(data.n, ==, 1);
540 g_assert(g_main_context_iteration(NULL, true));
541 g_assert_cmpint(data.n, ==, 2);
543 while (g_main_context_iteration(NULL, false));
544 g_assert_cmpint(data.n, ==, 10);
546 g_assert(!g_main_context_iteration(NULL, false));
547 g_assert_cmpint(data.n, ==, 10);
548 qemu_bh_delete(data.bh);
551 static void test_source_bh_cancel(void)
553 BHTestData data = { .n = 0 };
554 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
556 qemu_bh_schedule(data.bh);
557 g_assert_cmpint(data.n, ==, 0);
559 qemu_bh_cancel(data.bh);
560 g_assert_cmpint(data.n, ==, 0);
562 while (g_main_context_iteration(NULL, false));
563 g_assert_cmpint(data.n, ==, 0);
564 qemu_bh_delete(data.bh);
567 static void test_source_bh_delete(void)
569 BHTestData data = { .n = 0 };
570 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
572 qemu_bh_schedule(data.bh);
573 g_assert_cmpint(data.n, ==, 0);
575 qemu_bh_delete(data.bh);
576 g_assert_cmpint(data.n, ==, 0);
578 while (g_main_context_iteration(NULL, false));
579 g_assert_cmpint(data.n, ==, 0);
582 static void test_source_bh_delete_from_cb(void)
584 BHTestData data1 = { .n = 0, .max = 1 };
586 data1.bh = aio_bh_new(ctx, bh_delete_cb, &data1);
588 qemu_bh_schedule(data1.bh);
589 g_assert_cmpint(data1.n, ==, 0);
591 g_main_context_iteration(NULL, true);
592 g_assert_cmpint(data1.n, ==, data1.max);
593 g_assert(data1.bh == NULL);
595 g_assert(!g_main_context_iteration(NULL, false));
598 static void test_source_bh_delete_from_cb_many(void)
600 BHTestData data1 = { .n = 0, .max = 1 };
601 BHTestData data2 = { .n = 0, .max = 3 };
602 BHTestData data3 = { .n = 0, .max = 2 };
603 BHTestData data4 = { .n = 0, .max = 4 };
605 data1.bh = aio_bh_new(ctx, bh_delete_cb, &data1);
606 data2.bh = aio_bh_new(ctx, bh_delete_cb, &data2);
607 data3.bh = aio_bh_new(ctx, bh_delete_cb, &data3);
608 data4.bh = aio_bh_new(ctx, bh_delete_cb, &data4);
610 qemu_bh_schedule(data1.bh);
611 qemu_bh_schedule(data2.bh);
612 qemu_bh_schedule(data3.bh);
613 qemu_bh_schedule(data4.bh);
614 g_assert_cmpint(data1.n, ==, 0);
615 g_assert_cmpint(data2.n, ==, 0);
616 g_assert_cmpint(data3.n, ==, 0);
617 g_assert_cmpint(data4.n, ==, 0);
619 g_assert(g_main_context_iteration(NULL, false));
620 g_assert_cmpint(data1.n, ==, 1);
621 g_assert_cmpint(data2.n, ==, 1);
622 g_assert_cmpint(data3.n, ==, 1);
623 g_assert_cmpint(data4.n, ==, 1);
624 g_assert(data1.bh == NULL);
626 while (g_main_context_iteration(NULL, false));
627 g_assert_cmpint(data1.n, ==, data1.max);
628 g_assert_cmpint(data2.n, ==, data2.max);
629 g_assert_cmpint(data3.n, ==, data3.max);
630 g_assert_cmpint(data4.n, ==, data4.max);
631 g_assert(data1.bh == NULL);
632 g_assert(data2.bh == NULL);
633 g_assert(data3.bh == NULL);
634 g_assert(data4.bh == NULL);
637 static void test_source_bh_flush(void)
639 BHTestData data = { .n = 0 };
640 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
642 qemu_bh_schedule(data.bh);
643 g_assert_cmpint(data.n, ==, 0);
645 g_assert(g_main_context_iteration(NULL, true));
646 g_assert_cmpint(data.n, ==, 1);
648 g_assert(!g_main_context_iteration(NULL, false));
649 g_assert_cmpint(data.n, ==, 1);
650 qemu_bh_delete(data.bh);
653 static void test_source_set_event_notifier(void)
655 EventNotifierTestData data = { .n = 0, .active = 0 };
656 event_notifier_init(&data.e, false);
657 aio_set_event_notifier(ctx, &data.e, event_ready_cb);
658 while (g_main_context_iteration(NULL, false));
659 g_assert_cmpint(data.n, ==, 0);
661 aio_set_event_notifier(ctx, &data.e, NULL);
662 while (g_main_context_iteration(NULL, false));
663 g_assert_cmpint(data.n, ==, 0);
664 event_notifier_cleanup(&data.e);
667 static void test_source_wait_event_notifier(void)
669 EventNotifierTestData data = { .n = 0, .active = 1 };
670 event_notifier_init(&data.e, false);
671 aio_set_event_notifier(ctx, &data.e, event_ready_cb);
672 g_assert(g_main_context_iteration(NULL, false));
673 g_assert_cmpint(data.n, ==, 0);
674 g_assert_cmpint(data.active, ==, 1);
676 event_notifier_set(&data.e);
677 g_assert(g_main_context_iteration(NULL, false));
678 g_assert_cmpint(data.n, ==, 1);
679 g_assert_cmpint(data.active, ==, 0);
681 while (g_main_context_iteration(NULL, false));
682 g_assert_cmpint(data.n, ==, 1);
683 g_assert_cmpint(data.active, ==, 0);
685 aio_set_event_notifier(ctx, &data.e, NULL);
686 while (g_main_context_iteration(NULL, false));
687 g_assert_cmpint(data.n, ==, 1);
689 event_notifier_cleanup(&data.e);
692 static void test_source_flush_event_notifier(void)
694 EventNotifierTestData data = { .n = 0, .active = 10, .auto_set = true };
695 event_notifier_init(&data.e, false);
696 aio_set_event_notifier(ctx, &data.e, event_ready_cb);
697 g_assert(g_main_context_iteration(NULL, false));
698 g_assert_cmpint(data.n, ==, 0);
699 g_assert_cmpint(data.active, ==, 10);
701 event_notifier_set(&data.e);
702 g_assert(g_main_context_iteration(NULL, false));
703 g_assert_cmpint(data.n, ==, 1);
704 g_assert_cmpint(data.active, ==, 9);
705 g_assert(g_main_context_iteration(NULL, false));
707 while (g_main_context_iteration(NULL, false));
708 g_assert_cmpint(data.n, ==, 10);
709 g_assert_cmpint(data.active, ==, 0);
710 g_assert(!g_main_context_iteration(NULL, false));
712 aio_set_event_notifier(ctx, &data.e, NULL);
713 while (g_main_context_iteration(NULL, false));
714 event_notifier_cleanup(&data.e);
717 static void test_source_wait_event_notifier_noflush(void)
719 EventNotifierTestData data = { .n = 0 };
720 EventNotifierTestData dummy = { .n = 0, .active = 1 };
722 event_notifier_init(&data.e, false);
723 aio_set_event_notifier(ctx, &data.e, event_ready_cb);
725 while (g_main_context_iteration(NULL, false));
726 g_assert_cmpint(data.n, ==, 0);
728 /* Until there is an active descriptor, glib may or may not call
729 * event_ready_cb. Still, it must not block. */
730 event_notifier_set(&data.e);
731 g_main_context_iteration(NULL, true);
732 data.n = 0;
734 /* An active event notifier forces aio_poll to look at EventNotifiers. */
735 event_notifier_init(&dummy.e, false);
736 aio_set_event_notifier(ctx, &dummy.e, event_ready_cb);
738 event_notifier_set(&data.e);
739 g_assert(g_main_context_iteration(NULL, false));
740 g_assert_cmpint(data.n, ==, 1);
741 g_assert(!g_main_context_iteration(NULL, false));
742 g_assert_cmpint(data.n, ==, 1);
744 event_notifier_set(&data.e);
745 g_assert(g_main_context_iteration(NULL, false));
746 g_assert_cmpint(data.n, ==, 2);
747 g_assert(!g_main_context_iteration(NULL, false));
748 g_assert_cmpint(data.n, ==, 2);
750 event_notifier_set(&dummy.e);
751 while (g_main_context_iteration(NULL, false));
752 g_assert_cmpint(data.n, ==, 2);
753 g_assert_cmpint(dummy.n, ==, 1);
754 g_assert_cmpint(dummy.active, ==, 0);
756 aio_set_event_notifier(ctx, &dummy.e, NULL);
757 event_notifier_cleanup(&dummy.e);
759 aio_set_event_notifier(ctx, &data.e, NULL);
760 while (g_main_context_iteration(NULL, false));
761 g_assert_cmpint(data.n, ==, 2);
763 event_notifier_cleanup(&data.e);
766 static void test_source_timer_schedule(void)
768 TimerTestData data = { .n = 0, .ctx = ctx, .ns = SCALE_MS * 750LL,
769 .max = 2,
770 .clock_type = QEMU_CLOCK_VIRTUAL };
771 EventNotifier e;
772 int64_t expiry;
774 /* aio_poll will not block to wait for timers to complete unless it has
775 * an fd to wait on. Fixing this breaks other tests. So create a dummy one.
777 event_notifier_init(&e, false);
778 aio_set_event_notifier(ctx, &e, dummy_io_handler_read);
779 do {} while (g_main_context_iteration(NULL, false));
781 aio_timer_init(ctx, &data.timer, data.clock_type,
782 SCALE_NS, timer_test_cb, &data);
783 expiry = qemu_clock_get_ns(data.clock_type) +
784 data.ns;
785 timer_mod(&data.timer, expiry);
787 g_assert_cmpint(data.n, ==, 0);
789 g_usleep(1 * G_USEC_PER_SEC);
790 g_assert_cmpint(data.n, ==, 0);
792 g_assert(g_main_context_iteration(NULL, true));
793 g_assert_cmpint(data.n, ==, 1);
794 expiry += data.ns;
796 while (data.n < 2) {
797 g_main_context_iteration(NULL, true);
800 g_assert_cmpint(data.n, ==, 2);
801 g_assert(qemu_clock_get_ns(data.clock_type) > expiry);
803 aio_set_event_notifier(ctx, &e, NULL);
804 event_notifier_cleanup(&e);
806 timer_del(&data.timer);
810 /* End of tests. */
812 int main(int argc, char **argv)
814 Error *local_error = NULL;
815 GSource *src;
817 init_clocks();
819 ctx = aio_context_new(&local_error);
820 if (!ctx) {
821 error_report("Failed to create AIO Context: '%s'",
822 error_get_pretty(local_error));
823 error_free(local_error);
824 exit(1);
826 src = aio_get_g_source(ctx);
827 g_source_attach(src, NULL);
828 g_source_unref(src);
830 while (g_main_context_iteration(NULL, false));
832 g_test_init(&argc, &argv, NULL);
833 g_test_add_func("/aio/notify", test_notify);
834 g_test_add_func("/aio/acquire", test_acquire);
835 g_test_add_func("/aio/bh/schedule", test_bh_schedule);
836 g_test_add_func("/aio/bh/schedule10", test_bh_schedule10);
837 g_test_add_func("/aio/bh/cancel", test_bh_cancel);
838 g_test_add_func("/aio/bh/delete", test_bh_delete);
839 g_test_add_func("/aio/bh/callback-delete/one", test_bh_delete_from_cb);
840 g_test_add_func("/aio/bh/callback-delete/many", test_bh_delete_from_cb_many);
841 g_test_add_func("/aio/bh/flush", test_bh_flush);
842 g_test_add_func("/aio/event/add-remove", test_set_event_notifier);
843 g_test_add_func("/aio/event/wait", test_wait_event_notifier);
844 g_test_add_func("/aio/event/wait/no-flush-cb", test_wait_event_notifier_noflush);
845 g_test_add_func("/aio/event/flush", test_flush_event_notifier);
846 g_test_add_func("/aio/timer/schedule", test_timer_schedule);
848 g_test_add_func("/aio-gsource/notify", test_source_notify);
849 g_test_add_func("/aio-gsource/flush", test_source_flush);
850 g_test_add_func("/aio-gsource/bh/schedule", test_source_bh_schedule);
851 g_test_add_func("/aio-gsource/bh/schedule10", test_source_bh_schedule10);
852 g_test_add_func("/aio-gsource/bh/cancel", test_source_bh_cancel);
853 g_test_add_func("/aio-gsource/bh/delete", test_source_bh_delete);
854 g_test_add_func("/aio-gsource/bh/callback-delete/one", test_source_bh_delete_from_cb);
855 g_test_add_func("/aio-gsource/bh/callback-delete/many", test_source_bh_delete_from_cb_many);
856 g_test_add_func("/aio-gsource/bh/flush", test_source_bh_flush);
857 g_test_add_func("/aio-gsource/event/add-remove", test_source_set_event_notifier);
858 g_test_add_func("/aio-gsource/event/wait", test_source_wait_event_notifier);
859 g_test_add_func("/aio-gsource/event/wait/no-flush-cb", test_source_wait_event_notifier_noflush);
860 g_test_add_func("/aio-gsource/event/flush", test_source_flush_event_notifier);
861 g_test_add_func("/aio-gsource/timer/schedule", test_source_timer_schedule);
862 return g_test_run();