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