balloon: fix segfault and harden the stats queue
[qemu/kevin.git] / tests / test-blockjob-txn.c
blobc866da66c884782049f24b73a94757c96b2adfbb
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 "qemu/main-loop.h"
16 #include "block/blockjob.h"
18 typedef struct {
19 BlockJob common;
20 unsigned int iterations;
21 bool use_timer;
22 int rc;
23 int *result;
24 } TestBlockJob;
26 static const BlockJobDriver test_block_job_driver = {
27 .instance_size = sizeof(TestBlockJob),
30 static void test_block_job_complete(BlockJob *job, void *opaque)
32 BlockDriverState *bs = job->bs;
33 int rc = (intptr_t)opaque;
35 if (block_job_is_cancelled(job)) {
36 rc = -ECANCELED;
39 block_job_completed(job, rc);
40 bdrv_unref(bs);
43 static void coroutine_fn test_block_job_run(void *opaque)
45 TestBlockJob *s = opaque;
46 BlockJob *job = &s->common;
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);
55 if (block_job_is_cancelled(job)) {
56 break;
60 block_job_defer_to_main_loop(job, test_block_job_complete,
61 (void *)(intptr_t)s->rc);
64 typedef struct {
65 TestBlockJob *job;
66 int *result;
67 } TestBlockJobCBData;
69 static void test_block_job_cb(void *opaque, int ret)
71 TestBlockJobCBData *data = opaque;
72 if (!ret && block_job_is_cancelled(&data->job->common)) {
73 ret = -ECANCELED;
75 *data->result = ret;
76 g_free(data);
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.
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.
86 static BlockJob *test_block_job_start(unsigned int iterations,
87 bool use_timer,
88 int rc, int *result)
90 BlockDriverState *bs;
91 TestBlockJob *s;
92 TestBlockJobCBData *data;
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;
109 static void test_single_job(int expected)
111 BlockJob *job;
112 BlockJobTxn *txn;
113 int result = -EINPROGRESS;
115 txn = block_job_txn_new();
116 job = test_block_job_start(1, true, expected, &result);
117 block_job_txn_add_job(txn, job);
119 if (expected == -ECANCELED) {
120 block_job_cancel(job);
123 while (result == -EINPROGRESS) {
124 aio_poll(qemu_get_aio_context(), true);
126 g_assert_cmpint(result, ==, expected);
128 block_job_txn_unref(txn);
131 static void test_single_job_success(void)
133 test_single_job(0);
136 static void test_single_job_failure(void)
138 test_single_job(-EIO);
141 static void test_single_job_cancel(void)
143 test_single_job(-ECANCELED);
146 static void test_pair_jobs(int expected1, int expected2)
148 BlockJob *job1;
149 BlockJob *job2;
150 BlockJobTxn *txn;
151 int result1 = -EINPROGRESS;
152 int result2 = -EINPROGRESS;
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);
160 if (expected1 == -ECANCELED) {
161 block_job_cancel(job1);
163 if (expected2 == -ECANCELED) {
164 block_job_cancel(job2);
167 while (result1 == -EINPROGRESS || result2 == -EINPROGRESS) {
168 aio_poll(qemu_get_aio_context(), true);
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;
178 g_assert_cmpint(result1, ==, expected1);
179 g_assert_cmpint(result2, ==, expected2);
181 block_job_txn_unref(txn);
184 static void test_pair_jobs_success(void)
186 test_pair_jobs(0, 0);
189 static void test_pair_jobs_failure(void)
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.
195 test_pair_jobs(-EIO, 0);
196 test_pair_jobs(0, -EIO);
199 static void test_pair_jobs_cancel(void)
201 test_pair_jobs(-ECANCELED, 0);
202 test_pair_jobs(0, -ECANCELED);
205 static void test_pair_jobs_fail_cancel_race(void)
207 BlockJob *job1;
208 BlockJob *job2;
209 BlockJobTxn *txn;
210 int result1 = -EINPROGRESS;
211 int result2 = -EINPROGRESS;
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);
219 block_job_cancel(job1);
221 /* Now make job2 finish before the main loop kicks jobs. This simulates
222 * the race between a pending kick and another job completing.
224 block_job_enter(job2);
225 block_job_enter(job2);
227 while (result1 == -EINPROGRESS || result2 == -EINPROGRESS) {
228 aio_poll(qemu_get_aio_context(), true);
231 g_assert_cmpint(result1, ==, -ECANCELED);
232 g_assert_cmpint(result2, ==, -ECANCELED);
234 block_job_txn_unref(txn);
237 int main(int argc, char **argv)
239 qemu_init_main_loop(&error_abort);
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();