]> git.proxmox.com Git - mirror_qemu.git/blob - tests/test-io-task.c
io: change the QIOTask callback signature
[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
26 #define TYPE_DUMMY "qemu:dummy"
27
28 typedef struct DummyObject DummyObject;
29 typedef struct DummyObjectClass DummyObjectClass;
30
31 struct DummyObject {
32 Object parent;
33 };
34
35 struct DummyObjectClass {
36 ObjectClass parent;
37 };
38
39 static const TypeInfo dummy_info = {
40 .parent = TYPE_OBJECT,
41 .name = TYPE_DUMMY,
42 .instance_size = sizeof(DummyObject),
43 .class_size = sizeof(DummyObjectClass),
44 };
45
46 struct TestTaskData {
47 Object *source;
48 Error *err;
49 bool freed;
50 };
51
52
53 static void task_callback(QIOTask *task,
54 gpointer opaque)
55 {
56 struct TestTaskData *data = opaque;
57
58 data->source = qio_task_get_source(task);
59 qio_task_propagate_error(task, &data->err);
60 }
61
62
63 static 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);
78
79 g_assert(data.source == obj);
80 g_assert(data.err == NULL);
81 g_assert(data.freed == false);
82 }
83
84
85 static void task_data_free(gpointer opaque)
86 {
87 struct TestTaskData *data = opaque;
88
89 data->freed = true;
90 }
91
92
93 static 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
111 static void test_task_failure(void)
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
122 qio_task_set_error(task, err);
123 qio_task_complete(task);
124
125 object_unref(obj);
126
127 g_assert(data.source == obj);
128 g_assert(data.err == err);
129 g_assert(data.freed == false);
130
131 }
132
133
134 struct TestThreadWorkerData {
135 Object *source;
136 Error *err;
137 bool fail;
138 GThread *worker;
139 GThread *complete;
140 GMainLoop *loop;
141 };
142
143 static int test_task_thread_worker(QIOTask *task,
144 Error **errp,
145 gpointer opaque)
146 {
147 struct TestThreadWorkerData *data = opaque;
148
149 data->worker = g_thread_self();
150
151 if (data->fail) {
152 error_setg(errp, "Testing fail");
153 return -1;
154 }
155
156 return 0;
157 }
158
159
160 static void test_task_thread_callback(QIOTask *task,
161 gpointer opaque)
162 {
163 struct TestThreadWorkerData *data = opaque;
164
165 data->source = qio_task_get_source(task);
166 qio_task_propagate_error(task, &data->err);
167
168 data->complete = g_thread_self();
169
170 g_main_loop_quit(data->loop);
171 }
172
173
174 static void test_task_thread_complete(void)
175 {
176 QIOTask *task;
177 Object *obj = object_new(TYPE_DUMMY);
178 struct TestThreadWorkerData data = { 0 };
179 GThread *self;
180
181 data.loop = g_main_loop_new(g_main_context_default(),
182 TRUE);
183
184 task = qio_task_new(obj,
185 test_task_thread_callback,
186 &data,
187 NULL);
188
189 qio_task_run_in_thread(task,
190 test_task_thread_worker,
191 &data,
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
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
243 self = g_thread_self();
244
245 /* Make sure the test_task_thread_worker actually got
246 * run in a different thread */
247 g_assert(data.worker != self);
248
249 /* And that the test_task_thread_callback got rnu in
250 * the main loop thread (ie this one) */
251 g_assert(data.complete == self);
252 }
253
254
255 int main(int argc, char **argv)
256 {
257 g_test_init(&argc, &argv, NULL);
258 module_call_init(MODULE_INIT_QOM);
259 type_register_static(&dummy_info);
260 g_test_add_func("/crypto/task/complete", test_task_complete);
261 g_test_add_func("/crypto/task/datafree", test_task_data_free);
262 g_test_add_func("/crypto/task/failure", test_task_failure);
263 g_test_add_func("/crypto/task/thread_complete", test_task_thread_complete);
264 g_test_add_func("/crypto/task/thread_failure", test_task_thread_failure);
265 return g_test_run();
266 }