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