diff --git a/tests/test-bdrv-drain.c b/tests/test-bdrv-drain.c index 3503ce3b69..cf1194754e 100644 --- a/tests/test-bdrv-drain.c +++ b/tests/test-bdrv-drain.c @@ -1497,6 +1497,19 @@ static void test_append_to_drained(void) blk_unref(blk); } +typedef struct BHParams { + BlockDriverState *bs; + AioContext *target; + bool done; +} BHParams; + +static void bh_fun(void *opaque) +{ + BHParams *bhp = opaque; + bdrv_try_set_aio_context(bhp->bs, bhp->target, &error_abort); + bhp->done = true; +} + static void test_set_aio_context(void) { BlockDriverState *bs; @@ -1504,22 +1517,38 @@ static void test_set_aio_context(void) IOThread *b = iothread_new(); AioContext *ctx_a = iothread_get_aio_context(a); AioContext *ctx_b = iothread_get_aio_context(b); + BHParams bhp; bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR, &error_abort); bdrv_drained_begin(bs); - bdrv_try_set_aio_context(bs, ctx_a, &error_abort); + bhp = (BHParams){ bs, ctx_a }; + aio_bh_schedule_oneshot(qemu_get_aio_context(), bh_fun, &bhp); + while (!bhp.done) { + aio_poll(qemu_get_aio_context(), true); + } aio_context_acquire(ctx_a); bdrv_drained_end(bs); bdrv_drained_begin(bs); - bdrv_try_set_aio_context(bs, ctx_b, &error_abort); + + bhp = (BHParams){ bs, ctx_b }; + aio_bh_schedule_oneshot(ctx_a, bh_fun, &bhp); aio_context_release(ctx_a); + while (!bhp.done) { + aio_poll(qemu_get_aio_context(), true); + } + aio_context_acquire(ctx_b); - bdrv_try_set_aio_context(bs, qemu_get_aio_context(), &error_abort); + bhp = (BHParams){ bs, qemu_get_aio_context() }; + aio_bh_schedule_oneshot(ctx_b, bh_fun, &bhp); aio_context_release(ctx_b); + while (!bhp.done) { + aio_poll(qemu_get_aio_context(), true); + } + bdrv_drained_end(bs); bdrv_unref(bs);