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(Job
*job
, void *opaque
)
29 BlockJob
*bjob
= container_of(job
, BlockJob
, job
);
30 BlockDriverState
*bs
= blk_bs(bjob
->blk
);
31 int rc
= (intptr_t)opaque
;
33 if (job_is_cancelled(job
)) {
37 block_job_completed(bjob
, rc
);
41 static void coroutine_fn
test_block_job_run(void *opaque
)
43 TestBlockJob
*s
= opaque
;
44 BlockJob
*job
= &s
->common
;
46 while (s
->iterations
--) {
48 job_sleep_ns(&job
->job
, 0);
53 if (job_is_cancelled(&job
->job
)) {
58 job_defer_to_main_loop(&job
->job
, test_block_job_complete
,
59 (void *)(intptr_t)s
->rc
);
67 static void test_block_job_cb(void *opaque
, int ret
)
69 TestBlockJobCBData
*data
= opaque
;
70 if (!ret
&& job_is_cancelled(&data
->job
->common
.job
)) {
77 static const BlockJobDriver test_block_job_driver
= {
79 .instance_size
= sizeof(TestBlockJob
),
80 .free
= block_job_free
,
81 .start
= test_block_job_run
,
85 /* Create a block job that completes with a given return code after a given
86 * number of event loop iterations. The return code is stored in the given
89 * The event loop iterations can either be handled automatically with a 0 delay
90 * timer, or they can be stepped manually by entering the coroutine.
92 static BlockJob
*test_block_job_start(unsigned int iterations
,
94 int rc
, int *result
, BlockJobTxn
*txn
)
98 TestBlockJobCBData
*data
;
99 static unsigned counter
;
102 data
= g_new0(TestBlockJobCBData
, 1);
104 bs
= bdrv_open("null-co://", NULL
, NULL
, 0, &error_abort
);
105 g_assert_nonnull(bs
);
107 snprintf(job_id
, sizeof(job_id
), "job%u", counter
++);
108 s
= block_job_create(job_id
, &test_block_job_driver
, txn
, bs
,
109 0, BLK_PERM_ALL
, 0, BLOCK_JOB_DEFAULT
,
110 test_block_job_cb
, data
, &error_abort
);
111 s
->iterations
= iterations
;
112 s
->use_timer
= use_timer
;
116 data
->result
= result
;
120 static void test_single_job(int expected
)
124 int result
= -EINPROGRESS
;
126 txn
= block_job_txn_new();
127 job
= test_block_job_start(1, true, expected
, &result
, txn
);
128 job_start(&job
->job
);
130 if (expected
== -ECANCELED
) {
131 block_job_cancel(job
, false);
134 while (result
== -EINPROGRESS
) {
135 aio_poll(qemu_get_aio_context(), true);
137 g_assert_cmpint(result
, ==, expected
);
139 block_job_txn_unref(txn
);
142 static void test_single_job_success(void)
147 static void test_single_job_failure(void)
149 test_single_job(-EIO
);
152 static void test_single_job_cancel(void)
154 test_single_job(-ECANCELED
);
157 static void test_pair_jobs(int expected1
, int expected2
)
162 int result1
= -EINPROGRESS
;
163 int result2
= -EINPROGRESS
;
165 txn
= block_job_txn_new();
166 job1
= test_block_job_start(1, true, expected1
, &result1
, txn
);
167 job2
= test_block_job_start(2, true, expected2
, &result2
, txn
);
168 job_start(&job1
->job
);
169 job_start(&job2
->job
);
171 /* Release our reference now to trigger as many nice
172 * use-after-free bugs as possible.
174 block_job_txn_unref(txn
);
176 if (expected1
== -ECANCELED
) {
177 block_job_cancel(job1
, false);
179 if (expected2
== -ECANCELED
) {
180 block_job_cancel(job2
, false);
183 while (result1
== -EINPROGRESS
|| result2
== -EINPROGRESS
) {
184 aio_poll(qemu_get_aio_context(), true);
187 /* Failure or cancellation of one job cancels the other job */
188 if (expected1
!= 0) {
189 expected2
= -ECANCELED
;
190 } else if (expected2
!= 0) {
191 expected1
= -ECANCELED
;
194 g_assert_cmpint(result1
, ==, expected1
);
195 g_assert_cmpint(result2
, ==, expected2
);
198 static void test_pair_jobs_success(void)
200 test_pair_jobs(0, 0);
203 static void test_pair_jobs_failure(void)
205 /* Test both orderings. The two jobs run for a different number of
206 * iterations so the code path is different depending on which job fails
209 test_pair_jobs(-EIO
, 0);
210 test_pair_jobs(0, -EIO
);
213 static void test_pair_jobs_cancel(void)
215 test_pair_jobs(-ECANCELED
, 0);
216 test_pair_jobs(0, -ECANCELED
);
219 static void test_pair_jobs_fail_cancel_race(void)
224 int result1
= -EINPROGRESS
;
225 int result2
= -EINPROGRESS
;
227 txn
= block_job_txn_new();
228 job1
= test_block_job_start(1, true, -ECANCELED
, &result1
, txn
);
229 job2
= test_block_job_start(2, false, 0, &result2
, txn
);
230 job_start(&job1
->job
);
231 job_start(&job2
->job
);
233 block_job_cancel(job1
, false);
235 /* Now make job2 finish before the main loop kicks jobs. This simulates
236 * the race between a pending kick and another job completing.
238 block_job_enter(job2
);
239 block_job_enter(job2
);
241 while (result1
== -EINPROGRESS
|| result2
== -EINPROGRESS
) {
242 aio_poll(qemu_get_aio_context(), true);
245 g_assert_cmpint(result1
, ==, -ECANCELED
);
246 g_assert_cmpint(result2
, ==, -ECANCELED
);
248 block_job_txn_unref(txn
);
251 int main(int argc
, char **argv
)
253 qemu_init_main_loop(&error_abort
);
256 g_test_init(&argc
, &argv
, NULL
);
257 g_test_add_func("/single/success", test_single_job_success
);
258 g_test_add_func("/single/failure", test_single_job_failure
);
259 g_test_add_func("/single/cancel", test_single_job_cancel
);
260 g_test_add_func("/pair/success", test_pair_jobs_success
);
261 g_test_add_func("/pair/failure", test_pair_jobs_failure
);
262 g_test_add_func("/pair/cancel", test_pair_jobs_cancel
);
263 g_test_add_func("/pair/fail-cancel-race", test_pair_jobs_fail_cancel_race
);