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"
25 } EventNotifierTestData
;
27 /* Wait until there are no more BHs or AIO requests */
28 static void wait_for_aio(void)
30 while (aio_poll(ctx
, true)) {
35 /* Wait until event notifier becomes inactive */
36 static void wait_until_inactive(EventNotifierTestData
*data
)
38 while (data
->active
> 0) {
43 /* Simple callbacks for testing. */
53 QEMUClockType clock_type
;
60 static void bh_test_cb(void *opaque
)
62 BHTestData
*data
= opaque
;
63 if (++data
->n
< data
->max
) {
64 qemu_bh_schedule(data
->bh
);
68 static void timer_test_cb(void *opaque
)
70 TimerTestData
*data
= opaque
;
71 if (++data
->n
< data
->max
) {
72 timer_mod(&data
->timer
,
73 qemu_clock_get_ns(data
->clock_type
) + data
->ns
);
77 static void dummy_io_handler_read(void *opaque
)
81 static void bh_delete_cb(void *opaque
)
83 BHTestData
*data
= opaque
;
84 if (++data
->n
< data
->max
) {
85 qemu_bh_schedule(data
->bh
);
87 qemu_bh_delete(data
->bh
);
92 static void event_ready_cb(EventNotifier
*e
)
94 EventNotifierTestData
*data
= container_of(e
, EventNotifierTestData
, e
);
95 g_assert(event_notifier_test_and_clear(e
));
97 if (data
->active
> 0) {
100 if (data
->auto_set
&& data
->active
) {
101 event_notifier_set(e
);
105 /* Tests using aio_*. */
107 static void test_notify(void)
109 g_assert(!aio_poll(ctx
, false));
111 g_assert(!aio_poll(ctx
, true));
112 g_assert(!aio_poll(ctx
, false));
116 QemuMutex start_lock
;
117 bool thread_acquired
;
120 static void *test_acquire_thread(void *opaque
)
122 AcquireTestData
*data
= opaque
;
124 /* Wait for other thread to let us start */
125 qemu_mutex_lock(&data
->start_lock
);
126 qemu_mutex_unlock(&data
->start_lock
);
128 aio_context_acquire(ctx
);
129 aio_context_release(ctx
);
131 data
->thread_acquired
= true; /* success, we got here */
136 static void dummy_notifier_read(EventNotifier
*unused
)
138 g_assert(false); /* should never be invoked */
141 static void test_acquire(void)
144 EventNotifier notifier
;
145 AcquireTestData data
;
147 /* Dummy event notifier ensures aio_poll() will block */
148 event_notifier_init(¬ifier
, false);
149 aio_set_event_notifier(ctx
, ¬ifier
, dummy_notifier_read
);
150 g_assert(!aio_poll(ctx
, false)); /* consume aio_notify() */
152 qemu_mutex_init(&data
.start_lock
);
153 qemu_mutex_lock(&data
.start_lock
);
154 data
.thread_acquired
= false;
156 qemu_thread_create(&thread
, "test_acquire_thread",
158 &data
, QEMU_THREAD_JOINABLE
);
160 /* Block in aio_poll(), let other thread kick us and acquire context */
161 aio_context_acquire(ctx
);
162 qemu_mutex_unlock(&data
.start_lock
); /* let the thread run */
163 g_assert(!aio_poll(ctx
, true));
164 aio_context_release(ctx
);
166 qemu_thread_join(&thread
);
167 aio_set_event_notifier(ctx
, ¬ifier
, NULL
);
168 event_notifier_cleanup(¬ifier
);
170 g_assert(data
.thread_acquired
);
173 static void test_bh_schedule(void)
175 BHTestData data
= { .n
= 0 };
176 data
.bh
= aio_bh_new(ctx
, bh_test_cb
, &data
);
178 qemu_bh_schedule(data
.bh
);
179 g_assert_cmpint(data
.n
, ==, 0);
181 g_assert(aio_poll(ctx
, true));
182 g_assert_cmpint(data
.n
, ==, 1);
184 g_assert(!aio_poll(ctx
, false));
185 g_assert_cmpint(data
.n
, ==, 1);
186 qemu_bh_delete(data
.bh
);
189 static void test_bh_schedule10(void)
191 BHTestData data
= { .n
= 0, .max
= 10 };
192 data
.bh
= aio_bh_new(ctx
, bh_test_cb
, &data
);
194 qemu_bh_schedule(data
.bh
);
195 g_assert_cmpint(data
.n
, ==, 0);
197 g_assert(aio_poll(ctx
, false));
198 g_assert_cmpint(data
.n
, ==, 1);
200 g_assert(aio_poll(ctx
, true));
201 g_assert_cmpint(data
.n
, ==, 2);
204 g_assert_cmpint(data
.n
, ==, 10);
206 g_assert(!aio_poll(ctx
, false));
207 g_assert_cmpint(data
.n
, ==, 10);
208 qemu_bh_delete(data
.bh
);
211 static void test_bh_cancel(void)
213 BHTestData data
= { .n
= 0 };
214 data
.bh
= aio_bh_new(ctx
, bh_test_cb
, &data
);
216 qemu_bh_schedule(data
.bh
);
217 g_assert_cmpint(data
.n
, ==, 0);
219 qemu_bh_cancel(data
.bh
);
220 g_assert_cmpint(data
.n
, ==, 0);
222 g_assert(!aio_poll(ctx
, false));
223 g_assert_cmpint(data
.n
, ==, 0);
224 qemu_bh_delete(data
.bh
);
227 static void test_bh_delete(void)
229 BHTestData data
= { .n
= 0 };
230 data
.bh
= aio_bh_new(ctx
, bh_test_cb
, &data
);
232 qemu_bh_schedule(data
.bh
);
233 g_assert_cmpint(data
.n
, ==, 0);
235 qemu_bh_delete(data
.bh
);
236 g_assert_cmpint(data
.n
, ==, 0);
238 g_assert(!aio_poll(ctx
, false));
239 g_assert_cmpint(data
.n
, ==, 0);
242 static void test_bh_delete_from_cb(void)
244 BHTestData data1
= { .n
= 0, .max
= 1 };
246 data1
.bh
= aio_bh_new(ctx
, bh_delete_cb
, &data1
);
248 qemu_bh_schedule(data1
.bh
);
249 g_assert_cmpint(data1
.n
, ==, 0);
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
);
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);
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 aio_set_event_notifier(ctx
, &data
.e
, event_ready_cb
);
318 g_assert(!aio_poll(ctx
, false));
319 g_assert_cmpint(data
.n
, ==, 0);
321 aio_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 aio_set_event_notifier(ctx
, &data
.e
, event_ready_cb
);
332 g_assert(!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 aio_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 aio_set_event_notifier(ctx
, &data
.e
, event_ready_cb
);
357 g_assert(!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 aio_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 aio_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));
394 /* An active event notifier forces aio_poll to look at EventNotifiers. */
395 event_notifier_init(&dummy
.e
, false);
396 aio_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 aio_set_event_notifier(ctx
, &dummy
.e
, NULL
);
417 event_notifier_cleanup(&dummy
.e
);
419 aio_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,
430 .clock_type
= QEMU_CLOCK_VIRTUAL
};
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 g_assert(!qemu_pipe(pipefd
));
437 qemu_set_nonblock(pipefd
[0]);
438 qemu_set_nonblock(pipefd
[1]);
440 aio_set_fd_handler(ctx
, pipefd
[0],
441 dummy_io_handler_read
, NULL
, NULL
);
442 aio_poll(ctx
, false);
444 aio_timer_init(ctx
, &data
.timer
, data
.clock_type
,
445 SCALE_NS
, timer_test_cb
, &data
);
446 timer_mod(&data
.timer
,
447 qemu_clock_get_ns(data
.clock_type
) +
450 g_assert_cmpint(data
.n
, ==, 0);
452 /* timer_mod may well cause an event notifer to have gone off,
455 do {} while (aio_poll(ctx
, false));
457 g_assert(!aio_poll(ctx
, false));
458 g_assert_cmpint(data
.n
, ==, 0);
460 g_usleep(1 * G_USEC_PER_SEC
);
461 g_assert_cmpint(data
.n
, ==, 0);
463 g_assert(aio_poll(ctx
, false));
464 g_assert_cmpint(data
.n
, ==, 1);
466 /* timer_mod called by our callback */
467 do {} while (aio_poll(ctx
, false));
469 g_assert(!aio_poll(ctx
, false));
470 g_assert_cmpint(data
.n
, ==, 1);
472 g_assert(aio_poll(ctx
, true));
473 g_assert_cmpint(data
.n
, ==, 2);
475 /* As max is now 2, an event notifier should not have gone off */
477 g_assert(!aio_poll(ctx
, false));
478 g_assert_cmpint(data
.n
, ==, 2);
480 aio_set_fd_handler(ctx
, pipefd
[0], NULL
, NULL
, NULL
);
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));
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));
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);
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,
773 .clock_type
= QEMU_CLOCK_VIRTUAL
};
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 g_assert(!qemu_pipe(pipefd
));
781 qemu_set_nonblock(pipefd
[0]);
782 qemu_set_nonblock(pipefd
[1]);
784 aio_set_fd_handler(ctx
, pipefd
[0],
785 dummy_io_handler_read
, NULL
, NULL
);
786 do {} while (g_main_context_iteration(NULL
, false));
788 aio_timer_init(ctx
, &data
.timer
, data
.clock_type
,
789 SCALE_NS
, timer_test_cb
, &data
);
790 expiry
= qemu_clock_get_ns(data
.clock_type
) +
792 timer_mod(&data
.timer
, expiry
);
794 g_assert_cmpint(data
.n
, ==, 0);
796 g_usleep(1 * G_USEC_PER_SEC
);
797 g_assert_cmpint(data
.n
, ==, 0);
799 g_assert(g_main_context_iteration(NULL
, false));
800 g_assert_cmpint(data
.n
, ==, 1);
802 /* The comment above was not kidding when it said this wakes up itself */
804 g_assert(g_main_context_iteration(NULL
, true));
805 } while (qemu_clock_get_ns(data
.clock_type
) <= expiry
);
806 g_usleep(1 * G_USEC_PER_SEC
);
807 g_main_context_iteration(NULL
, false);
809 g_assert_cmpint(data
.n
, ==, 2);
811 aio_set_fd_handler(ctx
, pipefd
[0], NULL
, NULL
, NULL
);
815 timer_del(&data
.timer
);
821 int main(int argc
, char **argv
)
827 ctx
= aio_context_new();
828 src
= aio_get_g_source(ctx
);
829 g_source_attach(src
, NULL
);
832 while (g_main_context_iteration(NULL
, false));
834 g_test_init(&argc
, &argv
, NULL
);
835 g_test_add_func("/aio/notify", test_notify
);
836 g_test_add_func("/aio/acquire", test_acquire
);
837 g_test_add_func("/aio/bh/schedule", test_bh_schedule
);
838 g_test_add_func("/aio/bh/schedule10", test_bh_schedule10
);
839 g_test_add_func("/aio/bh/cancel", test_bh_cancel
);
840 g_test_add_func("/aio/bh/delete", test_bh_delete
);
841 g_test_add_func("/aio/bh/callback-delete/one", test_bh_delete_from_cb
);
842 g_test_add_func("/aio/bh/callback-delete/many", test_bh_delete_from_cb_many
);
843 g_test_add_func("/aio/bh/flush", test_bh_flush
);
844 g_test_add_func("/aio/event/add-remove", test_set_event_notifier
);
845 g_test_add_func("/aio/event/wait", test_wait_event_notifier
);
846 g_test_add_func("/aio/event/wait/no-flush-cb", test_wait_event_notifier_noflush
);
847 g_test_add_func("/aio/event/flush", test_flush_event_notifier
);
848 g_test_add_func("/aio/timer/schedule", test_timer_schedule
);
850 g_test_add_func("/aio-gsource/notify", test_source_notify
);
851 g_test_add_func("/aio-gsource/flush", test_source_flush
);
852 g_test_add_func("/aio-gsource/bh/schedule", test_source_bh_schedule
);
853 g_test_add_func("/aio-gsource/bh/schedule10", test_source_bh_schedule10
);
854 g_test_add_func("/aio-gsource/bh/cancel", test_source_bh_cancel
);
855 g_test_add_func("/aio-gsource/bh/delete", test_source_bh_delete
);
856 g_test_add_func("/aio-gsource/bh/callback-delete/one", test_source_bh_delete_from_cb
);
857 g_test_add_func("/aio-gsource/bh/callback-delete/many", test_source_bh_delete_from_cb_many
);
858 g_test_add_func("/aio-gsource/bh/flush", test_source_bh_flush
);
859 g_test_add_func("/aio-gsource/event/add-remove", test_source_set_event_notifier
);
860 g_test_add_func("/aio-gsource/event/wait", test_source_wait_event_notifier
);
861 g_test_add_func("/aio-gsource/event/wait/no-flush-cb", test_source_wait_event_notifier_noflush
);
862 g_test_add_func("/aio-gsource/event/flush", test_source_flush_event_notifier
);
863 g_test_add_func("/aio-gsource/timer/schedule", test_source_timer_schedule
);