]> git.proxmox.com Git - mirror_qemu.git/blob - tests/test-aio.c
Merge remote-tracking branch 'remotes/borntraeger/tags/kvm-s390-20140901' into staging
[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 static 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 static void timer_test_cb(void *opaque)
61 {
62 TimerTestData *data = opaque;
63 if (++data->n < data->max) {
64 timer_mod(&data->timer,
65 qemu_clock_get_ns(data->clock_type) + data->ns);
66 }
67 }
68
69 static void dummy_io_handler_read(EventNotifier *e)
70 {
71 }
72
73 static void bh_delete_cb(void *opaque)
74 {
75 BHTestData *data = opaque;
76 if (++data->n < data->max) {
77 qemu_bh_schedule(data->bh);
78 } else {
79 qemu_bh_delete(data->bh);
80 data->bh = NULL;
81 }
82 }
83
84 static void event_ready_cb(EventNotifier *e)
85 {
86 EventNotifierTestData *data = container_of(e, EventNotifierTestData, e);
87 g_assert(event_notifier_test_and_clear(e));
88 data->n++;
89 if (data->active > 0) {
90 data->active--;
91 }
92 if (data->auto_set && data->active) {
93 event_notifier_set(e);
94 }
95 }
96
97 /* Tests using aio_*. */
98
99 static void test_notify(void)
100 {
101 g_assert(!aio_poll(ctx, false));
102 aio_notify(ctx);
103 g_assert(!aio_poll(ctx, true));
104 g_assert(!aio_poll(ctx, false));
105 }
106
107 typedef struct {
108 QemuMutex start_lock;
109 bool thread_acquired;
110 } AcquireTestData;
111
112 static void *test_acquire_thread(void *opaque)
113 {
114 AcquireTestData *data = opaque;
115
116 /* Wait for other thread to let us start */
117 qemu_mutex_lock(&data->start_lock);
118 qemu_mutex_unlock(&data->start_lock);
119
120 aio_context_acquire(ctx);
121 aio_context_release(ctx);
122
123 data->thread_acquired = true; /* success, we got here */
124
125 return NULL;
126 }
127
128 static void dummy_notifier_read(EventNotifier *unused)
129 {
130 g_assert(false); /* should never be invoked */
131 }
132
133 static void test_acquire(void)
134 {
135 QemuThread thread;
136 EventNotifier notifier;
137 AcquireTestData data;
138
139 /* Dummy event notifier ensures aio_poll() will block */
140 event_notifier_init(&notifier, false);
141 aio_set_event_notifier(ctx, &notifier, dummy_notifier_read);
142 g_assert(!aio_poll(ctx, false)); /* consume aio_notify() */
143
144 qemu_mutex_init(&data.start_lock);
145 qemu_mutex_lock(&data.start_lock);
146 data.thread_acquired = false;
147
148 qemu_thread_create(&thread, "test_acquire_thread",
149 test_acquire_thread,
150 &data, QEMU_THREAD_JOINABLE);
151
152 /* Block in aio_poll(), let other thread kick us and acquire context */
153 aio_context_acquire(ctx);
154 qemu_mutex_unlock(&data.start_lock); /* let the thread run */
155 g_assert(!aio_poll(ctx, true));
156 aio_context_release(ctx);
157
158 qemu_thread_join(&thread);
159 aio_set_event_notifier(ctx, &notifier, NULL);
160 event_notifier_cleanup(&notifier);
161
162 g_assert(data.thread_acquired);
163 }
164
165 static void test_bh_schedule(void)
166 {
167 BHTestData data = { .n = 0 };
168 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
169
170 qemu_bh_schedule(data.bh);
171 g_assert_cmpint(data.n, ==, 0);
172
173 g_assert(aio_poll(ctx, true));
174 g_assert_cmpint(data.n, ==, 1);
175
176 g_assert(!aio_poll(ctx, false));
177 g_assert_cmpint(data.n, ==, 1);
178 qemu_bh_delete(data.bh);
179 }
180
181 static void test_bh_schedule10(void)
182 {
183 BHTestData data = { .n = 0, .max = 10 };
184 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
185
186 qemu_bh_schedule(data.bh);
187 g_assert_cmpint(data.n, ==, 0);
188
189 g_assert(aio_poll(ctx, false));
190 g_assert_cmpint(data.n, ==, 1);
191
192 g_assert(aio_poll(ctx, true));
193 g_assert_cmpint(data.n, ==, 2);
194
195 while (data.n < 10) {
196 aio_poll(ctx, true);
197 }
198 g_assert_cmpint(data.n, ==, 10);
199
200 g_assert(!aio_poll(ctx, false));
201 g_assert_cmpint(data.n, ==, 10);
202 qemu_bh_delete(data.bh);
203 }
204
205 static void test_bh_cancel(void)
206 {
207 BHTestData data = { .n = 0 };
208 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
209
210 qemu_bh_schedule(data.bh);
211 g_assert_cmpint(data.n, ==, 0);
212
213 qemu_bh_cancel(data.bh);
214 g_assert_cmpint(data.n, ==, 0);
215
216 g_assert(!aio_poll(ctx, false));
217 g_assert_cmpint(data.n, ==, 0);
218 qemu_bh_delete(data.bh);
219 }
220
221 static void test_bh_delete(void)
222 {
223 BHTestData data = { .n = 0 };
224 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
225
226 qemu_bh_schedule(data.bh);
227 g_assert_cmpint(data.n, ==, 0);
228
229 qemu_bh_delete(data.bh);
230 g_assert_cmpint(data.n, ==, 0);
231
232 g_assert(!aio_poll(ctx, false));
233 g_assert_cmpint(data.n, ==, 0);
234 }
235
236 static void test_bh_delete_from_cb(void)
237 {
238 BHTestData data1 = { .n = 0, .max = 1 };
239
240 data1.bh = aio_bh_new(ctx, bh_delete_cb, &data1);
241
242 qemu_bh_schedule(data1.bh);
243 g_assert_cmpint(data1.n, ==, 0);
244
245 while (data1.n < data1.max) {
246 aio_poll(ctx, true);
247 }
248 g_assert_cmpint(data1.n, ==, data1.max);
249 g_assert(data1.bh == NULL);
250
251 g_assert(!aio_poll(ctx, false));
252 }
253
254 static void test_bh_delete_from_cb_many(void)
255 {
256 BHTestData data1 = { .n = 0, .max = 1 };
257 BHTestData data2 = { .n = 0, .max = 3 };
258 BHTestData data3 = { .n = 0, .max = 2 };
259 BHTestData data4 = { .n = 0, .max = 4 };
260
261 data1.bh = aio_bh_new(ctx, bh_delete_cb, &data1);
262 data2.bh = aio_bh_new(ctx, bh_delete_cb, &data2);
263 data3.bh = aio_bh_new(ctx, bh_delete_cb, &data3);
264 data4.bh = aio_bh_new(ctx, bh_delete_cb, &data4);
265
266 qemu_bh_schedule(data1.bh);
267 qemu_bh_schedule(data2.bh);
268 qemu_bh_schedule(data3.bh);
269 qemu_bh_schedule(data4.bh);
270 g_assert_cmpint(data1.n, ==, 0);
271 g_assert_cmpint(data2.n, ==, 0);
272 g_assert_cmpint(data3.n, ==, 0);
273 g_assert_cmpint(data4.n, ==, 0);
274
275 g_assert(aio_poll(ctx, false));
276 g_assert_cmpint(data1.n, ==, 1);
277 g_assert_cmpint(data2.n, ==, 1);
278 g_assert_cmpint(data3.n, ==, 1);
279 g_assert_cmpint(data4.n, ==, 1);
280 g_assert(data1.bh == NULL);
281
282 while (data1.n < data1.max ||
283 data2.n < data2.max ||
284 data3.n < data3.max ||
285 data4.n < data4.max) {
286 aio_poll(ctx, true);
287 }
288 g_assert_cmpint(data1.n, ==, data1.max);
289 g_assert_cmpint(data2.n, ==, data2.max);
290 g_assert_cmpint(data3.n, ==, data3.max);
291 g_assert_cmpint(data4.n, ==, data4.max);
292 g_assert(data1.bh == NULL);
293 g_assert(data2.bh == NULL);
294 g_assert(data3.bh == NULL);
295 g_assert(data4.bh == NULL);
296 }
297
298 static void test_bh_flush(void)
299 {
300 BHTestData data = { .n = 0 };
301 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
302
303 qemu_bh_schedule(data.bh);
304 g_assert_cmpint(data.n, ==, 0);
305
306 g_assert(aio_poll(ctx, true));
307 g_assert_cmpint(data.n, ==, 1);
308
309 g_assert(!aio_poll(ctx, false));
310 g_assert_cmpint(data.n, ==, 1);
311 qemu_bh_delete(data.bh);
312 }
313
314 static void test_set_event_notifier(void)
315 {
316 EventNotifierTestData data = { .n = 0, .active = 0 };
317 event_notifier_init(&data.e, false);
318 aio_set_event_notifier(ctx, &data.e, event_ready_cb);
319 g_assert(!aio_poll(ctx, false));
320 g_assert_cmpint(data.n, ==, 0);
321
322 aio_set_event_notifier(ctx, &data.e, NULL);
323 g_assert(!aio_poll(ctx, false));
324 g_assert_cmpint(data.n, ==, 0);
325 event_notifier_cleanup(&data.e);
326 }
327
328 static void test_wait_event_notifier(void)
329 {
330 EventNotifierTestData data = { .n = 0, .active = 1 };
331 event_notifier_init(&data.e, false);
332 aio_set_event_notifier(ctx, &data.e, event_ready_cb);
333 g_assert(!aio_poll(ctx, false));
334 g_assert_cmpint(data.n, ==, 0);
335 g_assert_cmpint(data.active, ==, 1);
336
337 event_notifier_set(&data.e);
338 g_assert(aio_poll(ctx, false));
339 g_assert_cmpint(data.n, ==, 1);
340 g_assert_cmpint(data.active, ==, 0);
341
342 g_assert(!aio_poll(ctx, false));
343 g_assert_cmpint(data.n, ==, 1);
344 g_assert_cmpint(data.active, ==, 0);
345
346 aio_set_event_notifier(ctx, &data.e, NULL);
347 g_assert(!aio_poll(ctx, false));
348 g_assert_cmpint(data.n, ==, 1);
349
350 event_notifier_cleanup(&data.e);
351 }
352
353 static void test_flush_event_notifier(void)
354 {
355 EventNotifierTestData data = { .n = 0, .active = 10, .auto_set = true };
356 event_notifier_init(&data.e, false);
357 aio_set_event_notifier(ctx, &data.e, event_ready_cb);
358 g_assert(!aio_poll(ctx, false));
359 g_assert_cmpint(data.n, ==, 0);
360 g_assert_cmpint(data.active, ==, 10);
361
362 event_notifier_set(&data.e);
363 g_assert(aio_poll(ctx, false));
364 g_assert_cmpint(data.n, ==, 1);
365 g_assert_cmpint(data.active, ==, 9);
366 g_assert(aio_poll(ctx, false));
367
368 wait_until_inactive(&data);
369 g_assert_cmpint(data.n, ==, 10);
370 g_assert_cmpint(data.active, ==, 0);
371 g_assert(!aio_poll(ctx, false));
372
373 aio_set_event_notifier(ctx, &data.e, NULL);
374 g_assert(!aio_poll(ctx, false));
375 event_notifier_cleanup(&data.e);
376 }
377
378 static void test_wait_event_notifier_noflush(void)
379 {
380 EventNotifierTestData data = { .n = 0 };
381 EventNotifierTestData dummy = { .n = 0, .active = 1 };
382
383 event_notifier_init(&data.e, false);
384 aio_set_event_notifier(ctx, &data.e, event_ready_cb);
385
386 g_assert(!aio_poll(ctx, false));
387 g_assert_cmpint(data.n, ==, 0);
388
389 /* Until there is an active descriptor, aio_poll may or may not call
390 * event_ready_cb. Still, it must not block. */
391 event_notifier_set(&data.e);
392 g_assert(aio_poll(ctx, true));
393 data.n = 0;
394
395 /* An active event notifier forces aio_poll to look at EventNotifiers. */
396 event_notifier_init(&dummy.e, false);
397 aio_set_event_notifier(ctx, &dummy.e, event_ready_cb);
398
399 event_notifier_set(&data.e);
400 g_assert(aio_poll(ctx, false));
401 g_assert_cmpint(data.n, ==, 1);
402 g_assert(!aio_poll(ctx, false));
403 g_assert_cmpint(data.n, ==, 1);
404
405 event_notifier_set(&data.e);
406 g_assert(aio_poll(ctx, false));
407 g_assert_cmpint(data.n, ==, 2);
408 g_assert(!aio_poll(ctx, false));
409 g_assert_cmpint(data.n, ==, 2);
410
411 event_notifier_set(&dummy.e);
412 wait_until_inactive(&dummy);
413 g_assert_cmpint(data.n, ==, 2);
414 g_assert_cmpint(dummy.n, ==, 1);
415 g_assert_cmpint(dummy.active, ==, 0);
416
417 aio_set_event_notifier(ctx, &dummy.e, NULL);
418 event_notifier_cleanup(&dummy.e);
419
420 aio_set_event_notifier(ctx, &data.e, NULL);
421 g_assert(!aio_poll(ctx, false));
422 g_assert_cmpint(data.n, ==, 2);
423
424 event_notifier_cleanup(&data.e);
425 }
426
427 static void test_timer_schedule(void)
428 {
429 TimerTestData data = { .n = 0, .ctx = ctx, .ns = SCALE_MS * 750LL,
430 .max = 2,
431 .clock_type = QEMU_CLOCK_VIRTUAL };
432 EventNotifier e;
433
434 /* aio_poll will not block to wait for timers to complete unless it has
435 * an fd to wait on. Fixing this breaks other tests. So create a dummy one.
436 */
437 event_notifier_init(&e, false);
438 aio_set_event_notifier(ctx, &e, dummy_io_handler_read);
439 aio_poll(ctx, false);
440
441 aio_timer_init(ctx, &data.timer, data.clock_type,
442 SCALE_NS, timer_test_cb, &data);
443 timer_mod(&data.timer,
444 qemu_clock_get_ns(data.clock_type) +
445 data.ns);
446
447 g_assert_cmpint(data.n, ==, 0);
448
449 /* timer_mod may well cause an event notifer to have gone off,
450 * so clear that
451 */
452 do {} while (aio_poll(ctx, false));
453
454 g_assert(!aio_poll(ctx, false));
455 g_assert_cmpint(data.n, ==, 0);
456
457 g_usleep(1 * G_USEC_PER_SEC);
458 g_assert_cmpint(data.n, ==, 0);
459
460 g_assert(aio_poll(ctx, false));
461 g_assert_cmpint(data.n, ==, 1);
462
463 /* timer_mod called by our callback */
464 do {} while (aio_poll(ctx, false));
465
466 g_assert(!aio_poll(ctx, false));
467 g_assert_cmpint(data.n, ==, 1);
468
469 g_assert(aio_poll(ctx, true));
470 g_assert_cmpint(data.n, ==, 2);
471
472 /* As max is now 2, an event notifier should not have gone off */
473
474 g_assert(!aio_poll(ctx, false));
475 g_assert_cmpint(data.n, ==, 2);
476
477 aio_set_event_notifier(ctx, &e, NULL);
478 event_notifier_cleanup(&e);
479
480 timer_del(&data.timer);
481 }
482
483 /* Now the same tests, using the context as a GSource. They are
484 * very similar to the ones above, with g_main_context_iteration
485 * replacing aio_poll. However:
486 * - sometimes both the AioContext and the glib main loop wake
487 * themselves up. Hence, some "g_assert(!aio_poll(ctx, false));"
488 * are replaced by "while (g_main_context_iteration(NULL, false));".
489 * - there is no exact replacement for a blocking wait.
490 * "while (g_main_context_iteration(NULL, true)" seems to work,
491 * but it is not documented _why_ it works. For these tests a
492 * non-blocking loop like "while (g_main_context_iteration(NULL, false)"
493 * works well, and that's what I am using.
494 */
495
496 static void test_source_notify(void)
497 {
498 while (g_main_context_iteration(NULL, false));
499 aio_notify(ctx);
500 g_assert(g_main_context_iteration(NULL, true));
501 g_assert(!g_main_context_iteration(NULL, false));
502 }
503
504 static void test_source_flush(void)
505 {
506 g_assert(!g_main_context_iteration(NULL, false));
507 aio_notify(ctx);
508 while (g_main_context_iteration(NULL, false));
509 g_assert(!g_main_context_iteration(NULL, false));
510 }
511
512 static void test_source_bh_schedule(void)
513 {
514 BHTestData data = { .n = 0 };
515 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
516
517 qemu_bh_schedule(data.bh);
518 g_assert_cmpint(data.n, ==, 0);
519
520 g_assert(g_main_context_iteration(NULL, true));
521 g_assert_cmpint(data.n, ==, 1);
522
523 g_assert(!g_main_context_iteration(NULL, false));
524 g_assert_cmpint(data.n, ==, 1);
525 qemu_bh_delete(data.bh);
526 }
527
528 static void test_source_bh_schedule10(void)
529 {
530 BHTestData data = { .n = 0, .max = 10 };
531 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
532
533 qemu_bh_schedule(data.bh);
534 g_assert_cmpint(data.n, ==, 0);
535
536 g_assert(g_main_context_iteration(NULL, false));
537 g_assert_cmpint(data.n, ==, 1);
538
539 g_assert(g_main_context_iteration(NULL, true));
540 g_assert_cmpint(data.n, ==, 2);
541
542 while (g_main_context_iteration(NULL, false));
543 g_assert_cmpint(data.n, ==, 10);
544
545 g_assert(!g_main_context_iteration(NULL, false));
546 g_assert_cmpint(data.n, ==, 10);
547 qemu_bh_delete(data.bh);
548 }
549
550 static void test_source_bh_cancel(void)
551 {
552 BHTestData data = { .n = 0 };
553 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
554
555 qemu_bh_schedule(data.bh);
556 g_assert_cmpint(data.n, ==, 0);
557
558 qemu_bh_cancel(data.bh);
559 g_assert_cmpint(data.n, ==, 0);
560
561 while (g_main_context_iteration(NULL, false));
562 g_assert_cmpint(data.n, ==, 0);
563 qemu_bh_delete(data.bh);
564 }
565
566 static void test_source_bh_delete(void)
567 {
568 BHTestData data = { .n = 0 };
569 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
570
571 qemu_bh_schedule(data.bh);
572 g_assert_cmpint(data.n, ==, 0);
573
574 qemu_bh_delete(data.bh);
575 g_assert_cmpint(data.n, ==, 0);
576
577 while (g_main_context_iteration(NULL, false));
578 g_assert_cmpint(data.n, ==, 0);
579 }
580
581 static void test_source_bh_delete_from_cb(void)
582 {
583 BHTestData data1 = { .n = 0, .max = 1 };
584
585 data1.bh = aio_bh_new(ctx, bh_delete_cb, &data1);
586
587 qemu_bh_schedule(data1.bh);
588 g_assert_cmpint(data1.n, ==, 0);
589
590 g_main_context_iteration(NULL, true);
591 g_assert_cmpint(data1.n, ==, data1.max);
592 g_assert(data1.bh == NULL);
593
594 g_assert(!g_main_context_iteration(NULL, false));
595 }
596
597 static void test_source_bh_delete_from_cb_many(void)
598 {
599 BHTestData data1 = { .n = 0, .max = 1 };
600 BHTestData data2 = { .n = 0, .max = 3 };
601 BHTestData data3 = { .n = 0, .max = 2 };
602 BHTestData data4 = { .n = 0, .max = 4 };
603
604 data1.bh = aio_bh_new(ctx, bh_delete_cb, &data1);
605 data2.bh = aio_bh_new(ctx, bh_delete_cb, &data2);
606 data3.bh = aio_bh_new(ctx, bh_delete_cb, &data3);
607 data4.bh = aio_bh_new(ctx, bh_delete_cb, &data4);
608
609 qemu_bh_schedule(data1.bh);
610 qemu_bh_schedule(data2.bh);
611 qemu_bh_schedule(data3.bh);
612 qemu_bh_schedule(data4.bh);
613 g_assert_cmpint(data1.n, ==, 0);
614 g_assert_cmpint(data2.n, ==, 0);
615 g_assert_cmpint(data3.n, ==, 0);
616 g_assert_cmpint(data4.n, ==, 0);
617
618 g_assert(g_main_context_iteration(NULL, false));
619 g_assert_cmpint(data1.n, ==, 1);
620 g_assert_cmpint(data2.n, ==, 1);
621 g_assert_cmpint(data3.n, ==, 1);
622 g_assert_cmpint(data4.n, ==, 1);
623 g_assert(data1.bh == NULL);
624
625 while (g_main_context_iteration(NULL, false));
626 g_assert_cmpint(data1.n, ==, data1.max);
627 g_assert_cmpint(data2.n, ==, data2.max);
628 g_assert_cmpint(data3.n, ==, data3.max);
629 g_assert_cmpint(data4.n, ==, data4.max);
630 g_assert(data1.bh == NULL);
631 g_assert(data2.bh == NULL);
632 g_assert(data3.bh == NULL);
633 g_assert(data4.bh == NULL);
634 }
635
636 static void test_source_bh_flush(void)
637 {
638 BHTestData data = { .n = 0 };
639 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
640
641 qemu_bh_schedule(data.bh);
642 g_assert_cmpint(data.n, ==, 0);
643
644 g_assert(g_main_context_iteration(NULL, true));
645 g_assert_cmpint(data.n, ==, 1);
646
647 g_assert(!g_main_context_iteration(NULL, false));
648 g_assert_cmpint(data.n, ==, 1);
649 qemu_bh_delete(data.bh);
650 }
651
652 static void test_source_set_event_notifier(void)
653 {
654 EventNotifierTestData data = { .n = 0, .active = 0 };
655 event_notifier_init(&data.e, false);
656 aio_set_event_notifier(ctx, &data.e, event_ready_cb);
657 while (g_main_context_iteration(NULL, false));
658 g_assert_cmpint(data.n, ==, 0);
659
660 aio_set_event_notifier(ctx, &data.e, NULL);
661 while (g_main_context_iteration(NULL, false));
662 g_assert_cmpint(data.n, ==, 0);
663 event_notifier_cleanup(&data.e);
664 }
665
666 static void test_source_wait_event_notifier(void)
667 {
668 EventNotifierTestData data = { .n = 0, .active = 1 };
669 event_notifier_init(&data.e, false);
670 aio_set_event_notifier(ctx, &data.e, event_ready_cb);
671 g_assert(g_main_context_iteration(NULL, false));
672 g_assert_cmpint(data.n, ==, 0);
673 g_assert_cmpint(data.active, ==, 1);
674
675 event_notifier_set(&data.e);
676 g_assert(g_main_context_iteration(NULL, false));
677 g_assert_cmpint(data.n, ==, 1);
678 g_assert_cmpint(data.active, ==, 0);
679
680 while (g_main_context_iteration(NULL, false));
681 g_assert_cmpint(data.n, ==, 1);
682 g_assert_cmpint(data.active, ==, 0);
683
684 aio_set_event_notifier(ctx, &data.e, NULL);
685 while (g_main_context_iteration(NULL, false));
686 g_assert_cmpint(data.n, ==, 1);
687
688 event_notifier_cleanup(&data.e);
689 }
690
691 static void test_source_flush_event_notifier(void)
692 {
693 EventNotifierTestData data = { .n = 0, .active = 10, .auto_set = true };
694 event_notifier_init(&data.e, false);
695 aio_set_event_notifier(ctx, &data.e, event_ready_cb);
696 g_assert(g_main_context_iteration(NULL, false));
697 g_assert_cmpint(data.n, ==, 0);
698 g_assert_cmpint(data.active, ==, 10);
699
700 event_notifier_set(&data.e);
701 g_assert(g_main_context_iteration(NULL, false));
702 g_assert_cmpint(data.n, ==, 1);
703 g_assert_cmpint(data.active, ==, 9);
704 g_assert(g_main_context_iteration(NULL, false));
705
706 while (g_main_context_iteration(NULL, false));
707 g_assert_cmpint(data.n, ==, 10);
708 g_assert_cmpint(data.active, ==, 0);
709 g_assert(!g_main_context_iteration(NULL, false));
710
711 aio_set_event_notifier(ctx, &data.e, NULL);
712 while (g_main_context_iteration(NULL, false));
713 event_notifier_cleanup(&data.e);
714 }
715
716 static void test_source_wait_event_notifier_noflush(void)
717 {
718 EventNotifierTestData data = { .n = 0 };
719 EventNotifierTestData dummy = { .n = 0, .active = 1 };
720
721 event_notifier_init(&data.e, false);
722 aio_set_event_notifier(ctx, &data.e, event_ready_cb);
723
724 while (g_main_context_iteration(NULL, false));
725 g_assert_cmpint(data.n, ==, 0);
726
727 /* Until there is an active descriptor, glib may or may not call
728 * event_ready_cb. Still, it must not block. */
729 event_notifier_set(&data.e);
730 g_main_context_iteration(NULL, true);
731 data.n = 0;
732
733 /* An active event notifier forces aio_poll to look at EventNotifiers. */
734 event_notifier_init(&dummy.e, false);
735 aio_set_event_notifier(ctx, &dummy.e, event_ready_cb);
736
737 event_notifier_set(&data.e);
738 g_assert(g_main_context_iteration(NULL, false));
739 g_assert_cmpint(data.n, ==, 1);
740 g_assert(!g_main_context_iteration(NULL, false));
741 g_assert_cmpint(data.n, ==, 1);
742
743 event_notifier_set(&data.e);
744 g_assert(g_main_context_iteration(NULL, false));
745 g_assert_cmpint(data.n, ==, 2);
746 g_assert(!g_main_context_iteration(NULL, false));
747 g_assert_cmpint(data.n, ==, 2);
748
749 event_notifier_set(&dummy.e);
750 while (g_main_context_iteration(NULL, false));
751 g_assert_cmpint(data.n, ==, 2);
752 g_assert_cmpint(dummy.n, ==, 1);
753 g_assert_cmpint(dummy.active, ==, 0);
754
755 aio_set_event_notifier(ctx, &dummy.e, NULL);
756 event_notifier_cleanup(&dummy.e);
757
758 aio_set_event_notifier(ctx, &data.e, NULL);
759 while (g_main_context_iteration(NULL, false));
760 g_assert_cmpint(data.n, ==, 2);
761
762 event_notifier_cleanup(&data.e);
763 }
764
765 static void test_source_timer_schedule(void)
766 {
767 TimerTestData data = { .n = 0, .ctx = ctx, .ns = SCALE_MS * 750LL,
768 .max = 2,
769 .clock_type = QEMU_CLOCK_VIRTUAL };
770 EventNotifier e;
771 int64_t expiry;
772
773 /* aio_poll will not block to wait for timers to complete unless it has
774 * an fd to wait on. Fixing this breaks other tests. So create a dummy one.
775 */
776 event_notifier_init(&e, false);
777 aio_set_event_notifier(ctx, &e, dummy_io_handler_read);
778 do {} while (g_main_context_iteration(NULL, false));
779
780 aio_timer_init(ctx, &data.timer, data.clock_type,
781 SCALE_NS, timer_test_cb, &data);
782 expiry = qemu_clock_get_ns(data.clock_type) +
783 data.ns;
784 timer_mod(&data.timer, expiry);
785
786 g_assert_cmpint(data.n, ==, 0);
787
788 g_usleep(1 * G_USEC_PER_SEC);
789 g_assert_cmpint(data.n, ==, 0);
790
791 g_assert(g_main_context_iteration(NULL, true));
792 g_assert_cmpint(data.n, ==, 1);
793 expiry += data.ns;
794
795 while (data.n < 2) {
796 g_main_context_iteration(NULL, true);
797 }
798
799 g_assert_cmpint(data.n, ==, 2);
800 g_assert(qemu_clock_get_ns(data.clock_type) > expiry);
801
802 aio_set_event_notifier(ctx, &e, NULL);
803 event_notifier_cleanup(&e);
804
805 timer_del(&data.timer);
806 }
807
808
809 /* End of tests. */
810
811 int main(int argc, char **argv)
812 {
813 GSource *src;
814
815 init_clocks();
816
817 ctx = aio_context_new();
818 src = aio_get_g_source(ctx);
819 g_source_attach(src, NULL);
820 g_source_unref(src);
821
822 while (g_main_context_iteration(NULL, false));
823
824 g_test_init(&argc, &argv, NULL);
825 g_test_add_func("/aio/notify", test_notify);
826 g_test_add_func("/aio/acquire", test_acquire);
827 g_test_add_func("/aio/bh/schedule", test_bh_schedule);
828 g_test_add_func("/aio/bh/schedule10", test_bh_schedule10);
829 g_test_add_func("/aio/bh/cancel", test_bh_cancel);
830 g_test_add_func("/aio/bh/delete", test_bh_delete);
831 g_test_add_func("/aio/bh/callback-delete/one", test_bh_delete_from_cb);
832 g_test_add_func("/aio/bh/callback-delete/many", test_bh_delete_from_cb_many);
833 g_test_add_func("/aio/bh/flush", test_bh_flush);
834 g_test_add_func("/aio/event/add-remove", test_set_event_notifier);
835 g_test_add_func("/aio/event/wait", test_wait_event_notifier);
836 g_test_add_func("/aio/event/wait/no-flush-cb", test_wait_event_notifier_noflush);
837 g_test_add_func("/aio/event/flush", test_flush_event_notifier);
838 g_test_add_func("/aio/timer/schedule", test_timer_schedule);
839
840 g_test_add_func("/aio-gsource/notify", test_source_notify);
841 g_test_add_func("/aio-gsource/flush", test_source_flush);
842 g_test_add_func("/aio-gsource/bh/schedule", test_source_bh_schedule);
843 g_test_add_func("/aio-gsource/bh/schedule10", test_source_bh_schedule10);
844 g_test_add_func("/aio-gsource/bh/cancel", test_source_bh_cancel);
845 g_test_add_func("/aio-gsource/bh/delete", test_source_bh_delete);
846 g_test_add_func("/aio-gsource/bh/callback-delete/one", test_source_bh_delete_from_cb);
847 g_test_add_func("/aio-gsource/bh/callback-delete/many", test_source_bh_delete_from_cb_many);
848 g_test_add_func("/aio-gsource/bh/flush", test_source_bh_flush);
849 g_test_add_func("/aio-gsource/event/add-remove", test_source_set_event_notifier);
850 g_test_add_func("/aio-gsource/event/wait", test_source_wait_event_notifier);
851 g_test_add_func("/aio-gsource/event/wait/no-flush-cb", test_source_wait_event_notifier_noflush);
852 g_test_add_func("/aio-gsource/event/flush", test_source_flush_event_notifier);
853 g_test_add_func("/aio-gsource/timer/schedule", test_source_timer_schedule);
854 return g_test_run();
855 }