2 * Blockjob transactions tests
4 * Copyright Red Hat, Inc. 2015
7 * Stefan Hajnoczi <stefanha@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 "qapi/error.h"
16 #include "qemu/main-loop.h"
17 #include "block/blockjob.h"
18 #include "sysemu/block-backend.h"
22 unsigned int iterations
;
28 static const BlockJobDriver test_block_job_driver
= {
29 .instance_size
= sizeof(TestBlockJob
),
32 static void test_block_job_complete(BlockJob
*job
, void *opaque
)
34 BlockDriverState
*bs
= blk_bs(job
->blk
);
35 int rc
= (intptr_t)opaque
;
37 if (block_job_is_cancelled(job
)) {
41 block_job_completed(job
, rc
);
45 static void coroutine_fn
test_block_job_run(void *opaque
)
47 TestBlockJob
*s
= opaque
;
48 BlockJob
*job
= &s
->common
;
50 while (s
->iterations
--) {
52 block_job_sleep_ns(job
, QEMU_CLOCK_REALTIME
, 0);
57 if (block_job_is_cancelled(job
)) {
62 block_job_defer_to_main_loop(job
, test_block_job_complete
,
63 (void *)(intptr_t)s
->rc
);
71 static void test_block_job_cb(void *opaque
, int ret
)
73 TestBlockJobCBData
*data
= opaque
;
74 if (!ret
&& block_job_is_cancelled(&data
->job
->common
)) {
81 /* Create a block job that completes with a given return code after a given
82 * number of event loop iterations. The return code is stored in the given
85 * The event loop iterations can either be handled automatically with a 0 delay
86 * timer, or they can be stepped manually by entering the coroutine.
88 static BlockJob
*test_block_job_start(unsigned int iterations
,
94 TestBlockJobCBData
*data
;
96 data
= g_new0(TestBlockJobCBData
, 1);
98 s
= block_job_create(&test_block_job_driver
, bs
, 0, test_block_job_cb
,
100 s
->iterations
= iterations
;
101 s
->use_timer
= use_timer
;
104 s
->common
.co
= qemu_coroutine_create(test_block_job_run
);
106 data
->result
= result
;
107 qemu_coroutine_enter(s
->common
.co
, s
);
111 static void test_single_job(int expected
)
115 int result
= -EINPROGRESS
;
117 txn
= block_job_txn_new();
118 job
= test_block_job_start(1, true, expected
, &result
);
119 block_job_txn_add_job(txn
, job
);
121 if (expected
== -ECANCELED
) {
122 block_job_cancel(job
);
125 while (result
== -EINPROGRESS
) {
126 aio_poll(qemu_get_aio_context(), true);
128 g_assert_cmpint(result
, ==, expected
);
130 block_job_txn_unref(txn
);
133 static void test_single_job_success(void)
138 static void test_single_job_failure(void)
140 test_single_job(-EIO
);
143 static void test_single_job_cancel(void)
145 test_single_job(-ECANCELED
);
148 static void test_pair_jobs(int expected1
, int expected2
)
153 int result1
= -EINPROGRESS
;
154 int result2
= -EINPROGRESS
;
156 txn
= block_job_txn_new();
157 job1
= test_block_job_start(1, true, expected1
, &result1
);
158 block_job_txn_add_job(txn
, job1
);
159 job2
= test_block_job_start(2, true, expected2
, &result2
);
160 block_job_txn_add_job(txn
, job2
);
162 if (expected1
== -ECANCELED
) {
163 block_job_cancel(job1
);
165 if (expected2
== -ECANCELED
) {
166 block_job_cancel(job2
);
169 while (result1
== -EINPROGRESS
|| result2
== -EINPROGRESS
) {
170 aio_poll(qemu_get_aio_context(), true);
173 /* Failure or cancellation of one job cancels the other job */
174 if (expected1
!= 0) {
175 expected2
= -ECANCELED
;
176 } else if (expected2
!= 0) {
177 expected1
= -ECANCELED
;
180 g_assert_cmpint(result1
, ==, expected1
);
181 g_assert_cmpint(result2
, ==, expected2
);
183 block_job_txn_unref(txn
);
186 static void test_pair_jobs_success(void)
188 test_pair_jobs(0, 0);
191 static void test_pair_jobs_failure(void)
193 /* Test both orderings. The two jobs run for a different number of
194 * iterations so the code path is different depending on which job fails
197 test_pair_jobs(-EIO
, 0);
198 test_pair_jobs(0, -EIO
);
201 static void test_pair_jobs_cancel(void)
203 test_pair_jobs(-ECANCELED
, 0);
204 test_pair_jobs(0, -ECANCELED
);
207 static void test_pair_jobs_fail_cancel_race(void)
212 int result1
= -EINPROGRESS
;
213 int result2
= -EINPROGRESS
;
215 txn
= block_job_txn_new();
216 job1
= test_block_job_start(1, true, -ECANCELED
, &result1
);
217 block_job_txn_add_job(txn
, job1
);
218 job2
= test_block_job_start(2, false, 0, &result2
);
219 block_job_txn_add_job(txn
, job2
);
221 block_job_cancel(job1
);
223 /* Now make job2 finish before the main loop kicks jobs. This simulates
224 * the race between a pending kick and another job completing.
226 block_job_enter(job2
);
227 block_job_enter(job2
);
229 while (result1
== -EINPROGRESS
|| result2
== -EINPROGRESS
) {
230 aio_poll(qemu_get_aio_context(), true);
233 g_assert_cmpint(result1
, ==, -ECANCELED
);
234 g_assert_cmpint(result2
, ==, -ECANCELED
);
236 block_job_txn_unref(txn
);
239 int main(int argc
, char **argv
)
241 qemu_init_main_loop(&error_abort
);
243 g_test_init(&argc
, &argv
, NULL
);
244 g_test_add_func("/single/success", test_single_job_success
);
245 g_test_add_func("/single/failure", test_single_job_failure
);
246 g_test_add_func("/single/cancel", test_single_job_cancel
);
247 g_test_add_func("/pair/success", test_pair_jobs_success
);
248 g_test_add_func("/pair/failure", test_pair_jobs_failure
);
249 g_test_add_func("/pair/cancel", test_pair_jobs_cancel
);
250 g_test_add_func("/pair/fail-cancel-race", test_pair_jobs_fail_cancel_race
);