]> git.proxmox.com Git - mirror_qemu.git/blob - tests/test-blockjob-txn.c
job: Add job_sleep_ns()
[mirror_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 "qapi/error.h"
15 #include "qemu/main-loop.h"
16 #include "block/blockjob_int.h"
17 #include "sysemu/block-backend.h"
18
19 typedef struct {
20 BlockJob common;
21 unsigned int iterations;
22 bool use_timer;
23 int rc;
24 int *result;
25 } TestBlockJob;
26
27 static void test_block_job_complete(Job *job, void *opaque)
28 {
29 BlockJob *bjob = container_of(job, BlockJob, job);
30 BlockDriverState *bs = blk_bs(bjob->blk);
31 int rc = (intptr_t)opaque;
32
33 if (job_is_cancelled(job)) {
34 rc = -ECANCELED;
35 }
36
37 block_job_completed(bjob, rc);
38 bdrv_unref(bs);
39 }
40
41 static void coroutine_fn test_block_job_run(void *opaque)
42 {
43 TestBlockJob *s = opaque;
44 BlockJob *job = &s->common;
45
46 while (s->iterations--) {
47 if (s->use_timer) {
48 job_sleep_ns(&job->job, 0);
49 } else {
50 block_job_yield(job);
51 }
52
53 if (job_is_cancelled(&job->job)) {
54 break;
55 }
56 }
57
58 job_defer_to_main_loop(&job->job, test_block_job_complete,
59 (void *)(intptr_t)s->rc);
60 }
61
62 typedef struct {
63 TestBlockJob *job;
64 int *result;
65 } TestBlockJobCBData;
66
67 static void test_block_job_cb(void *opaque, int ret)
68 {
69 TestBlockJobCBData *data = opaque;
70 if (!ret && job_is_cancelled(&data->job->common.job)) {
71 ret = -ECANCELED;
72 }
73 *data->result = ret;
74 g_free(data);
75 }
76
77 static const BlockJobDriver test_block_job_driver = {
78 .job_driver = {
79 .instance_size = sizeof(TestBlockJob),
80 .free = block_job_free,
81 .start = test_block_job_run,
82 },
83 };
84
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
87 * result pointer.
88 *
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.
91 */
92 static BlockJob *test_block_job_start(unsigned int iterations,
93 bool use_timer,
94 int rc, int *result, BlockJobTxn *txn)
95 {
96 BlockDriverState *bs;
97 TestBlockJob *s;
98 TestBlockJobCBData *data;
99 static unsigned counter;
100 char job_id[24];
101
102 data = g_new0(TestBlockJobCBData, 1);
103
104 bs = bdrv_open("null-co://", NULL, NULL, 0, &error_abort);
105 g_assert_nonnull(bs);
106
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;
113 s->rc = rc;
114 s->result = result;
115 data->job = s;
116 data->result = result;
117 return &s->common;
118 }
119
120 static void test_single_job(int expected)
121 {
122 BlockJob *job;
123 BlockJobTxn *txn;
124 int result = -EINPROGRESS;
125
126 txn = block_job_txn_new();
127 job = test_block_job_start(1, true, expected, &result, txn);
128 job_start(&job->job);
129
130 if (expected == -ECANCELED) {
131 block_job_cancel(job, false);
132 }
133
134 while (result == -EINPROGRESS) {
135 aio_poll(qemu_get_aio_context(), true);
136 }
137 g_assert_cmpint(result, ==, expected);
138
139 block_job_txn_unref(txn);
140 }
141
142 static void test_single_job_success(void)
143 {
144 test_single_job(0);
145 }
146
147 static void test_single_job_failure(void)
148 {
149 test_single_job(-EIO);
150 }
151
152 static void test_single_job_cancel(void)
153 {
154 test_single_job(-ECANCELED);
155 }
156
157 static void test_pair_jobs(int expected1, int expected2)
158 {
159 BlockJob *job1;
160 BlockJob *job2;
161 BlockJobTxn *txn;
162 int result1 = -EINPROGRESS;
163 int result2 = -EINPROGRESS;
164
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);
170
171 /* Release our reference now to trigger as many nice
172 * use-after-free bugs as possible.
173 */
174 block_job_txn_unref(txn);
175
176 if (expected1 == -ECANCELED) {
177 block_job_cancel(job1, false);
178 }
179 if (expected2 == -ECANCELED) {
180 block_job_cancel(job2, false);
181 }
182
183 while (result1 == -EINPROGRESS || result2 == -EINPROGRESS) {
184 aio_poll(qemu_get_aio_context(), true);
185 }
186
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;
192 }
193
194 g_assert_cmpint(result1, ==, expected1);
195 g_assert_cmpint(result2, ==, expected2);
196 }
197
198 static void test_pair_jobs_success(void)
199 {
200 test_pair_jobs(0, 0);
201 }
202
203 static void test_pair_jobs_failure(void)
204 {
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
207 * first.
208 */
209 test_pair_jobs(-EIO, 0);
210 test_pair_jobs(0, -EIO);
211 }
212
213 static void test_pair_jobs_cancel(void)
214 {
215 test_pair_jobs(-ECANCELED, 0);
216 test_pair_jobs(0, -ECANCELED);
217 }
218
219 static void test_pair_jobs_fail_cancel_race(void)
220 {
221 BlockJob *job1;
222 BlockJob *job2;
223 BlockJobTxn *txn;
224 int result1 = -EINPROGRESS;
225 int result2 = -EINPROGRESS;
226
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);
232
233 block_job_cancel(job1, false);
234
235 /* Now make job2 finish before the main loop kicks jobs. This simulates
236 * the race between a pending kick and another job completing.
237 */
238 block_job_enter(job2);
239 block_job_enter(job2);
240
241 while (result1 == -EINPROGRESS || result2 == -EINPROGRESS) {
242 aio_poll(qemu_get_aio_context(), true);
243 }
244
245 g_assert_cmpint(result1, ==, -ECANCELED);
246 g_assert_cmpint(result2, ==, -ECANCELED);
247
248 block_job_txn_unref(txn);
249 }
250
251 int main(int argc, char **argv)
252 {
253 qemu_init_main_loop(&error_abort);
254 bdrv_init();
255
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);
264 return g_test_run();
265 }