Update version for v2.7.0-rc5 release
[qemu/ar7.git] / tests / test-blockjob-txn.c
blobd049cba8a307b89ad3d306f390e3d04c7dec1bf8
1 /*
2 * Blockjob transactions tests
4 * Copyright Red Hat, Inc. 2015
6 * Authors:
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.h"
17 #include "sysemu/block-backend.h"
19 typedef struct {
20 BlockJob common;
21 unsigned int iterations;
22 bool use_timer;
23 int rc;
24 int *result;
25 } TestBlockJob;
27 static const BlockJobDriver test_block_job_driver = {
28 .instance_size = sizeof(TestBlockJob),
31 static void test_block_job_complete(BlockJob *job, void *opaque)
33 BlockDriverState *bs = blk_bs(job->blk);
34 int rc = (intptr_t)opaque;
36 if (block_job_is_cancelled(job)) {
37 rc = -ECANCELED;
40 block_job_completed(job, rc);
41 bdrv_unref(bs);
44 static void coroutine_fn test_block_job_run(void *opaque)
46 TestBlockJob *s = opaque;
47 BlockJob *job = &s->common;
49 while (s->iterations--) {
50 if (s->use_timer) {
51 block_job_sleep_ns(job, QEMU_CLOCK_REALTIME, 0);
52 } else {
53 block_job_yield(job);
56 if (block_job_is_cancelled(job)) {
57 break;
61 block_job_defer_to_main_loop(job, test_block_job_complete,
62 (void *)(intptr_t)s->rc);
65 typedef struct {
66 TestBlockJob *job;
67 int *result;
68 } TestBlockJobCBData;
70 static void test_block_job_cb(void *opaque, int ret)
72 TestBlockJobCBData *data = opaque;
73 if (!ret && block_job_is_cancelled(&data->job->common)) {
74 ret = -ECANCELED;
76 *data->result = ret;
77 g_free(data);
80 /* Create a block job that completes with a given return code after a given
81 * number of event loop iterations. The return code is stored in the given
82 * result pointer.
84 * The event loop iterations can either be handled automatically with a 0 delay
85 * timer, or they can be stepped manually by entering the coroutine.
87 static BlockJob *test_block_job_start(unsigned int iterations,
88 bool use_timer,
89 int rc, int *result)
91 BlockDriverState *bs;
92 TestBlockJob *s;
93 TestBlockJobCBData *data;
94 static unsigned counter;
95 char job_id[24];
97 data = g_new0(TestBlockJobCBData, 1);
98 bs = bdrv_new();
99 snprintf(job_id, sizeof(job_id), "job%u", counter++);
100 s = block_job_create(job_id, &test_block_job_driver, bs, 0,
101 test_block_job_cb, data, &error_abort);
102 s->iterations = iterations;
103 s->use_timer = use_timer;
104 s->rc = rc;
105 s->result = result;
106 s->common.co = qemu_coroutine_create(test_block_job_run, s);
107 data->job = s;
108 data->result = result;
109 qemu_coroutine_enter(s->common.co);
110 return &s->common;
113 static void test_single_job(int expected)
115 BlockJob *job;
116 BlockJobTxn *txn;
117 int result = -EINPROGRESS;
119 txn = block_job_txn_new();
120 job = test_block_job_start(1, true, expected, &result);
121 block_job_txn_add_job(txn, job);
123 if (expected == -ECANCELED) {
124 block_job_cancel(job);
127 while (result == -EINPROGRESS) {
128 aio_poll(qemu_get_aio_context(), true);
130 g_assert_cmpint(result, ==, expected);
132 block_job_txn_unref(txn);
135 static void test_single_job_success(void)
137 test_single_job(0);
140 static void test_single_job_failure(void)
142 test_single_job(-EIO);
145 static void test_single_job_cancel(void)
147 test_single_job(-ECANCELED);
150 static void test_pair_jobs(int expected1, int expected2)
152 BlockJob *job1;
153 BlockJob *job2;
154 BlockJobTxn *txn;
155 int result1 = -EINPROGRESS;
156 int result2 = -EINPROGRESS;
158 txn = block_job_txn_new();
159 job1 = test_block_job_start(1, true, expected1, &result1);
160 block_job_txn_add_job(txn, job1);
161 job2 = test_block_job_start(2, true, expected2, &result2);
162 block_job_txn_add_job(txn, job2);
164 if (expected1 == -ECANCELED) {
165 block_job_cancel(job1);
167 if (expected2 == -ECANCELED) {
168 block_job_cancel(job2);
171 while (result1 == -EINPROGRESS || result2 == -EINPROGRESS) {
172 aio_poll(qemu_get_aio_context(), true);
175 /* Failure or cancellation of one job cancels the other job */
176 if (expected1 != 0) {
177 expected2 = -ECANCELED;
178 } else if (expected2 != 0) {
179 expected1 = -ECANCELED;
182 g_assert_cmpint(result1, ==, expected1);
183 g_assert_cmpint(result2, ==, expected2);
185 block_job_txn_unref(txn);
188 static void test_pair_jobs_success(void)
190 test_pair_jobs(0, 0);
193 static void test_pair_jobs_failure(void)
195 /* Test both orderings. The two jobs run for a different number of
196 * iterations so the code path is different depending on which job fails
197 * first.
199 test_pair_jobs(-EIO, 0);
200 test_pair_jobs(0, -EIO);
203 static void test_pair_jobs_cancel(void)
205 test_pair_jobs(-ECANCELED, 0);
206 test_pair_jobs(0, -ECANCELED);
209 static void test_pair_jobs_fail_cancel_race(void)
211 BlockJob *job1;
212 BlockJob *job2;
213 BlockJobTxn *txn;
214 int result1 = -EINPROGRESS;
215 int result2 = -EINPROGRESS;
217 txn = block_job_txn_new();
218 job1 = test_block_job_start(1, true, -ECANCELED, &result1);
219 block_job_txn_add_job(txn, job1);
220 job2 = test_block_job_start(2, false, 0, &result2);
221 block_job_txn_add_job(txn, job2);
223 block_job_cancel(job1);
225 /* Now make job2 finish before the main loop kicks jobs. This simulates
226 * the race between a pending kick and another job completing.
228 block_job_enter(job2);
229 block_job_enter(job2);
231 while (result1 == -EINPROGRESS || result2 == -EINPROGRESS) {
232 aio_poll(qemu_get_aio_context(), true);
235 g_assert_cmpint(result1, ==, -ECANCELED);
236 g_assert_cmpint(result2, ==, -ECANCELED);
238 block_job_txn_unref(txn);
241 int main(int argc, char **argv)
243 qemu_init_main_loop(&error_abort);
245 g_test_init(&argc, &argv, NULL);
246 g_test_add_func("/single/success", test_single_job_success);
247 g_test_add_func("/single/failure", test_single_job_failure);
248 g_test_add_func("/single/cancel", test_single_job_cancel);
249 g_test_add_func("/pair/success", test_pair_jobs_success);
250 g_test_add_func("/pair/failure", test_pair_jobs_failure);
251 g_test_add_func("/pair/cancel", test_pair_jobs_cancel);
252 g_test_add_func("/pair/fail-cancel-race", test_pair_jobs_fail_cancel_race);
253 return g_test_run();