]> git.proxmox.com Git - mirror_qemu.git/blob - tests/test-aio.c
AioContext: do not rely on aio_poll(ctx, true) result to end a loop
[mirror_qemu.git] / tests / test-aio.c
1 /*
2 * AioContext tests
3 *
4 * Copyright Red Hat, Inc. 2012
5 *
6 * Authors:
7 * Paolo Bonzini <pbonzini@redhat.com>
8 *
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.
11 */
12
13 #include <glib.h>
14 #include "block/aio.h"
15 #include "qemu/timer.h"
16 #include "qemu/sockets.h"
17
18 AioContext *ctx;
19
20 typedef struct {
21 EventNotifier e;
22 int n;
23 int active;
24 bool auto_set;
25 } EventNotifierTestData;
26
27 /* Wait until event notifier becomes inactive */
28 static void wait_until_inactive(EventNotifierTestData *data)
29 {
30 while (data->active > 0) {
31 aio_poll(ctx, true);
32 }
33 }
34
35 /* Simple callbacks for testing. */
36
37 typedef struct {
38 QEMUBH *bh;
39 int n;
40 int max;
41 } BHTestData;
42
43 typedef struct {
44 QEMUTimer timer;
45 QEMUClockType clock_type;
46 int n;
47 int max;
48 int64_t ns;
49 AioContext *ctx;
50 } TimerTestData;
51
52 static void bh_test_cb(void *opaque)
53 {
54 BHTestData *data = opaque;
55 if (++data->n < data->max) {
56 qemu_bh_schedule(data->bh);
57 }
58 }
59
60 #if !defined(_WIN32)
61
62 static void timer_test_cb(void *opaque)
63 {
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);
68 }
69 }
70
71 static void dummy_io_handler_read(void *opaque)
72 {
73 }
74
75 #endif /* !_WIN32 */
76
77 static void bh_delete_cb(void *opaque)
78 {
79 BHTestData *data = opaque;
80 if (++data->n < data->max) {
81 qemu_bh_schedule(data->bh);
82 } else {
83 qemu_bh_delete(data->bh);
84 data->bh = NULL;
85 }
86 }
87
88 static void event_ready_cb(EventNotifier *e)
89 {
90 EventNotifierTestData *data = container_of(e, EventNotifierTestData, e);
91 g_assert(event_notifier_test_and_clear(e));
92 data->n++;
93 if (data->active > 0) {
94 data->active--;
95 }
96 if (data->auto_set && data->active) {
97 event_notifier_set(e);
98 }
99 }
100
101 /* Tests using aio_*. */
102
103 static void test_notify(void)
104 {
105 g_assert(!aio_poll(ctx, false));
106 aio_notify(ctx);
107 g_assert(!aio_poll(ctx, true));
108 g_assert(!aio_poll(ctx, false));
109 }
110
111 typedef struct {
112 QemuMutex start_lock;
113 bool thread_acquired;
114 } AcquireTestData;
115
116 static void *test_acquire_thread(void *opaque)
117 {
118 AcquireTestData *data = opaque;
119
120 /* Wait for other thread to let us start */
121 qemu_mutex_lock(&data->start_lock);
122 qemu_mutex_unlock(&data->start_lock);
123
124 aio_context_acquire(ctx);
125 aio_context_release(ctx);
126
127 data->thread_acquired = true; /* success, we got here */
128
129 return NULL;
130 }
131
132 static void dummy_notifier_read(EventNotifier *unused)
133 {
134 g_assert(false); /* should never be invoked */
135 }
136
137 static void test_acquire(void)
138 {
139 QemuThread thread;
140 EventNotifier notifier;
141 AcquireTestData data;
142
143 /* Dummy event notifier ensures aio_poll() will block */
144 event_notifier_init(&notifier, false);
145 aio_set_event_notifier(ctx, &notifier, dummy_notifier_read);
146 g_assert(!aio_poll(ctx, false)); /* consume aio_notify() */
147
148 qemu_mutex_init(&data.start_lock);
149 qemu_mutex_lock(&data.start_lock);
150 data.thread_acquired = false;
151
152 qemu_thread_create(&thread, "test_acquire_thread",
153 test_acquire_thread,
154 &data, QEMU_THREAD_JOINABLE);
155
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);
161
162 qemu_thread_join(&thread);
163 aio_set_event_notifier(ctx, &notifier, NULL);
164 event_notifier_cleanup(&notifier);
165
166 g_assert(data.thread_acquired);
167 }
168
169 static void test_bh_schedule(void)
170 {
171 BHTestData data = { .n = 0 };
172 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
173
174 qemu_bh_schedule(data.bh);
175 g_assert_cmpint(data.n, ==, 0);
176
177 g_assert(aio_poll(ctx, true));
178 g_assert_cmpint(data.n, ==, 1);
179
180 g_assert(!aio_poll(ctx, false));
181 g_assert_cmpint(data.n, ==, 1);
182 qemu_bh_delete(data.bh);
183 }
184
185 static void test_bh_schedule10(void)
186 {
187 BHTestData data = { .n = 0, .max = 10 };
188 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
189
190 qemu_bh_schedule(data.bh);
191 g_assert_cmpint(data.n, ==, 0);
192
193 g_assert(aio_poll(ctx, false));
194 g_assert_cmpint(data.n, ==, 1);
195
196 g_assert(aio_poll(ctx, true));
197 g_assert_cmpint(data.n, ==, 2);
198
199 while (data.n < 10) {
200 aio_poll(ctx, true);
201 }
202 g_assert_cmpint(data.n, ==, 10);
203
204 g_assert(!aio_poll(ctx, false));
205 g_assert_cmpint(data.n, ==, 10);
206 qemu_bh_delete(data.bh);
207 }
208
209 static void test_bh_cancel(void)
210 {
211 BHTestData data = { .n = 0 };
212 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
213
214 qemu_bh_schedule(data.bh);
215 g_assert_cmpint(data.n, ==, 0);
216
217 qemu_bh_cancel(data.bh);
218 g_assert_cmpint(data.n, ==, 0);
219
220 g_assert(!aio_poll(ctx, false));
221 g_assert_cmpint(data.n, ==, 0);
222 qemu_bh_delete(data.bh);
223 }
224
225 static void test_bh_delete(void)
226 {
227 BHTestData data = { .n = 0 };
228 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
229
230 qemu_bh_schedule(data.bh);
231 g_assert_cmpint(data.n, ==, 0);
232
233 qemu_bh_delete(data.bh);
234 g_assert_cmpint(data.n, ==, 0);
235
236 g_assert(!aio_poll(ctx, false));
237 g_assert_cmpint(data.n, ==, 0);
238 }
239
240 static void test_bh_delete_from_cb(void)
241 {
242 BHTestData data1 = { .n = 0, .max = 1 };
243
244 data1.bh = aio_bh_new(ctx, bh_delete_cb, &data1);
245
246 qemu_bh_schedule(data1.bh);
247 g_assert_cmpint(data1.n, ==, 0);
248
249 while (data1.n < data1.max) {
250 aio_poll(ctx, true);
251 }
252 g_assert_cmpint(data1.n, ==, data1.max);
253 g_assert(data1.bh == NULL);
254
255 g_assert(!aio_poll(ctx, false));
256 }
257
258 static void test_bh_delete_from_cb_many(void)
259 {
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 };
264
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);
269
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);
278
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);
285
286 while (data1.n < data1.max ||
287 data2.n < data2.max ||
288 data3.n < data3.max ||
289 data4.n < data4.max) {
290 aio_poll(ctx, true);
291 }
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);
300 }
301
302 static void test_bh_flush(void)
303 {
304 BHTestData data = { .n = 0 };
305 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
306
307 qemu_bh_schedule(data.bh);
308 g_assert_cmpint(data.n, ==, 0);
309
310 g_assert(aio_poll(ctx, true));
311 g_assert_cmpint(data.n, ==, 1);
312
313 g_assert(!aio_poll(ctx, false));
314 g_assert_cmpint(data.n, ==, 1);
315 qemu_bh_delete(data.bh);
316 }
317
318 static void test_set_event_notifier(void)
319 {
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);
325
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);
330 }
331
332 static void test_wait_event_notifier(void)
333 {
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);
340
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);
345
346 g_assert(!aio_poll(ctx, false));
347 g_assert_cmpint(data.n, ==, 1);
348 g_assert_cmpint(data.active, ==, 0);
349
350 aio_set_event_notifier(ctx, &data.e, NULL);
351 g_assert(!aio_poll(ctx, false));
352 g_assert_cmpint(data.n, ==, 1);
353
354 event_notifier_cleanup(&data.e);
355 }
356
357 static void test_flush_event_notifier(void)
358 {
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);
365
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));
371
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));
376
377 aio_set_event_notifier(ctx, &data.e, NULL);
378 g_assert(!aio_poll(ctx, false));
379 event_notifier_cleanup(&data.e);
380 }
381
382 static void test_wait_event_notifier_noflush(void)
383 {
384 EventNotifierTestData data = { .n = 0 };
385 EventNotifierTestData dummy = { .n = 0, .active = 1 };
386
387 event_notifier_init(&data.e, false);
388 aio_set_event_notifier(ctx, &data.e, event_ready_cb);
389
390 g_assert(!aio_poll(ctx, false));
391 g_assert_cmpint(data.n, ==, 0);
392
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));
397 data.n = 0;
398
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);
402
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);
408
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);
414
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);
420
421 aio_set_event_notifier(ctx, &dummy.e, NULL);
422 event_notifier_cleanup(&dummy.e);
423
424 aio_set_event_notifier(ctx, &data.e, NULL);
425 g_assert(!aio_poll(ctx, false));
426 g_assert_cmpint(data.n, ==, 2);
427
428 event_notifier_cleanup(&data.e);
429 }
430
431 #if !defined(_WIN32)
432
433 static void test_timer_schedule(void)
434 {
435 TimerTestData data = { .n = 0, .ctx = ctx, .ns = SCALE_MS * 750LL,
436 .max = 2,
437 .clock_type = QEMU_CLOCK_VIRTUAL };
438 int pipefd[2];
439
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.
442 */
443 g_assert(!qemu_pipe(pipefd));
444 qemu_set_nonblock(pipefd[0]);
445 qemu_set_nonblock(pipefd[1]);
446
447 aio_set_fd_handler(ctx, pipefd[0],
448 dummy_io_handler_read, NULL, NULL);
449 aio_poll(ctx, false);
450
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) +
455 data.ns);
456
457 g_assert_cmpint(data.n, ==, 0);
458
459 /* timer_mod may well cause an event notifer to have gone off,
460 * so clear that
461 */
462 do {} while (aio_poll(ctx, false));
463
464 g_assert(!aio_poll(ctx, false));
465 g_assert_cmpint(data.n, ==, 0);
466
467 g_usleep(1 * G_USEC_PER_SEC);
468 g_assert_cmpint(data.n, ==, 0);
469
470 g_assert(aio_poll(ctx, false));
471 g_assert_cmpint(data.n, ==, 1);
472
473 /* timer_mod called by our callback */
474 do {} while (aio_poll(ctx, false));
475
476 g_assert(!aio_poll(ctx, false));
477 g_assert_cmpint(data.n, ==, 1);
478
479 g_assert(aio_poll(ctx, true));
480 g_assert_cmpint(data.n, ==, 2);
481
482 /* As max is now 2, an event notifier should not have gone off */
483
484 g_assert(!aio_poll(ctx, false));
485 g_assert_cmpint(data.n, ==, 2);
486
487 aio_set_fd_handler(ctx, pipefd[0], NULL, NULL, NULL);
488 close(pipefd[0]);
489 close(pipefd[1]);
490
491 timer_del(&data.timer);
492 }
493
494 #endif /* !_WIN32 */
495
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.
507 */
508
509 static void test_source_notify(void)
510 {
511 while (g_main_context_iteration(NULL, false));
512 aio_notify(ctx);
513 g_assert(g_main_context_iteration(NULL, true));
514 g_assert(!g_main_context_iteration(NULL, false));
515 }
516
517 static void test_source_flush(void)
518 {
519 g_assert(!g_main_context_iteration(NULL, false));
520 aio_notify(ctx);
521 while (g_main_context_iteration(NULL, false));
522 g_assert(!g_main_context_iteration(NULL, false));
523 }
524
525 static void test_source_bh_schedule(void)
526 {
527 BHTestData data = { .n = 0 };
528 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
529
530 qemu_bh_schedule(data.bh);
531 g_assert_cmpint(data.n, ==, 0);
532
533 g_assert(g_main_context_iteration(NULL, true));
534 g_assert_cmpint(data.n, ==, 1);
535
536 g_assert(!g_main_context_iteration(NULL, false));
537 g_assert_cmpint(data.n, ==, 1);
538 qemu_bh_delete(data.bh);
539 }
540
541 static void test_source_bh_schedule10(void)
542 {
543 BHTestData data = { .n = 0, .max = 10 };
544 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
545
546 qemu_bh_schedule(data.bh);
547 g_assert_cmpint(data.n, ==, 0);
548
549 g_assert(g_main_context_iteration(NULL, false));
550 g_assert_cmpint(data.n, ==, 1);
551
552 g_assert(g_main_context_iteration(NULL, true));
553 g_assert_cmpint(data.n, ==, 2);
554
555 while (g_main_context_iteration(NULL, false));
556 g_assert_cmpint(data.n, ==, 10);
557
558 g_assert(!g_main_context_iteration(NULL, false));
559 g_assert_cmpint(data.n, ==, 10);
560 qemu_bh_delete(data.bh);
561 }
562
563 static void test_source_bh_cancel(void)
564 {
565 BHTestData data = { .n = 0 };
566 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
567
568 qemu_bh_schedule(data.bh);
569 g_assert_cmpint(data.n, ==, 0);
570
571 qemu_bh_cancel(data.bh);
572 g_assert_cmpint(data.n, ==, 0);
573
574 while (g_main_context_iteration(NULL, false));
575 g_assert_cmpint(data.n, ==, 0);
576 qemu_bh_delete(data.bh);
577 }
578
579 static void test_source_bh_delete(void)
580 {
581 BHTestData data = { .n = 0 };
582 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
583
584 qemu_bh_schedule(data.bh);
585 g_assert_cmpint(data.n, ==, 0);
586
587 qemu_bh_delete(data.bh);
588 g_assert_cmpint(data.n, ==, 0);
589
590 while (g_main_context_iteration(NULL, false));
591 g_assert_cmpint(data.n, ==, 0);
592 }
593
594 static void test_source_bh_delete_from_cb(void)
595 {
596 BHTestData data1 = { .n = 0, .max = 1 };
597
598 data1.bh = aio_bh_new(ctx, bh_delete_cb, &data1);
599
600 qemu_bh_schedule(data1.bh);
601 g_assert_cmpint(data1.n, ==, 0);
602
603 g_main_context_iteration(NULL, true);
604 g_assert_cmpint(data1.n, ==, data1.max);
605 g_assert(data1.bh == NULL);
606
607 g_assert(!g_main_context_iteration(NULL, false));
608 }
609
610 static void test_source_bh_delete_from_cb_many(void)
611 {
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 };
616
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);
621
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);
630
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);
637
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);
647 }
648
649 static void test_source_bh_flush(void)
650 {
651 BHTestData data = { .n = 0 };
652 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
653
654 qemu_bh_schedule(data.bh);
655 g_assert_cmpint(data.n, ==, 0);
656
657 g_assert(g_main_context_iteration(NULL, true));
658 g_assert_cmpint(data.n, ==, 1);
659
660 g_assert(!g_main_context_iteration(NULL, false));
661 g_assert_cmpint(data.n, ==, 1);
662 qemu_bh_delete(data.bh);
663 }
664
665 static void test_source_set_event_notifier(void)
666 {
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);
672
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);
677 }
678
679 static void test_source_wait_event_notifier(void)
680 {
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);
687
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);
692
693 while (g_main_context_iteration(NULL, false));
694 g_assert_cmpint(data.n, ==, 1);
695 g_assert_cmpint(data.active, ==, 0);
696
697 aio_set_event_notifier(ctx, &data.e, NULL);
698 while (g_main_context_iteration(NULL, false));
699 g_assert_cmpint(data.n, ==, 1);
700
701 event_notifier_cleanup(&data.e);
702 }
703
704 static void test_source_flush_event_notifier(void)
705 {
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);
712
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));
718
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));
723
724 aio_set_event_notifier(ctx, &data.e, NULL);
725 while (g_main_context_iteration(NULL, false));
726 event_notifier_cleanup(&data.e);
727 }
728
729 static void test_source_wait_event_notifier_noflush(void)
730 {
731 EventNotifierTestData data = { .n = 0 };
732 EventNotifierTestData dummy = { .n = 0, .active = 1 };
733
734 event_notifier_init(&data.e, false);
735 aio_set_event_notifier(ctx, &data.e, event_ready_cb);
736
737 while (g_main_context_iteration(NULL, false));
738 g_assert_cmpint(data.n, ==, 0);
739
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);
744 data.n = 0;
745
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);
749
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);
755
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);
761
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);
767
768 aio_set_event_notifier(ctx, &dummy.e, NULL);
769 event_notifier_cleanup(&dummy.e);
770
771 aio_set_event_notifier(ctx, &data.e, NULL);
772 while (g_main_context_iteration(NULL, false));
773 g_assert_cmpint(data.n, ==, 2);
774
775 event_notifier_cleanup(&data.e);
776 }
777
778 #if !defined(_WIN32)
779
780 static void test_source_timer_schedule(void)
781 {
782 TimerTestData data = { .n = 0, .ctx = ctx, .ns = SCALE_MS * 750LL,
783 .max = 2,
784 .clock_type = QEMU_CLOCK_VIRTUAL };
785 int pipefd[2];
786 int64_t expiry;
787
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.
790 */
791 g_assert(!qemu_pipe(pipefd));
792 qemu_set_nonblock(pipefd[0]);
793 qemu_set_nonblock(pipefd[1]);
794
795 aio_set_fd_handler(ctx, pipefd[0],
796 dummy_io_handler_read, NULL, NULL);
797 do {} while (g_main_context_iteration(NULL, false));
798
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) +
802 data.ns;
803 timer_mod(&data.timer, expiry);
804
805 g_assert_cmpint(data.n, ==, 0);
806
807 g_usleep(1 * G_USEC_PER_SEC);
808 g_assert_cmpint(data.n, ==, 0);
809
810 g_assert(g_main_context_iteration(NULL, true));
811 g_assert_cmpint(data.n, ==, 1);
812 expiry += data.ns;
813
814 while (data.n < 2) {
815 g_main_context_iteration(NULL, true);
816 }
817
818 g_assert_cmpint(data.n, ==, 2);
819 g_assert(qemu_clock_get_ns(data.clock_type) > expiry);
820
821 aio_set_fd_handler(ctx, pipefd[0], NULL, NULL, NULL);
822 close(pipefd[0]);
823 close(pipefd[1]);
824
825 timer_del(&data.timer);
826 }
827
828 #endif /* !_WIN32 */
829
830
831 /* End of tests. */
832
833 int main(int argc, char **argv)
834 {
835 GSource *src;
836
837 init_clocks();
838
839 ctx = aio_context_new();
840 src = aio_get_g_source(ctx);
841 g_source_attach(src, NULL);
842 g_source_unref(src);
843
844 while (g_main_context_iteration(NULL, false));
845
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);
860 #if !defined(_WIN32)
861 g_test_add_func("/aio/timer/schedule", test_timer_schedule);
862 #endif
863
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);
877 #if !defined(_WIN32)
878 g_test_add_func("/aio-gsource/timer/schedule", test_source_timer_schedule);
879 #endif
880 return g_test_run();
881 }