4 * Copyright Red Hat, Inc. 2012
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.
14 #include "block/aio.h"
15 #include "qemu/timer.h"
16 #include "qemu/sockets.h"
18 static AioContext
*ctx
;
25 } EventNotifierTestData
;
27 /* Wait until event notifier becomes inactive */
28 static void wait_until_inactive(EventNotifierTestData
*data
)
30 while (data
->active
> 0) {
35 /* Simple callbacks for testing. */
45 QEMUClockType clock_type
;
52 static void bh_test_cb(void *opaque
)
54 BHTestData
*data
= opaque
;
55 if (++data
->n
< data
->max
) {
56 qemu_bh_schedule(data
->bh
);
62 static void timer_test_cb(void *opaque
)
64 TimerTestData
*data
= opaque
;
65 if (++data
->n
< data
->max
) {
66 timer_mod(&data
->timer
,
67 qemu_clock_get_ns(data
->clock_type
) + data
->ns
);
71 static void dummy_io_handler_read(void *opaque
)
77 static void bh_delete_cb(void *opaque
)
79 BHTestData
*data
= opaque
;
80 if (++data
->n
< data
->max
) {
81 qemu_bh_schedule(data
->bh
);
83 qemu_bh_delete(data
->bh
);
88 static void event_ready_cb(EventNotifier
*e
)
90 EventNotifierTestData
*data
= container_of(e
, EventNotifierTestData
, e
);
91 g_assert(event_notifier_test_and_clear(e
));
93 if (data
->active
> 0) {
96 if (data
->auto_set
&& data
->active
) {
97 event_notifier_set(e
);
101 /* Tests using aio_*. */
103 static void test_notify(void)
105 g_assert(!aio_poll(ctx
, false));
107 g_assert(!aio_poll(ctx
, true));
108 g_assert(!aio_poll(ctx
, false));
112 QemuMutex start_lock
;
113 bool thread_acquired
;
116 static void *test_acquire_thread(void *opaque
)
118 AcquireTestData
*data
= opaque
;
120 /* Wait for other thread to let us start */
121 qemu_mutex_lock(&data
->start_lock
);
122 qemu_mutex_unlock(&data
->start_lock
);
124 aio_context_acquire(ctx
);
125 aio_context_release(ctx
);
127 data
->thread_acquired
= true; /* success, we got here */
132 static void dummy_notifier_read(EventNotifier
*unused
)
134 g_assert(false); /* should never be invoked */
137 static void test_acquire(void)
140 EventNotifier notifier
;
141 AcquireTestData data
;
143 /* Dummy event notifier ensures aio_poll() will block */
144 event_notifier_init(¬ifier
, false);
145 aio_set_event_notifier(ctx
, ¬ifier
, dummy_notifier_read
);
146 g_assert(!aio_poll(ctx
, false)); /* consume aio_notify() */
148 qemu_mutex_init(&data
.start_lock
);
149 qemu_mutex_lock(&data
.start_lock
);
150 data
.thread_acquired
= false;
152 qemu_thread_create(&thread
, "test_acquire_thread",
154 &data
, QEMU_THREAD_JOINABLE
);
156 /* Block in aio_poll(), let other thread kick us and acquire context */
157 aio_context_acquire(ctx
);
158 qemu_mutex_unlock(&data
.start_lock
); /* let the thread run */
159 g_assert(!aio_poll(ctx
, true));
160 aio_context_release(ctx
);
162 qemu_thread_join(&thread
);
163 aio_set_event_notifier(ctx
, ¬ifier
, NULL
);
164 event_notifier_cleanup(¬ifier
);
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) {
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
) {
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
) {
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));
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
);
433 static void test_timer_schedule(void)
435 TimerTestData data
= { .n
= 0, .ctx
= ctx
, .ns
= SCALE_MS
* 750LL,
437 .clock_type
= QEMU_CLOCK_VIRTUAL
};
440 /* aio_poll will not block to wait for timers to complete unless it has
441 * an fd to wait on. Fixing this breaks other tests. So create a dummy one.
443 g_assert(!qemu_pipe(pipefd
));
444 qemu_set_nonblock(pipefd
[0]);
445 qemu_set_nonblock(pipefd
[1]);
447 aio_set_fd_handler(ctx
, pipefd
[0],
448 dummy_io_handler_read
, NULL
, NULL
);
449 aio_poll(ctx
, false);
451 aio_timer_init(ctx
, &data
.timer
, data
.clock_type
,
452 SCALE_NS
, timer_test_cb
, &data
);
453 timer_mod(&data
.timer
,
454 qemu_clock_get_ns(data
.clock_type
) +
457 g_assert_cmpint(data
.n
, ==, 0);
459 /* timer_mod may well cause an event notifer to have gone off,
462 do {} while (aio_poll(ctx
, false));
464 g_assert(!aio_poll(ctx
, false));
465 g_assert_cmpint(data
.n
, ==, 0);
467 g_usleep(1 * G_USEC_PER_SEC
);
468 g_assert_cmpint(data
.n
, ==, 0);
470 g_assert(aio_poll(ctx
, false));
471 g_assert_cmpint(data
.n
, ==, 1);
473 /* timer_mod called by our callback */
474 do {} while (aio_poll(ctx
, false));
476 g_assert(!aio_poll(ctx
, false));
477 g_assert_cmpint(data
.n
, ==, 1);
479 g_assert(aio_poll(ctx
, true));
480 g_assert_cmpint(data
.n
, ==, 2);
482 /* As max is now 2, an event notifier should not have gone off */
484 g_assert(!aio_poll(ctx
, false));
485 g_assert_cmpint(data
.n
, ==, 2);
487 aio_set_fd_handler(ctx
, pipefd
[0], NULL
, NULL
, NULL
);
491 timer_del(&data
.timer
);
496 /* Now the same tests, using the context as a GSource. They are
497 * very similar to the ones above, with g_main_context_iteration
498 * replacing aio_poll. However:
499 * - sometimes both the AioContext and the glib main loop wake
500 * themselves up. Hence, some "g_assert(!aio_poll(ctx, false));"
501 * are replaced by "while (g_main_context_iteration(NULL, false));".
502 * - there is no exact replacement for a blocking wait.
503 * "while (g_main_context_iteration(NULL, true)" seems to work,
504 * but it is not documented _why_ it works. For these tests a
505 * non-blocking loop like "while (g_main_context_iteration(NULL, false)"
506 * works well, and that's what I am using.
509 static void test_source_notify(void)
511 while (g_main_context_iteration(NULL
, false));
513 g_assert(g_main_context_iteration(NULL
, true));
514 g_assert(!g_main_context_iteration(NULL
, false));
517 static void test_source_flush(void)
519 g_assert(!g_main_context_iteration(NULL
, false));
521 while (g_main_context_iteration(NULL
, false));
522 g_assert(!g_main_context_iteration(NULL
, false));
525 static void test_source_bh_schedule(void)
527 BHTestData data
= { .n
= 0 };
528 data
.bh
= aio_bh_new(ctx
, bh_test_cb
, &data
);
530 qemu_bh_schedule(data
.bh
);
531 g_assert_cmpint(data
.n
, ==, 0);
533 g_assert(g_main_context_iteration(NULL
, true));
534 g_assert_cmpint(data
.n
, ==, 1);
536 g_assert(!g_main_context_iteration(NULL
, false));
537 g_assert_cmpint(data
.n
, ==, 1);
538 qemu_bh_delete(data
.bh
);
541 static void test_source_bh_schedule10(void)
543 BHTestData data
= { .n
= 0, .max
= 10 };
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 g_assert(g_main_context_iteration(NULL
, false));
550 g_assert_cmpint(data
.n
, ==, 1);
552 g_assert(g_main_context_iteration(NULL
, true));
553 g_assert_cmpint(data
.n
, ==, 2);
555 while (g_main_context_iteration(NULL
, false));
556 g_assert_cmpint(data
.n
, ==, 10);
558 g_assert(!g_main_context_iteration(NULL
, false));
559 g_assert_cmpint(data
.n
, ==, 10);
560 qemu_bh_delete(data
.bh
);
563 static void test_source_bh_cancel(void)
565 BHTestData data
= { .n
= 0 };
566 data
.bh
= aio_bh_new(ctx
, bh_test_cb
, &data
);
568 qemu_bh_schedule(data
.bh
);
569 g_assert_cmpint(data
.n
, ==, 0);
571 qemu_bh_cancel(data
.bh
);
572 g_assert_cmpint(data
.n
, ==, 0);
574 while (g_main_context_iteration(NULL
, false));
575 g_assert_cmpint(data
.n
, ==, 0);
576 qemu_bh_delete(data
.bh
);
579 static void test_source_bh_delete(void)
581 BHTestData data
= { .n
= 0 };
582 data
.bh
= aio_bh_new(ctx
, bh_test_cb
, &data
);
584 qemu_bh_schedule(data
.bh
);
585 g_assert_cmpint(data
.n
, ==, 0);
587 qemu_bh_delete(data
.bh
);
588 g_assert_cmpint(data
.n
, ==, 0);
590 while (g_main_context_iteration(NULL
, false));
591 g_assert_cmpint(data
.n
, ==, 0);
594 static void test_source_bh_delete_from_cb(void)
596 BHTestData data1
= { .n
= 0, .max
= 1 };
598 data1
.bh
= aio_bh_new(ctx
, bh_delete_cb
, &data1
);
600 qemu_bh_schedule(data1
.bh
);
601 g_assert_cmpint(data1
.n
, ==, 0);
603 g_main_context_iteration(NULL
, true);
604 g_assert_cmpint(data1
.n
, ==, data1
.max
);
605 g_assert(data1
.bh
== NULL
);
607 g_assert(!g_main_context_iteration(NULL
, false));
610 static void test_source_bh_delete_from_cb_many(void)
612 BHTestData data1
= { .n
= 0, .max
= 1 };
613 BHTestData data2
= { .n
= 0, .max
= 3 };
614 BHTestData data3
= { .n
= 0, .max
= 2 };
615 BHTestData data4
= { .n
= 0, .max
= 4 };
617 data1
.bh
= aio_bh_new(ctx
, bh_delete_cb
, &data1
);
618 data2
.bh
= aio_bh_new(ctx
, bh_delete_cb
, &data2
);
619 data3
.bh
= aio_bh_new(ctx
, bh_delete_cb
, &data3
);
620 data4
.bh
= aio_bh_new(ctx
, bh_delete_cb
, &data4
);
622 qemu_bh_schedule(data1
.bh
);
623 qemu_bh_schedule(data2
.bh
);
624 qemu_bh_schedule(data3
.bh
);
625 qemu_bh_schedule(data4
.bh
);
626 g_assert_cmpint(data1
.n
, ==, 0);
627 g_assert_cmpint(data2
.n
, ==, 0);
628 g_assert_cmpint(data3
.n
, ==, 0);
629 g_assert_cmpint(data4
.n
, ==, 0);
631 g_assert(g_main_context_iteration(NULL
, false));
632 g_assert_cmpint(data1
.n
, ==, 1);
633 g_assert_cmpint(data2
.n
, ==, 1);
634 g_assert_cmpint(data3
.n
, ==, 1);
635 g_assert_cmpint(data4
.n
, ==, 1);
636 g_assert(data1
.bh
== NULL
);
638 while (g_main_context_iteration(NULL
, false));
639 g_assert_cmpint(data1
.n
, ==, data1
.max
);
640 g_assert_cmpint(data2
.n
, ==, data2
.max
);
641 g_assert_cmpint(data3
.n
, ==, data3
.max
);
642 g_assert_cmpint(data4
.n
, ==, data4
.max
);
643 g_assert(data1
.bh
== NULL
);
644 g_assert(data2
.bh
== NULL
);
645 g_assert(data3
.bh
== NULL
);
646 g_assert(data4
.bh
== NULL
);
649 static void test_source_bh_flush(void)
651 BHTestData data
= { .n
= 0 };
652 data
.bh
= aio_bh_new(ctx
, bh_test_cb
, &data
);
654 qemu_bh_schedule(data
.bh
);
655 g_assert_cmpint(data
.n
, ==, 0);
657 g_assert(g_main_context_iteration(NULL
, true));
658 g_assert_cmpint(data
.n
, ==, 1);
660 g_assert(!g_main_context_iteration(NULL
, false));
661 g_assert_cmpint(data
.n
, ==, 1);
662 qemu_bh_delete(data
.bh
);
665 static void test_source_set_event_notifier(void)
667 EventNotifierTestData data
= { .n
= 0, .active
= 0 };
668 event_notifier_init(&data
.e
, false);
669 aio_set_event_notifier(ctx
, &data
.e
, event_ready_cb
);
670 while (g_main_context_iteration(NULL
, false));
671 g_assert_cmpint(data
.n
, ==, 0);
673 aio_set_event_notifier(ctx
, &data
.e
, NULL
);
674 while (g_main_context_iteration(NULL
, false));
675 g_assert_cmpint(data
.n
, ==, 0);
676 event_notifier_cleanup(&data
.e
);
679 static void test_source_wait_event_notifier(void)
681 EventNotifierTestData data
= { .n
= 0, .active
= 1 };
682 event_notifier_init(&data
.e
, false);
683 aio_set_event_notifier(ctx
, &data
.e
, event_ready_cb
);
684 g_assert(g_main_context_iteration(NULL
, false));
685 g_assert_cmpint(data
.n
, ==, 0);
686 g_assert_cmpint(data
.active
, ==, 1);
688 event_notifier_set(&data
.e
);
689 g_assert(g_main_context_iteration(NULL
, false));
690 g_assert_cmpint(data
.n
, ==, 1);
691 g_assert_cmpint(data
.active
, ==, 0);
693 while (g_main_context_iteration(NULL
, false));
694 g_assert_cmpint(data
.n
, ==, 1);
695 g_assert_cmpint(data
.active
, ==, 0);
697 aio_set_event_notifier(ctx
, &data
.e
, NULL
);
698 while (g_main_context_iteration(NULL
, false));
699 g_assert_cmpint(data
.n
, ==, 1);
701 event_notifier_cleanup(&data
.e
);
704 static void test_source_flush_event_notifier(void)
706 EventNotifierTestData data
= { .n
= 0, .active
= 10, .auto_set
= true };
707 event_notifier_init(&data
.e
, false);
708 aio_set_event_notifier(ctx
, &data
.e
, event_ready_cb
);
709 g_assert(g_main_context_iteration(NULL
, false));
710 g_assert_cmpint(data
.n
, ==, 0);
711 g_assert_cmpint(data
.active
, ==, 10);
713 event_notifier_set(&data
.e
);
714 g_assert(g_main_context_iteration(NULL
, false));
715 g_assert_cmpint(data
.n
, ==, 1);
716 g_assert_cmpint(data
.active
, ==, 9);
717 g_assert(g_main_context_iteration(NULL
, false));
719 while (g_main_context_iteration(NULL
, false));
720 g_assert_cmpint(data
.n
, ==, 10);
721 g_assert_cmpint(data
.active
, ==, 0);
722 g_assert(!g_main_context_iteration(NULL
, false));
724 aio_set_event_notifier(ctx
, &data
.e
, NULL
);
725 while (g_main_context_iteration(NULL
, false));
726 event_notifier_cleanup(&data
.e
);
729 static void test_source_wait_event_notifier_noflush(void)
731 EventNotifierTestData data
= { .n
= 0 };
732 EventNotifierTestData dummy
= { .n
= 0, .active
= 1 };
734 event_notifier_init(&data
.e
, false);
735 aio_set_event_notifier(ctx
, &data
.e
, event_ready_cb
);
737 while (g_main_context_iteration(NULL
, false));
738 g_assert_cmpint(data
.n
, ==, 0);
740 /* Until there is an active descriptor, glib may or may not call
741 * event_ready_cb. Still, it must not block. */
742 event_notifier_set(&data
.e
);
743 g_main_context_iteration(NULL
, true);
746 /* An active event notifier forces aio_poll to look at EventNotifiers. */
747 event_notifier_init(&dummy
.e
, false);
748 aio_set_event_notifier(ctx
, &dummy
.e
, event_ready_cb
);
750 event_notifier_set(&data
.e
);
751 g_assert(g_main_context_iteration(NULL
, false));
752 g_assert_cmpint(data
.n
, ==, 1);
753 g_assert(!g_main_context_iteration(NULL
, false));
754 g_assert_cmpint(data
.n
, ==, 1);
756 event_notifier_set(&data
.e
);
757 g_assert(g_main_context_iteration(NULL
, false));
758 g_assert_cmpint(data
.n
, ==, 2);
759 g_assert(!g_main_context_iteration(NULL
, false));
760 g_assert_cmpint(data
.n
, ==, 2);
762 event_notifier_set(&dummy
.e
);
763 while (g_main_context_iteration(NULL
, false));
764 g_assert_cmpint(data
.n
, ==, 2);
765 g_assert_cmpint(dummy
.n
, ==, 1);
766 g_assert_cmpint(dummy
.active
, ==, 0);
768 aio_set_event_notifier(ctx
, &dummy
.e
, NULL
);
769 event_notifier_cleanup(&dummy
.e
);
771 aio_set_event_notifier(ctx
, &data
.e
, NULL
);
772 while (g_main_context_iteration(NULL
, false));
773 g_assert_cmpint(data
.n
, ==, 2);
775 event_notifier_cleanup(&data
.e
);
780 static void test_source_timer_schedule(void)
782 TimerTestData data
= { .n
= 0, .ctx
= ctx
, .ns
= SCALE_MS
* 750LL,
784 .clock_type
= QEMU_CLOCK_VIRTUAL
};
788 /* aio_poll will not block to wait for timers to complete unless it has
789 * an fd to wait on. Fixing this breaks other tests. So create a dummy one.
791 g_assert(!qemu_pipe(pipefd
));
792 qemu_set_nonblock(pipefd
[0]);
793 qemu_set_nonblock(pipefd
[1]);
795 aio_set_fd_handler(ctx
, pipefd
[0],
796 dummy_io_handler_read
, NULL
, NULL
);
797 do {} while (g_main_context_iteration(NULL
, false));
799 aio_timer_init(ctx
, &data
.timer
, data
.clock_type
,
800 SCALE_NS
, timer_test_cb
, &data
);
801 expiry
= qemu_clock_get_ns(data
.clock_type
) +
803 timer_mod(&data
.timer
, expiry
);
805 g_assert_cmpint(data
.n
, ==, 0);
807 g_usleep(1 * G_USEC_PER_SEC
);
808 g_assert_cmpint(data
.n
, ==, 0);
810 g_assert(g_main_context_iteration(NULL
, true));
811 g_assert_cmpint(data
.n
, ==, 1);
815 g_main_context_iteration(NULL
, true);
818 g_assert_cmpint(data
.n
, ==, 2);
819 g_assert(qemu_clock_get_ns(data
.clock_type
) > expiry
);
821 aio_set_fd_handler(ctx
, pipefd
[0], NULL
, NULL
, NULL
);
825 timer_del(&data
.timer
);
833 int main(int argc
, char **argv
)
839 ctx
= aio_context_new();
840 src
= aio_get_g_source(ctx
);
841 g_source_attach(src
, NULL
);
844 while (g_main_context_iteration(NULL
, false));
846 g_test_init(&argc
, &argv
, NULL
);
847 g_test_add_func("/aio/notify", test_notify
);
848 g_test_add_func("/aio/acquire", test_acquire
);
849 g_test_add_func("/aio/bh/schedule", test_bh_schedule
);
850 g_test_add_func("/aio/bh/schedule10", test_bh_schedule10
);
851 g_test_add_func("/aio/bh/cancel", test_bh_cancel
);
852 g_test_add_func("/aio/bh/delete", test_bh_delete
);
853 g_test_add_func("/aio/bh/callback-delete/one", test_bh_delete_from_cb
);
854 g_test_add_func("/aio/bh/callback-delete/many", test_bh_delete_from_cb_many
);
855 g_test_add_func("/aio/bh/flush", test_bh_flush
);
856 g_test_add_func("/aio/event/add-remove", test_set_event_notifier
);
857 g_test_add_func("/aio/event/wait", test_wait_event_notifier
);
858 g_test_add_func("/aio/event/wait/no-flush-cb", test_wait_event_notifier_noflush
);
859 g_test_add_func("/aio/event/flush", test_flush_event_notifier
);
861 g_test_add_func("/aio/timer/schedule", test_timer_schedule
);
864 g_test_add_func("/aio-gsource/notify", test_source_notify
);
865 g_test_add_func("/aio-gsource/flush", test_source_flush
);
866 g_test_add_func("/aio-gsource/bh/schedule", test_source_bh_schedule
);
867 g_test_add_func("/aio-gsource/bh/schedule10", test_source_bh_schedule10
);
868 g_test_add_func("/aio-gsource/bh/cancel", test_source_bh_cancel
);
869 g_test_add_func("/aio-gsource/bh/delete", test_source_bh_delete
);
870 g_test_add_func("/aio-gsource/bh/callback-delete/one", test_source_bh_delete_from_cb
);
871 g_test_add_func("/aio-gsource/bh/callback-delete/many", test_source_bh_delete_from_cb_many
);
872 g_test_add_func("/aio-gsource/bh/flush", test_source_bh_flush
);
873 g_test_add_func("/aio-gsource/event/add-remove", test_source_set_event_notifier
);
874 g_test_add_func("/aio-gsource/event/wait", test_source_wait_event_notifier
);
875 g_test_add_func("/aio-gsource/event/wait/no-flush-cb", test_source_wait_event_notifier_noflush
);
876 g_test_add_func("/aio-gsource/event/flush", test_source_flush_event_notifier
);
878 g_test_add_func("/aio-gsource/timer/schedule", test_source_timer_schedule
);