| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#include "config.h" |
| 22 |
|
| 23 |
#include <libdex.h> |
| 24 |
#include <string.h> |
| 25 |
|
| 26 |
#include "test-util.h" |
| 27 |
|
| 28 |
static void test_coroutine_g_weak_ref_clear (GWeakRef *weak_ref); |
| 29 |
|
| 30 |
DEX_DEFINE_CLOSURE_TYPE (TestCoroutineCompleteState, test_coroutine_complete_state, |
| 31 |
DEX_DEFINE_CLOSURE_POINTER (guint *, ran, g_free)) |
| 32 |
|
| 33 |
DEX_DEFINE_CLOSURE_TYPE (TestCoroutineSuspendState, test_coroutine_suspend_state, |
| 34 |
DEX_DEFINE_CLOSURE_POINTER (DexPromise *, gate, dex_unref), |
| 35 |
DEX_DEFINE_CLOSURE_POINTER (gboolean *, saw_result, g_free), |
| 36 |
DEX_DEFINE_CLOSURE_POINTER (guint *, run_count, g_free), |
| 37 |
DEX_DEFINE_CLOSURE_VALUE (gboolean, gate_value)) |
| 38 |
|
| 39 |
DEX_DEFINE_CLOSURE_TYPE (TestCoroutineSuspendVoidState, test_coroutine_suspend_void_state, |
| 40 |
DEX_DEFINE_CLOSURE_POINTER (DexPromise *, gate, dex_unref), |
| 41 |
DEX_DEFINE_CLOSURE_POINTER (guint *, run_count, g_free)) |
| 42 |
|
| 43 |
DEX_DEFINE_CLOSURE_TYPE (TestCoroutineSuspendGauntletState, |
| 44 |
test_coroutine_suspend_gauntlet_state, |
| 45 |
DEX_DEFINE_CLOSURE_POINTER (guint *, run_count, g_free), |
| 46 |
DEX_DEFINE_CLOSURE_VALUE (guint, limit), |
| 47 |
DEX_DEFINE_CLOSURE_VALUE (guint, suspend_count)) |
| 48 |
|
| 49 |
DEX_DEFINE_CLOSURE_TYPE (TestCoroutineTaskGroupCancelState, |
| 50 |
test_coroutine_task_group_cancel_state, |
| 51 |
DEX_DEFINE_CLOSURE_POINTER (DexPromise *, gate, dex_unref)) |
| 52 |
DEX_DEFINE_CLOSURE_TYPE (TestCoroutineDestroyState, |
| 53 |
test_coroutine_destroy_state, |
| 54 |
DEX_DEFINE_CLOSURE_VALUE (gboolean, value)) |
| 55 |
|
| 56 |
DEX_DEFINE_CLOSURE_TYPE (TestCoroutineValueWithClearState, |
| 57 |
test_coroutine_value_with_clear_state, |
| 58 |
DEX_DEFINE_CLOSURE_VALUE_WITH_CLEAR (GWeakRef, |
| 59 |
object_ref, |
| 60 |
test_coroutine_g_weak_ref_clear)) |
| 61 |
|
| 62 |
static gint test_coroutine_destroy_state_freed = 0; |
| 63 |
static int test_coroutine_value_with_clear_freed = 0; |
| 64 |
|
| 65 |
#define TEST_COROUTINE_TIMEOUT() dex_timeout_new_usec (G_USEC_PER_SEC / 10000) |
| 66 |
|
| 67 |
#define TEST_COROUTINE_JUMP_STEP(state) \ |
| 68 |
G_STMT_START { \ |
| 69 |
(*((state)->run_count))++; \ |
| 70 |
DEX_COROUTINE_SUSPEND_BOOLEAN (&value, &error, \ |
| 71 |
TEST_COROUTINE_TIMEOUT ()); \ |
| 72 |
g_assert_error (error, DEX_ERROR, DEX_ERROR_TIMED_OUT); \ |
| 73 |
g_clear_error (&error); \ |
| 74 |
} G_STMT_END |
| 75 |
|
| 76 |
static DexFuture * |
| 77 |
test_coroutine_complete_func (DexCoroutineContext *context, |
| 78 |
gpointer user_data) |
| 79 |
{ |
| 80 |
TestCoroutineCompleteState *state = user_data; |
| 81 |
|
| 82 |
DEX_COROUTINE_BEGIN (context); |
| 83 |
|
| 84 |
if (state->ran != NULL) |
| 85 |
*state->ran = 1; |
| 86 |
|
| 87 |
return dex_future_new_for_int (123); |
| 88 |
|
| 89 |
DEX_COROUTINE_END; |
| 90 |
} |
| 91 |
|
| 92 |
static DexFuture * |
| 93 |
test_coroutine_suspend_func (DexCoroutineContext *context, |
| 94 |
gpointer user_data) |
| 95 |
{ |
| 96 |
TestCoroutineSuspendState *state = user_data; |
| 97 |
GError *error = NULL; |
| 98 |
|
| 99 |
DEX_COROUTINE_BEGIN (context); |
| 100 |
|
| 101 |
(*state->run_count)++; |
| 102 |
|
| 103 |
DEX_COROUTINE_SUSPEND_BOOLEAN (&state->gate_value, |
| 104 |
&error, |
| 105 |
dex_ref (DEX_FUTURE (state->gate))); |
| 106 |
|
| 107 |
if (error != NULL) |
| 108 |
return dex_future_new_for_error (g_steal_pointer (&error)); |
| 109 |
|
| 110 |
(*state->run_count)++; |
| 111 |
|
| 112 |
if (state->saw_result != NULL) |
| 113 |
*state->saw_result = state->gate_value; |
| 114 |
|
| 115 |
return dex_future_new_for_boolean (state->gate_value); |
| 116 |
|
| 117 |
DEX_COROUTINE_END; |
| 118 |
} |
| 119 |
|
| 120 |
static DexFuture * |
| 121 |
test_coroutine_suspend_void_func (DexCoroutineContext *context, |
| 122 |
gpointer user_data) |
| 123 |
{ |
| 124 |
TestCoroutineSuspendVoidState *state = user_data; |
| 125 |
|
| 126 |
DEX_COROUTINE_BEGIN (context); |
| 127 |
|
| 128 |
(*state->run_count)++; |
| 129 |
DEX_COROUTINE_SUSPEND (dex_ref (DEX_FUTURE (state->gate)), NULL); |
| 130 |
(*state->run_count)++; |
| 131 |
|
| 132 |
return dex_future_new_true (); |
| 133 |
|
| 134 |
DEX_COROUTINE_END; |
| 135 |
} |
| 136 |
|
| 137 |
static DexFuture * |
| 138 |
test_coroutine_suspend_gauntlet_func (DexCoroutineContext *context, |
| 139 |
gpointer user_data) |
| 140 |
{ |
| 141 |
TestCoroutineSuspendGauntletState *state = user_data; |
| 142 |
GError *error = NULL; |
| 143 |
gboolean value = FALSE; |
| 144 |
|
| 145 |
DEX_COROUTINE_BEGIN (context); |
| 146 |
|
| 147 |
while (state->suspend_count < state->limit) |
| 148 |
{ |
| 149 |
state->suspend_count++; |
| 150 |
(*state->run_count)++; |
| 151 |
|
| 152 |
DEX_COROUTINE_SUSPEND_BOOLEAN (&value, |
| 153 |
&error, |
| 154 |
TEST_COROUTINE_TIMEOUT ()); |
| 155 |
|
| 156 |
g_assert_error (error, DEX_ERROR, DEX_ERROR_TIMED_OUT); |
| 157 |
g_clear_error (&error); |
| 158 |
} |
| 159 |
|
| 160 |
return dex_future_new_true (); |
| 161 |
|
| 162 |
DEX_COROUTINE_END; |
| 163 |
} |
| 164 |
|
| 165 |
static DexFuture * |
| 166 |
test_coroutine_suspend_jump_func (DexCoroutineContext *context, |
| 167 |
gpointer user_data) |
| 168 |
{ |
| 169 |
TestCoroutineSuspendGauntletState *state = user_data; |
| 170 |
GError *error = NULL; |
| 171 |
gboolean value = FALSE; |
| 172 |
|
| 173 |
DEX_COROUTINE_BEGIN (context); |
| 174 |
|
| 175 |
TEST_COROUTINE_JUMP_STEP (state); |
| 176 |
TEST_COROUTINE_JUMP_STEP (state); |
| 177 |
TEST_COROUTINE_JUMP_STEP (state); |
| 178 |
TEST_COROUTINE_JUMP_STEP (state); |
| 179 |
TEST_COROUTINE_JUMP_STEP (state); |
| 180 |
TEST_COROUTINE_JUMP_STEP (state); |
| 181 |
TEST_COROUTINE_JUMP_STEP (state); |
| 182 |
TEST_COROUTINE_JUMP_STEP (state); |
| 183 |
TEST_COROUTINE_JUMP_STEP (state); |
| 184 |
TEST_COROUTINE_JUMP_STEP (state); |
| 185 |
|
| 186 |
return dex_future_new_true (); |
| 187 |
|
| 188 |
DEX_COROUTINE_END; |
| 189 |
} |
| 190 |
|
| 191 |
static DexFuture * |
| 192 |
test_coroutine_task_group_cancel_func (DexCoroutineContext *context, |
| 193 |
gpointer user_data) |
| 194 |
{ |
| 195 |
TestCoroutineTaskGroupCancelState *state = user_data; |
| 196 |
GError *error = NULL; |
| 197 |
gboolean value = FALSE; |
| 198 |
|
| 199 |
DEX_COROUTINE_BEGIN (context); |
| 200 |
|
| 201 |
DEX_COROUTINE_SUSPEND_BOOLEAN (&value, &error, dex_ref (DEX_FUTURE (state->gate))); |
| 202 |
if (error != NULL) |
| 203 |
return dex_future_new_for_error (g_steal_pointer (&error)); |
| 204 |
|
| 205 |
return dex_future_new_for_boolean (value); |
| 206 |
|
| 207 |
DEX_COROUTINE_END; |
| 208 |
} |
| 209 |
|
| 210 |
static DexFuture * |
| 211 |
test_coroutine_return_int_func (DexCoroutineContext *context, |
| 212 |
gpointer user_data) |
| 213 |
{ |
| 214 |
return dex_future_new_for_int (123); |
| 215 |
} |
| 216 |
|
| 217 |
static DexFuture * |
| 218 |
test_coroutine_return_uint_func (DexCoroutineContext *context, |
| 219 |
gpointer user_data) |
| 220 |
{ |
| 221 |
return dex_future_new_for_uint (123u); |
| 222 |
} |
| 223 |
|
| 224 |
static DexFuture * |
| 225 |
test_coroutine_return_boolean_func (DexCoroutineContext *context, |
| 226 |
gpointer user_data) |
| 227 |
{ |
| 228 |
return dex_future_new_for_boolean (TRUE); |
| 229 |
} |
| 230 |
|
| 231 |
static DexFuture * |
| 232 |
test_coroutine_return_double_func (DexCoroutineContext *context, |
| 233 |
gpointer user_data) |
| 234 |
{ |
| 235 |
return dex_future_new_for_double (3.25); |
| 236 |
} |
| 237 |
|
| 238 |
static DexFuture * |
| 239 |
test_coroutine_return_float_func (DexCoroutineContext *context, |
| 240 |
gpointer user_data) |
| 241 |
{ |
| 242 |
return dex_future_new_for_float (6.5f); |
| 243 |
} |
| 244 |
|
| 245 |
static DexFuture * |
| 246 |
test_coroutine_return_object_func (DexCoroutineContext *context, |
| 247 |
gpointer user_data) |
| 248 |
{ |
| 249 |
GObject *object = g_object_new (G_TYPE_OBJECT, NULL); |
| 250 |
DexFuture *future = dex_future_new_take_object (object); |
| 251 |
|
| 252 |
return future; |
| 253 |
} |
| 254 |
|
| 255 |
static DexFuture * |
| 256 |
test_coroutine_return_boxed_func (DexCoroutineContext *context, |
| 257 |
gpointer user_data) |
| 258 |
{ |
| 259 |
GBytes *boxed = g_bytes_new_static ("boxed", 5); |
| 260 |
GValue value = G_VALUE_INIT; |
| 261 |
DexFuture *future; |
| 262 |
|
| 263 |
g_value_init (&value, G_TYPE_BYTES); |
| 264 |
g_value_take_boxed (&value, boxed); |
| 265 |
future = dex_future_new_for_value (&value); |
| 266 |
g_value_unset (&value); |
| 267 |
|
| 268 |
return future; |
| 269 |
} |
| 270 |
|
| 271 |
static DexFuture * |
| 272 |
test_coroutine_return_pointer_func (DexCoroutineContext *context, |
| 273 |
gpointer user_data) |
| 274 |
{ |
| 275 |
return dex_future_new_for_pointer (GSIZE_TO_POINTER (0xdeadbeef)); |
| 276 |
} |
| 277 |
|
| 278 |
static DexFuture * |
| 279 |
test_coroutine_thread_pool_return_uint_func (DexCoroutineContext *context, |
| 280 |
gpointer user_data) |
| 281 |
{ |
| 282 |
return dex_future_new_for_uint (7u); |
| 283 |
} |
| 284 |
|
| 285 |
static void |
| 286 |
test_coroutine_spawn_complete (void) |
| 287 |
{ |
| 288 |
TestCoroutineCompleteState *state; |
| 289 |
DexFuture *future; |
| 290 |
int value; |
| 291 |
|
| 292 |
state = test_coroutine_complete_state_new (); |
| 293 |
state->ran = g_new0 (guint, 1); |
| 294 |
|
| 295 |
future = dex_scheduler_spawn_coroutine (NULL, |
| 296 |
test_coroutine_complete_func, |
| 297 |
state, |
| 298 |
NULL); |
| 299 |
|
| 300 |
value = dex_await_int (dex_ref (future), NULL); |
| 301 |
g_assert_cmpint (value, ==, 123); |
| 302 |
g_assert_cmpuint (*state->ran, ==, 1); |
| 303 |
test_coroutine_complete_state_free (state); |
| 304 |
dex_clear (&future); |
| 305 |
} |
| 306 |
|
| 307 |
static void |
| 308 |
test_coroutine_destroy_state_destroy_cb (gpointer user_data) |
| 309 |
{ |
| 310 |
g_atomic_int_inc (&test_coroutine_destroy_state_freed); |
| 311 |
g_free (user_data); |
| 312 |
} |
| 313 |
|
| 314 |
static void |
| 315 |
test_coroutine_g_weak_ref_clear (GWeakRef *weak_ref) |
| 316 |
{ |
| 317 |
GObject *object = NULL; |
| 318 |
|
| 319 |
g_assert (weak_ref != NULL); |
| 320 |
|
| 321 |
object = g_weak_ref_get (weak_ref); |
| 322 |
g_assert_nonnull (object); |
| 323 |
g_clear_object (&object); |
| 324 |
|
| 325 |
g_weak_ref_clear (weak_ref); |
| 326 |
g_atomic_int_inc (&test_coroutine_value_with_clear_freed); |
| 327 |
} |
| 328 |
|
| 329 |
static DexFuture * |
| 330 |
test_coroutine_destroy_func (DexCoroutineContext *context, |
| 331 |
gpointer user_data) |
| 332 |
{ |
| 333 |
TestCoroutineDestroyState *state = user_data; |
| 334 |
|
| 335 |
DEX_COROUTINE_BEGIN (context); |
| 336 |
|
| 337 |
return dex_future_new_for_boolean (state->value); |
| 338 |
|
| 339 |
DEX_COROUTINE_END; |
| 340 |
} |
| 341 |
|
| 342 |
static void |
| 343 |
test_coroutine_destroy_state (void) |
| 344 |
{ |
| 345 |
TestCoroutineDestroyState *state; |
| 346 |
DexFuture *future; |
| 347 |
GError *error = NULL; |
| 348 |
gboolean value; |
| 349 |
|
| 350 |
g_atomic_int_set (&test_coroutine_destroy_state_freed, 0); |
| 351 |
|
| 352 |
state = test_coroutine_destroy_state_new (); |
| 353 |
state->value = TRUE; |
| 354 |
|
| 355 |
future = dex_scheduler_spawn_coroutine (NULL, |
| 356 |
test_coroutine_destroy_func, |
| 357 |
state, |
| 358 |
(GDestroyNotify) test_coroutine_destroy_state_destroy_cb); |
| 359 |
|
| 360 |
value = dex_await_boolean (dex_ref (future), &error); |
| 361 |
g_assert_no_error (error); |
| 362 |
g_assert_true (value); |
| 363 |
g_assert_cmpint (g_atomic_int_get (&test_coroutine_destroy_state_freed), ==, 1); |
| 364 |
|
| 365 |
dex_clear (&future); |
| 366 |
} |
| 367 |
|
| 368 |
static void |
| 369 |
test_coroutine_value_with_clear (void) |
| 370 |
{ |
| 371 |
TestCoroutineValueWithClearState *state; |
| 372 |
GObject *object = NULL; |
| 373 |
|
| 374 |
g_atomic_int_set (&test_coroutine_value_with_clear_freed, 0); |
| 375 |
|
| 376 |
state = test_coroutine_value_with_clear_state_new (); |
| 377 |
object = g_object_new (G_TYPE_OBJECT, NULL); |
| 378 |
g_weak_ref_init (&state->object_ref, object); |
| 379 |
|
| 380 |
test_coroutine_value_with_clear_state_free (state); |
| 381 |
g_assert_cmpint (g_atomic_int_get (&test_coroutine_value_with_clear_freed), ==, 1); |
| 382 |
|
| 383 |
g_clear_object (&object); |
| 384 |
} |
| 385 |
|
| 386 |
typedef struct _TestCoroutineCancelRaceState |
| 387 |
{ |
| 388 |
DexFuture *future; |
| 389 |
DexPromise *gate; |
| 390 |
gint entered; |
| 391 |
} TestCoroutineCancelRaceState; |
| 392 |
|
| 393 |
static DexFuture * |
| 394 |
test_coroutine_cancel_race_func (DexCoroutineContext *context, |
| 395 |
gpointer user_data) |
| 396 |
{ |
| 397 |
TestCoroutineCancelRaceState *state = user_data; |
| 398 |
GError *error = NULL; |
| 399 |
gboolean value = FALSE; |
| 400 |
|
| 401 |
DEX_COROUTINE_BEGIN (context); |
| 402 |
|
| 403 |
g_atomic_int_set (&state->entered, 1); |
| 404 |
DEX_COROUTINE_SUSPEND_BOOLEAN (&value, &error, dex_ref (DEX_FUTURE (state->gate))); |
| 405 |
if (error != NULL) |
| 406 |
return dex_future_new_for_error (g_steal_pointer (&error)); |
| 407 |
|
| 408 |
return dex_future_new_for_boolean (value); |
| 409 |
|
| 410 |
DEX_COROUTINE_END; |
| 411 |
} |
| 412 |
|
| 413 |
static void |
| 414 |
test_coroutine_cancel_race (void) |
| 415 |
{ |
| 416 |
GError *error = NULL; |
| 417 |
|
| 418 |
for (guint i = 0; i < 128; i++) |
| 419 |
{ |
| 420 |
TestCoroutineCancelRaceState state = {0}; |
| 421 |
DexFuture *future; |
| 422 |
DexTaskGroup *group; |
| 423 |
|
| 424 |
state.gate = dex_promise_new (); |
| 425 |
state.future = future = dex_scheduler_spawn_coroutine (NULL, |
| 426 |
test_coroutine_cancel_race_func, |
| 427 |
&state, |
| 428 |
NULL); |
| 429 |
group = dex_task_group_new (DEX_TASK_GROUP_FLAGS_NONE); |
| 430 |
g_assert_true (dex_task_group_add (group, dex_ref (future))); |
| 431 |
|
| 432 |
while (g_atomic_int_get (&state.entered) == 0) |
| 433 |
g_main_context_iteration (NULL, TRUE); |
| 434 |
|
| 435 |
dex_task_group_cancel (group); |
| 436 |
dex_promise_resolve_boolean (state.gate, TRUE); |
| 437 |
|
| 438 |
g_assert_false (dex_await_boolean (dex_ref (future), &error)); |
| 439 |
g_assert_error (error, DEX_ERROR, DEX_ERROR_FIBER_CANCELLED); |
| 440 |
g_clear_error (&error); |
| 441 |
|
| 442 |
dex_clear (&group); |
| 443 |
dex_clear (&state.gate); |
| 444 |
dex_clear (&future); |
| 445 |
state.entered = 0; |
| 446 |
} |
| 447 |
} |
| 448 |
|
| 449 |
static void |
| 450 |
test_coroutine_suspend_resume (void) |
| 451 |
{ |
| 452 |
TestCoroutineSuspendState *state; |
| 453 |
DexFuture *future; |
| 454 |
GError *error = NULL; |
| 455 |
gboolean value; |
| 456 |
|
| 457 |
state = test_coroutine_suspend_state_new (); |
| 458 |
state->gate = dex_promise_new (); |
| 459 |
state->saw_result = g_new0 (gboolean, 1); |
| 460 |
state->run_count = g_new0 (guint, 1); |
| 461 |
|
| 462 |
future = dex_scheduler_spawn_coroutine (NULL, |
| 463 |
test_coroutine_suspend_func, |
| 464 |
state, |
| 465 |
NULL); |
| 466 |
|
| 467 |
while (*state->run_count == 0) |
| 468 |
g_main_context_iteration (NULL, TRUE); |
| 469 |
g_assert_cmpuint (*state->run_count, ==, 1); |
| 470 |
|
| 471 |
dex_promise_resolve_boolean (state->gate, TRUE); |
| 472 |
value = dex_await_boolean (dex_ref (future), &error); |
| 473 |
g_assert_no_error (error); |
| 474 |
g_assert_true (value); |
| 475 |
g_assert_cmpuint (*state->run_count, ==, 2); |
| 476 |
g_assert_true (*state->saw_result); |
| 477 |
|
| 478 |
test_coroutine_suspend_state_free (state); |
| 479 |
dex_clear (&future); |
| 480 |
} |
| 481 |
|
| 482 |
static void |
| 483 |
test_coroutine_suspend_void (void) |
| 484 |
{ |
| 485 |
TestCoroutineSuspendVoidState *state; |
| 486 |
DexFuture *future; |
| 487 |
gboolean value; |
| 488 |
|
| 489 |
state = test_coroutine_suspend_void_state_new (); |
| 490 |
state->gate = dex_promise_new (); |
| 491 |
state->run_count = g_new0 (guint, 1); |
| 492 |
|
| 493 |
future = dex_scheduler_spawn_coroutine (NULL, |
| 494 |
test_coroutine_suspend_void_func, |
| 495 |
state, |
| 496 |
NULL); |
| 497 |
|
| 498 |
while (*state->run_count == 0) |
| 499 |
g_main_context_iteration (NULL, TRUE); |
| 500 |
g_assert_cmpuint (*state->run_count, ==, 1); |
| 501 |
|
| 502 |
dex_promise_resolve_boolean (state->gate, TRUE); |
| 503 |
value = dex_await_boolean (dex_ref (future), NULL); |
| 504 |
g_assert_true (value); |
| 505 |
g_assert_cmpuint (*state->run_count, ==, 2); |
| 506 |
|
| 507 |
test_coroutine_suspend_void_state_free (state); |
| 508 |
dex_clear (&future); |
| 509 |
} |
| 510 |
|
| 511 |
static void |
| 512 |
test_coroutine_suspend_gauntlet (void) |
| 513 |
{ |
| 514 |
TestCoroutineSuspendGauntletState *state; |
| 515 |
DexFuture *future; |
| 516 |
|
| 517 |
state = test_coroutine_suspend_gauntlet_state_new (); |
| 518 |
state->run_count = g_new0 (guint, 1); |
| 519 |
state->limit = 10000; |
| 520 |
|
| 521 |
future = dex_scheduler_spawn_coroutine (NULL, |
| 522 |
test_coroutine_suspend_gauntlet_func, |
| 523 |
state, |
| 524 |
NULL); |
| 525 |
|
| 526 |
g_assert_true (dex_await_boolean (dex_ref (future), NULL)); |
| 527 |
g_assert_cmpuint (*state->run_count, ==, state->limit); |
| 528 |
|
| 529 |
test_coroutine_suspend_gauntlet_state_free (state); |
| 530 |
dex_clear (&future); |
| 531 |
} |
| 532 |
|
| 533 |
static void |
| 534 |
test_coroutine_suspend_jump (void) |
| 535 |
{ |
| 536 |
TestCoroutineSuspendGauntletState *state; |
| 537 |
DexFuture *future; |
| 538 |
|
| 539 |
state = test_coroutine_suspend_gauntlet_state_new (); |
| 540 |
state->run_count = g_new0 (guint, 1); |
| 541 |
state->limit = 10; |
| 542 |
|
| 543 |
future = dex_scheduler_spawn_coroutine (NULL, |
| 544 |
test_coroutine_suspend_jump_func, |
| 545 |
state, |
| 546 |
NULL); |
| 547 |
|
| 548 |
g_assert_true (dex_await_boolean (dex_ref (future), NULL)); |
| 549 |
g_assert_cmpuint (*state->run_count, ==, state->limit); |
| 550 |
|
| 551 |
test_coroutine_suspend_gauntlet_state_free (state); |
| 552 |
dex_clear (&future); |
| 553 |
} |
| 554 |
|
| 555 |
static void |
| 556 |
test_coroutine_returns_int (void) |
| 557 |
{ |
| 558 |
GError *error = NULL; |
| 559 |
int value; |
| 560 |
|
| 561 |
value = dex_await_int (dex_scheduler_spawn_coroutine (NULL, |
| 562 |
test_coroutine_return_int_func, |
| 563 |
NULL, |
| 564 |
NULL), |
| 565 |
&error); |
| 566 |
g_assert_no_error (error); |
| 567 |
g_assert_cmpint (value, ==, 123); |
| 568 |
} |
| 569 |
|
| 570 |
static void |
| 571 |
test_coroutine_returns_uint (void) |
| 572 |
{ |
| 573 |
GError *error = NULL; |
| 574 |
guint value; |
| 575 |
|
| 576 |
value = dex_await_uint (dex_scheduler_spawn_coroutine (NULL, test_coroutine_return_uint_func, NULL, NULL), &error); |
| 577 |
g_assert_no_error (error); |
| 578 |
g_assert_cmpuint (value, ==, 123u); |
| 579 |
} |
| 580 |
|
| 581 |
static void |
| 582 |
test_coroutine_returns_boolean (void) |
| 583 |
{ |
| 584 |
GError *error = NULL; |
| 585 |
gboolean value; |
| 586 |
|
| 587 |
value = dex_await_boolean (dex_scheduler_spawn_coroutine (NULL, test_coroutine_return_boolean_func, NULL, NULL), &error); |
| 588 |
g_assert_no_error (error); |
| 589 |
g_assert_true (value); |
| 590 |
} |
| 591 |
|
| 592 |
static void |
| 593 |
test_coroutine_returns_double (void) |
| 594 |
{ |
| 595 |
GError *error = NULL; |
| 596 |
double value; |
| 597 |
|
| 598 |
value = dex_await_double (dex_scheduler_spawn_coroutine (NULL, test_coroutine_return_double_func, NULL, NULL), &error); |
| 599 |
g_assert_no_error (error); |
| 600 |
g_assert_cmpfloat_with_epsilon (value, 3.25, 0.0001); |
| 601 |
} |
| 602 |
|
| 603 |
static void |
| 604 |
test_coroutine_returns_float (void) |
| 605 |
{ |
| 606 |
GError *error = NULL; |
| 607 |
float value; |
| 608 |
|
| 609 |
value = dex_await_float (dex_scheduler_spawn_coroutine (NULL, test_coroutine_return_float_func, NULL, NULL), &error); |
| 610 |
g_assert_no_error (error); |
| 611 |
g_assert_cmpfloat_with_epsilon (value, 6.5f, 0.0001); |
| 612 |
} |
| 613 |
|
| 614 |
static void |
| 615 |
test_coroutine_returns_object (void) |
| 616 |
{ |
| 617 |
GError *error = NULL; |
| 618 |
GObject *value = NULL; |
| 619 |
|
| 620 |
value = dex_await_object (dex_scheduler_spawn_coroutine (NULL, |
| 621 |
test_coroutine_return_object_func, |
| 622 |
NULL, |
| 623 |
NULL), |
| 624 |
&error); |
| 625 |
g_assert_no_error (error); |
| 626 |
g_assert_nonnull (value); |
| 627 |
g_assert_true (G_IS_OBJECT (value)); |
| 628 |
|
| 629 |
g_clear_object (&value); |
| 630 |
} |
| 631 |
|
| 632 |
static void |
| 633 |
test_coroutine_returns_boxed (void) |
| 634 |
{ |
| 635 |
GError *error = NULL; |
| 636 |
GBytes *boxed = NULL; |
| 637 |
const char *boxed_data; |
| 638 |
gsize length = 0; |
| 639 |
|
| 640 |
boxed = dex_await_boxed (dex_scheduler_spawn_coroutine (NULL, test_coroutine_return_boxed_func, NULL, NULL), &error); |
| 641 |
g_assert_no_error (error); |
| 642 |
|
| 643 |
boxed_data = g_bytes_get_data (boxed, &length); |
| 644 |
g_assert_cmpuint (length, ==, 5); |
| 645 |
g_assert_true (memcmp (boxed_data, "boxed", 5) == 0); |
| 646 |
|
| 647 |
g_clear_pointer (&boxed, g_bytes_unref); |
| 648 |
} |
| 649 |
|
| 650 |
static void |
| 651 |
test_coroutine_returns_pointer (void) |
| 652 |
{ |
| 653 |
GError *error = NULL; |
| 654 |
gpointer expected = GSIZE_TO_POINTER (0xdeadbeef); |
| 655 |
gpointer value; |
| 656 |
|
| 657 |
value = dex_await_pointer (dex_scheduler_spawn_coroutine (NULL, test_coroutine_return_pointer_func, NULL, NULL), &error); |
| 658 |
g_assert_no_error (error); |
| 659 |
g_assert_nonnull (value); |
| 660 |
g_assert_true (value == expected); |
| 661 |
} |
| 662 |
|
| 663 |
static void |
| 664 |
test_coroutine_task_group_cancel (void) |
| 665 |
{ |
| 666 |
DexTaskGroup *group = dex_task_group_new (DEX_TASK_GROUP_FLAGS_NONE); |
| 667 |
DexFuture *coroutines[3] = { NULL, NULL, NULL }; |
| 668 |
GError *error = NULL; |
| 669 |
TestCoroutineTaskGroupCancelState *states[3] = { NULL, NULL, NULL }; |
| 670 |
|
| 671 |
for (guint i = 0; i < G_N_ELEMENTS (coroutines); i++) |
| 672 |
{ |
| 673 |
TestCoroutineTaskGroupCancelState *state; |
| 674 |
|
| 675 |
state = test_coroutine_task_group_cancel_state_new (); |
| 676 |
state->gate = dex_promise_new (); |
| 677 |
states[i] = state; |
| 678 |
|
| 679 |
coroutines[i] = dex_scheduler_spawn_coroutine (NULL, test_coroutine_task_group_cancel_func, state, NULL); |
| 680 |
|
| 681 |
g_assert_true (dex_task_group_add (group, dex_ref (coroutines[i]))); |
| 682 |
} |
| 683 |
|
| 684 |
dex_task_group_cancel (group); |
| 685 |
|
| 686 |
for (guint i = 0; i < G_N_ELEMENTS (coroutines); i++) |
| 687 |
{ |
| 688 |
g_assert_false (dex_await_boolean (dex_ref (coroutines[i]), &error)); |
| 689 |
g_assert_true (g_error_matches (error, DEX_ERROR, DEX_ERROR_FIBER_CANCELLED)); |
| 690 |
g_clear_error (&error); |
| 691 |
test_coroutine_task_group_cancel_state_free (states[i]); |
| 692 |
} |
| 693 |
|
| 694 |
dex_clear (&group); |
| 695 |
|
| 696 |
for (guint i = 0; i < G_N_ELEMENTS (coroutines); i++) |
| 697 |
dex_clear (&coroutines[i]); |
| 698 |
} |
| 699 |
|
| 700 |
static void |
| 701 |
test_coroutine_thread_pool_await (void) |
| 702 |
{ |
| 703 |
DexScheduler *thread_pool = dex_thread_pool_scheduler_get_default (); |
| 704 |
GError *error = NULL; |
| 705 |
guint value; |
| 706 |
|
| 707 |
g_assert_true (DEX_IS_THREAD_POOL_SCHEDULER (thread_pool)); |
| 708 |
value = dex_await_uint (dex_scheduler_spawn_coroutine (thread_pool, |
| 709 |
test_coroutine_thread_pool_return_uint_func, |
| 710 |
NULL, |
| 711 |
NULL), |
| 712 |
&error); |
| 713 |
g_assert_no_error (error); |
| 714 |
g_assert_cmpuint (value, ==, 7u); |
| 715 |
} |
| 716 |
|
| 717 |
int |
| 718 |
main (int argc, |
| 719 |
char *argv[]) |
| 720 |
{ |
| 721 |
dex_init (); |
| 722 |
g_test_init (&argc, &argv, NULL); |
| 723 |
|
| 724 |
_g_test_add_func ("/Dex/TestSuite/Coroutine/spawn_complete", test_coroutine_spawn_complete); |
| 725 |
_g_test_add_func ("/Dex/TestSuite/Coroutine/cancel_race_awaited", test_coroutine_cancel_race); |
| 726 |
_g_test_add_func ("/Dex/TestSuite/Coroutine/suspend_resume", test_coroutine_suspend_resume); |
| 727 |
_g_test_add_func ("/Dex/TestSuite/Coroutine/suspend_void", test_coroutine_suspend_void); |
| 728 |
_g_test_add_func ("/Dex/TestSuite/Coroutine/suspend_gauntlet", test_coroutine_suspend_gauntlet); |
| 729 |
_g_test_add_func ("/Dex/TestSuite/Coroutine/suspend_jump", test_coroutine_suspend_jump); |
| 730 |
_g_test_add_func ("/Dex/TestSuite/Coroutine/returns-int", test_coroutine_returns_int); |
| 731 |
_g_test_add_func ("/Dex/TestSuite/Coroutine/returns-uint", test_coroutine_returns_uint); |
| 732 |
_g_test_add_func ("/Dex/TestSuite/Coroutine/returns-boolean", test_coroutine_returns_boolean); |
| 733 |
_g_test_add_func ("/Dex/TestSuite/Coroutine/returns-double", test_coroutine_returns_double); |
| 734 |
_g_test_add_func ("/Dex/TestSuite/Coroutine/returns-float", test_coroutine_returns_float); |
| 735 |
_g_test_add_func ("/Dex/TestSuite/Coroutine/returns-object", test_coroutine_returns_object); |
| 736 |
_g_test_add_func ("/Dex/TestSuite/Coroutine/returns-boxed", test_coroutine_returns_boxed); |
| 737 |
_g_test_add_func ("/Dex/TestSuite/Coroutine/returns-pointer", test_coroutine_returns_pointer); |
| 738 |
_g_test_add_func ("/Dex/TestSuite/Coroutine/destroy-state", test_coroutine_destroy_state); |
| 739 |
_g_test_add_func ("/Dex/TestSuite/Coroutine/value-with-clear", |
| 740 |
test_coroutine_value_with_clear); |
| 741 |
_g_test_add_func ("/Dex/TestSuite/Coroutine/task-group-cancel", test_coroutine_task_group_cancel); |
| 742 |
_g_test_add_func ("/Dex/TestSuite/Coroutine/thread-pool-await", test_coroutine_thread_pool_await); |
| 743 |
|
| 744 |
return g_test_run (); |
| 745 |
} |