| 1 |
/* test-fiber.c |
| 2 |
* |
| 3 |
* Copyright 2022 Christian Hergert <[email protected]> |
| 4 |
* |
| 5 |
* This library is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU Lesser General Public License as |
| 7 |
* published by the Free Software Foundation; either version 2.1 of the |
| 8 |
* License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This library is distributed in the hope that it will be useful, but |
| 11 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* Lesser General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License along |
| 16 |
* with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
* |
| 18 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "config.h" |
| 22 |
|
| 23 |
#include <libdex.h> |
| 24 |
|
| 25 |
#include "dex-fiber-private.h" |
| 26 |
|
| 27 |
#define ASSERT_STATUS(f,status) g_assert_cmpint(status, ==, dex_future_get_status(DEX_FUTURE(f))) |
| 28 |
#define ASSERT_ERROR(f,d,c) \ |
| 29 |
G_STMT_START { \ |
| 30 |
GError *_error = NULL; \ |
| 31 |
ASSERT_STATUS (f, DEX_FUTURE_STATUS_REJECTED); \ |
| 32 |
g_assert_null (dex_future_get_value (DEX_FUTURE (f), &_error)); \ |
| 33 |
g_assert_cmpint (_error->domain, ==, d); \ |
| 34 |
g_assert_cmpint (_error->code, ==, c); \ |
| 35 |
g_clear_error (&_error); \ |
| 36 |
} G_STMT_END |
| 37 |
|
| 38 |
static int test_arg = 123; |
| 39 |
|
| 40 |
static DexFuture * |
| 41 |
scheduler_fiber_func (gpointer user_data) |
| 42 |
{ |
| 43 |
test_arg = 99; |
| 44 |
return dex_future_new_for_int (99); |
| 45 |
} |
| 46 |
|
| 47 |
static DexFuture * |
| 48 |
scheduler_fiber_error (gpointer user_data) |
| 49 |
{ |
| 50 |
return NULL; |
| 51 |
} |
| 52 |
|
| 53 |
static void |
| 54 |
test_fiber_scheduler_basic (void) |
| 55 |
{ |
| 56 |
DexFiberScheduler *fiber_scheduler = dex_fiber_scheduler_new (); |
| 57 |
DexFiber *fiber = dex_fiber_new (scheduler_fiber_func, NULL, NULL, 0); |
| 58 |
const GValue *value; |
| 59 |
|
| 60 |
g_source_attach ((GSource *)fiber_scheduler, NULL); |
| 61 |
|
| 62 |
test_arg = 0; |
| 63 |
dex_future_set_static_name (DEX_FUTURE (fiber), "fiber_func"); |
| 64 |
dex_fiber_scheduler_register (fiber_scheduler, fiber); |
| 65 |
while (g_main_context_pending (NULL)) |
| 66 |
g_main_context_iteration (NULL, FALSE); |
| 67 |
g_assert_cmpint (test_arg, ==, 99); |
| 68 |
|
| 69 |
ASSERT_STATUS (fiber, DEX_FUTURE_STATUS_RESOLVED); |
| 70 |
value = dex_future_get_value (DEX_FUTURE (fiber), NULL); |
| 71 |
g_assert_cmpint (99, ==, g_value_get_int (value)); |
| 72 |
dex_clear (&fiber); |
| 73 |
|
| 74 |
fiber = dex_fiber_new (scheduler_fiber_error, NULL, NULL, 0); |
| 75 |
dex_future_set_static_name (DEX_FUTURE (fiber), "fiber_error"); |
| 76 |
dex_fiber_scheduler_register (fiber_scheduler, fiber); |
| 77 |
while (g_main_context_pending (NULL)) |
| 78 |
g_main_context_iteration (NULL, FALSE); |
| 79 |
ASSERT_ERROR (fiber, DEX_ERROR, DEX_ERROR_FIBER_EXITED); |
| 80 |
dex_clear (&fiber); |
| 81 |
|
| 82 |
g_source_destroy ((GSource *)fiber_scheduler); |
| 83 |
g_source_unref ((GSource *)fiber_scheduler); |
| 84 |
} |
| 85 |
|
| 86 |
static DexFuture * |
| 87 |
test_await_send (gpointer user_data) |
| 88 |
{ |
| 89 |
DexChannel *channel = user_data; |
| 90 |
|
| 91 |
for (guint i = 0; i < 4; i++) |
| 92 |
{ |
| 93 |
DexFuture *send; |
| 94 |
const GValue *value; |
| 95 |
GError *error = NULL; |
| 96 |
|
| 97 |
send = dex_channel_send (channel, dex_future_new_for_int (200)); |
| 98 |
g_assert_true (dex_await (dex_ref (send), &error)); |
| 99 |
g_assert_no_error (error); |
| 100 |
value = dex_future_get_value (send, NULL); |
| 101 |
g_assert_nonnull (value); |
| 102 |
g_assert_true (G_VALUE_HOLDS_UINT (value)); |
| 103 |
dex_unref (send); |
| 104 |
} |
| 105 |
|
| 106 |
return dex_future_new_for_boolean (TRUE); |
| 107 |
} |
| 108 |
|
| 109 |
static DexFuture * |
| 110 |
test_await_recv (gpointer user_data) |
| 111 |
{ |
| 112 |
DexChannel *channel = user_data; |
| 113 |
|
| 114 |
for (guint i = 0; i < 4; i++) |
| 115 |
{ |
| 116 |
DexFuture *recv; |
| 117 |
GError *error = NULL; |
| 118 |
const GValue *value; |
| 119 |
|
| 120 |
recv = dex_channel_receive (channel); |
| 121 |
g_assert_true (dex_await (dex_ref (recv), &error)); |
| 122 |
g_assert_no_error (error); |
| 123 |
value = dex_future_get_value (recv, NULL); |
| 124 |
g_assert_nonnull (value); |
| 125 |
g_assert_true (G_VALUE_HOLDS_INT (value)); |
| 126 |
g_assert_cmpint (200, ==, g_value_get_int (value)); |
| 127 |
dex_unref (recv); |
| 128 |
} |
| 129 |
|
| 130 |
return dex_future_new_for_boolean (TRUE); |
| 131 |
} |
| 132 |
|
| 133 |
static void |
| 134 |
test_fiber_scheduler_await (void) |
| 135 |
{ |
| 136 |
DexFiberScheduler *fiber_scheduler = dex_fiber_scheduler_new (); |
| 137 |
DexChannel *channel = dex_channel_new (0); |
| 138 |
DexFiber *fiber1 = dex_fiber_new (test_await_send, dex_ref (channel), dex_unref, 0); |
| 139 |
DexFiber *fiber2 = dex_fiber_new (test_await_recv, dex_ref (channel), dex_unref, 0); |
| 140 |
|
| 141 |
dex_future_set_static_name (DEX_FUTURE (fiber1), "fiber1"); |
| 142 |
dex_future_set_static_name (DEX_FUTURE (fiber2), "fiber2"); |
| 143 |
|
| 144 |
dex_fiber_scheduler_register (fiber_scheduler, fiber2); |
| 145 |
dex_fiber_scheduler_register (fiber_scheduler, fiber1); |
| 146 |
|
| 147 |
g_source_attach ((GSource *)fiber_scheduler, NULL); |
| 148 |
|
| 149 |
while (g_main_context_pending (NULL)) |
| 150 |
g_main_context_iteration (NULL, FALSE); |
| 151 |
|
| 152 |
ASSERT_STATUS (fiber1, DEX_FUTURE_STATUS_RESOLVED); |
| 153 |
ASSERT_STATUS (fiber2, DEX_FUTURE_STATUS_RESOLVED); |
| 154 |
|
| 155 |
dex_clear (&fiber1); |
| 156 |
dex_clear (&fiber2); |
| 157 |
dex_clear (&channel); |
| 158 |
|
| 159 |
g_source_destroy ((GSource *)fiber_scheduler); |
| 160 |
g_source_unref ((GSource *)fiber_scheduler); |
| 161 |
} |
| 162 |
|
| 163 |
static DexFuture * |
| 164 |
test_fiber_cancel_propagate_fiber (gpointer data) |
| 165 |
{ |
| 166 |
DexFuture *conditional = data; |
| 167 |
GError *error = NULL; |
| 168 |
gboolean ret; |
| 169 |
|
| 170 |
g_assert (DEX_IS_PROMISE (conditional)); |
| 171 |
|
| 172 |
ret = dex_await (dex_ref (conditional), &error); |
| 173 |
g_assert_false (ret); |
| 174 |
g_assert_error (error, DEX_ERROR, DEX_ERROR_FIBER_CANCELLED); |
| 175 |
|
| 176 |
return dex_future_new_for_error (g_steal_pointer (&error)); |
| 177 |
} |
| 178 |
|
| 179 |
static DexFuture * |
| 180 |
after_fiber_cancelled (DexFuture *future, |
| 181 |
gpointer user_data) |
| 182 |
{ |
| 183 |
g_assert_not_reached (); |
| 184 |
return NULL; |
| 185 |
} |
| 186 |
|
| 187 |
static void |
| 188 |
test_fiber_cancel_propagate (void) |
| 189 |
{ |
| 190 |
DexFiberScheduler *fiber_scheduler = dex_fiber_scheduler_new (); |
| 191 |
DexPromise *conditional = dex_promise_new (); |
| 192 |
DexFiber *fiber = dex_fiber_new (test_fiber_cancel_propagate_fiber, dex_ref (conditional), dex_unref, 0); |
| 193 |
DexFuture *block = dex_future_then (dex_ref (DEX_FUTURE (fiber)), after_fiber_cancelled, NULL, NULL); |
| 194 |
|
| 195 |
dex_fiber_scheduler_register (fiber_scheduler, fiber); |
| 196 |
|
| 197 |
g_source_attach ((GSource *)fiber_scheduler, NULL); |
| 198 |
|
| 199 |
while (g_main_context_pending (NULL)) |
| 200 |
g_main_context_iteration (NULL, FALSE); |
| 201 |
|
| 202 |
ASSERT_STATUS (fiber, DEX_FUTURE_STATUS_PENDING); |
| 203 |
ASSERT_STATUS (block, DEX_FUTURE_STATUS_PENDING); |
| 204 |
ASSERT_STATUS (conditional, DEX_FUTURE_STATUS_PENDING); |
| 205 |
|
| 206 |
dex_clear (&block); |
| 207 |
|
| 208 |
while (g_main_context_pending (NULL)) |
| 209 |
g_main_context_iteration (NULL, FALSE); |
| 210 |
|
| 211 |
ASSERT_STATUS (fiber, DEX_FUTURE_STATUS_REJECTED); |
| 212 |
ASSERT_STATUS (conditional, DEX_FUTURE_STATUS_PENDING); |
| 213 |
|
| 214 |
dex_promise_resolve_boolean (conditional, TRUE); |
| 215 |
|
| 216 |
ASSERT_STATUS (conditional, DEX_FUTURE_STATUS_RESOLVED); |
| 217 |
|
| 218 |
while (g_main_context_pending (NULL)) |
| 219 |
g_main_context_iteration (NULL, FALSE); |
| 220 |
|
| 221 |
dex_clear (&fiber); |
| 222 |
dex_clear (&conditional); |
| 223 |
|
| 224 |
g_source_destroy ((GSource *)fiber_scheduler); |
| 225 |
g_source_unref ((GSource *)fiber_scheduler); |
| 226 |
} |
| 227 |
|
| 228 |
int |
| 229 |
main (int argc, |
| 230 |
char *argv[]) |
| 231 |
{ |
| 232 |
dex_init (); |
| 233 |
g_test_init (&argc, &argv, NULL); |
| 234 |
g_test_add_func ("/Dex/TestSuite/FiberScheduler/basic", test_fiber_scheduler_basic); |
| 235 |
g_test_add_func ("/Dex/TestSuite/FiberScheduler/await", test_fiber_scheduler_await); |
| 236 |
g_test_add_func ("/Dex/TestSuite/FiberScheduler/cancel_propagate", test_fiber_cancel_propagate); |
| 237 |
return g_test_run (); |
| 238 |
} |