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.1 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 "qom/object.h"
25 #include "qapi/error.h"
26 #include "qemu/module.h"
28 #define TYPE_DUMMY "qemu:dummy"
30 typedef struct DummyObject DummyObject
;
31 typedef struct DummyObjectClass DummyObjectClass
;
37 struct DummyObjectClass
{
41 static const TypeInfo dummy_info
= {
42 .parent
= TYPE_OBJECT
,
44 .instance_size
= sizeof(DummyObject
),
45 .class_size
= sizeof(DummyObjectClass
),
55 static void task_callback(QIOTask
*task
,
58 struct TestTaskData
*data
= opaque
;
60 data
->source
= qio_task_get_source(task
);
61 qio_task_propagate_error(task
, &data
->err
);
65 static void test_task_complete(void)
68 Object
*obj
= object_new(TYPE_DUMMY
);
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
);
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_set_error(task
, err
);
125 qio_task_complete(task
);
129 g_assert(data
.source
== obj
);
130 g_assert(data
.err
== err
);
131 g_assert(data
.freed
== false);
132 error_free(data
.err
);
136 struct TestThreadWorkerData
{
145 static void test_task_thread_worker(QIOTask
*task
,
148 struct TestThreadWorkerData
*data
= opaque
;
150 data
->worker
= g_thread_self();
154 error_setg(&err
, "Testing fail");
155 qio_task_set_error(task
, err
);
160 static void test_task_thread_callback(QIOTask
*task
,
163 struct TestThreadWorkerData
*data
= opaque
;
165 data
->source
= qio_task_get_source(task
);
166 qio_task_propagate_error(task
, &data
->err
);
168 data
->complete
= g_thread_self();
170 g_main_loop_quit(data
->loop
);
174 static void test_task_thread_complete(void)
177 Object
*obj
= object_new(TYPE_DUMMY
);
178 struct TestThreadWorkerData data
= { 0 };
181 data
.loop
= g_main_loop_new(g_main_context_default(),
184 task
= qio_task_new(obj
,
185 test_task_thread_callback
,
189 qio_task_run_in_thread(task
,
190 test_task_thread_worker
,
195 g_main_loop_run(data
.loop
);
197 g_main_loop_unref(data
.loop
);
200 g_assert(data
.source
== obj
);
201 g_assert(data
.err
== NULL
);
203 self
= g_thread_self();
205 /* Make sure the test_task_thread_worker actually got
206 * run in a different thread */
207 g_assert(data
.worker
!= self
);
209 /* And that the test_task_thread_callback got rnu in
210 * the main loop thread (ie this one) */
211 g_assert(data
.complete
== self
);
215 static void test_task_thread_failure(void)
218 Object
*obj
= object_new(TYPE_DUMMY
);
219 struct TestThreadWorkerData data
= { 0 };
222 data
.loop
= g_main_loop_new(g_main_context_default(),
226 task
= qio_task_new(obj
,
227 test_task_thread_callback
,
231 qio_task_run_in_thread(task
,
232 test_task_thread_worker
,
237 g_main_loop_run(data
.loop
);
239 g_main_loop_unref(data
.loop
);
242 g_assert(data
.source
== obj
);
243 error_free_or_abort(&data
.err
);
245 self
= g_thread_self();
247 /* Make sure the test_task_thread_worker actually got
248 * run in a different thread */
249 g_assert(data
.worker
!= self
);
251 /* And that the test_task_thread_callback got rnu in
252 * the main loop thread (ie this one) */
253 g_assert(data
.complete
== self
);
257 int main(int argc
, char **argv
)
259 g_test_init(&argc
, &argv
, NULL
);
260 module_call_init(MODULE_INIT_QOM
);
261 type_register_static(&dummy_info
);
262 g_test_add_func("/crypto/task/complete", test_task_complete
);
263 g_test_add_func("/crypto/task/datafree", test_task_data_free
);
264 g_test_add_func("/crypto/task/failure", test_task_failure
);
265 g_test_add_func("/crypto/task/thread_complete", test_task_thread_complete
);
266 g_test_add_func("/crypto/task/thread_failure", test_task_thread_failure
);