]>
Commit | Line | Data |
---|---|---|
6c6f312d SH |
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 | ||
681c28a3 | 13 | #include "qemu/osdep.h" |
da34e65c | 14 | #include "qapi/error.h" |
6c6f312d | 15 | #include "qemu/main-loop.h" |
c87621ea | 16 | #include "block/blockjob_int.h" |
b75536c9 | 17 | #include "sysemu/block-backend.h" |
6c6f312d SH |
18 | |
19 | typedef struct { | |
20 | BlockJob common; | |
21 | unsigned int iterations; | |
22 | bool use_timer; | |
23 | int rc; | |
24 | int *result; | |
25 | } TestBlockJob; | |
26 | ||
6c6f312d SH |
27 | static void test_block_job_complete(BlockJob *job, void *opaque) |
28 | { | |
b75536c9 | 29 | BlockDriverState *bs = blk_bs(job->blk); |
6c6f312d SH |
30 | int rc = (intptr_t)opaque; |
31 | ||
32 | if (block_job_is_cancelled(job)) { | |
33 | rc = -ECANCELED; | |
34 | } | |
35 | ||
36 | block_job_completed(job, rc); | |
37 | bdrv_unref(bs); | |
38 | } | |
39 | ||
40 | static void coroutine_fn test_block_job_run(void *opaque) | |
41 | { | |
42 | TestBlockJob *s = opaque; | |
43 | BlockJob *job = &s->common; | |
44 | ||
45 | while (s->iterations--) { | |
46 | if (s->use_timer) { | |
47 | block_job_sleep_ns(job, QEMU_CLOCK_REALTIME, 0); | |
48 | } else { | |
49 | block_job_yield(job); | |
50 | } | |
51 | ||
52 | if (block_job_is_cancelled(job)) { | |
53 | break; | |
54 | } | |
55 | } | |
56 | ||
57 | block_job_defer_to_main_loop(job, test_block_job_complete, | |
58 | (void *)(intptr_t)s->rc); | |
59 | } | |
60 | ||
61 | typedef struct { | |
62 | TestBlockJob *job; | |
63 | int *result; | |
64 | } TestBlockJobCBData; | |
65 | ||
66 | static void test_block_job_cb(void *opaque, int ret) | |
67 | { | |
68 | TestBlockJobCBData *data = opaque; | |
69 | if (!ret && block_job_is_cancelled(&data->job->common)) { | |
70 | ret = -ECANCELED; | |
71 | } | |
72 | *data->result = ret; | |
73 | g_free(data); | |
74 | } | |
75 | ||
5ccac6f1 JS |
76 | static const BlockJobDriver test_block_job_driver = { |
77 | .instance_size = sizeof(TestBlockJob), | |
78 | .start = test_block_job_run, | |
79 | }; | |
80 | ||
6c6f312d SH |
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; | |
7f0317cf AG |
95 | static unsigned counter; |
96 | char job_id[24]; | |
6c6f312d SH |
97 | |
98 | data = g_new0(TestBlockJobCBData, 1); | |
99 | bs = bdrv_new(); | |
7f0317cf AG |
100 | snprintf(job_id, sizeof(job_id), "job%u", counter++); |
101 | s = block_job_create(job_id, &test_block_job_driver, bs, 0, | |
f81e0b45 JS |
102 | BLOCK_JOB_DEFAULT, test_block_job_cb, |
103 | data, &error_abort); | |
6c6f312d SH |
104 | s->iterations = iterations; |
105 | s->use_timer = use_timer; | |
106 | s->rc = rc; | |
107 | s->result = result; | |
6c6f312d SH |
108 | data->job = s; |
109 | data->result = result; | |
5ccac6f1 | 110 | block_job_start(&s->common); |
6c6f312d SH |
111 | return &s->common; |
112 | } | |
113 | ||
114 | static void test_single_job(int expected) | |
115 | { | |
116 | BlockJob *job; | |
117 | BlockJobTxn *txn; | |
118 | int result = -EINPROGRESS; | |
119 | ||
120 | txn = block_job_txn_new(); | |
121 | job = test_block_job_start(1, true, expected, &result); | |
122 | block_job_txn_add_job(txn, job); | |
123 | ||
124 | if (expected == -ECANCELED) { | |
125 | block_job_cancel(job); | |
126 | } | |
127 | ||
128 | while (result == -EINPROGRESS) { | |
129 | aio_poll(qemu_get_aio_context(), true); | |
130 | } | |
131 | g_assert_cmpint(result, ==, expected); | |
132 | ||
133 | block_job_txn_unref(txn); | |
134 | } | |
135 | ||
136 | static void test_single_job_success(void) | |
137 | { | |
138 | test_single_job(0); | |
139 | } | |
140 | ||
141 | static void test_single_job_failure(void) | |
142 | { | |
143 | test_single_job(-EIO); | |
144 | } | |
145 | ||
146 | static void test_single_job_cancel(void) | |
147 | { | |
148 | test_single_job(-ECANCELED); | |
149 | } | |
150 | ||
151 | static void test_pair_jobs(int expected1, int expected2) | |
152 | { | |
153 | BlockJob *job1; | |
154 | BlockJob *job2; | |
155 | BlockJobTxn *txn; | |
156 | int result1 = -EINPROGRESS; | |
157 | int result2 = -EINPROGRESS; | |
158 | ||
159 | txn = block_job_txn_new(); | |
160 | job1 = test_block_job_start(1, true, expected1, &result1); | |
161 | block_job_txn_add_job(txn, job1); | |
162 | job2 = test_block_job_start(2, true, expected2, &result2); | |
163 | block_job_txn_add_job(txn, job2); | |
164 | ||
165 | if (expected1 == -ECANCELED) { | |
166 | block_job_cancel(job1); | |
167 | } | |
168 | if (expected2 == -ECANCELED) { | |
169 | block_job_cancel(job2); | |
170 | } | |
171 | ||
172 | while (result1 == -EINPROGRESS || result2 == -EINPROGRESS) { | |
173 | aio_poll(qemu_get_aio_context(), true); | |
174 | } | |
175 | ||
176 | /* Failure or cancellation of one job cancels the other job */ | |
177 | if (expected1 != 0) { | |
178 | expected2 = -ECANCELED; | |
179 | } else if (expected2 != 0) { | |
180 | expected1 = -ECANCELED; | |
181 | } | |
182 | ||
183 | g_assert_cmpint(result1, ==, expected1); | |
184 | g_assert_cmpint(result2, ==, expected2); | |
185 | ||
186 | block_job_txn_unref(txn); | |
187 | } | |
188 | ||
189 | static void test_pair_jobs_success(void) | |
190 | { | |
191 | test_pair_jobs(0, 0); | |
192 | } | |
193 | ||
194 | static void test_pair_jobs_failure(void) | |
195 | { | |
196 | /* Test both orderings. The two jobs run for a different number of | |
197 | * iterations so the code path is different depending on which job fails | |
198 | * first. | |
199 | */ | |
200 | test_pair_jobs(-EIO, 0); | |
201 | test_pair_jobs(0, -EIO); | |
202 | } | |
203 | ||
204 | static void test_pair_jobs_cancel(void) | |
205 | { | |
206 | test_pair_jobs(-ECANCELED, 0); | |
207 | test_pair_jobs(0, -ECANCELED); | |
208 | } | |
209 | ||
210 | static void test_pair_jobs_fail_cancel_race(void) | |
211 | { | |
212 | BlockJob *job1; | |
213 | BlockJob *job2; | |
214 | BlockJobTxn *txn; | |
215 | int result1 = -EINPROGRESS; | |
216 | int result2 = -EINPROGRESS; | |
217 | ||
218 | txn = block_job_txn_new(); | |
219 | job1 = test_block_job_start(1, true, -ECANCELED, &result1); | |
220 | block_job_txn_add_job(txn, job1); | |
221 | job2 = test_block_job_start(2, false, 0, &result2); | |
222 | block_job_txn_add_job(txn, job2); | |
223 | ||
224 | block_job_cancel(job1); | |
225 | ||
226 | /* Now make job2 finish before the main loop kicks jobs. This simulates | |
227 | * the race between a pending kick and another job completing. | |
228 | */ | |
229 | block_job_enter(job2); | |
230 | block_job_enter(job2); | |
231 | ||
232 | while (result1 == -EINPROGRESS || result2 == -EINPROGRESS) { | |
233 | aio_poll(qemu_get_aio_context(), true); | |
234 | } | |
235 | ||
236 | g_assert_cmpint(result1, ==, -ECANCELED); | |
237 | g_assert_cmpint(result2, ==, -ECANCELED); | |
238 | ||
239 | block_job_txn_unref(txn); | |
240 | } | |
241 | ||
242 | int main(int argc, char **argv) | |
243 | { | |
244 | qemu_init_main_loop(&error_abort); | |
245 | ||
246 | g_test_init(&argc, &argv, NULL); | |
247 | g_test_add_func("/single/success", test_single_job_success); | |
248 | g_test_add_func("/single/failure", test_single_job_failure); | |
249 | g_test_add_func("/single/cancel", test_single_job_cancel); | |
250 | g_test_add_func("/pair/success", test_pair_jobs_success); | |
251 | g_test_add_func("/pair/failure", test_pair_jobs_failure); | |
252 | g_test_add_func("/pair/cancel", test_pair_jobs_cancel); | |
253 | g_test_add_func("/pair/fail-cancel-race", test_pair_jobs_fail_cancel_race); | |
254 | return g_test_run(); | |
255 | } |