]> git.proxmox.com Git - mirror_qemu.git/blame - tests/test-io-task.c
iotests: Test cancelling a job and closing the VM
[mirror_qemu.git] / tests / test-io-task.c
CommitLineData
b02db2d9
DB
1/*
2 * QEMU I/O task tests
3 *
4 * Copyright (c) 2015 Red Hat, Inc.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
681c28a3 21#include "qemu/osdep.h"
b02db2d9
DB
22
23#include "io/task.h"
da34e65c 24#include "qapi/error.h"
b02db2d9
DB
25
26#define TYPE_DUMMY "qemu:dummy"
27
28typedef struct DummyObject DummyObject;
29typedef struct DummyObjectClass DummyObjectClass;
30
31struct DummyObject {
32 Object parent;
33};
34
35struct DummyObjectClass {
36 ObjectClass parent;
37};
38
39static const TypeInfo dummy_info = {
40 .parent = TYPE_OBJECT,
41 .name = TYPE_DUMMY,
42 .instance_size = sizeof(DummyObject),
43 .class_size = sizeof(DummyObjectClass),
44};
45
46struct TestTaskData {
47 Object *source;
48 Error *err;
49 bool freed;
50};
51
52
60e705c5 53static void task_callback(QIOTask *task,
b02db2d9
DB
54 gpointer opaque)
55{
56 struct TestTaskData *data = opaque;
57
60e705c5
DB
58 data->source = qio_task_get_source(task);
59 qio_task_propagate_error(task, &data->err);
b02db2d9
DB
60}
61
62
63static void test_task_complete(void)
64{
65 QIOTask *task;
66 Object *obj = object_new(TYPE_DUMMY);
67 Object *src;
68 struct TestTaskData data = { NULL, NULL, false };
69
70 task = qio_task_new(obj, task_callback, &data, NULL);
71 src = qio_task_get_source(task);
72
73 qio_task_complete(task);
74
75 g_assert(obj == src);
76
77 object_unref(obj);
b02db2d9
DB
78
79 g_assert(data.source == obj);
80 g_assert(data.err == NULL);
81 g_assert(data.freed == false);
82}
83
84
85static void task_data_free(gpointer opaque)
86{
87 struct TestTaskData *data = opaque;
88
89 data->freed = true;
90}
91
92
93static void test_task_data_free(void)
94{
95 QIOTask *task;
96 Object *obj = object_new(TYPE_DUMMY);
97 struct TestTaskData data = { NULL, NULL, false };
98
99 task = qio_task_new(obj, task_callback, &data, task_data_free);
100
101 qio_task_complete(task);
102
103 object_unref(obj);
104
105 g_assert(data.source == obj);
106 g_assert(data.err == NULL);
107 g_assert(data.freed == true);
108}
109
110
51009170 111static void test_task_failure(void)
b02db2d9
DB
112{
113 QIOTask *task;
114 Object *obj = object_new(TYPE_DUMMY);
115 struct TestTaskData data = { NULL, NULL, false };
116 Error *err = NULL;
117
118 task = qio_task_new(obj, task_callback, &data, NULL);
119
120 error_setg(&err, "Some error");
121
60e705c5
DB
122 qio_task_set_error(task, err);
123 qio_task_complete(task);
b02db2d9 124
b02db2d9
DB
125 object_unref(obj);
126
127 g_assert(data.source == obj);
128 g_assert(data.err == err);
129 g_assert(data.freed == false);
80fb34ed 130 error_free(data.err);
b02db2d9
DB
131}
132
133
134struct TestThreadWorkerData {
135 Object *source;
136 Error *err;
137 bool fail;
138 GThread *worker;
139 GThread *complete;
140 GMainLoop *loop;
141};
142
59de517d
DB
143static void test_task_thread_worker(QIOTask *task,
144 gpointer opaque)
b02db2d9
DB
145{
146 struct TestThreadWorkerData *data = opaque;
147
148 data->worker = g_thread_self();
149
150 if (data->fail) {
59de517d
DB
151 Error *err = NULL;
152 error_setg(&err, "Testing fail");
153 qio_task_set_error(task, err);
b02db2d9 154 }
b02db2d9
DB
155}
156
157
60e705c5 158static void test_task_thread_callback(QIOTask *task,
b02db2d9
DB
159 gpointer opaque)
160{
161 struct TestThreadWorkerData *data = opaque;
162
60e705c5
DB
163 data->source = qio_task_get_source(task);
164 qio_task_propagate_error(task, &data->err);
b02db2d9
DB
165
166 data->complete = g_thread_self();
167
168 g_main_loop_quit(data->loop);
169}
170
171
172static void test_task_thread_complete(void)
173{
174 QIOTask *task;
175 Object *obj = object_new(TYPE_DUMMY);
176 struct TestThreadWorkerData data = { 0 };
177 GThread *self;
178
179 data.loop = g_main_loop_new(g_main_context_default(),
180 TRUE);
181
182 task = qio_task_new(obj,
183 test_task_thread_callback,
184 &data,
185 NULL);
186
187 qio_task_run_in_thread(task,
188 test_task_thread_worker,
189 &data,
a17536c5 190 NULL,
b02db2d9
DB
191 NULL);
192
193 g_main_loop_run(data.loop);
194
195 g_main_loop_unref(data.loop);
196 object_unref(obj);
197
198 g_assert(data.source == obj);
199 g_assert(data.err == NULL);
200
201 self = g_thread_self();
202
203 /* Make sure the test_task_thread_worker actually got
204 * run in a different thread */
205 g_assert(data.worker != self);
206
207 /* And that the test_task_thread_callback got rnu in
208 * the main loop thread (ie this one) */
209 g_assert(data.complete == self);
210}
211
212
51009170 213static void test_task_thread_failure(void)
b02db2d9
DB
214{
215 QIOTask *task;
216 Object *obj = object_new(TYPE_DUMMY);
217 struct TestThreadWorkerData data = { 0 };
218 GThread *self;
219
220 data.loop = g_main_loop_new(g_main_context_default(),
221 TRUE);
222 data.fail = true;
223
224 task = qio_task_new(obj,
225 test_task_thread_callback,
226 &data,
227 NULL);
228
229 qio_task_run_in_thread(task,
230 test_task_thread_worker,
231 &data,
a17536c5 232 NULL,
b02db2d9
DB
233 NULL);
234
235 g_main_loop_run(data.loop);
236
237 g_main_loop_unref(data.loop);
238 object_unref(obj);
239
240 g_assert(data.source == obj);
241 g_assert(data.err != NULL);
242
80fb34ed
DB
243 error_free(data.err);
244
b02db2d9
DB
245 self = g_thread_self();
246
247 /* Make sure the test_task_thread_worker actually got
248 * run in a different thread */
249 g_assert(data.worker != self);
250
251 /* And that the test_task_thread_callback got rnu in
252 * the main loop thread (ie this one) */
253 g_assert(data.complete == self);
254}
255
256
257int main(int argc, char **argv)
258{
259 g_test_init(&argc, &argv, NULL);
260 module_call_init(MODULE_INIT_QOM);
261 type_register_static(&dummy_info);
262 g_test_add_func("/crypto/task/complete", test_task_complete);
263 g_test_add_func("/crypto/task/datafree", test_task_data_free);
51009170 264 g_test_add_func("/crypto/task/failure", test_task_failure);
b02db2d9 265 g_test_add_func("/crypto/task/thread_complete", test_task_thread_complete);
51009170 266 g_test_add_func("/crypto/task/thread_failure", test_task_thread_failure);
b02db2d9
DB
267 return g_test_run();
268}