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