]> git.proxmox.com Git - mirror_qemu.git/blob - tests/test-io-task.c
Merge remote-tracking branch 'remotes/kevin/tags/for-upstream' into staging
[mirror_qemu.git] / tests / test-io-task.c
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
21 #include "qemu/osdep.h"
22
23 #include "io/task.h"
24 #include "qapi/error.h"
25 #include "qemu/module.h"
26
27 #define TYPE_DUMMY "qemu:dummy"
28
29 typedef struct DummyObject DummyObject;
30 typedef struct DummyObjectClass DummyObjectClass;
31
32 struct DummyObject {
33 Object parent;
34 };
35
36 struct DummyObjectClass {
37 ObjectClass parent;
38 };
39
40 static const TypeInfo dummy_info = {
41 .parent = TYPE_OBJECT,
42 .name = TYPE_DUMMY,
43 .instance_size = sizeof(DummyObject),
44 .class_size = sizeof(DummyObjectClass),
45 };
46
47 struct TestTaskData {
48 Object *source;
49 Error *err;
50 bool freed;
51 };
52
53
54 static void task_callback(QIOTask *task,
55 gpointer opaque)
56 {
57 struct TestTaskData *data = opaque;
58
59 data->source = qio_task_get_source(task);
60 qio_task_propagate_error(task, &data->err);
61 }
62
63
64 static void test_task_complete(void)
65 {
66 QIOTask *task;
67 Object *obj = object_new(TYPE_DUMMY);
68 Object *src;
69 struct TestTaskData data = { NULL, NULL, false };
70
71 task = qio_task_new(obj, task_callback, &data, NULL);
72 src = qio_task_get_source(task);
73
74 qio_task_complete(task);
75
76 g_assert(obj == src);
77
78 object_unref(obj);
79
80 g_assert(data.source == obj);
81 g_assert(data.err == NULL);
82 g_assert(data.freed == false);
83 }
84
85
86 static void task_data_free(gpointer opaque)
87 {
88 struct TestTaskData *data = opaque;
89
90 data->freed = true;
91 }
92
93
94 static void test_task_data_free(void)
95 {
96 QIOTask *task;
97 Object *obj = object_new(TYPE_DUMMY);
98 struct TestTaskData data = { NULL, NULL, false };
99
100 task = qio_task_new(obj, task_callback, &data, task_data_free);
101
102 qio_task_complete(task);
103
104 object_unref(obj);
105
106 g_assert(data.source == obj);
107 g_assert(data.err == NULL);
108 g_assert(data.freed == true);
109 }
110
111
112 static void test_task_failure(void)
113 {
114 QIOTask *task;
115 Object *obj = object_new(TYPE_DUMMY);
116 struct TestTaskData data = { NULL, NULL, false };
117 Error *err = NULL;
118
119 task = qio_task_new(obj, task_callback, &data, NULL);
120
121 error_setg(&err, "Some error");
122
123 qio_task_set_error(task, err);
124 qio_task_complete(task);
125
126 object_unref(obj);
127
128 g_assert(data.source == obj);
129 g_assert(data.err == err);
130 g_assert(data.freed == false);
131 error_free(data.err);
132 }
133
134
135 struct TestThreadWorkerData {
136 Object *source;
137 Error *err;
138 bool fail;
139 GThread *worker;
140 GThread *complete;
141 GMainLoop *loop;
142 };
143
144 static void test_task_thread_worker(QIOTask *task,
145 gpointer opaque)
146 {
147 struct TestThreadWorkerData *data = opaque;
148
149 data->worker = g_thread_self();
150
151 if (data->fail) {
152 Error *err = NULL;
153 error_setg(&err, "Testing fail");
154 qio_task_set_error(task, err);
155 }
156 }
157
158
159 static void test_task_thread_callback(QIOTask *task,
160 gpointer opaque)
161 {
162 struct TestThreadWorkerData *data = opaque;
163
164 data->source = qio_task_get_source(task);
165 qio_task_propagate_error(task, &data->err);
166
167 data->complete = g_thread_self();
168
169 g_main_loop_quit(data->loop);
170 }
171
172
173 static void test_task_thread_complete(void)
174 {
175 QIOTask *task;
176 Object *obj = object_new(TYPE_DUMMY);
177 struct TestThreadWorkerData data = { 0 };
178 GThread *self;
179
180 data.loop = g_main_loop_new(g_main_context_default(),
181 TRUE);
182
183 task = qio_task_new(obj,
184 test_task_thread_callback,
185 &data,
186 NULL);
187
188 qio_task_run_in_thread(task,
189 test_task_thread_worker,
190 &data,
191 NULL,
192 NULL);
193
194 g_main_loop_run(data.loop);
195
196 g_main_loop_unref(data.loop);
197 object_unref(obj);
198
199 g_assert(data.source == obj);
200 g_assert(data.err == NULL);
201
202 self = g_thread_self();
203
204 /* Make sure the test_task_thread_worker actually got
205 * run in a different thread */
206 g_assert(data.worker != self);
207
208 /* And that the test_task_thread_callback got rnu in
209 * the main loop thread (ie this one) */
210 g_assert(data.complete == self);
211 }
212
213
214 static void test_task_thread_failure(void)
215 {
216 QIOTask *task;
217 Object *obj = object_new(TYPE_DUMMY);
218 struct TestThreadWorkerData data = { 0 };
219 GThread *self;
220
221 data.loop = g_main_loop_new(g_main_context_default(),
222 TRUE);
223 data.fail = true;
224
225 task = qio_task_new(obj,
226 test_task_thread_callback,
227 &data,
228 NULL);
229
230 qio_task_run_in_thread(task,
231 test_task_thread_worker,
232 &data,
233 NULL,
234 NULL);
235
236 g_main_loop_run(data.loop);
237
238 g_main_loop_unref(data.loop);
239 object_unref(obj);
240
241 g_assert(data.source == obj);
242 g_assert(data.err != NULL);
243
244 error_free(data.err);
245
246 self = g_thread_self();
247
248 /* Make sure the test_task_thread_worker actually got
249 * run in a different thread */
250 g_assert(data.worker != self);
251
252 /* And that the test_task_thread_callback got rnu in
253 * the main loop thread (ie this one) */
254 g_assert(data.complete == self);
255 }
256
257
258 int main(int argc, char **argv)
259 {
260 g_test_init(&argc, &argv, NULL);
261 module_call_init(MODULE_INIT_QOM);
262 type_register_static(&dummy_info);
263 g_test_add_func("/crypto/task/complete", test_task_complete);
264 g_test_add_func("/crypto/task/datafree", test_task_data_free);
265 g_test_add_func("/crypto/task/failure", test_task_failure);
266 g_test_add_func("/crypto/task/thread_complete", test_task_thread_complete);
267 g_test_add_func("/crypto/task/thread_failure", test_task_thread_failure);
268 return g_test_run();
269 }