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"
14 #include "qapi/error.h"
15 #include "qemu/main-loop.h"
16 #include "block/blockjob_int.h"
17 #include "sysemu/block-backend.h"
21 unsigned int iterations
;
27 static void test_block_job_complete(BlockJob
*job
, void *opaque
)
29 BlockDriverState
*bs
= blk_bs(job
->blk
);
30 int rc
= (intptr_t)opaque
;
32 if (job_is_cancelled(&job
->job
)) {
36 block_job_completed(job
, rc
);
40 static void coroutine_fn
test_block_job_run(void *opaque
)
42 TestBlockJob
*s
= opaque
;
43 BlockJob
*job
= &s
->common
;
45 while (s
->iterations
--) {
47 block_job_sleep_ns(job
, 0);
52 if (job_is_cancelled(&job
->job
)) {
57 block_job_defer_to_main_loop(job
, test_block_job_complete
,
58 (void *)(intptr_t)s
->rc
);
66 static void test_block_job_cb(void *opaque
, int ret
)
68 TestBlockJobCBData
*data
= opaque
;
69 if (!ret
&& job_is_cancelled(&data
->job
->common
.job
)) {
76 static const BlockJobDriver test_block_job_driver
= {
78 .instance_size
= sizeof(TestBlockJob
),
79 .free
= block_job_free
,
81 .start
= test_block_job_run
,
84 /* Create a block job that completes with a given return code after a given
85 * number of event loop iterations. The return code is stored in the given
88 * The event loop iterations can either be handled automatically with a 0 delay
89 * timer, or they can be stepped manually by entering the coroutine.
91 static BlockJob
*test_block_job_start(unsigned int iterations
,
93 int rc
, int *result
, BlockJobTxn
*txn
)
97 TestBlockJobCBData
*data
;
98 static unsigned counter
;
101 data
= g_new0(TestBlockJobCBData
, 1);
103 bs
= bdrv_open("null-co://", NULL
, NULL
, 0, &error_abort
);
104 g_assert_nonnull(bs
);
106 snprintf(job_id
, sizeof(job_id
), "job%u", counter
++);
107 s
= block_job_create(job_id
, &test_block_job_driver
, txn
, bs
,
108 0, BLK_PERM_ALL
, 0, BLOCK_JOB_DEFAULT
,
109 test_block_job_cb
, data
, &error_abort
);
110 s
->iterations
= iterations
;
111 s
->use_timer
= use_timer
;
115 data
->result
= result
;
119 static void test_single_job(int expected
)
123 int result
= -EINPROGRESS
;
125 txn
= block_job_txn_new();
126 job
= test_block_job_start(1, true, expected
, &result
, txn
);
127 block_job_start(job
);
129 if (expected
== -ECANCELED
) {
130 block_job_cancel(job
, false);
133 while (result
== -EINPROGRESS
) {
134 aio_poll(qemu_get_aio_context(), true);
136 g_assert_cmpint(result
, ==, expected
);
138 block_job_txn_unref(txn
);
141 static void test_single_job_success(void)
146 static void test_single_job_failure(void)
148 test_single_job(-EIO
);
151 static void test_single_job_cancel(void)
153 test_single_job(-ECANCELED
);
156 static void test_pair_jobs(int expected1
, int expected2
)
161 int result1
= -EINPROGRESS
;
162 int result2
= -EINPROGRESS
;
164 txn
= block_job_txn_new();
165 job1
= test_block_job_start(1, true, expected1
, &result1
, txn
);
166 job2
= test_block_job_start(2, true, expected2
, &result2
, txn
);
167 block_job_start(job1
);
168 block_job_start(job2
);
170 /* Release our reference now to trigger as many nice
171 * use-after-free bugs as possible.
173 block_job_txn_unref(txn
);
175 if (expected1
== -ECANCELED
) {
176 block_job_cancel(job1
, false);
178 if (expected2
== -ECANCELED
) {
179 block_job_cancel(job2
, false);
182 while (result1
== -EINPROGRESS
|| result2
== -EINPROGRESS
) {
183 aio_poll(qemu_get_aio_context(), true);
186 /* Failure or cancellation of one job cancels the other job */
187 if (expected1
!= 0) {
188 expected2
= -ECANCELED
;
189 } else if (expected2
!= 0) {
190 expected1
= -ECANCELED
;
193 g_assert_cmpint(result1
, ==, expected1
);
194 g_assert_cmpint(result2
, ==, expected2
);
197 static void test_pair_jobs_success(void)
199 test_pair_jobs(0, 0);
202 static void test_pair_jobs_failure(void)
204 /* Test both orderings. The two jobs run for a different number of
205 * iterations so the code path is different depending on which job fails
208 test_pair_jobs(-EIO
, 0);
209 test_pair_jobs(0, -EIO
);
212 static void test_pair_jobs_cancel(void)
214 test_pair_jobs(-ECANCELED
, 0);
215 test_pair_jobs(0, -ECANCELED
);
218 static void test_pair_jobs_fail_cancel_race(void)
223 int result1
= -EINPROGRESS
;
224 int result2
= -EINPROGRESS
;
226 txn
= block_job_txn_new();
227 job1
= test_block_job_start(1, true, -ECANCELED
, &result1
, txn
);
228 job2
= test_block_job_start(2, false, 0, &result2
, txn
);
229 block_job_start(job1
);
230 block_job_start(job2
);
232 block_job_cancel(job1
, false);
234 /* Now make job2 finish before the main loop kicks jobs. This simulates
235 * the race between a pending kick and another job completing.
237 block_job_enter(job2
);
238 block_job_enter(job2
);
240 while (result1
== -EINPROGRESS
|| result2
== -EINPROGRESS
) {
241 aio_poll(qemu_get_aio_context(), true);
244 g_assert_cmpint(result1
, ==, -ECANCELED
);
245 g_assert_cmpint(result2
, ==, -ECANCELED
);
247 block_job_txn_unref(txn
);
250 int main(int argc
, char **argv
)
252 qemu_init_main_loop(&error_abort
);
255 g_test_init(&argc
, &argv
, NULL
);
256 g_test_add_func("/single/success", test_single_job_success
);
257 g_test_add_func("/single/failure", test_single_job_failure
);
258 g_test_add_func("/single/cancel", test_single_job_cancel
);
259 g_test_add_func("/pair/success", test_pair_jobs_success
);
260 g_test_add_func("/pair/failure", test_pair_jobs_failure
);
261 g_test_add_func("/pair/cancel", test_pair_jobs_cancel
);
262 g_test_add_func("/pair/fail-cancel-race", test_pair_jobs_fail_cancel_race
);