blockjob: Remove BlockJob.bs
[qemu.git] / tests / test-blockjob-txn.c
1 /*
2 * Blockjob transactions tests
3 *
4 * Copyright Red Hat, Inc. 2015
5 *
6 * Authors:
7 * Stefan Hajnoczi <stefanha@redhat.com>
8 *
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.
11 */
12
13 #include "qemu/osdep.h"
14 #include <glib.h>
15 #include "qapi/error.h"
16 #include "qemu/main-loop.h"
17 #include "block/blockjob.h"
18 #include "sysemu/block-backend.h"
19
20 typedef struct {
21 BlockJob common;
22 unsigned int iterations;
23 bool use_timer;
24 int rc;
25 int *result;
26 } TestBlockJob;
27
28 static const BlockJobDriver test_block_job_driver = {
29 .instance_size = sizeof(TestBlockJob),
30 };
31
32 static void test_block_job_complete(BlockJob *job, void *opaque)
33 {
34 BlockDriverState *bs = blk_bs(job->blk);
35 int rc = (intptr_t)opaque;
36
37 if (block_job_is_cancelled(job)) {
38 rc = -ECANCELED;
39 }
40
41 block_job_completed(job, rc);
42 bdrv_unref(bs);
43 }
44
45 static void coroutine_fn test_block_job_run(void *opaque)
46 {
47 TestBlockJob *s = opaque;
48 BlockJob *job = &s->common;
49
50 while (s->iterations--) {
51 if (s->use_timer) {
52 block_job_sleep_ns(job, QEMU_CLOCK_REALTIME, 0);
53 } else {
54 block_job_yield(job);
55 }
56
57 if (block_job_is_cancelled(job)) {
58 break;
59 }
60 }
61
62 block_job_defer_to_main_loop(job, test_block_job_complete,
63 (void *)(intptr_t)s->rc);
64 }
65
66 typedef struct {
67 TestBlockJob *job;
68 int *result;
69 } TestBlockJobCBData;
70
71 static void test_block_job_cb(void *opaque, int ret)
72 {
73 TestBlockJobCBData *data = opaque;
74 if (!ret && block_job_is_cancelled(&data->job->common)) {
75 ret = -ECANCELED;
76 }
77 *data->result = ret;
78 g_free(data);
79 }
80
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
83 * result pointer.
84 *
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.
87 */
88 static BlockJob *test_block_job_start(unsigned int iterations,
89 bool use_timer,
90 int rc, int *result)
91 {
92 BlockDriverState *bs;
93 TestBlockJob *s;
94 TestBlockJobCBData *data;
95
96 data = g_new0(TestBlockJobCBData, 1);
97 bs = bdrv_new();
98 s = block_job_create(&test_block_job_driver, bs, 0, test_block_job_cb,
99 data, &error_abort);
100 s->iterations = iterations;
101 s->use_timer = use_timer;
102 s->rc = rc;
103 s->result = result;
104 s->common.co = qemu_coroutine_create(test_block_job_run);
105 data->job = s;
106 data->result = result;
107 qemu_coroutine_enter(s->common.co, s);
108 return &s->common;
109 }
110
111 static void test_single_job(int expected)
112 {
113 BlockJob *job;
114 BlockJobTxn *txn;
115 int result = -EINPROGRESS;
116
117 txn = block_job_txn_new();
118 job = test_block_job_start(1, true, expected, &result);
119 block_job_txn_add_job(txn, job);
120
121 if (expected == -ECANCELED) {
122 block_job_cancel(job);
123 }
124
125 while (result == -EINPROGRESS) {
126 aio_poll(qemu_get_aio_context(), true);
127 }
128 g_assert_cmpint(result, ==, expected);
129
130 block_job_txn_unref(txn);
131 }
132
133 static void test_single_job_success(void)
134 {
135 test_single_job(0);
136 }
137
138 static void test_single_job_failure(void)
139 {
140 test_single_job(-EIO);
141 }
142
143 static void test_single_job_cancel(void)
144 {
145 test_single_job(-ECANCELED);
146 }
147
148 static void test_pair_jobs(int expected1, int expected2)
149 {
150 BlockJob *job1;
151 BlockJob *job2;
152 BlockJobTxn *txn;
153 int result1 = -EINPROGRESS;
154 int result2 = -EINPROGRESS;
155
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);
161
162 if (expected1 == -ECANCELED) {
163 block_job_cancel(job1);
164 }
165 if (expected2 == -ECANCELED) {
166 block_job_cancel(job2);
167 }
168
169 while (result1 == -EINPROGRESS || result2 == -EINPROGRESS) {
170 aio_poll(qemu_get_aio_context(), true);
171 }
172
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;
178 }
179
180 g_assert_cmpint(result1, ==, expected1);
181 g_assert_cmpint(result2, ==, expected2);
182
183 block_job_txn_unref(txn);
184 }
185
186 static void test_pair_jobs_success(void)
187 {
188 test_pair_jobs(0, 0);
189 }
190
191 static void test_pair_jobs_failure(void)
192 {
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
195 * first.
196 */
197 test_pair_jobs(-EIO, 0);
198 test_pair_jobs(0, -EIO);
199 }
200
201 static void test_pair_jobs_cancel(void)
202 {
203 test_pair_jobs(-ECANCELED, 0);
204 test_pair_jobs(0, -ECANCELED);
205 }
206
207 static void test_pair_jobs_fail_cancel_race(void)
208 {
209 BlockJob *job1;
210 BlockJob *job2;
211 BlockJobTxn *txn;
212 int result1 = -EINPROGRESS;
213 int result2 = -EINPROGRESS;
214
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);
220
221 block_job_cancel(job1);
222
223 /* Now make job2 finish before the main loop kicks jobs. This simulates
224 * the race between a pending kick and another job completing.
225 */
226 block_job_enter(job2);
227 block_job_enter(job2);
228
229 while (result1 == -EINPROGRESS || result2 == -EINPROGRESS) {
230 aio_poll(qemu_get_aio_context(), true);
231 }
232
233 g_assert_cmpint(result1, ==, -ECANCELED);
234 g_assert_cmpint(result2, ==, -ECANCELED);
235
236 block_job_txn_unref(txn);
237 }
238
239 int main(int argc, char **argv)
240 {
241 qemu_init_main_loop(&error_abort);
242
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);
251 return g_test_run();
252 }