nbd: Mark fd handlers client type as "external"
[qemu/ar7.git] / tests / test-aio.c
blob03cd45d82b923f2debfc38304f65e17ff3e3f582
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 typedef struct {
101 QemuMutex start_lock;
102 bool thread_acquired;
103 } AcquireTestData;
105 static void *test_acquire_thread(void *opaque)
107 AcquireTestData *data = opaque;
109 /* Wait for other thread to let us start */
110 qemu_mutex_lock(&data->start_lock);
111 qemu_mutex_unlock(&data->start_lock);
113 aio_context_acquire(ctx);
114 aio_context_release(ctx);
116 data->thread_acquired = true; /* success, we got here */
118 return NULL;
121 static void set_event_notifier(AioContext *ctx, EventNotifier *notifier,
122 EventNotifierHandler *handler)
124 aio_set_event_notifier(ctx, notifier, false, handler);
127 static void dummy_notifier_read(EventNotifier *unused)
129 g_assert(false); /* should never be invoked */
132 static void test_acquire(void)
134 QemuThread thread;
135 EventNotifier notifier;
136 AcquireTestData data;
138 /* Dummy event notifier ensures aio_poll() will block */
139 event_notifier_init(&notifier, false);
140 set_event_notifier(ctx, &notifier, dummy_notifier_read);
141 g_assert(!aio_poll(ctx, false)); /* consume aio_notify() */
143 qemu_mutex_init(&data.start_lock);
144 qemu_mutex_lock(&data.start_lock);
145 data.thread_acquired = false;
147 qemu_thread_create(&thread, "test_acquire_thread",
148 test_acquire_thread,
149 &data, QEMU_THREAD_JOINABLE);
151 /* Block in aio_poll(), let other thread kick us and acquire context */
152 aio_context_acquire(ctx);
153 qemu_mutex_unlock(&data.start_lock); /* let the thread run */
154 g_assert(!aio_poll(ctx, true));
155 aio_context_release(ctx);
157 qemu_thread_join(&thread);
158 set_event_notifier(ctx, &notifier, NULL);
159 event_notifier_cleanup(&notifier);
161 g_assert(data.thread_acquired);
164 static void test_bh_schedule(void)
166 BHTestData data = { .n = 0 };
167 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
169 qemu_bh_schedule(data.bh);
170 g_assert_cmpint(data.n, ==, 0);
172 g_assert(aio_poll(ctx, true));
173 g_assert_cmpint(data.n, ==, 1);
175 g_assert(!aio_poll(ctx, false));
176 g_assert_cmpint(data.n, ==, 1);
177 qemu_bh_delete(data.bh);
180 static void test_bh_schedule10(void)
182 BHTestData data = { .n = 0, .max = 10 };
183 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
185 qemu_bh_schedule(data.bh);
186 g_assert_cmpint(data.n, ==, 0);
188 g_assert(aio_poll(ctx, false));
189 g_assert_cmpint(data.n, ==, 1);
191 g_assert(aio_poll(ctx, true));
192 g_assert_cmpint(data.n, ==, 2);
194 while (data.n < 10) {
195 aio_poll(ctx, true);
197 g_assert_cmpint(data.n, ==, 10);
199 g_assert(!aio_poll(ctx, false));
200 g_assert_cmpint(data.n, ==, 10);
201 qemu_bh_delete(data.bh);
204 static void test_bh_cancel(void)
206 BHTestData data = { .n = 0 };
207 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
209 qemu_bh_schedule(data.bh);
210 g_assert_cmpint(data.n, ==, 0);
212 qemu_bh_cancel(data.bh);
213 g_assert_cmpint(data.n, ==, 0);
215 g_assert(!aio_poll(ctx, false));
216 g_assert_cmpint(data.n, ==, 0);
217 qemu_bh_delete(data.bh);
220 static void test_bh_delete(void)
222 BHTestData data = { .n = 0 };
223 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
225 qemu_bh_schedule(data.bh);
226 g_assert_cmpint(data.n, ==, 0);
228 qemu_bh_delete(data.bh);
229 g_assert_cmpint(data.n, ==, 0);
231 g_assert(!aio_poll(ctx, false));
232 g_assert_cmpint(data.n, ==, 0);
235 static void test_bh_delete_from_cb(void)
237 BHTestData data1 = { .n = 0, .max = 1 };
239 data1.bh = aio_bh_new(ctx, bh_delete_cb, &data1);
241 qemu_bh_schedule(data1.bh);
242 g_assert_cmpint(data1.n, ==, 0);
244 while (data1.n < data1.max) {
245 aio_poll(ctx, true);
247 g_assert_cmpint(data1.n, ==, data1.max);
248 g_assert(data1.bh == NULL);
250 g_assert(!aio_poll(ctx, false));
253 static void test_bh_delete_from_cb_many(void)
255 BHTestData data1 = { .n = 0, .max = 1 };
256 BHTestData data2 = { .n = 0, .max = 3 };
257 BHTestData data3 = { .n = 0, .max = 2 };
258 BHTestData data4 = { .n = 0, .max = 4 };
260 data1.bh = aio_bh_new(ctx, bh_delete_cb, &data1);
261 data2.bh = aio_bh_new(ctx, bh_delete_cb, &data2);
262 data3.bh = aio_bh_new(ctx, bh_delete_cb, &data3);
263 data4.bh = aio_bh_new(ctx, bh_delete_cb, &data4);
265 qemu_bh_schedule(data1.bh);
266 qemu_bh_schedule(data2.bh);
267 qemu_bh_schedule(data3.bh);
268 qemu_bh_schedule(data4.bh);
269 g_assert_cmpint(data1.n, ==, 0);
270 g_assert_cmpint(data2.n, ==, 0);
271 g_assert_cmpint(data3.n, ==, 0);
272 g_assert_cmpint(data4.n, ==, 0);
274 g_assert(aio_poll(ctx, false));
275 g_assert_cmpint(data1.n, ==, 1);
276 g_assert_cmpint(data2.n, ==, 1);
277 g_assert_cmpint(data3.n, ==, 1);
278 g_assert_cmpint(data4.n, ==, 1);
279 g_assert(data1.bh == NULL);
281 while (data1.n < data1.max ||
282 data2.n < data2.max ||
283 data3.n < data3.max ||
284 data4.n < data4.max) {
285 aio_poll(ctx, true);
287 g_assert_cmpint(data1.n, ==, data1.max);
288 g_assert_cmpint(data2.n, ==, data2.max);
289 g_assert_cmpint(data3.n, ==, data3.max);
290 g_assert_cmpint(data4.n, ==, data4.max);
291 g_assert(data1.bh == NULL);
292 g_assert(data2.bh == NULL);
293 g_assert(data3.bh == NULL);
294 g_assert(data4.bh == NULL);
297 static void test_bh_flush(void)
299 BHTestData data = { .n = 0 };
300 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
302 qemu_bh_schedule(data.bh);
303 g_assert_cmpint(data.n, ==, 0);
305 g_assert(aio_poll(ctx, true));
306 g_assert_cmpint(data.n, ==, 1);
308 g_assert(!aio_poll(ctx, false));
309 g_assert_cmpint(data.n, ==, 1);
310 qemu_bh_delete(data.bh);
313 static void test_set_event_notifier(void)
315 EventNotifierTestData data = { .n = 0, .active = 0 };
316 event_notifier_init(&data.e, false);
317 set_event_notifier(ctx, &data.e, event_ready_cb);
318 g_assert(!aio_poll(ctx, false));
319 g_assert_cmpint(data.n, ==, 0);
321 set_event_notifier(ctx, &data.e, NULL);
322 g_assert(!aio_poll(ctx, false));
323 g_assert_cmpint(data.n, ==, 0);
324 event_notifier_cleanup(&data.e);
327 static void test_wait_event_notifier(void)
329 EventNotifierTestData data = { .n = 0, .active = 1 };
330 event_notifier_init(&data.e, false);
331 set_event_notifier(ctx, &data.e, event_ready_cb);
332 while (aio_poll(ctx, false));
333 g_assert_cmpint(data.n, ==, 0);
334 g_assert_cmpint(data.active, ==, 1);
336 event_notifier_set(&data.e);
337 g_assert(aio_poll(ctx, false));
338 g_assert_cmpint(data.n, ==, 1);
339 g_assert_cmpint(data.active, ==, 0);
341 g_assert(!aio_poll(ctx, false));
342 g_assert_cmpint(data.n, ==, 1);
343 g_assert_cmpint(data.active, ==, 0);
345 set_event_notifier(ctx, &data.e, NULL);
346 g_assert(!aio_poll(ctx, false));
347 g_assert_cmpint(data.n, ==, 1);
349 event_notifier_cleanup(&data.e);
352 static void test_flush_event_notifier(void)
354 EventNotifierTestData data = { .n = 0, .active = 10, .auto_set = true };
355 event_notifier_init(&data.e, false);
356 set_event_notifier(ctx, &data.e, event_ready_cb);
357 while (aio_poll(ctx, false));
358 g_assert_cmpint(data.n, ==, 0);
359 g_assert_cmpint(data.active, ==, 10);
361 event_notifier_set(&data.e);
362 g_assert(aio_poll(ctx, false));
363 g_assert_cmpint(data.n, ==, 1);
364 g_assert_cmpint(data.active, ==, 9);
365 g_assert(aio_poll(ctx, false));
367 wait_until_inactive(&data);
368 g_assert_cmpint(data.n, ==, 10);
369 g_assert_cmpint(data.active, ==, 0);
370 g_assert(!aio_poll(ctx, false));
372 set_event_notifier(ctx, &data.e, NULL);
373 g_assert(!aio_poll(ctx, false));
374 event_notifier_cleanup(&data.e);
377 static void test_wait_event_notifier_noflush(void)
379 EventNotifierTestData data = { .n = 0 };
380 EventNotifierTestData dummy = { .n = 0, .active = 1 };
382 event_notifier_init(&data.e, false);
383 set_event_notifier(ctx, &data.e, event_ready_cb);
385 g_assert(!aio_poll(ctx, false));
386 g_assert_cmpint(data.n, ==, 0);
388 /* Until there is an active descriptor, aio_poll may or may not call
389 * event_ready_cb. Still, it must not block. */
390 event_notifier_set(&data.e);
391 g_assert(aio_poll(ctx, true));
392 data.n = 0;
394 /* An active event notifier forces aio_poll to look at EventNotifiers. */
395 event_notifier_init(&dummy.e, false);
396 set_event_notifier(ctx, &dummy.e, event_ready_cb);
398 event_notifier_set(&data.e);
399 g_assert(aio_poll(ctx, false));
400 g_assert_cmpint(data.n, ==, 1);
401 g_assert(!aio_poll(ctx, false));
402 g_assert_cmpint(data.n, ==, 1);
404 event_notifier_set(&data.e);
405 g_assert(aio_poll(ctx, false));
406 g_assert_cmpint(data.n, ==, 2);
407 g_assert(!aio_poll(ctx, false));
408 g_assert_cmpint(data.n, ==, 2);
410 event_notifier_set(&dummy.e);
411 wait_until_inactive(&dummy);
412 g_assert_cmpint(data.n, ==, 2);
413 g_assert_cmpint(dummy.n, ==, 1);
414 g_assert_cmpint(dummy.active, ==, 0);
416 set_event_notifier(ctx, &dummy.e, NULL);
417 event_notifier_cleanup(&dummy.e);
419 set_event_notifier(ctx, &data.e, NULL);
420 g_assert(!aio_poll(ctx, false));
421 g_assert_cmpint(data.n, ==, 2);
423 event_notifier_cleanup(&data.e);
426 static void test_timer_schedule(void)
428 TimerTestData data = { .n = 0, .ctx = ctx, .ns = SCALE_MS * 750LL,
429 .max = 2,
430 .clock_type = QEMU_CLOCK_VIRTUAL };
431 EventNotifier e;
433 /* aio_poll will not block to wait for timers to complete unless it has
434 * an fd to wait on. Fixing this breaks other tests. So create a dummy one.
436 event_notifier_init(&e, false);
437 set_event_notifier(ctx, &e, dummy_io_handler_read);
438 aio_poll(ctx, false);
440 aio_timer_init(ctx, &data.timer, data.clock_type,
441 SCALE_NS, timer_test_cb, &data);
442 timer_mod(&data.timer,
443 qemu_clock_get_ns(data.clock_type) +
444 data.ns);
446 g_assert_cmpint(data.n, ==, 0);
448 /* timer_mod may well cause an event notifer to have gone off,
449 * so clear that
451 do {} while (aio_poll(ctx, false));
453 g_assert(!aio_poll(ctx, false));
454 g_assert_cmpint(data.n, ==, 0);
456 g_usleep(1 * G_USEC_PER_SEC);
457 g_assert_cmpint(data.n, ==, 0);
459 g_assert(aio_poll(ctx, false));
460 g_assert_cmpint(data.n, ==, 1);
462 /* timer_mod called by our callback */
463 do {} while (aio_poll(ctx, false));
465 g_assert(!aio_poll(ctx, false));
466 g_assert_cmpint(data.n, ==, 1);
468 g_assert(aio_poll(ctx, true));
469 g_assert_cmpint(data.n, ==, 2);
471 /* As max is now 2, an event notifier should not have gone off */
473 g_assert(!aio_poll(ctx, false));
474 g_assert_cmpint(data.n, ==, 2);
476 set_event_notifier(ctx, &e, NULL);
477 event_notifier_cleanup(&e);
479 timer_del(&data.timer);
482 /* Now the same tests, using the context as a GSource. They are
483 * very similar to the ones above, with g_main_context_iteration
484 * replacing aio_poll. However:
485 * - sometimes both the AioContext and the glib main loop wake
486 * themselves up. Hence, some "g_assert(!aio_poll(ctx, false));"
487 * are replaced by "while (g_main_context_iteration(NULL, false));".
488 * - there is no exact replacement for a blocking wait.
489 * "while (g_main_context_iteration(NULL, true)" seems to work,
490 * but it is not documented _why_ it works. For these tests a
491 * non-blocking loop like "while (g_main_context_iteration(NULL, false)"
492 * works well, and that's what I am using.
495 static void test_source_flush(void)
497 g_assert(!g_main_context_iteration(NULL, false));
498 aio_notify(ctx);
499 while (g_main_context_iteration(NULL, false));
500 g_assert(!g_main_context_iteration(NULL, false));
503 static void test_source_bh_schedule(void)
505 BHTestData data = { .n = 0 };
506 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
508 qemu_bh_schedule(data.bh);
509 g_assert_cmpint(data.n, ==, 0);
511 g_assert(g_main_context_iteration(NULL, true));
512 g_assert_cmpint(data.n, ==, 1);
514 g_assert(!g_main_context_iteration(NULL, false));
515 g_assert_cmpint(data.n, ==, 1);
516 qemu_bh_delete(data.bh);
519 static void test_source_bh_schedule10(void)
521 BHTestData data = { .n = 0, .max = 10 };
522 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
524 qemu_bh_schedule(data.bh);
525 g_assert_cmpint(data.n, ==, 0);
527 g_assert(g_main_context_iteration(NULL, false));
528 g_assert_cmpint(data.n, ==, 1);
530 g_assert(g_main_context_iteration(NULL, true));
531 g_assert_cmpint(data.n, ==, 2);
533 while (g_main_context_iteration(NULL, false));
534 g_assert_cmpint(data.n, ==, 10);
536 g_assert(!g_main_context_iteration(NULL, false));
537 g_assert_cmpint(data.n, ==, 10);
538 qemu_bh_delete(data.bh);
541 static void test_source_bh_cancel(void)
543 BHTestData data = { .n = 0 };
544 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
546 qemu_bh_schedule(data.bh);
547 g_assert_cmpint(data.n, ==, 0);
549 qemu_bh_cancel(data.bh);
550 g_assert_cmpint(data.n, ==, 0);
552 while (g_main_context_iteration(NULL, false));
553 g_assert_cmpint(data.n, ==, 0);
554 qemu_bh_delete(data.bh);
557 static void test_source_bh_delete(void)
559 BHTestData data = { .n = 0 };
560 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
562 qemu_bh_schedule(data.bh);
563 g_assert_cmpint(data.n, ==, 0);
565 qemu_bh_delete(data.bh);
566 g_assert_cmpint(data.n, ==, 0);
568 while (g_main_context_iteration(NULL, false));
569 g_assert_cmpint(data.n, ==, 0);
572 static void test_source_bh_delete_from_cb(void)
574 BHTestData data1 = { .n = 0, .max = 1 };
576 data1.bh = aio_bh_new(ctx, bh_delete_cb, &data1);
578 qemu_bh_schedule(data1.bh);
579 g_assert_cmpint(data1.n, ==, 0);
581 g_main_context_iteration(NULL, true);
582 g_assert_cmpint(data1.n, ==, data1.max);
583 g_assert(data1.bh == NULL);
585 g_assert(!g_main_context_iteration(NULL, false));
588 static void test_source_bh_delete_from_cb_many(void)
590 BHTestData data1 = { .n = 0, .max = 1 };
591 BHTestData data2 = { .n = 0, .max = 3 };
592 BHTestData data3 = { .n = 0, .max = 2 };
593 BHTestData data4 = { .n = 0, .max = 4 };
595 data1.bh = aio_bh_new(ctx, bh_delete_cb, &data1);
596 data2.bh = aio_bh_new(ctx, bh_delete_cb, &data2);
597 data3.bh = aio_bh_new(ctx, bh_delete_cb, &data3);
598 data4.bh = aio_bh_new(ctx, bh_delete_cb, &data4);
600 qemu_bh_schedule(data1.bh);
601 qemu_bh_schedule(data2.bh);
602 qemu_bh_schedule(data3.bh);
603 qemu_bh_schedule(data4.bh);
604 g_assert_cmpint(data1.n, ==, 0);
605 g_assert_cmpint(data2.n, ==, 0);
606 g_assert_cmpint(data3.n, ==, 0);
607 g_assert_cmpint(data4.n, ==, 0);
609 g_assert(g_main_context_iteration(NULL, false));
610 g_assert_cmpint(data1.n, ==, 1);
611 g_assert_cmpint(data2.n, ==, 1);
612 g_assert_cmpint(data3.n, ==, 1);
613 g_assert_cmpint(data4.n, ==, 1);
614 g_assert(data1.bh == NULL);
616 while (g_main_context_iteration(NULL, false));
617 g_assert_cmpint(data1.n, ==, data1.max);
618 g_assert_cmpint(data2.n, ==, data2.max);
619 g_assert_cmpint(data3.n, ==, data3.max);
620 g_assert_cmpint(data4.n, ==, data4.max);
621 g_assert(data1.bh == NULL);
622 g_assert(data2.bh == NULL);
623 g_assert(data3.bh == NULL);
624 g_assert(data4.bh == NULL);
627 static void test_source_bh_flush(void)
629 BHTestData data = { .n = 0 };
630 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
632 qemu_bh_schedule(data.bh);
633 g_assert_cmpint(data.n, ==, 0);
635 g_assert(g_main_context_iteration(NULL, true));
636 g_assert_cmpint(data.n, ==, 1);
638 g_assert(!g_main_context_iteration(NULL, false));
639 g_assert_cmpint(data.n, ==, 1);
640 qemu_bh_delete(data.bh);
643 static void test_source_set_event_notifier(void)
645 EventNotifierTestData data = { .n = 0, .active = 0 };
646 event_notifier_init(&data.e, false);
647 set_event_notifier(ctx, &data.e, event_ready_cb);
648 while (g_main_context_iteration(NULL, false));
649 g_assert_cmpint(data.n, ==, 0);
651 set_event_notifier(ctx, &data.e, NULL);
652 while (g_main_context_iteration(NULL, false));
653 g_assert_cmpint(data.n, ==, 0);
654 event_notifier_cleanup(&data.e);
657 static void test_source_wait_event_notifier(void)
659 EventNotifierTestData data = { .n = 0, .active = 1 };
660 event_notifier_init(&data.e, false);
661 set_event_notifier(ctx, &data.e, event_ready_cb);
662 while (g_main_context_iteration(NULL, false));
663 g_assert_cmpint(data.n, ==, 0);
664 g_assert_cmpint(data.active, ==, 1);
666 event_notifier_set(&data.e);
667 g_assert(g_main_context_iteration(NULL, false));
668 g_assert_cmpint(data.n, ==, 1);
669 g_assert_cmpint(data.active, ==, 0);
671 while (g_main_context_iteration(NULL, false));
672 g_assert_cmpint(data.n, ==, 1);
673 g_assert_cmpint(data.active, ==, 0);
675 set_event_notifier(ctx, &data.e, NULL);
676 while (g_main_context_iteration(NULL, false));
677 g_assert_cmpint(data.n, ==, 1);
679 event_notifier_cleanup(&data.e);
682 static void test_source_flush_event_notifier(void)
684 EventNotifierTestData data = { .n = 0, .active = 10, .auto_set = true };
685 event_notifier_init(&data.e, false);
686 set_event_notifier(ctx, &data.e, event_ready_cb);
687 while (g_main_context_iteration(NULL, false));
688 g_assert_cmpint(data.n, ==, 0);
689 g_assert_cmpint(data.active, ==, 10);
691 event_notifier_set(&data.e);
692 g_assert(g_main_context_iteration(NULL, false));
693 g_assert_cmpint(data.n, ==, 1);
694 g_assert_cmpint(data.active, ==, 9);
695 g_assert(g_main_context_iteration(NULL, false));
697 while (g_main_context_iteration(NULL, false));
698 g_assert_cmpint(data.n, ==, 10);
699 g_assert_cmpint(data.active, ==, 0);
700 g_assert(!g_main_context_iteration(NULL, false));
702 set_event_notifier(ctx, &data.e, NULL);
703 while (g_main_context_iteration(NULL, false));
704 event_notifier_cleanup(&data.e);
707 static void test_source_wait_event_notifier_noflush(void)
709 EventNotifierTestData data = { .n = 0 };
710 EventNotifierTestData dummy = { .n = 0, .active = 1 };
712 event_notifier_init(&data.e, false);
713 set_event_notifier(ctx, &data.e, event_ready_cb);
715 while (g_main_context_iteration(NULL, false));
716 g_assert_cmpint(data.n, ==, 0);
718 /* Until there is an active descriptor, glib may or may not call
719 * event_ready_cb. Still, it must not block. */
720 event_notifier_set(&data.e);
721 g_main_context_iteration(NULL, true);
722 data.n = 0;
724 /* An active event notifier forces aio_poll to look at EventNotifiers. */
725 event_notifier_init(&dummy.e, false);
726 set_event_notifier(ctx, &dummy.e, event_ready_cb);
728 event_notifier_set(&data.e);
729 g_assert(g_main_context_iteration(NULL, false));
730 g_assert_cmpint(data.n, ==, 1);
731 g_assert(!g_main_context_iteration(NULL, false));
732 g_assert_cmpint(data.n, ==, 1);
734 event_notifier_set(&data.e);
735 g_assert(g_main_context_iteration(NULL, false));
736 g_assert_cmpint(data.n, ==, 2);
737 g_assert(!g_main_context_iteration(NULL, false));
738 g_assert_cmpint(data.n, ==, 2);
740 event_notifier_set(&dummy.e);
741 while (g_main_context_iteration(NULL, false));
742 g_assert_cmpint(data.n, ==, 2);
743 g_assert_cmpint(dummy.n, ==, 1);
744 g_assert_cmpint(dummy.active, ==, 0);
746 set_event_notifier(ctx, &dummy.e, NULL);
747 event_notifier_cleanup(&dummy.e);
749 set_event_notifier(ctx, &data.e, NULL);
750 while (g_main_context_iteration(NULL, false));
751 g_assert_cmpint(data.n, ==, 2);
753 event_notifier_cleanup(&data.e);
756 static void test_source_timer_schedule(void)
758 TimerTestData data = { .n = 0, .ctx = ctx, .ns = SCALE_MS * 750LL,
759 .max = 2,
760 .clock_type = QEMU_CLOCK_VIRTUAL };
761 EventNotifier e;
762 int64_t expiry;
764 /* aio_poll will not block to wait for timers to complete unless it has
765 * an fd to wait on. Fixing this breaks other tests. So create a dummy one.
767 event_notifier_init(&e, false);
768 set_event_notifier(ctx, &e, dummy_io_handler_read);
769 do {} while (g_main_context_iteration(NULL, false));
771 aio_timer_init(ctx, &data.timer, data.clock_type,
772 SCALE_NS, timer_test_cb, &data);
773 expiry = qemu_clock_get_ns(data.clock_type) +
774 data.ns;
775 timer_mod(&data.timer, expiry);
777 g_assert_cmpint(data.n, ==, 0);
779 g_usleep(1 * G_USEC_PER_SEC);
780 g_assert_cmpint(data.n, ==, 0);
782 g_assert(g_main_context_iteration(NULL, true));
783 g_assert_cmpint(data.n, ==, 1);
784 expiry += data.ns;
786 while (data.n < 2) {
787 g_main_context_iteration(NULL, true);
790 g_assert_cmpint(data.n, ==, 2);
791 g_assert(qemu_clock_get_ns(data.clock_type) > expiry);
793 set_event_notifier(ctx, &e, NULL);
794 event_notifier_cleanup(&e);
796 timer_del(&data.timer);
800 /* End of tests. */
802 int main(int argc, char **argv)
804 Error *local_error = NULL;
805 GSource *src;
807 init_clocks();
809 ctx = aio_context_new(&local_error);
810 if (!ctx) {
811 error_report("Failed to create AIO Context: '%s'",
812 error_get_pretty(local_error));
813 error_free(local_error);
814 exit(1);
816 src = aio_get_g_source(ctx);
817 g_source_attach(src, NULL);
818 g_source_unref(src);
820 while (g_main_context_iteration(NULL, false));
822 g_test_init(&argc, &argv, NULL);
823 g_test_add_func("/aio/acquire", test_acquire);
824 g_test_add_func("/aio/bh/schedule", test_bh_schedule);
825 g_test_add_func("/aio/bh/schedule10", test_bh_schedule10);
826 g_test_add_func("/aio/bh/cancel", test_bh_cancel);
827 g_test_add_func("/aio/bh/delete", test_bh_delete);
828 g_test_add_func("/aio/bh/callback-delete/one", test_bh_delete_from_cb);
829 g_test_add_func("/aio/bh/callback-delete/many", test_bh_delete_from_cb_many);
830 g_test_add_func("/aio/bh/flush", test_bh_flush);
831 g_test_add_func("/aio/event/add-remove", test_set_event_notifier);
832 g_test_add_func("/aio/event/wait", test_wait_event_notifier);
833 g_test_add_func("/aio/event/wait/no-flush-cb", test_wait_event_notifier_noflush);
834 g_test_add_func("/aio/event/flush", test_flush_event_notifier);
835 g_test_add_func("/aio/timer/schedule", test_timer_schedule);
837 g_test_add_func("/aio-gsource/flush", test_source_flush);
838 g_test_add_func("/aio-gsource/bh/schedule", test_source_bh_schedule);
839 g_test_add_func("/aio-gsource/bh/schedule10", test_source_bh_schedule10);
840 g_test_add_func("/aio-gsource/bh/cancel", test_source_bh_cancel);
841 g_test_add_func("/aio-gsource/bh/delete", test_source_bh_delete);
842 g_test_add_func("/aio-gsource/bh/callback-delete/one", test_source_bh_delete_from_cb);
843 g_test_add_func("/aio-gsource/bh/callback-delete/many", test_source_bh_delete_from_cb_many);
844 g_test_add_func("/aio-gsource/bh/flush", test_source_bh_flush);
845 g_test_add_func("/aio-gsource/event/add-remove", test_source_set_event_notifier);
846 g_test_add_func("/aio-gsource/event/wait", test_source_wait_event_notifier);
847 g_test_add_func("/aio-gsource/event/wait/no-flush-cb", test_source_wait_event_notifier_noflush);
848 g_test_add_func("/aio-gsource/event/flush", test_source_flush_event_notifier);
849 g_test_add_func("/aio-gsource/timer/schedule", test_source_timer_schedule);
850 return g_test_run();