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.
13 #include "qemu/osdep.h"
15 #include "block/aio.h"
16 #include "qapi/error.h"
17 #include "qemu/timer.h"
18 #include "qemu/sockets.h"
19 #include "qemu/error-report.h"
21 static AioContext
*ctx
;
28 } EventNotifierTestData
;
30 /* Wait until event notifier becomes inactive */
31 static void wait_until_inactive(EventNotifierTestData
*data
)
33 while (data
->active
> 0) {
38 /* Simple callbacks for testing. */
48 QEMUClockType clock_type
;
55 static void bh_test_cb(void *opaque
)
57 BHTestData
*data
= opaque
;
58 if (++data
->n
< data
->max
) {
59 qemu_bh_schedule(data
->bh
);
63 static void timer_test_cb(void *opaque
)
65 TimerTestData
*data
= opaque
;
66 if (++data
->n
< data
->max
) {
67 timer_mod(&data
->timer
,
68 qemu_clock_get_ns(data
->clock_type
) + data
->ns
);
72 static void dummy_io_handler_read(EventNotifier
*e
)
76 static void bh_delete_cb(void *opaque
)
78 BHTestData
*data
= opaque
;
79 if (++data
->n
< data
->max
) {
80 qemu_bh_schedule(data
->bh
);
82 qemu_bh_delete(data
->bh
);
87 static void event_ready_cb(EventNotifier
*e
)
89 EventNotifierTestData
*data
= container_of(e
, EventNotifierTestData
, e
);
90 g_assert(event_notifier_test_and_clear(e
));
92 if (data
->active
> 0) {
95 if (data
->auto_set
&& data
->active
) {
96 event_notifier_set(e
);
100 /* Tests using aio_*. */
103 QemuMutex start_lock
;
104 bool thread_acquired
;
107 static void *test_acquire_thread(void *opaque
)
109 AcquireTestData
*data
= opaque
;
111 /* Wait for other thread to let us start */
112 qemu_mutex_lock(&data
->start_lock
);
113 qemu_mutex_unlock(&data
->start_lock
);
115 aio_context_acquire(ctx
);
116 aio_context_release(ctx
);
118 data
->thread_acquired
= true; /* success, we got here */
123 static void set_event_notifier(AioContext
*ctx
, EventNotifier
*notifier
,
124 EventNotifierHandler
*handler
)
126 aio_set_event_notifier(ctx
, notifier
, false, handler
);
129 static void dummy_notifier_read(EventNotifier
*unused
)
131 g_assert(false); /* should never be invoked */
134 static void test_acquire(void)
137 EventNotifier notifier
;
138 AcquireTestData data
;
140 /* Dummy event notifier ensures aio_poll() will block */
141 event_notifier_init(¬ifier
, false);
142 set_event_notifier(ctx
, ¬ifier
, 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",
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 set_event_notifier(ctx
, ¬ifier
, NULL
);
161 event_notifier_cleanup(¬ifier
);
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) {
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
) {
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
) {
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 set_event_notifier(ctx
, &data
.e
, event_ready_cb
);
320 g_assert(!aio_poll(ctx
, false));
321 g_assert_cmpint(data
.n
, ==, 0);
323 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 set_event_notifier(ctx
, &data
.e
, event_ready_cb
);
334 while (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 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 set_event_notifier(ctx
, &data
.e
, event_ready_cb
);
359 while (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 set_event_notifier(ctx
, &data
.e
, NULL
);
375 g_assert(!aio_poll(ctx
, false));
376 event_notifier_cleanup(&data
.e
);
379 static void test_aio_external_client(void)
383 for (i
= 1; i
< 3; i
++) {
384 EventNotifierTestData data
= { .n
= 0, .active
= 10, .auto_set
= true };
385 event_notifier_init(&data
.e
, false);
386 aio_set_event_notifier(ctx
, &data
.e
, true, event_ready_cb
);
387 event_notifier_set(&data
.e
);
388 for (j
= 0; j
< i
; j
++) {
389 aio_disable_external(ctx
);
391 for (j
= 0; j
< i
; j
++) {
392 assert(!aio_poll(ctx
, false));
393 assert(event_notifier_test_and_clear(&data
.e
));
394 event_notifier_set(&data
.e
);
395 aio_enable_external(ctx
);
397 assert(aio_poll(ctx
, false));
398 set_event_notifier(ctx
, &data
.e
, NULL
);
399 event_notifier_cleanup(&data
.e
);
403 static void test_wait_event_notifier_noflush(void)
405 EventNotifierTestData data
= { .n
= 0 };
406 EventNotifierTestData dummy
= { .n
= 0, .active
= 1 };
408 event_notifier_init(&data
.e
, false);
409 set_event_notifier(ctx
, &data
.e
, event_ready_cb
);
411 g_assert(!aio_poll(ctx
, false));
412 g_assert_cmpint(data
.n
, ==, 0);
414 /* Until there is an active descriptor, aio_poll may or may not call
415 * event_ready_cb. Still, it must not block. */
416 event_notifier_set(&data
.e
);
417 g_assert(aio_poll(ctx
, true));
420 /* An active event notifier forces aio_poll to look at EventNotifiers. */
421 event_notifier_init(&dummy
.e
, false);
422 set_event_notifier(ctx
, &dummy
.e
, event_ready_cb
);
424 event_notifier_set(&data
.e
);
425 g_assert(aio_poll(ctx
, false));
426 g_assert_cmpint(data
.n
, ==, 1);
427 g_assert(!aio_poll(ctx
, false));
428 g_assert_cmpint(data
.n
, ==, 1);
430 event_notifier_set(&data
.e
);
431 g_assert(aio_poll(ctx
, false));
432 g_assert_cmpint(data
.n
, ==, 2);
433 g_assert(!aio_poll(ctx
, false));
434 g_assert_cmpint(data
.n
, ==, 2);
436 event_notifier_set(&dummy
.e
);
437 wait_until_inactive(&dummy
);
438 g_assert_cmpint(data
.n
, ==, 2);
439 g_assert_cmpint(dummy
.n
, ==, 1);
440 g_assert_cmpint(dummy
.active
, ==, 0);
442 set_event_notifier(ctx
, &dummy
.e
, NULL
);
443 event_notifier_cleanup(&dummy
.e
);
445 set_event_notifier(ctx
, &data
.e
, NULL
);
446 g_assert(!aio_poll(ctx
, false));
447 g_assert_cmpint(data
.n
, ==, 2);
449 event_notifier_cleanup(&data
.e
);
452 static void test_timer_schedule(void)
454 TimerTestData data
= { .n
= 0, .ctx
= ctx
, .ns
= SCALE_MS
* 750LL,
456 .clock_type
= QEMU_CLOCK_VIRTUAL
};
459 /* aio_poll will not block to wait for timers to complete unless it has
460 * an fd to wait on. Fixing this breaks other tests. So create a dummy one.
462 event_notifier_init(&e
, false);
463 set_event_notifier(ctx
, &e
, dummy_io_handler_read
);
464 aio_poll(ctx
, false);
466 aio_timer_init(ctx
, &data
.timer
, data
.clock_type
,
467 SCALE_NS
, timer_test_cb
, &data
);
468 timer_mod(&data
.timer
,
469 qemu_clock_get_ns(data
.clock_type
) +
472 g_assert_cmpint(data
.n
, ==, 0);
474 /* timer_mod may well cause an event notifer to have gone off,
477 do {} while (aio_poll(ctx
, false));
479 g_assert(!aio_poll(ctx
, false));
480 g_assert_cmpint(data
.n
, ==, 0);
482 g_usleep(1 * G_USEC_PER_SEC
);
483 g_assert_cmpint(data
.n
, ==, 0);
485 g_assert(aio_poll(ctx
, false));
486 g_assert_cmpint(data
.n
, ==, 1);
488 /* timer_mod called by our callback */
489 do {} while (aio_poll(ctx
, false));
491 g_assert(!aio_poll(ctx
, false));
492 g_assert_cmpint(data
.n
, ==, 1);
494 g_assert(aio_poll(ctx
, true));
495 g_assert_cmpint(data
.n
, ==, 2);
497 /* As max is now 2, an event notifier should not have gone off */
499 g_assert(!aio_poll(ctx
, false));
500 g_assert_cmpint(data
.n
, ==, 2);
502 set_event_notifier(ctx
, &e
, NULL
);
503 event_notifier_cleanup(&e
);
505 timer_del(&data
.timer
);
508 /* Now the same tests, using the context as a GSource. They are
509 * very similar to the ones above, with g_main_context_iteration
510 * replacing aio_poll. However:
511 * - sometimes both the AioContext and the glib main loop wake
512 * themselves up. Hence, some "g_assert(!aio_poll(ctx, false));"
513 * are replaced by "while (g_main_context_iteration(NULL, false));".
514 * - there is no exact replacement for a blocking wait.
515 * "while (g_main_context_iteration(NULL, true)" seems to work,
516 * but it is not documented _why_ it works. For these tests a
517 * non-blocking loop like "while (g_main_context_iteration(NULL, false)"
518 * works well, and that's what I am using.
521 static void test_source_flush(void)
523 g_assert(!g_main_context_iteration(NULL
, false));
525 while (g_main_context_iteration(NULL
, false));
526 g_assert(!g_main_context_iteration(NULL
, false));
529 static void test_source_bh_schedule(void)
531 BHTestData data
= { .n
= 0 };
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
, true));
538 g_assert_cmpint(data
.n
, ==, 1);
540 g_assert(!g_main_context_iteration(NULL
, false));
541 g_assert_cmpint(data
.n
, ==, 1);
542 qemu_bh_delete(data
.bh
);
545 static void test_source_bh_schedule10(void)
547 BHTestData data
= { .n
= 0, .max
= 10 };
548 data
.bh
= aio_bh_new(ctx
, bh_test_cb
, &data
);
550 qemu_bh_schedule(data
.bh
);
551 g_assert_cmpint(data
.n
, ==, 0);
553 g_assert(g_main_context_iteration(NULL
, false));
554 g_assert_cmpint(data
.n
, ==, 1);
556 g_assert(g_main_context_iteration(NULL
, true));
557 g_assert_cmpint(data
.n
, ==, 2);
559 while (g_main_context_iteration(NULL
, false));
560 g_assert_cmpint(data
.n
, ==, 10);
562 g_assert(!g_main_context_iteration(NULL
, false));
563 g_assert_cmpint(data
.n
, ==, 10);
564 qemu_bh_delete(data
.bh
);
567 static void test_source_bh_cancel(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_cancel(data
.bh
);
576 g_assert_cmpint(data
.n
, ==, 0);
578 while (g_main_context_iteration(NULL
, false));
579 g_assert_cmpint(data
.n
, ==, 0);
580 qemu_bh_delete(data
.bh
);
583 static void test_source_bh_delete(void)
585 BHTestData data
= { .n
= 0 };
586 data
.bh
= aio_bh_new(ctx
, bh_test_cb
, &data
);
588 qemu_bh_schedule(data
.bh
);
589 g_assert_cmpint(data
.n
, ==, 0);
591 qemu_bh_delete(data
.bh
);
592 g_assert_cmpint(data
.n
, ==, 0);
594 while (g_main_context_iteration(NULL
, false));
595 g_assert_cmpint(data
.n
, ==, 0);
598 static void test_source_bh_delete_from_cb(void)
600 BHTestData data1
= { .n
= 0, .max
= 1 };
602 data1
.bh
= aio_bh_new(ctx
, bh_delete_cb
, &data1
);
604 qemu_bh_schedule(data1
.bh
);
605 g_assert_cmpint(data1
.n
, ==, 0);
607 g_main_context_iteration(NULL
, true);
608 g_assert_cmpint(data1
.n
, ==, data1
.max
);
609 g_assert(data1
.bh
== NULL
);
611 g_assert(!g_main_context_iteration(NULL
, false));
614 static void test_source_bh_delete_from_cb_many(void)
616 BHTestData data1
= { .n
= 0, .max
= 1 };
617 BHTestData data2
= { .n
= 0, .max
= 3 };
618 BHTestData data3
= { .n
= 0, .max
= 2 };
619 BHTestData data4
= { .n
= 0, .max
= 4 };
621 data1
.bh
= aio_bh_new(ctx
, bh_delete_cb
, &data1
);
622 data2
.bh
= aio_bh_new(ctx
, bh_delete_cb
, &data2
);
623 data3
.bh
= aio_bh_new(ctx
, bh_delete_cb
, &data3
);
624 data4
.bh
= aio_bh_new(ctx
, bh_delete_cb
, &data4
);
626 qemu_bh_schedule(data1
.bh
);
627 qemu_bh_schedule(data2
.bh
);
628 qemu_bh_schedule(data3
.bh
);
629 qemu_bh_schedule(data4
.bh
);
630 g_assert_cmpint(data1
.n
, ==, 0);
631 g_assert_cmpint(data2
.n
, ==, 0);
632 g_assert_cmpint(data3
.n
, ==, 0);
633 g_assert_cmpint(data4
.n
, ==, 0);
635 g_assert(g_main_context_iteration(NULL
, false));
636 g_assert_cmpint(data1
.n
, ==, 1);
637 g_assert_cmpint(data2
.n
, ==, 1);
638 g_assert_cmpint(data3
.n
, ==, 1);
639 g_assert_cmpint(data4
.n
, ==, 1);
640 g_assert(data1
.bh
== NULL
);
642 while (g_main_context_iteration(NULL
, false));
643 g_assert_cmpint(data1
.n
, ==, data1
.max
);
644 g_assert_cmpint(data2
.n
, ==, data2
.max
);
645 g_assert_cmpint(data3
.n
, ==, data3
.max
);
646 g_assert_cmpint(data4
.n
, ==, data4
.max
);
647 g_assert(data1
.bh
== NULL
);
648 g_assert(data2
.bh
== NULL
);
649 g_assert(data3
.bh
== NULL
);
650 g_assert(data4
.bh
== NULL
);
653 static void test_source_bh_flush(void)
655 BHTestData data
= { .n
= 0 };
656 data
.bh
= aio_bh_new(ctx
, bh_test_cb
, &data
);
658 qemu_bh_schedule(data
.bh
);
659 g_assert_cmpint(data
.n
, ==, 0);
661 g_assert(g_main_context_iteration(NULL
, true));
662 g_assert_cmpint(data
.n
, ==, 1);
664 g_assert(!g_main_context_iteration(NULL
, false));
665 g_assert_cmpint(data
.n
, ==, 1);
666 qemu_bh_delete(data
.bh
);
669 static void test_source_set_event_notifier(void)
671 EventNotifierTestData data
= { .n
= 0, .active
= 0 };
672 event_notifier_init(&data
.e
, false);
673 set_event_notifier(ctx
, &data
.e
, event_ready_cb
);
674 while (g_main_context_iteration(NULL
, false));
675 g_assert_cmpint(data
.n
, ==, 0);
677 set_event_notifier(ctx
, &data
.e
, NULL
);
678 while (g_main_context_iteration(NULL
, false));
679 g_assert_cmpint(data
.n
, ==, 0);
680 event_notifier_cleanup(&data
.e
);
683 static void test_source_wait_event_notifier(void)
685 EventNotifierTestData data
= { .n
= 0, .active
= 1 };
686 event_notifier_init(&data
.e
, false);
687 set_event_notifier(ctx
, &data
.e
, event_ready_cb
);
688 while (g_main_context_iteration(NULL
, false));
689 g_assert_cmpint(data
.n
, ==, 0);
690 g_assert_cmpint(data
.active
, ==, 1);
692 event_notifier_set(&data
.e
);
693 g_assert(g_main_context_iteration(NULL
, false));
694 g_assert_cmpint(data
.n
, ==, 1);
695 g_assert_cmpint(data
.active
, ==, 0);
697 while (g_main_context_iteration(NULL
, false));
698 g_assert_cmpint(data
.n
, ==, 1);
699 g_assert_cmpint(data
.active
, ==, 0);
701 set_event_notifier(ctx
, &data
.e
, NULL
);
702 while (g_main_context_iteration(NULL
, false));
703 g_assert_cmpint(data
.n
, ==, 1);
705 event_notifier_cleanup(&data
.e
);
708 static void test_source_flush_event_notifier(void)
710 EventNotifierTestData data
= { .n
= 0, .active
= 10, .auto_set
= true };
711 event_notifier_init(&data
.e
, false);
712 set_event_notifier(ctx
, &data
.e
, event_ready_cb
);
713 while (g_main_context_iteration(NULL
, false));
714 g_assert_cmpint(data
.n
, ==, 0);
715 g_assert_cmpint(data
.active
, ==, 10);
717 event_notifier_set(&data
.e
);
718 g_assert(g_main_context_iteration(NULL
, false));
719 g_assert_cmpint(data
.n
, ==, 1);
720 g_assert_cmpint(data
.active
, ==, 9);
721 g_assert(g_main_context_iteration(NULL
, false));
723 while (g_main_context_iteration(NULL
, false));
724 g_assert_cmpint(data
.n
, ==, 10);
725 g_assert_cmpint(data
.active
, ==, 0);
726 g_assert(!g_main_context_iteration(NULL
, false));
728 set_event_notifier(ctx
, &data
.e
, NULL
);
729 while (g_main_context_iteration(NULL
, false));
730 event_notifier_cleanup(&data
.e
);
733 static void test_source_wait_event_notifier_noflush(void)
735 EventNotifierTestData data
= { .n
= 0 };
736 EventNotifierTestData dummy
= { .n
= 0, .active
= 1 };
738 event_notifier_init(&data
.e
, false);
739 set_event_notifier(ctx
, &data
.e
, event_ready_cb
);
741 while (g_main_context_iteration(NULL
, false));
742 g_assert_cmpint(data
.n
, ==, 0);
744 /* Until there is an active descriptor, glib may or may not call
745 * event_ready_cb. Still, it must not block. */
746 event_notifier_set(&data
.e
);
747 g_main_context_iteration(NULL
, true);
750 /* An active event notifier forces aio_poll to look at EventNotifiers. */
751 event_notifier_init(&dummy
.e
, false);
752 set_event_notifier(ctx
, &dummy
.e
, event_ready_cb
);
754 event_notifier_set(&data
.e
);
755 g_assert(g_main_context_iteration(NULL
, false));
756 g_assert_cmpint(data
.n
, ==, 1);
757 g_assert(!g_main_context_iteration(NULL
, false));
758 g_assert_cmpint(data
.n
, ==, 1);
760 event_notifier_set(&data
.e
);
761 g_assert(g_main_context_iteration(NULL
, false));
762 g_assert_cmpint(data
.n
, ==, 2);
763 g_assert(!g_main_context_iteration(NULL
, false));
764 g_assert_cmpint(data
.n
, ==, 2);
766 event_notifier_set(&dummy
.e
);
767 while (g_main_context_iteration(NULL
, false));
768 g_assert_cmpint(data
.n
, ==, 2);
769 g_assert_cmpint(dummy
.n
, ==, 1);
770 g_assert_cmpint(dummy
.active
, ==, 0);
772 set_event_notifier(ctx
, &dummy
.e
, NULL
);
773 event_notifier_cleanup(&dummy
.e
);
775 set_event_notifier(ctx
, &data
.e
, NULL
);
776 while (g_main_context_iteration(NULL
, false));
777 g_assert_cmpint(data
.n
, ==, 2);
779 event_notifier_cleanup(&data
.e
);
782 static void test_source_timer_schedule(void)
784 TimerTestData data
= { .n
= 0, .ctx
= ctx
, .ns
= SCALE_MS
* 750LL,
786 .clock_type
= QEMU_CLOCK_VIRTUAL
};
790 /* aio_poll will not block to wait for timers to complete unless it has
791 * an fd to wait on. Fixing this breaks other tests. So create a dummy one.
793 event_notifier_init(&e
, false);
794 set_event_notifier(ctx
, &e
, dummy_io_handler_read
);
795 do {} while (g_main_context_iteration(NULL
, false));
797 aio_timer_init(ctx
, &data
.timer
, data
.clock_type
,
798 SCALE_NS
, timer_test_cb
, &data
);
799 expiry
= qemu_clock_get_ns(data
.clock_type
) +
801 timer_mod(&data
.timer
, expiry
);
803 g_assert_cmpint(data
.n
, ==, 0);
805 g_usleep(1 * G_USEC_PER_SEC
);
806 g_assert_cmpint(data
.n
, ==, 0);
808 g_assert(g_main_context_iteration(NULL
, true));
809 g_assert_cmpint(data
.n
, ==, 1);
813 g_main_context_iteration(NULL
, true);
816 g_assert_cmpint(data
.n
, ==, 2);
817 g_assert(qemu_clock_get_ns(data
.clock_type
) > expiry
);
819 set_event_notifier(ctx
, &e
, NULL
);
820 event_notifier_cleanup(&e
);
822 timer_del(&data
.timer
);
828 int main(int argc
, char **argv
)
830 Error
*local_error
= NULL
;
835 ctx
= aio_context_new(&local_error
);
837 error_reportf_err(local_error
, "Failed to create AIO Context: ");
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/acquire", test_acquire
);
848 g_test_add_func("/aio/bh/schedule", test_bh_schedule
);
849 g_test_add_func("/aio/bh/schedule10", test_bh_schedule10
);
850 g_test_add_func("/aio/bh/cancel", test_bh_cancel
);
851 g_test_add_func("/aio/bh/delete", test_bh_delete
);
852 g_test_add_func("/aio/bh/callback-delete/one", test_bh_delete_from_cb
);
853 g_test_add_func("/aio/bh/callback-delete/many", test_bh_delete_from_cb_many
);
854 g_test_add_func("/aio/bh/flush", test_bh_flush
);
855 g_test_add_func("/aio/event/add-remove", test_set_event_notifier
);
856 g_test_add_func("/aio/event/wait", test_wait_event_notifier
);
857 g_test_add_func("/aio/event/wait/no-flush-cb", test_wait_event_notifier_noflush
);
858 g_test_add_func("/aio/event/flush", test_flush_event_notifier
);
859 g_test_add_func("/aio/external-client", test_aio_external_client
);
860 g_test_add_func("/aio/timer/schedule", test_timer_schedule
);
862 g_test_add_func("/aio-gsource/flush", test_source_flush
);
863 g_test_add_func("/aio-gsource/bh/schedule", test_source_bh_schedule
);
864 g_test_add_func("/aio-gsource/bh/schedule10", test_source_bh_schedule10
);
865 g_test_add_func("/aio-gsource/bh/cancel", test_source_bh_cancel
);
866 g_test_add_func("/aio-gsource/bh/delete", test_source_bh_delete
);
867 g_test_add_func("/aio-gsource/bh/callback-delete/one", test_source_bh_delete_from_cb
);
868 g_test_add_func("/aio-gsource/bh/callback-delete/many", test_source_bh_delete_from_cb_many
);
869 g_test_add_func("/aio-gsource/bh/flush", test_source_bh_flush
);
870 g_test_add_func("/aio-gsource/event/add-remove", test_source_set_event_notifier
);
871 g_test_add_func("/aio-gsource/event/wait", test_source_wait_event_notifier
);
872 g_test_add_func("/aio-gsource/event/wait/no-flush-cb", test_source_wait_event_notifier_noflush
);
873 g_test_add_func("/aio-gsource/event/flush", test_source_flush_event_notifier
);
874 g_test_add_func("/aio-gsource/timer/schedule", test_source_timer_schedule
);