ppc/kvm: Use error_report() instead of cpu_abort() for user-triggerable errors
[qemu/rayw.git] / tests / test-io-task.c
blobae46c56a471e455e6472a16048905572ba4dafc8
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 "qemu/osdep.h"
22 #include <glib.h>
24 #include "io/task.h"
26 #define TYPE_DUMMY "qemu:dummy"
28 typedef struct DummyObject DummyObject;
29 typedef struct DummyObjectClass DummyObjectClass;
31 struct DummyObject {
32 Object parent;
35 struct DummyObjectClass {
36 ObjectClass parent;
39 static const TypeInfo dummy_info = {
40 .parent = TYPE_OBJECT,
41 .name = TYPE_DUMMY,
42 .instance_size = sizeof(DummyObject),
43 .class_size = sizeof(DummyObjectClass),
46 struct TestTaskData {
47 Object *source;
48 Error *err;
49 bool freed;
53 static void task_callback(Object *source,
54 Error *err,
55 gpointer opaque)
57 struct TestTaskData *data = opaque;
59 data->source = source;
60 data->err = err;
64 static void test_task_complete(void)
66 QIOTask *task;
67 Object *obj = object_new(TYPE_DUMMY);
68 Object *src;
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);
76 g_assert(obj == src);
78 object_unref(obj);
79 object_unref(src);
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;
91 data->freed = true;
95 static void test_task_data_free(void)
97 QIOTask *task;
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);
105 object_unref(obj);
107 g_assert(data.source == obj);
108 g_assert(data.err == NULL);
109 g_assert(data.freed == true);
113 static void test_task_error(void)
115 QIOTask *task;
116 Object *obj = object_new(TYPE_DUMMY);
117 struct TestTaskData data = { NULL, NULL, false };
118 Error *err = NULL;
120 task = qio_task_new(obj, task_callback, &data, NULL);
122 error_setg(&err, "Some error");
124 qio_task_abort(task, err);
126 error_free(err);
127 object_unref(obj);
129 g_assert(data.source == obj);
130 g_assert(data.err == err);
131 g_assert(data.freed == false);
136 struct TestThreadWorkerData {
137 Object *source;
138 Error *err;
139 bool fail;
140 GThread *worker;
141 GThread *complete;
142 GMainLoop *loop;
145 static int test_task_thread_worker(QIOTask *task,
146 Error **errp,
147 gpointer opaque)
149 struct TestThreadWorkerData *data = opaque;
151 data->worker = g_thread_self();
153 if (data->fail) {
154 error_setg(errp, "Testing fail");
155 return -1;
158 return 0;
162 static void test_task_thread_callback(Object *source,
163 Error *err,
164 gpointer opaque)
166 struct TestThreadWorkerData *data = opaque;
168 data->source = source;
169 data->err = err;
171 data->complete = g_thread_self();
173 g_main_loop_quit(data->loop);
177 static void test_task_thread_complete(void)
179 QIOTask *task;
180 Object *obj = object_new(TYPE_DUMMY);
181 struct TestThreadWorkerData data = { 0 };
182 GThread *self;
184 data.loop = g_main_loop_new(g_main_context_default(),
185 TRUE);
187 task = qio_task_new(obj,
188 test_task_thread_callback,
189 &data,
190 NULL);
192 qio_task_run_in_thread(task,
193 test_task_thread_worker,
194 &data,
195 NULL);
197 g_main_loop_run(data.loop);
199 g_main_loop_unref(data.loop);
200 object_unref(obj);
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_error(void)
219 QIOTask *task;
220 Object *obj = object_new(TYPE_DUMMY);
221 struct TestThreadWorkerData data = { 0 };
222 GThread *self;
224 data.loop = g_main_loop_new(g_main_context_default(),
225 TRUE);
226 data.fail = true;
228 task = qio_task_new(obj,
229 test_task_thread_callback,
230 &data,
231 NULL);
233 qio_task_run_in_thread(task,
234 test_task_thread_worker,
235 &data,
236 NULL);
238 g_main_loop_run(data.loop);
240 g_main_loop_unref(data.loop);
241 object_unref(obj);
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/error", test_task_error);
266 g_test_add_func("/crypto/task/thread_complete", test_task_thread_complete);
267 g_test_add_func("/crypto/task/thread_error", test_task_thread_error);
268 return g_test_run();