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"
25 #include "qapi/error.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(Object
*source
,
58 struct TestTaskData
*data
= opaque
;
60 data
->source
= source
;
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
);
82 g_assert(data
.source
== obj
);
83 g_assert(data
.err
== NULL
);
84 g_assert(data
.freed
== false);
88 static void task_data_free(gpointer opaque
)
90 struct TestTaskData
*data
= opaque
;
96 static void test_task_data_free(void)
99 Object
*obj
= object_new(TYPE_DUMMY
);
100 struct TestTaskData data
= { NULL
, NULL
, false };
102 task
= qio_task_new(obj
, task_callback
, &data
, task_data_free
);
104 qio_task_complete(task
);
108 g_assert(data
.source
== obj
);
109 g_assert(data
.err
== NULL
);
110 g_assert(data
.freed
== true);
114 static void test_task_error(void)
117 Object
*obj
= object_new(TYPE_DUMMY
);
118 struct TestTaskData data
= { NULL
, NULL
, false };
121 task
= qio_task_new(obj
, task_callback
, &data
, NULL
);
123 error_setg(&err
, "Some error");
125 qio_task_abort(task
, err
);
130 g_assert(data
.source
== obj
);
131 g_assert(data
.err
== err
);
132 g_assert(data
.freed
== false);
137 struct TestThreadWorkerData
{
146 static int test_task_thread_worker(QIOTask
*task
,
150 struct TestThreadWorkerData
*data
= opaque
;
152 data
->worker
= g_thread_self();
155 error_setg(errp
, "Testing fail");
163 static void test_task_thread_callback(Object
*source
,
167 struct TestThreadWorkerData
*data
= opaque
;
169 data
->source
= source
;
172 data
->complete
= g_thread_self();
174 g_main_loop_quit(data
->loop
);
178 static void test_task_thread_complete(void)
181 Object
*obj
= object_new(TYPE_DUMMY
);
182 struct TestThreadWorkerData data
= { 0 };
185 data
.loop
= g_main_loop_new(g_main_context_default(),
188 task
= qio_task_new(obj
,
189 test_task_thread_callback
,
193 qio_task_run_in_thread(task
,
194 test_task_thread_worker
,
198 g_main_loop_run(data
.loop
);
200 g_main_loop_unref(data
.loop
);
203 g_assert(data
.source
== obj
);
204 g_assert(data
.err
== NULL
);
206 self
= g_thread_self();
208 /* Make sure the test_task_thread_worker actually got
209 * run in a different thread */
210 g_assert(data
.worker
!= self
);
212 /* And that the test_task_thread_callback got rnu in
213 * the main loop thread (ie this one) */
214 g_assert(data
.complete
== self
);
218 static void test_task_thread_error(void)
221 Object
*obj
= object_new(TYPE_DUMMY
);
222 struct TestThreadWorkerData data
= { 0 };
225 data
.loop
= g_main_loop_new(g_main_context_default(),
229 task
= qio_task_new(obj
,
230 test_task_thread_callback
,
234 qio_task_run_in_thread(task
,
235 test_task_thread_worker
,
239 g_main_loop_run(data
.loop
);
241 g_main_loop_unref(data
.loop
);
244 g_assert(data
.source
== obj
);
245 g_assert(data
.err
!= NULL
);
247 self
= g_thread_self();
249 /* Make sure the test_task_thread_worker actually got
250 * run in a different thread */
251 g_assert(data
.worker
!= self
);
253 /* And that the test_task_thread_callback got rnu in
254 * the main loop thread (ie this one) */
255 g_assert(data
.complete
== self
);
259 int main(int argc
, char **argv
)
261 g_test_init(&argc
, &argv
, NULL
);
262 module_call_init(MODULE_INIT_QOM
);
263 type_register_static(&dummy_info
);
264 g_test_add_func("/crypto/task/complete", test_task_complete
);
265 g_test_add_func("/crypto/task/datafree", test_task_data_free
);
266 g_test_add_func("/crypto/task/error", test_task_error
);
267 g_test_add_func("/crypto/task/thread_complete", test_task_thread_complete
);
268 g_test_add_func("/crypto/task/thread_error", test_task_thread_error
);