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