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"
25 #include "qemu/module.h"
27 #define TYPE_DUMMY "qemu:dummy"
29 typedef struct DummyObject DummyObject
;
30 typedef struct DummyObjectClass DummyObjectClass
;
36 struct DummyObjectClass
{
40 static const TypeInfo dummy_info
= {
41 .parent
= TYPE_OBJECT
,
43 .instance_size
= sizeof(DummyObject
),
44 .class_size
= sizeof(DummyObjectClass
),
54 static void task_callback(QIOTask
*task
,
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)
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
);
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
;
94 static void test_task_data_free(void)
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
);
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)
115 Object
*obj
= object_new(TYPE_DUMMY
);
116 struct TestTaskData data
= { NULL
, NULL
, false };
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
);
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
{
144 static void test_task_thread_worker(QIOTask
*task
,
147 struct TestThreadWorkerData
*data
= opaque
;
149 data
->worker
= g_thread_self();
153 error_setg(&err
, "Testing fail");
154 qio_task_set_error(task
, err
);
159 static void test_task_thread_callback(QIOTask
*task
,
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)
176 Object
*obj
= object_new(TYPE_DUMMY
);
177 struct TestThreadWorkerData data
= { 0 };
180 data
.loop
= g_main_loop_new(g_main_context_default(),
183 task
= qio_task_new(obj
,
184 test_task_thread_callback
,
188 qio_task_run_in_thread(task
,
189 test_task_thread_worker
,
194 g_main_loop_run(data
.loop
);
196 g_main_loop_unref(data
.loop
);
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)
217 Object
*obj
= object_new(TYPE_DUMMY
);
218 struct TestThreadWorkerData data
= { 0 };
221 data
.loop
= g_main_loop_new(g_main_context_default(),
225 task
= qio_task_new(obj
,
226 test_task_thread_callback
,
230 qio_task_run_in_thread(task
,
231 test_task_thread_worker
,
236 g_main_loop_run(data
.loop
);
238 g_main_loop_unref(data
.loop
);
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
);