vmsvga: don't process more than 1024 fifo commands at once
[qemu/kevin.git] / tests / test-blockjob-txn.c
blob828389bb454d65106f9f44fbedabccd78be787a8
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 <glib.h>
15 #include "qapi/error.h"
16 #include "qemu/main-loop.h"
17 #include "block/blockjob.h"
18 #include "sysemu/block-backend.h"
20 typedef struct {
21 BlockJob common;
22 unsigned int iterations;
23 bool use_timer;
24 int rc;
25 int *result;
26 } TestBlockJob;
28 static const BlockJobDriver test_block_job_driver = {
29 .instance_size = sizeof(TestBlockJob),
32 static void test_block_job_complete(BlockJob *job, void *opaque)
34 BlockDriverState *bs = blk_bs(job->blk);
35 int rc = (intptr_t)opaque;
37 if (block_job_is_cancelled(job)) {
38 rc = -ECANCELED;
41 block_job_completed(job, rc);
42 bdrv_unref(bs);
45 static void coroutine_fn test_block_job_run(void *opaque)
47 TestBlockJob *s = opaque;
48 BlockJob *job = &s->common;
50 while (s->iterations--) {
51 if (s->use_timer) {
52 block_job_sleep_ns(job, QEMU_CLOCK_REALTIME, 0);
53 } else {
54 block_job_yield(job);
57 if (block_job_is_cancelled(job)) {
58 break;
62 block_job_defer_to_main_loop(job, test_block_job_complete,
63 (void *)(intptr_t)s->rc);
66 typedef struct {
67 TestBlockJob *job;
68 int *result;
69 } TestBlockJobCBData;
71 static void test_block_job_cb(void *opaque, int ret)
73 TestBlockJobCBData *data = opaque;
74 if (!ret && block_job_is_cancelled(&data->job->common)) {
75 ret = -ECANCELED;
77 *data->result = ret;
78 g_free(data);
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;
96 data = g_new0(TestBlockJobCBData, 1);
97 bs = bdrv_new();
98 s = block_job_create(&test_block_job_driver, bs, 0, test_block_job_cb,
99 data, &error_abort);
100 s->iterations = iterations;
101 s->use_timer = use_timer;
102 s->rc = rc;
103 s->result = result;
104 s->common.co = qemu_coroutine_create(test_block_job_run);
105 data->job = s;
106 data->result = result;
107 qemu_coroutine_enter(s->common.co, s);
108 return &s->common;
111 static void test_single_job(int expected)
113 BlockJob *job;
114 BlockJobTxn *txn;
115 int result = -EINPROGRESS;
117 txn = block_job_txn_new();
118 job = test_block_job_start(1, true, expected, &result);
119 block_job_txn_add_job(txn, job);
121 if (expected == -ECANCELED) {
122 block_job_cancel(job);
125 while (result == -EINPROGRESS) {
126 aio_poll(qemu_get_aio_context(), true);
128 g_assert_cmpint(result, ==, expected);
130 block_job_txn_unref(txn);
133 static void test_single_job_success(void)
135 test_single_job(0);
138 static void test_single_job_failure(void)
140 test_single_job(-EIO);
143 static void test_single_job_cancel(void)
145 test_single_job(-ECANCELED);
148 static void test_pair_jobs(int expected1, int expected2)
150 BlockJob *job1;
151 BlockJob *job2;
152 BlockJobTxn *txn;
153 int result1 = -EINPROGRESS;
154 int result2 = -EINPROGRESS;
156 txn = block_job_txn_new();
157 job1 = test_block_job_start(1, true, expected1, &result1);
158 block_job_txn_add_job(txn, job1);
159 job2 = test_block_job_start(2, true, expected2, &result2);
160 block_job_txn_add_job(txn, job2);
162 if (expected1 == -ECANCELED) {
163 block_job_cancel(job1);
165 if (expected2 == -ECANCELED) {
166 block_job_cancel(job2);
169 while (result1 == -EINPROGRESS || result2 == -EINPROGRESS) {
170 aio_poll(qemu_get_aio_context(), true);
173 /* Failure or cancellation of one job cancels the other job */
174 if (expected1 != 0) {
175 expected2 = -ECANCELED;
176 } else if (expected2 != 0) {
177 expected1 = -ECANCELED;
180 g_assert_cmpint(result1, ==, expected1);
181 g_assert_cmpint(result2, ==, expected2);
183 block_job_txn_unref(txn);
186 static void test_pair_jobs_success(void)
188 test_pair_jobs(0, 0);
191 static void test_pair_jobs_failure(void)
193 /* Test both orderings. The two jobs run for a different number of
194 * iterations so the code path is different depending on which job fails
195 * first.
197 test_pair_jobs(-EIO, 0);
198 test_pair_jobs(0, -EIO);
201 static void test_pair_jobs_cancel(void)
203 test_pair_jobs(-ECANCELED, 0);
204 test_pair_jobs(0, -ECANCELED);
207 static void test_pair_jobs_fail_cancel_race(void)
209 BlockJob *job1;
210 BlockJob *job2;
211 BlockJobTxn *txn;
212 int result1 = -EINPROGRESS;
213 int result2 = -EINPROGRESS;
215 txn = block_job_txn_new();
216 job1 = test_block_job_start(1, true, -ECANCELED, &result1);
217 block_job_txn_add_job(txn, job1);
218 job2 = test_block_job_start(2, false, 0, &result2);
219 block_job_txn_add_job(txn, job2);
221 block_job_cancel(job1);
223 /* Now make job2 finish before the main loop kicks jobs. This simulates
224 * the race between a pending kick and another job completing.
226 block_job_enter(job2);
227 block_job_enter(job2);
229 while (result1 == -EINPROGRESS || result2 == -EINPROGRESS) {
230 aio_poll(qemu_get_aio_context(), true);
233 g_assert_cmpint(result1, ==, -ECANCELED);
234 g_assert_cmpint(result2, ==, -ECANCELED);
236 block_job_txn_unref(txn);
239 int main(int argc, char **argv)
241 qemu_init_main_loop(&error_abort);
243 g_test_init(&argc, &argv, NULL);
244 g_test_add_func("/single/success", test_single_job_success);
245 g_test_add_func("/single/failure", test_single_job_failure);
246 g_test_add_func("/single/cancel", test_single_job_cancel);
247 g_test_add_func("/pair/success", test_pair_jobs_success);
248 g_test_add_func("/pair/failure", test_pair_jobs_failure);
249 g_test_add_func("/pair/cancel", test_pair_jobs_cancel);
250 g_test_add_func("/pair/fail-cancel-race", test_pair_jobs_fail_cancel_race);
251 return g_test_run();