qemu/tests/test-io-task.c
<<
>>
Prefs
   1/*
   2 * QEMU I/O task tests
   3 *
   4 * Copyright (c) 2015 Red Hat, Inc.
   5 *
   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.
  10 *
  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.
  15 *
  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/>.
  18 *
  19 */
  20
  21#include "qemu/osdep.h"
  22
  23#include "io/task.h"
  24#include "qapi/error.h"
  25
  26#define TYPE_DUMMY "qemu:dummy"
  27
  28typedef struct DummyObject DummyObject;
  29typedef struct DummyObjectClass DummyObjectClass;
  30
  31struct DummyObject {
  32    Object parent;
  33};
  34
  35struct DummyObjectClass {
  36    ObjectClass parent;
  37};
  38
  39static const TypeInfo dummy_info = {
  40    .parent = TYPE_OBJECT,
  41    .name = TYPE_DUMMY,
  42    .instance_size = sizeof(DummyObject),
  43    .class_size = sizeof(DummyObjectClass),
  44};
  45
  46struct TestTaskData {
  47    Object *source;
  48    Error *err;
  49    bool freed;
  50};
  51
  52
  53static void task_callback(Object *source,
  54                          Error *err,
  55                          gpointer opaque)
  56{
  57    struct TestTaskData *data = opaque;
  58
  59    data->source = source;
  60    data->err = err;
  61}
  62
  63
  64static void test_task_complete(void)
  65{
  66    QIOTask *task;
  67    Object *obj = object_new(TYPE_DUMMY);
  68    Object *src;
  69    struct TestTaskData data = { NULL, NULL, false };
  70
  71    task = qio_task_new(obj, task_callback, &data, NULL);
  72    src = qio_task_get_source(task);
  73
  74    qio_task_complete(task);
  75
  76    g_assert(obj == src);
  77
  78    object_unref(obj);
  79    object_unref(src);
  80
  81    g_assert(data.source == obj);
  82    g_assert(data.err == NULL);
  83    g_assert(data.freed == false);
  84}
  85
  86
  87static void task_data_free(gpointer opaque)
  88{
  89    struct TestTaskData *data = opaque;
  90
  91    data->freed = true;
  92}
  93
  94
  95static void test_task_data_free(void)
  96{
  97    QIOTask *task;
  98    Object *obj = object_new(TYPE_DUMMY);
  99    struct TestTaskData data = { NULL, NULL, false };
 100
 101    task = qio_task_new(obj, task_callback, &data, task_data_free);
 102
 103    qio_task_complete(task);
 104
 105    object_unref(obj);
 106
 107    g_assert(data.source == obj);
 108    g_assert(data.err == NULL);
 109    g_assert(data.freed == true);
 110}
 111
 112
 113static void test_task_failure(void)
 114{
 115    QIOTask *task;
 116    Object *obj = object_new(TYPE_DUMMY);
 117    struct TestTaskData data = { NULL, NULL, false };
 118    Error *err = NULL;
 119
 120    task = qio_task_new(obj, task_callback, &data, NULL);
 121
 122    error_setg(&err, "Some error");
 123
 124    qio_task_abort(task, err);
 125
 126    error_free(err);
 127    object_unref(obj);
 128
 129    g_assert(data.source == obj);
 130    g_assert(data.err == err);
 131    g_assert(data.freed == false);
 132
 133}
 134
 135
 136struct TestThreadWorkerData {
 137    Object *source;
 138    Error *err;
 139    bool fail;
 140    GThread *worker;
 141    GThread *complete;
 142    GMainLoop *loop;
 143};
 144
 145static int test_task_thread_worker(QIOTask *task,
 146                                   Error **errp,
 147                                   gpointer opaque)
 148{
 149    struct TestThreadWorkerData *data = opaque;
 150
 151    data->worker = g_thread_self();
 152
 153    if (data->fail) {
 154        error_setg(errp, "Testing fail");
 155        return -1;
 156    }
 157
 158    return 0;
 159}
 160
 161
 162static void test_task_thread_callback(Object *source,
 163                                      Error *err,
 164                                      gpointer opaque)
 165{
 166    struct TestThreadWorkerData *data = opaque;
 167
 168    data->source = source;
 169    data->err = err;
 170
 171    data->complete = g_thread_self();
 172
 173    g_main_loop_quit(data->loop);
 174}
 175
 176
 177static void test_task_thread_complete(void)
 178{
 179    QIOTask *task;
 180    Object *obj = object_new(TYPE_DUMMY);
 181    struct TestThreadWorkerData data = { 0 };
 182    GThread *self;
 183
 184    data.loop = g_main_loop_new(g_main_context_default(),
 185                                TRUE);
 186
 187    task = qio_task_new(obj,
 188                        test_task_thread_callback,
 189                        &data,
 190                        NULL);
 191
 192    qio_task_run_in_thread(task,
 193                           test_task_thread_worker,
 194                           &data,
 195                           NULL);
 196
 197    g_main_loop_run(data.loop);
 198
 199    g_main_loop_unref(data.loop);
 200    object_unref(obj);
 201
 202    g_assert(data.source == obj);
 203    g_assert(data.err == NULL);
 204
 205    self = g_thread_self();
 206
 207    /* Make sure the test_task_thread_worker actually got
 208     * run in a different thread */
 209    g_assert(data.worker != self);
 210
 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);
 214}
 215
 216
 217static void test_task_thread_failure(void)
 218{
 219    QIOTask *task;
 220    Object *obj = object_new(TYPE_DUMMY);
 221    struct TestThreadWorkerData data = { 0 };
 222    GThread *self;
 223
 224    data.loop = g_main_loop_new(g_main_context_default(),
 225                                TRUE);
 226    data.fail = true;
 227
 228    task = qio_task_new(obj,
 229                        test_task_thread_callback,
 230                        &data,
 231                        NULL);
 232
 233    qio_task_run_in_thread(task,
 234                           test_task_thread_worker,
 235                           &data,
 236                           NULL);
 237
 238    g_main_loop_run(data.loop);
 239
 240    g_main_loop_unref(data.loop);
 241    object_unref(obj);
 242
 243    g_assert(data.source == obj);
 244    g_assert(data.err != NULL);
 245
 246    self = g_thread_self();
 247
 248    /* Make sure the test_task_thread_worker actually got
 249     * run in a different thread */
 250    g_assert(data.worker != self);
 251
 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);
 255}
 256
 257
 258int main(int argc, char **argv)
 259{
 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);
 268    return g_test_run();
 269}
 270