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 job_completed(job
, rc
, NULL
);
41 static int coroutine_fn
test_block_job_run(Job
*job
, Error
**errp
)
43 TestBlockJob
*s
= container_of(job
, TestBlockJob
, common
.job
);
45 while (s
->iterations
--) {
52 if (job_is_cancelled(job
)) {
57 job_defer_to_main_loop(job
, test_block_job_complete
,
58 (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 .user_resume
= block_job_user_resume
,
82 .drain
= block_job_drain
,
83 .run
= test_block_job_run
,
87 /* Create a block job that completes with a given return code after a given
88 * number of event loop iterations. The return code is stored in the given
91 * The event loop iterations can either be handled automatically with a 0 delay
92 * timer, or they can be stepped manually by entering the coroutine.
94 static BlockJob
*test_block_job_start(unsigned int iterations
,
96 int rc
, int *result
, JobTxn
*txn
)
100 TestBlockJobCBData
*data
;
101 static unsigned counter
;
104 data
= g_new0(TestBlockJobCBData
, 1);
106 bs
= bdrv_open("null-co://", NULL
, NULL
, 0, &error_abort
);
107 g_assert_nonnull(bs
);
109 snprintf(job_id
, sizeof(job_id
), "job%u", counter
++);
110 s
= block_job_create(job_id
, &test_block_job_driver
, txn
, bs
,
111 0, BLK_PERM_ALL
, 0, JOB_DEFAULT
,
112 test_block_job_cb
, data
, &error_abort
);
113 s
->iterations
= iterations
;
114 s
->use_timer
= use_timer
;
118 data
->result
= result
;
122 static void test_single_job(int expected
)
126 int result
= -EINPROGRESS
;
129 job
= test_block_job_start(1, true, expected
, &result
, txn
);
130 job_start(&job
->job
);
132 if (expected
== -ECANCELED
) {
133 job_cancel(&job
->job
, false);
136 while (result
== -EINPROGRESS
) {
137 aio_poll(qemu_get_aio_context(), true);
139 g_assert_cmpint(result
, ==, expected
);
144 static void test_single_job_success(void)
149 static void test_single_job_failure(void)
151 test_single_job(-EIO
);
154 static void test_single_job_cancel(void)
156 test_single_job(-ECANCELED
);
159 static void test_pair_jobs(int expected1
, int expected2
)
164 int result1
= -EINPROGRESS
;
165 int result2
= -EINPROGRESS
;
168 job1
= test_block_job_start(1, true, expected1
, &result1
, txn
);
169 job2
= test_block_job_start(2, true, expected2
, &result2
, txn
);
170 job_start(&job1
->job
);
171 job_start(&job2
->job
);
173 /* Release our reference now to trigger as many nice
174 * use-after-free bugs as possible.
178 if (expected1
== -ECANCELED
) {
179 job_cancel(&job1
->job
, false);
181 if (expected2
== -ECANCELED
) {
182 job_cancel(&job2
->job
, false);
185 while (result1
== -EINPROGRESS
|| result2
== -EINPROGRESS
) {
186 aio_poll(qemu_get_aio_context(), true);
189 /* Failure or cancellation of one job cancels the other job */
190 if (expected1
!= 0) {
191 expected2
= -ECANCELED
;
192 } else if (expected2
!= 0) {
193 expected1
= -ECANCELED
;
196 g_assert_cmpint(result1
, ==, expected1
);
197 g_assert_cmpint(result2
, ==, expected2
);
200 static void test_pair_jobs_success(void)
202 test_pair_jobs(0, 0);
205 static void test_pair_jobs_failure(void)
207 /* Test both orderings. The two jobs run for a different number of
208 * iterations so the code path is different depending on which job fails
211 test_pair_jobs(-EIO
, 0);
212 test_pair_jobs(0, -EIO
);
215 static void test_pair_jobs_cancel(void)
217 test_pair_jobs(-ECANCELED
, 0);
218 test_pair_jobs(0, -ECANCELED
);
221 static void test_pair_jobs_fail_cancel_race(void)
226 int result1
= -EINPROGRESS
;
227 int result2
= -EINPROGRESS
;
230 job1
= test_block_job_start(1, true, -ECANCELED
, &result1
, txn
);
231 job2
= test_block_job_start(2, false, 0, &result2
, txn
);
232 job_start(&job1
->job
);
233 job_start(&job2
->job
);
235 job_cancel(&job1
->job
, false);
237 /* Now make job2 finish before the main loop kicks jobs. This simulates
238 * the race between a pending kick and another job completing.
240 job_enter(&job2
->job
);
241 job_enter(&job2
->job
);
243 while (result1
== -EINPROGRESS
|| result2
== -EINPROGRESS
) {
244 aio_poll(qemu_get_aio_context(), true);
247 g_assert_cmpint(result1
, ==, -ECANCELED
);
248 g_assert_cmpint(result2
, ==, -ECANCELED
);
253 int main(int argc
, char **argv
)
255 qemu_init_main_loop(&error_abort
);
258 g_test_init(&argc
, &argv
, NULL
);
259 g_test_add_func("/single/success", test_single_job_success
);
260 g_test_add_func("/single/failure", test_single_job_failure
);
261 g_test_add_func("/single/cancel", test_single_job_cancel
);
262 g_test_add_func("/pair/success", test_pair_jobs_success
);
263 g_test_add_func("/pair/failure", test_pair_jobs_failure
);
264 g_test_add_func("/pair/cancel", test_pair_jobs_cancel
);
265 g_test_add_func("/pair/fail-cancel-race", test_pair_jobs_fail_cancel_race
);