hw/net/e1000e_core: Let e1000e_can_receive() return a boolean
[qemu/kevin.git] / tests / test-io-task.c
blobaa8b653bfac26315c42ff48a5ca66dcaee3578b2
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"
23 #include "io/task.h"
24 #include "qapi/error.h"
25 #include "qemu/module.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(QIOTask *task,
55 gpointer opaque)
57 struct TestTaskData *data = opaque;
59 data->source = qio_task_get_source(task);
60 qio_task_propagate_error(task, &data->err);
64 static void test_task_complete(void)
66 QIOTask *task;
67 Object *obj = object_new(TYPE_DUMMY);
68 Object *src;
69 struct TestTaskData data = { NULL, NULL, false };
71 task = qio_task_new(obj, task_callback, &data, NULL);
72 src = qio_task_get_source(task);
74 qio_task_complete(task);
76 g_assert(obj == src);
78 object_unref(obj);
80 g_assert(data.source == obj);
81 g_assert(data.err == NULL);
82 g_assert(data.freed == false);
86 static void task_data_free(gpointer opaque)
88 struct TestTaskData *data = opaque;
90 data->freed = true;
94 static void test_task_data_free(void)
96 QIOTask *task;
97 Object *obj = object_new(TYPE_DUMMY);
98 struct TestTaskData data = { NULL, NULL, false };
100 task = qio_task_new(obj, task_callback, &data, task_data_free);
102 qio_task_complete(task);
104 object_unref(obj);
106 g_assert(data.source == obj);
107 g_assert(data.err == NULL);
108 g_assert(data.freed == true);
112 static void test_task_failure(void)
114 QIOTask *task;
115 Object *obj = object_new(TYPE_DUMMY);
116 struct TestTaskData data = { NULL, NULL, false };
117 Error *err = NULL;
119 task = qio_task_new(obj, task_callback, &data, NULL);
121 error_setg(&err, "Some error");
123 qio_task_set_error(task, err);
124 qio_task_complete(task);
126 object_unref(obj);
128 g_assert(data.source == obj);
129 g_assert(data.err == err);
130 g_assert(data.freed == false);
131 error_free(data.err);
135 struct TestThreadWorkerData {
136 Object *source;
137 Error *err;
138 bool fail;
139 GThread *worker;
140 GThread *complete;
141 GMainLoop *loop;
144 static void test_task_thread_worker(QIOTask *task,
145 gpointer opaque)
147 struct TestThreadWorkerData *data = opaque;
149 data->worker = g_thread_self();
151 if (data->fail) {
152 Error *err = NULL;
153 error_setg(&err, "Testing fail");
154 qio_task_set_error(task, err);
159 static void test_task_thread_callback(QIOTask *task,
160 gpointer opaque)
162 struct TestThreadWorkerData *data = opaque;
164 data->source = qio_task_get_source(task);
165 qio_task_propagate_error(task, &data->err);
167 data->complete = g_thread_self();
169 g_main_loop_quit(data->loop);
173 static void test_task_thread_complete(void)
175 QIOTask *task;
176 Object *obj = object_new(TYPE_DUMMY);
177 struct TestThreadWorkerData data = { 0 };
178 GThread *self;
180 data.loop = g_main_loop_new(g_main_context_default(),
181 TRUE);
183 task = qio_task_new(obj,
184 test_task_thread_callback,
185 &data,
186 NULL);
188 qio_task_run_in_thread(task,
189 test_task_thread_worker,
190 &data,
191 NULL,
192 NULL);
194 g_main_loop_run(data.loop);
196 g_main_loop_unref(data.loop);
197 object_unref(obj);
199 g_assert(data.source == obj);
200 g_assert(data.err == NULL);
202 self = g_thread_self();
204 /* Make sure the test_task_thread_worker actually got
205 * run in a different thread */
206 g_assert(data.worker != self);
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);
214 static void test_task_thread_failure(void)
216 QIOTask *task;
217 Object *obj = object_new(TYPE_DUMMY);
218 struct TestThreadWorkerData data = { 0 };
219 GThread *self;
221 data.loop = g_main_loop_new(g_main_context_default(),
222 TRUE);
223 data.fail = true;
225 task = qio_task_new(obj,
226 test_task_thread_callback,
227 &data,
228 NULL);
230 qio_task_run_in_thread(task,
231 test_task_thread_worker,
232 &data,
233 NULL,
234 NULL);
236 g_main_loop_run(data.loop);
238 g_main_loop_unref(data.loop);
239 object_unref(obj);
241 g_assert(data.source == obj);
242 g_assert(data.err != NULL);
244 error_free(data.err);
246 self = g_thread_self();
248 /* Make sure the test_task_thread_worker actually got
249 * run in a different thread */
250 g_assert(data.worker != self);
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);
258 int main(int argc, char **argv)
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();