ppc: remove all unused CPU definitions
[qemu/ar7.git] / tests / test-blockjob-txn.c
blobc77343fc04d943054c8356fe5dd171c0a33e3f53
1 /*
2 * Blockjob transactions tests
4 * Copyright Red Hat, Inc. 2015
6 * Authors:
7 * Stefan Hajnoczi <stefanha@redhat.com>
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.
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"
19 typedef struct {
20 BlockJob common;
21 unsigned int iterations;
22 bool use_timer;
23 int rc;
24 int *result;
25 } TestBlockJob;
27 static void test_block_job_complete(BlockJob *job, void *opaque)
29 BlockDriverState *bs = blk_bs(job->blk);
30 int rc = (intptr_t)opaque;
32 if (block_job_is_cancelled(job)) {
33 rc = -ECANCELED;
36 block_job_completed(job, rc);
37 bdrv_unref(bs);
40 static void coroutine_fn test_block_job_run(void *opaque)
42 TestBlockJob *s = opaque;
43 BlockJob *job = &s->common;
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);
52 if (block_job_is_cancelled(job)) {
53 break;
57 block_job_defer_to_main_loop(job, test_block_job_complete,
58 (void *)(intptr_t)s->rc);
61 typedef struct {
62 TestBlockJob *job;
63 int *result;
64 } TestBlockJobCBData;
66 static void test_block_job_cb(void *opaque, int ret)
68 TestBlockJobCBData *data = opaque;
69 if (!ret && block_job_is_cancelled(&data->job->common)) {
70 ret = -ECANCELED;
72 *data->result = ret;
73 g_free(data);
76 static const BlockJobDriver test_block_job_driver = {
77 .instance_size = sizeof(TestBlockJob),
78 .start = test_block_job_run,
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.
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.
88 static BlockJob *test_block_job_start(unsigned int iterations,
89 bool use_timer,
90 int rc, int *result)
92 BlockDriverState *bs;
93 TestBlockJob *s;
94 TestBlockJobCBData *data;
95 static unsigned counter;
96 char job_id[24];
98 data = g_new0(TestBlockJobCBData, 1);
100 bs = bdrv_open("null-co://", NULL, NULL, 0, &error_abort);
101 g_assert_nonnull(bs);
103 snprintf(job_id, sizeof(job_id), "job%u", counter++);
104 s = block_job_create(job_id, &test_block_job_driver, bs,
105 0, BLK_PERM_ALL, 0, BLOCK_JOB_DEFAULT,
106 test_block_job_cb, data, &error_abort);
107 s->iterations = iterations;
108 s->use_timer = use_timer;
109 s->rc = rc;
110 s->result = result;
111 data->job = s;
112 data->result = result;
113 return &s->common;
116 static void test_single_job(int expected)
118 BlockJob *job;
119 BlockJobTxn *txn;
120 int result = -EINPROGRESS;
122 txn = block_job_txn_new();
123 job = test_block_job_start(1, true, expected, &result);
124 block_job_txn_add_job(txn, job);
125 block_job_start(job);
127 if (expected == -ECANCELED) {
128 block_job_cancel(job);
131 while (result == -EINPROGRESS) {
132 aio_poll(qemu_get_aio_context(), true);
134 g_assert_cmpint(result, ==, expected);
136 block_job_txn_unref(txn);
139 static void test_single_job_success(void)
141 test_single_job(0);
144 static void test_single_job_failure(void)
146 test_single_job(-EIO);
149 static void test_single_job_cancel(void)
151 test_single_job(-ECANCELED);
154 static void test_pair_jobs(int expected1, int expected2)
156 BlockJob *job1;
157 BlockJob *job2;
158 BlockJobTxn *txn;
159 int result1 = -EINPROGRESS;
160 int result2 = -EINPROGRESS;
162 txn = block_job_txn_new();
163 job1 = test_block_job_start(1, true, expected1, &result1);
164 block_job_txn_add_job(txn, job1);
165 job2 = test_block_job_start(2, true, expected2, &result2);
166 block_job_txn_add_job(txn, job2);
167 block_job_start(job1);
168 block_job_start(job2);
170 /* Release our reference now to trigger as many nice
171 * use-after-free bugs as possible.
173 block_job_txn_unref(txn);
175 if (expected1 == -ECANCELED) {
176 block_job_cancel(job1);
178 if (expected2 == -ECANCELED) {
179 block_job_cancel(job2);
182 while (result1 == -EINPROGRESS || result2 == -EINPROGRESS) {
183 aio_poll(qemu_get_aio_context(), true);
186 /* Failure or cancellation of one job cancels the other job */
187 if (expected1 != 0) {
188 expected2 = -ECANCELED;
189 } else if (expected2 != 0) {
190 expected1 = -ECANCELED;
193 g_assert_cmpint(result1, ==, expected1);
194 g_assert_cmpint(result2, ==, expected2);
197 static void test_pair_jobs_success(void)
199 test_pair_jobs(0, 0);
202 static void test_pair_jobs_failure(void)
204 /* Test both orderings. The two jobs run for a different number of
205 * iterations so the code path is different depending on which job fails
206 * first.
208 test_pair_jobs(-EIO, 0);
209 test_pair_jobs(0, -EIO);
212 static void test_pair_jobs_cancel(void)
214 test_pair_jobs(-ECANCELED, 0);
215 test_pair_jobs(0, -ECANCELED);
218 static void test_pair_jobs_fail_cancel_race(void)
220 BlockJob *job1;
221 BlockJob *job2;
222 BlockJobTxn *txn;
223 int result1 = -EINPROGRESS;
224 int result2 = -EINPROGRESS;
226 txn = block_job_txn_new();
227 job1 = test_block_job_start(1, true, -ECANCELED, &result1);
228 block_job_txn_add_job(txn, job1);
229 job2 = test_block_job_start(2, false, 0, &result2);
230 block_job_txn_add_job(txn, job2);
231 block_job_start(job1);
232 block_job_start(job2);
234 block_job_cancel(job1);
236 /* Now make job2 finish before the main loop kicks jobs. This simulates
237 * the race between a pending kick and another job completing.
239 block_job_enter(job2);
240 block_job_enter(job2);
242 while (result1 == -EINPROGRESS || result2 == -EINPROGRESS) {
243 aio_poll(qemu_get_aio_context(), true);
246 g_assert_cmpint(result1, ==, -ECANCELED);
247 g_assert_cmpint(result2, ==, -ECANCELED);
249 block_job_txn_unref(txn);
252 int main(int argc, char **argv)
254 qemu_init_main_loop(&error_abort);
255 bdrv_init();
257 g_test_init(&argc, &argv, NULL);
258 g_test_add_func("/single/success", test_single_job_success);
259 g_test_add_func("/single/failure", test_single_job_failure);
260 g_test_add_func("/single/cancel", test_single_job_cancel);
261 g_test_add_func("/pair/success", test_pair_jobs_success);
262 g_test_add_func("/pair/failure", test_pair_jobs_failure);
263 g_test_add_func("/pair/cancel", test_pair_jobs_cancel);
264 g_test_add_func("/pair/fail-cancel-race", test_pair_jobs_fail_cancel_race);
265 return g_test_run();