qapi: Consolidate QMP input visitor creation
[qemu/ar7.git] / tests / test-io-task.c
blob5a9775086c85e66fd61f707bfba7016e5b1354b6
1 /*
2 * QEMU I/O task tests
4 * Copyright (c) 2015 Red Hat, Inc.
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.
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.
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/>.
21 #include "qemu/osdep.h"
22 #include <glib.h>
24 #include "io/task.h"
25 #include "qapi/error.h"
27 #define TYPE_DUMMY "qemu:dummy"
29 typedef struct DummyObject DummyObject;
30 typedef struct DummyObjectClass DummyObjectClass;
32 struct DummyObject {
33 Object parent;
36 struct DummyObjectClass {
37 ObjectClass parent;
40 static const TypeInfo dummy_info = {
41 .parent = TYPE_OBJECT,
42 .name = TYPE_DUMMY,
43 .instance_size = sizeof(DummyObject),
44 .class_size = sizeof(DummyObjectClass),
47 struct TestTaskData {
48 Object *source;
49 Error *err;
50 bool freed;
54 static void task_callback(Object *source,
55 Error *err,
56 gpointer opaque)
58 struct TestTaskData *data = opaque;
60 data->source = source;
61 data->err = err;
65 static void test_task_complete(void)
67 QIOTask *task;
68 Object *obj = object_new(TYPE_DUMMY);
69 Object *src;
70 struct TestTaskData data = { NULL, NULL, false };
72 task = qio_task_new(obj, task_callback, &data, NULL);
73 src = qio_task_get_source(task);
75 qio_task_complete(task);
77 g_assert(obj == src);
79 object_unref(obj);
80 object_unref(src);
82 g_assert(data.source == obj);
83 g_assert(data.err == NULL);
84 g_assert(data.freed == false);
88 static void task_data_free(gpointer opaque)
90 struct TestTaskData *data = opaque;
92 data->freed = true;
96 static void test_task_data_free(void)
98 QIOTask *task;
99 Object *obj = object_new(TYPE_DUMMY);
100 struct TestTaskData data = { NULL, NULL, false };
102 task = qio_task_new(obj, task_callback, &data, task_data_free);
104 qio_task_complete(task);
106 object_unref(obj);
108 g_assert(data.source == obj);
109 g_assert(data.err == NULL);
110 g_assert(data.freed == true);
114 static void test_task_error(void)
116 QIOTask *task;
117 Object *obj = object_new(TYPE_DUMMY);
118 struct TestTaskData data = { NULL, NULL, false };
119 Error *err = NULL;
121 task = qio_task_new(obj, task_callback, &data, NULL);
123 error_setg(&err, "Some error");
125 qio_task_abort(task, err);
127 error_free(err);
128 object_unref(obj);
130 g_assert(data.source == obj);
131 g_assert(data.err == err);
132 g_assert(data.freed == false);
137 struct TestThreadWorkerData {
138 Object *source;
139 Error *err;
140 bool fail;
141 GThread *worker;
142 GThread *complete;
143 GMainLoop *loop;
146 static int test_task_thread_worker(QIOTask *task,
147 Error **errp,
148 gpointer opaque)
150 struct TestThreadWorkerData *data = opaque;
152 data->worker = g_thread_self();
154 if (data->fail) {
155 error_setg(errp, "Testing fail");
156 return -1;
159 return 0;
163 static void test_task_thread_callback(Object *source,
164 Error *err,
165 gpointer opaque)
167 struct TestThreadWorkerData *data = opaque;
169 data->source = source;
170 data->err = err;
172 data->complete = g_thread_self();
174 g_main_loop_quit(data->loop);
178 static void test_task_thread_complete(void)
180 QIOTask *task;
181 Object *obj = object_new(TYPE_DUMMY);
182 struct TestThreadWorkerData data = { 0 };
183 GThread *self;
185 data.loop = g_main_loop_new(g_main_context_default(),
186 TRUE);
188 task = qio_task_new(obj,
189 test_task_thread_callback,
190 &data,
191 NULL);
193 qio_task_run_in_thread(task,
194 test_task_thread_worker,
195 &data,
196 NULL);
198 g_main_loop_run(data.loop);
200 g_main_loop_unref(data.loop);
201 object_unref(obj);
203 g_assert(data.source == obj);
204 g_assert(data.err == NULL);
206 self = g_thread_self();
208 /* Make sure the test_task_thread_worker actually got
209 * run in a different thread */
210 g_assert(data.worker != self);
212 /* And that the test_task_thread_callback got rnu in
213 * the main loop thread (ie this one) */
214 g_assert(data.complete == self);
218 static void test_task_thread_error(void)
220 QIOTask *task;
221 Object *obj = object_new(TYPE_DUMMY);
222 struct TestThreadWorkerData data = { 0 };
223 GThread *self;
225 data.loop = g_main_loop_new(g_main_context_default(),
226 TRUE);
227 data.fail = true;
229 task = qio_task_new(obj,
230 test_task_thread_callback,
231 &data,
232 NULL);
234 qio_task_run_in_thread(task,
235 test_task_thread_worker,
236 &data,
237 NULL);
239 g_main_loop_run(data.loop);
241 g_main_loop_unref(data.loop);
242 object_unref(obj);
244 g_assert(data.source == obj);
245 g_assert(data.err != NULL);
247 self = g_thread_self();
249 /* Make sure the test_task_thread_worker actually got
250 * run in a different thread */
251 g_assert(data.worker != self);
253 /* And that the test_task_thread_callback got rnu in
254 * the main loop thread (ie this one) */
255 g_assert(data.complete == self);
259 int main(int argc, char **argv)
261 g_test_init(&argc, &argv, NULL);
262 module_call_init(MODULE_INIT_QOM);
263 type_register_static(&dummy_info);
264 g_test_add_func("/crypto/task/complete", test_task_complete);
265 g_test_add_func("/crypto/task/datafree", test_task_data_free);
266 g_test_add_func("/crypto/task/error", test_task_error);
267 g_test_add_func("/crypto/task/thread_complete", test_task_thread_complete);
268 g_test_add_func("/crypto/task/thread_error", test_task_thread_error);
269 return g_test_run();