block/qapi: explicitly warn if !has_full_backing_filename
[qemu/ar7.git] / tests / test-io-task.c
blob3344382c7f88dd90ec6817ec9b9117df40fb56ac
1 /*
2 * QEMU I/O task tests
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 <glib.h>
23 #include "io/task.h"
25 #define TYPE_DUMMY "qemu:dummy"
27 typedef struct DummyObject DummyObject;
28 typedef struct DummyObjectClass DummyObjectClass;
30 struct DummyObject {
31 Object parent;
34 struct DummyObjectClass {
35 ObjectClass parent;
38 static const TypeInfo dummy_info = {
39 .parent = TYPE_OBJECT,
40 .name = TYPE_DUMMY,
41 .instance_size = sizeof(DummyObject),
42 .class_size = sizeof(DummyObjectClass),
45 struct TestTaskData {
46 Object *source;
47 Error *err;
48 bool freed;
52 static void task_callback(Object *source,
53 Error *err,
54 gpointer opaque)
56 struct TestTaskData *data = opaque;
58 data->source = source;
59 data->err = err;
63 static void test_task_complete(void)
65 QIOTask *task;
66 Object *obj = object_new(TYPE_DUMMY);
67 Object *src;
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);
75 g_assert(obj == src);
77 object_unref(obj);
78 object_unref(src);
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;
90 data->freed = true;
94 static void test_task_data_free(void)
96 QIOTask *task;
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);
104 object_unref(obj);
106 g_assert(data.source == obj);
107 g_assert(data.err == NULL);
108 g_assert(data.freed == true);
112 static void test_task_error(void)
114 QIOTask *task;
115 Object *obj = object_new(TYPE_DUMMY);
116 struct TestTaskData data = { NULL, NULL, false };
117 Error *err = NULL;
119 task = qio_task_new(obj, task_callback, &data, NULL);
121 error_setg(&err, "Some error");
123 qio_task_abort(task, err);
125 error_free(err);
126 object_unref(obj);
128 g_assert(data.source == obj);
129 g_assert(data.err == err);
130 g_assert(data.freed == false);
135 struct TestThreadWorkerData {
136 Object *source;
137 Error *err;
138 bool fail;
139 GThread *worker;
140 GThread *complete;
141 GMainLoop *loop;
144 static int test_task_thread_worker(QIOTask *task,
145 Error **errp,
146 gpointer opaque)
148 struct TestThreadWorkerData *data = opaque;
150 data->worker = g_thread_self();
152 if (data->fail) {
153 error_setg(errp, "Testing fail");
154 return -1;
157 return 0;
161 static void test_task_thread_callback(Object *source,
162 Error *err,
163 gpointer opaque)
165 struct TestThreadWorkerData *data = opaque;
167 data->source = source;
168 data->err = err;
170 data->complete = g_thread_self();
172 g_main_loop_quit(data->loop);
176 static void test_task_thread_complete(void)
178 QIOTask *task;
179 Object *obj = object_new(TYPE_DUMMY);
180 struct TestThreadWorkerData data = { 0 };
181 GThread *self;
183 data.loop = g_main_loop_new(g_main_context_default(),
184 TRUE);
186 task = qio_task_new(obj,
187 test_task_thread_callback,
188 &data,
189 NULL);
191 qio_task_run_in_thread(task,
192 test_task_thread_worker,
193 &data,
194 NULL);
196 g_main_loop_run(data.loop);
198 g_main_loop_unref(data.loop);
199 object_unref(obj);
201 g_assert(data.source == obj);
202 g_assert(data.err == NULL);
204 self = g_thread_self();
206 /* Make sure the test_task_thread_worker actually got
207 * run in a different thread */
208 g_assert(data.worker != self);
210 /* And that the test_task_thread_callback got rnu in
211 * the main loop thread (ie this one) */
212 g_assert(data.complete == self);
216 static void test_task_thread_error(void)
218 QIOTask *task;
219 Object *obj = object_new(TYPE_DUMMY);
220 struct TestThreadWorkerData data = { 0 };
221 GThread *self;
223 data.loop = g_main_loop_new(g_main_context_default(),
224 TRUE);
225 data.fail = true;
227 task = qio_task_new(obj,
228 test_task_thread_callback,
229 &data,
230 NULL);
232 qio_task_run_in_thread(task,
233 test_task_thread_worker,
234 &data,
235 NULL);
237 g_main_loop_run(data.loop);
239 g_main_loop_unref(data.loop);
240 object_unref(obj);
242 g_assert(data.source == obj);
243 g_assert(data.err != NULL);
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/error", test_task_error);
265 g_test_add_func("/crypto/task/thread_complete", test_task_thread_complete);
266 g_test_add_func("/crypto/task/thread_error", test_task_thread_error);
267 return g_test_run();