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(Object *source,
57 struct TestTaskData *data = opaque;
59 data->source = source;
64 static void test_task_complete(void)
67 Object *obj = object_new(TYPE_DUMMY);
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);
81 g_assert(data.source == obj);
82 g_assert(data.err == NULL);
83 g_assert(data.freed == false);
87 static void task_data_free(gpointer opaque)
89 struct TestTaskData *data = opaque;
95 static void test_task_data_free(void)
98 Object *obj = object_new(TYPE_DUMMY);
99 struct TestTaskData data = { NULL, NULL, false };
101 task = qio_task_new(obj, task_callback, &data, task_data_free);
103 qio_task_complete(task);
107 g_assert(data.source == obj);
108 g_assert(data.err == NULL);
109 g_assert(data.freed == true);
113 static void test_task_failure(void)
116 Object *obj = object_new(TYPE_DUMMY);
117 struct TestTaskData data = { NULL, NULL, false };
120 task = qio_task_new(obj, task_callback, &data, NULL);
122 error_setg(&err, "Some error");
124 qio_task_abort(task, err);
129 g_assert(data.source == obj);
130 g_assert(data.err == err);
131 g_assert(data.freed == false);
136 struct TestThreadWorkerData {
145 static int test_task_thread_worker(QIOTask *task,
149 struct TestThreadWorkerData *data = opaque;
151 data->worker = g_thread_self();
154 error_setg(errp, "Testing fail");
162 static void test_task_thread_callback(Object *source,
166 struct TestThreadWorkerData *data = opaque;
168 data->source = source;
171 data->complete = g_thread_self();
173 g_main_loop_quit(data->loop);
177 static void test_task_thread_complete(void)
180 Object *obj = object_new(TYPE_DUMMY);
181 struct TestThreadWorkerData data = { 0 };
184 data.loop = g_main_loop_new(g_main_context_default(),
187 task = qio_task_new(obj,
188 test_task_thread_callback,
192 qio_task_run_in_thread(task,
193 test_task_thread_worker,
197 g_main_loop_run(data.loop);
199 g_main_loop_unref(data.loop);
202 g_assert(data.source == obj);
203 g_assert(data.err == NULL);
205 self = g_thread_self();
207 /* Make sure the test_task_thread_worker actually got
208 * run in a different thread */
209 g_assert(data.worker != self);
211 /* And that the test_task_thread_callback got rnu in
212 * the main loop thread (ie this one) */
213 g_assert(data.complete == self);
217 static void test_task_thread_failure(void)
220 Object *obj = object_new(TYPE_DUMMY);
221 struct TestThreadWorkerData data = { 0 };
224 data.loop = g_main_loop_new(g_main_context_default(),
228 task = qio_task_new(obj,
229 test_task_thread_callback,
233 qio_task_run_in_thread(task,
234 test_task_thread_worker,
238 g_main_loop_run(data.loop);
240 g_main_loop_unref(data.loop);
243 g_assert(data.source == obj);
244 g_assert(data.err != NULL);
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);