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"
24 #include "qapi/error.h"
26 #define TYPE_DUMMY "qemu:dummy"
28 typedef struct DummyObject DummyObject;
29 typedef struct DummyObjectClass DummyObjectClass;
35 struct DummyObjectClass {
39 static const TypeInfo dummy_info = {
40 .parent = TYPE_OBJECT,
42 .instance_size = sizeof(DummyObject),
43 .class_size = sizeof(DummyObjectClass),
53 static void task_callback(QIOTask *task,
56 struct TestTaskData *data = opaque;
58 data->source = qio_task_get_source(task);
59 qio_task_propagate_error(task, &data->err);
63 static void test_task_complete(void)
66 Object *obj = object_new(TYPE_DUMMY);
68 struct TestTaskData data = { NULL, NULL, false };
70 task = qio_task_new(obj, task_callback, &data, NULL);
71 src = qio_task_get_source(task);
73 qio_task_complete(task);
79 g_assert(data.source == obj);
80 g_assert(data.err == NULL);
81 g_assert(data.freed == false);
85 static void task_data_free(gpointer opaque)
87 struct TestTaskData *data = opaque;
93 static void test_task_data_free(void)
96 Object *obj = object_new(TYPE_DUMMY);
97 struct TestTaskData data = { NULL, NULL, false };
99 task = qio_task_new(obj, task_callback, &data, task_data_free);
101 qio_task_complete(task);
105 g_assert(data.source == obj);
106 g_assert(data.err == NULL);
107 g_assert(data.freed == true);
111 static void test_task_failure(void)
114 Object *obj = object_new(TYPE_DUMMY);
115 struct TestTaskData data = { NULL, NULL, false };
118 task = qio_task_new(obj, task_callback, &data, NULL);
120 error_setg(&err, "Some error");
122 qio_task_set_error(task, err);
123 qio_task_complete(task);
127 g_assert(data.source == obj);
128 g_assert(data.err == err);
129 g_assert(data.freed == false);
130 error_free(data.err);
134 struct TestThreadWorkerData {
143 static void test_task_thread_worker(QIOTask *task,
146 struct TestThreadWorkerData *data = opaque;
148 data->worker = g_thread_self();
152 error_setg(&err, "Testing fail");
153 qio_task_set_error(task, err);
158 static void test_task_thread_callback(QIOTask *task,
161 struct TestThreadWorkerData *data = opaque;
163 data->source = qio_task_get_source(task);
164 qio_task_propagate_error(task, &data->err);
166 data->complete = g_thread_self();
168 g_main_loop_quit(data->loop);
172 static void test_task_thread_complete(void)
175 Object *obj = object_new(TYPE_DUMMY);
176 struct TestThreadWorkerData data = { 0 };
179 data.loop = g_main_loop_new(g_main_context_default(),
182 task = qio_task_new(obj,
183 test_task_thread_callback,
187 qio_task_run_in_thread(task,
188 test_task_thread_worker,
192 g_main_loop_run(data.loop);
194 g_main_loop_unref(data.loop);
197 g_assert(data.source == obj);
198 g_assert(data.err == NULL);
200 self = g_thread_self();
202 /* Make sure the test_task_thread_worker actually got
203 * run in a different thread */
204 g_assert(data.worker != self);
206 /* And that the test_task_thread_callback got rnu in
207 * the main loop thread (ie this one) */
208 g_assert(data.complete == self);
212 static void test_task_thread_failure(void)
215 Object *obj = object_new(TYPE_DUMMY);
216 struct TestThreadWorkerData data = { 0 };
219 data.loop = g_main_loop_new(g_main_context_default(),
223 task = qio_task_new(obj,
224 test_task_thread_callback,
228 qio_task_run_in_thread(task,
229 test_task_thread_worker,
233 g_main_loop_run(data.loop);
235 g_main_loop_unref(data.loop);
238 g_assert(data.source == obj);
239 g_assert(data.err != NULL);
241 error_free(data.err);
243 self = g_thread_self();
245 /* Make sure the test_task_thread_worker actually got
246 * run in a different thread */
247 g_assert(data.worker != self);
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);
255 int main(int argc, char **argv)
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);