| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#include <libdex.h> |
| 22 |
|
| 23 |
typedef DexFuture *(*TestFiberFunc) (gpointer user_data); |
| 24 |
|
| 25 |
typedef struct |
| 26 |
{ |
| 27 |
GMainLoop *main_loop; |
| 28 |
DexFuture *future; |
| 29 |
} TestRun; |
| 30 |
|
| 31 |
typedef struct |
| 32 |
{ |
| 33 |
DexLimiter *limiter; |
| 34 |
int active; |
| 35 |
int max_active; |
| 36 |
int completed; |
| 37 |
} RunState; |
| 38 |
|
| 39 |
typedef struct |
| 40 |
{ |
| 41 |
RunState *state; |
| 42 |
guint value; |
| 43 |
} ItemState; |
| 44 |
|
| 45 |
static DexFuture * |
| 46 |
test_quit_cb (DexFuture *future, |
| 47 |
gpointer user_data) |
| 48 |
{ |
| 49 |
TestRun *run = user_data; |
| 50 |
|
| 51 |
g_main_loop_quit (run->main_loop); |
| 52 |
|
| 53 |
return NULL; |
| 54 |
} |
| 55 |
|
| 56 |
static void |
| 57 |
run_test_fiber (TestFiberFunc func, |
| 58 |
gpointer user_data) |
| 59 |
{ |
| 60 |
TestRun run = {0}; |
| 61 |
|
| 62 |
run.main_loop = g_main_loop_new (NULL, FALSE); |
| 63 |
run.future = dex_scheduler_spawn (NULL, 0, func, user_data, NULL); |
| 64 |
run.future = dex_future_finally (run.future, test_quit_cb, &run, NULL); |
| 65 |
|
| 66 |
g_main_loop_run (run.main_loop); |
| 67 |
|
| 68 |
while (dex_future_is_pending (run.future)) |
| 69 |
g_main_context_iteration (NULL, TRUE); |
| 70 |
|
| 71 |
g_assert_true (dex_future_is_resolved (run.future)); |
| 72 |
|
| 73 |
dex_clear (&run.future); |
| 74 |
g_main_loop_unref (run.main_loop); |
| 75 |
} |
| 76 |
|
| 77 |
static DexFuture * |
| 78 |
test_limiter_basic_fiber (gpointer user_data) |
| 79 |
{ |
| 80 |
DexLimiter * limiter = dex_limiter_new (1); |
| 81 |
GError * error = NULL; |
| 82 |
DexFuture *second; |
| 83 |
|
| 84 |
g_assert_true (DEX_IS_LIMITER (limiter)); |
| 85 |
g_assert_cmpuint (dex_limiter_get_max_concurrency (limiter), ==, 1); |
| 86 |
|
| 87 |
g_assert_true (dex_await (dex_limiter_acquire (limiter), &error)); |
| 88 |
g_assert_no_error (error); |
| 89 |
|
| 90 |
second = dex_limiter_acquire (limiter); |
| 91 |
g_assert_true (dex_future_is_pending (second)); |
| 92 |
|
| 93 |
dex_limiter_release (limiter); |
| 94 |
g_assert_true (dex_await (second, &error)); |
| 95 |
g_assert_no_error (error); |
| 96 |
|
| 97 |
dex_limiter_release (limiter); |
| 98 |
|
| 99 |
return dex_future_new_true (); |
| 100 |
} |
| 101 |
|
| 102 |
static void |
| 103 |
test_limiter_basic (void) |
| 104 |
{ |
| 105 |
run_test_fiber (test_limiter_basic_fiber, NULL); |
| 106 |
} |
| 107 |
|
| 108 |
static DexFuture * |
| 109 |
test_limiter_run_item (gpointer user_data) |
| 110 |
{ |
| 111 |
ItemState *item = user_data; |
| 112 |
RunState *state = item->state; |
| 113 |
int active; |
| 114 |
int max_active; |
| 115 |
|
| 116 |
active = g_atomic_int_add (&state->active, 1) + 1; |
| 117 |
|
| 118 |
while (active > (max_active = g_atomic_int_get (&state->max_active))) |
| 119 |
{ |
| 120 |
if (g_atomic_int_compare_and_exchange (&state->max_active, max_active, active)) |
| 121 |
break; |
| 122 |
} |
| 123 |
|
| 124 |
g_assert_cmpuint (active, <=, dex_limiter_get_max_concurrency (state->limiter)); |
| 125 |
|
| 126 |
dex_await (dex_timeout_new_msec (5), NULL); |
| 127 |
|
| 128 |
g_atomic_int_dec_and_test (&state->active); |
| 129 |
g_atomic_int_inc (&state->completed); |
| 130 |
|
| 131 |
return dex_future_new_for_uint (item->value); |
| 132 |
} |
| 133 |
|
| 134 |
static DexFuture * |
| 135 |
test_limiter_run_fiber (gpointer user_data) |
| 136 |
{ |
| 137 |
DexLimiter * limiter = dex_limiter_new (3); |
| 138 |
GPtrArray * futures = NULL; |
| 139 |
RunState state = {0}; |
| 140 |
|
| 141 |
state.limiter = limiter; |
| 142 |
futures = g_ptr_array_new_with_free_func (dex_unref); |
| 143 |
|
| 144 |
for (guint i = 0; i < 24; i++) |
| 145 |
{ |
| 146 |
ItemState *item = g_new0 (ItemState, 1); |
| 147 |
|
| 148 |
item->state = &state; |
| 149 |
item->value = i; |
| 150 |
g_ptr_array_add (futures, |
| 151 |
dex_limiter_run (limiter, |
| 152 |
NULL, |
| 153 |
0, |
| 154 |
test_limiter_run_item, |
| 155 |
item, |
| 156 |
g_free)); |
| 157 |
} |
| 158 |
|
| 159 |
dex_await (dex_future_allv ((DexFuture **)futures->pdata, futures->len), NULL); |
| 160 |
|
| 161 |
g_assert_cmpuint (g_atomic_int_get (&state.completed), ==, futures->len); |
| 162 |
g_assert_cmpuint (g_atomic_int_get (&state.max_active), ==, 3); |
| 163 |
g_assert_cmpuint (g_atomic_int_get (&state.active), ==, 0); |
| 164 |
|
| 165 |
return dex_future_new_true (); |
| 166 |
} |
| 167 |
|
| 168 |
static void |
| 169 |
test_limiter_run (void) |
| 170 |
{ |
| 171 |
run_test_fiber (test_limiter_run_fiber, NULL); |
| 172 |
} |
| 173 |
|
| 174 |
static DexFuture * |
| 175 |
test_limiter_run_on_pool_item (gpointer user_data) |
| 176 |
{ |
| 177 |
ItemState *item = user_data; |
| 178 |
RunState *state = item->state; |
| 179 |
int active; |
| 180 |
int max_active; |
| 181 |
|
| 182 |
active = g_atomic_int_add (&state->active, 1) + 1; |
| 183 |
|
| 184 |
while (active > (max_active = g_atomic_int_get (&state->max_active))) |
| 185 |
{ |
| 186 |
if (g_atomic_int_compare_and_exchange (&state->max_active, max_active, active)) |
| 187 |
break; |
| 188 |
} |
| 189 |
|
| 190 |
g_assert_cmpuint (active, <=, dex_limiter_get_max_concurrency (state->limiter)); |
| 191 |
|
| 192 |
g_usleep (5000); |
| 193 |
|
| 194 |
g_atomic_int_dec_and_test (&state->active); |
| 195 |
g_atomic_int_inc (&state->completed); |
| 196 |
|
| 197 |
return dex_future_new_for_uint (item->value); |
| 198 |
} |
| 199 |
|
| 200 |
static DexFuture * |
| 201 |
test_limiter_run_on_pool_fiber (gpointer user_data) |
| 202 |
{ |
| 203 |
DexLimiter * limiter = dex_limiter_new (3); |
| 204 |
DexThreadPool * pool = dex_thread_pool_new (8); |
| 205 |
GPtrArray * futures = NULL; |
| 206 |
GError * error = NULL; |
| 207 |
RunState state = {0}; |
| 208 |
|
| 209 |
state.limiter = limiter; |
| 210 |
futures = g_ptr_array_new_with_free_func (dex_unref); |
| 211 |
|
| 212 |
for (guint i = 0; i < 24; i++) |
| 213 |
{ |
| 214 |
ItemState *item = g_new0 (ItemState, 1); |
| 215 |
|
| 216 |
item->state = &state; |
| 217 |
item->value = i; |
| 218 |
g_ptr_array_add (futures, |
| 219 |
dex_limiter_run_on_pool (limiter, |
| 220 |
pool, |
| 221 |
test_limiter_run_on_pool_item, |
| 222 |
item, |
| 223 |
g_free)); |
| 224 |
} |
| 225 |
|
| 226 |
g_assert_true (dex_await (dex_future_allv ((DexFuture **)futures->pdata, futures->len), |
| 227 |
&error)); |
| 228 |
g_assert_no_error (error); |
| 229 |
|
| 230 |
g_assert_cmpuint (g_atomic_int_get (&state.completed), ==, futures->len); |
| 231 |
g_assert_cmpuint (g_atomic_int_get (&state.max_active), ==, 3); |
| 232 |
g_assert_cmpuint (g_atomic_int_get (&state.active), ==, 0); |
| 233 |
|
| 234 |
g_assert_true (dex_await (dex_thread_pool_close (pool, DEX_THREAD_POOL_SHUTDOWN_DRAIN), |
| 235 |
&error)); |
| 236 |
g_assert_no_error (error); |
| 237 |
|
| 238 |
return dex_future_new_true (); |
| 239 |
} |
| 240 |
|
| 241 |
static void |
| 242 |
test_limiter_run_on_pool (void) |
| 243 |
{ |
| 244 |
run_test_fiber (test_limiter_run_on_pool_fiber, NULL); |
| 245 |
} |
| 246 |
|
| 247 |
static gboolean |
| 248 |
test_limiter_sleep_cb (gpointer user_data) |
| 249 |
{ |
| 250 |
dex_promise_resolve_boolean (DEX_PROMISE (user_data), TRUE); |
| 251 |
return G_SOURCE_REMOVE; |
| 252 |
} |
| 253 |
|
| 254 |
static DexFuture * |
| 255 |
test_limiter_run_sleep (int msec) |
| 256 |
{ |
| 257 |
DexPromise *promise; |
| 258 |
|
| 259 |
promise = dex_promise_new (); |
| 260 |
g_timeout_add_full (G_PRIORITY_DEFAULT, |
| 261 |
msec, |
| 262 |
test_limiter_sleep_cb, |
| 263 |
dex_ref (promise), |
| 264 |
(GDestroyNotify) dex_unref); |
| 265 |
|
| 266 |
return DEX_FUTURE (promise); |
| 267 |
} |
| 268 |
|
| 269 |
static DexFuture * |
| 270 |
test_limiter_run_coroutine_item (DexCoroutineContext *context, |
| 271 |
gpointer user_data) |
| 272 |
{ |
| 273 |
ItemState *item = user_data; |
| 274 |
RunState *state = item->state; |
| 275 |
int active; |
| 276 |
int max_active; |
| 277 |
GError *error = NULL; |
| 278 |
gboolean timeout; |
| 279 |
|
| 280 |
DEX_COROUTINE_BEGIN (context); |
| 281 |
|
| 282 |
active = g_atomic_int_add (&state->active, 1) + 1; |
| 283 |
|
| 284 |
while (active > (max_active = g_atomic_int_get (&state->max_active))) |
| 285 |
{ |
| 286 |
if (g_atomic_int_compare_and_exchange (&state->max_active, max_active, active)) |
| 287 |
break; |
| 288 |
} |
| 289 |
|
| 290 |
g_assert_cmpuint (active, <=, dex_limiter_get_max_concurrency (state->limiter)); |
| 291 |
|
| 292 |
DEX_COROUTINE_SUSPEND_BOOLEAN (&timeout, &error, test_limiter_run_sleep (5)); |
| 293 |
if (error != NULL) |
| 294 |
{ |
| 295 |
g_atomic_int_dec_and_test (&state->active); |
| 296 |
|
| 297 |
return dex_future_new_for_error (g_steal_pointer (&error)); |
| 298 |
} |
| 299 |
|
| 300 |
g_atomic_int_dec_and_test (&state->active); |
| 301 |
g_atomic_int_inc (&state->completed); |
| 302 |
|
| 303 |
return dex_future_new_for_uint (item->value); |
| 304 |
|
| 305 |
DEX_COROUTINE_END; |
| 306 |
} |
| 307 |
|
| 308 |
static DexFuture * |
| 309 |
test_limiter_run_coroutine_fiber (gpointer user_data) |
| 310 |
{ |
| 311 |
DexLimiter * limiter = dex_limiter_new (3); |
| 312 |
GPtrArray *futures = NULL; |
| 313 |
RunState state = {0}; |
| 314 |
|
| 315 |
state.limiter = limiter; |
| 316 |
futures = g_ptr_array_new_with_free_func (dex_unref); |
| 317 |
|
| 318 |
for (guint i = 0; i < 24; i++) |
| 319 |
{ |
| 320 |
ItemState *item = g_new0 (ItemState, 1); |
| 321 |
|
| 322 |
item->state = &state; |
| 323 |
item->value = i; |
| 324 |
g_ptr_array_add (futures, |
| 325 |
dex_limiter_run_coroutine (limiter, |
| 326 |
NULL, |
| 327 |
test_limiter_run_coroutine_item, |
| 328 |
item, |
| 329 |
g_free)); |
| 330 |
} |
| 331 |
|
| 332 |
dex_await (dex_future_allv ((DexFuture **) futures->pdata, futures->len), NULL); |
| 333 |
|
| 334 |
g_assert_cmpuint (g_atomic_int_get (&state.completed), ==, futures->len); |
| 335 |
g_assert_cmpuint (g_atomic_int_get (&state.max_active), ==, 3); |
| 336 |
g_assert_cmpuint (g_atomic_int_get (&state.active), ==, 0); |
| 337 |
|
| 338 |
return dex_future_new_true (); |
| 339 |
} |
| 340 |
|
| 341 |
static void |
| 342 |
test_limiter_run_coroutine (void) |
| 343 |
{ |
| 344 |
run_test_fiber (test_limiter_run_coroutine_fiber, NULL); |
| 345 |
} |
| 346 |
|
| 347 |
static DexFuture * |
| 348 |
test_limiter_run_coroutine_error_item (DexCoroutineContext *context, |
| 349 |
gpointer user_data) |
| 350 |
{ |
| 351 |
return dex_future_new_reject (G_IO_ERROR, G_IO_ERROR_FAILED, "Failed"); |
| 352 |
} |
| 353 |
|
| 354 |
static DexFuture * |
| 355 |
test_limiter_run_coroutine_error_fiber (gpointer user_data) |
| 356 |
{ |
| 357 |
DexLimiter *limiter = dex_limiter_new (1); |
| 358 |
GError *error = NULL; |
| 359 |
DexFuture *future; |
| 360 |
|
| 361 |
future = dex_limiter_run_coroutine (limiter, |
| 362 |
NULL, |
| 363 |
test_limiter_run_coroutine_error_item, |
| 364 |
NULL, |
| 365 |
NULL); |
| 366 |
|
| 367 |
g_assert_false (dex_await (future, &error)); |
| 368 |
g_assert_error (error, G_IO_ERROR, G_IO_ERROR_FAILED); |
| 369 |
g_clear_error (&error); |
| 370 |
|
| 371 |
g_assert_true (dex_await (dex_limiter_acquire (limiter), &error)); |
| 372 |
g_assert_no_error (error); |
| 373 |
dex_limiter_release (limiter); |
| 374 |
|
| 375 |
return dex_future_new_true (); |
| 376 |
} |
| 377 |
|
| 378 |
static void |
| 379 |
test_limiter_run_coroutine_error (void) |
| 380 |
{ |
| 381 |
run_test_fiber (test_limiter_run_coroutine_error_fiber, NULL); |
| 382 |
} |
| 383 |
|
| 384 |
static DexFuture * |
| 385 |
test_limiter_error_item (gpointer user_data) |
| 386 |
{ |
| 387 |
return dex_future_new_reject (G_IO_ERROR, G_IO_ERROR_FAILED, "Failed"); |
| 388 |
} |
| 389 |
|
| 390 |
static DexFuture * |
| 391 |
test_limiter_run_error_fiber (gpointer user_data) |
| 392 |
{ |
| 393 |
DexLimiter * limiter = dex_limiter_new (1); |
| 394 |
GError * error = NULL; |
| 395 |
DexFuture *future; |
| 396 |
|
| 397 |
future = dex_limiter_run (limiter, NULL, 0, test_limiter_error_item, NULL, NULL); |
| 398 |
|
| 399 |
g_assert_false (dex_await (future, &error)); |
| 400 |
g_assert_error (error, G_IO_ERROR, G_IO_ERROR_FAILED); |
| 401 |
g_clear_error (&error); |
| 402 |
|
| 403 |
g_assert_true (dex_await (dex_limiter_acquire (limiter), &error)); |
| 404 |
g_assert_no_error (error); |
| 405 |
dex_limiter_release (limiter); |
| 406 |
|
| 407 |
return dex_future_new_true (); |
| 408 |
} |
| 409 |
|
| 410 |
static void |
| 411 |
test_limiter_run_error (void) |
| 412 |
{ |
| 413 |
run_test_fiber (test_limiter_run_error_fiber, NULL); |
| 414 |
} |
| 415 |
|
| 416 |
static DexFuture * |
| 417 |
test_limiter_run_on_pool_error_fiber (gpointer user_data) |
| 418 |
{ |
| 419 |
DexLimiter * limiter = dex_limiter_new (1); |
| 420 |
DexThreadPool * pool = dex_thread_pool_new (1); |
| 421 |
GError * error = NULL; |
| 422 |
DexFuture *future; |
| 423 |
|
| 424 |
future = dex_limiter_run_on_pool (limiter, pool, test_limiter_error_item, NULL, NULL); |
| 425 |
|
| 426 |
g_assert_false (dex_await (future, &error)); |
| 427 |
g_assert_error (error, G_IO_ERROR, G_IO_ERROR_FAILED); |
| 428 |
g_clear_error (&error); |
| 429 |
|
| 430 |
g_assert_true (dex_await (dex_limiter_acquire (limiter), &error)); |
| 431 |
g_assert_no_error (error); |
| 432 |
dex_limiter_release (limiter); |
| 433 |
|
| 434 |
g_assert_true (dex_await (dex_thread_pool_close (pool, DEX_THREAD_POOL_SHUTDOWN_DRAIN), |
| 435 |
&error)); |
| 436 |
g_assert_no_error (error); |
| 437 |
|
| 438 |
return dex_future_new_true (); |
| 439 |
} |
| 440 |
|
| 441 |
static void |
| 442 |
test_limiter_run_on_pool_error (void) |
| 443 |
{ |
| 444 |
run_test_fiber (test_limiter_run_on_pool_error_fiber, NULL); |
| 445 |
} |
| 446 |
|
| 447 |
static DexFuture * |
| 448 |
test_limiter_not_reached (DexFuture *future, |
| 449 |
gpointer user_data) |
| 450 |
{ |
| 451 |
g_assert_not_reached (); |
| 452 |
return dex_future_new_true (); |
| 453 |
} |
| 454 |
|
| 455 |
static DexFuture * |
| 456 |
test_limiter_discard_waiting_fiber (gpointer user_data) |
| 457 |
{ |
| 458 |
DexLimiter * limiter = dex_limiter_new (1); |
| 459 |
GError * error = NULL; |
| 460 |
DexFuture *block; |
| 461 |
DexFuture *second; |
| 462 |
DexFuture *third; |
| 463 |
|
| 464 |
g_assert_true (dex_await (dex_limiter_acquire (limiter), &error)); |
| 465 |
g_assert_no_error (error); |
| 466 |
|
| 467 |
second = dex_limiter_acquire (limiter); |
| 468 |
block = dex_future_then (second, test_limiter_not_reached, NULL, NULL); |
| 469 |
dex_clear (&block); |
| 470 |
|
| 471 |
dex_limiter_release (limiter); |
| 472 |
dex_await (dex_timeout_new_msec (10), NULL); |
| 473 |
|
| 474 |
third = dex_limiter_acquire (limiter); |
| 475 |
g_assert_true (dex_await (third, &error)); |
| 476 |
g_assert_no_error (error); |
| 477 |
dex_limiter_release (limiter); |
| 478 |
|
| 479 |
return dex_future_new_true (); |
| 480 |
} |
| 481 |
|
| 482 |
static void |
| 483 |
test_limiter_discard_waiting (void) |
| 484 |
{ |
| 485 |
run_test_fiber (test_limiter_discard_waiting_fiber, NULL); |
| 486 |
} |
| 487 |
|
| 488 |
static DexFuture * |
| 489 |
test_limiter_timeout_waiting_acquire_fiber (gpointer user_data) |
| 490 |
{ |
| 491 |
DexLimiter * limiter = dex_limiter_new (1); |
| 492 |
GError * error = NULL; |
| 493 |
DexFuture *second; |
| 494 |
DexFuture *third; |
| 495 |
|
| 496 |
g_assert_true (dex_await (dex_limiter_acquire (limiter), &error)); |
| 497 |
g_assert_no_error (error); |
| 498 |
|
| 499 |
second = dex_future_with_timeout_msec (dex_limiter_acquire (limiter), 1); |
| 500 |
g_assert_false (dex_await (second, &error)); |
| 501 |
g_assert_error (error, DEX_ERROR, DEX_ERROR_TIMED_OUT); |
| 502 |
g_clear_error (&error); |
| 503 |
|
| 504 |
third = dex_limiter_acquire (limiter); |
| 505 |
g_assert_true (dex_future_is_pending (third)); |
| 506 |
|
| 507 |
dex_limiter_release (limiter); |
| 508 |
|
| 509 |
g_assert_true (dex_await (dex_future_with_timeout_msec (third, 100), &error)); |
| 510 |
g_assert_no_error (error); |
| 511 |
|
| 512 |
dex_limiter_release (limiter); |
| 513 |
|
| 514 |
return dex_future_new_true (); |
| 515 |
} |
| 516 |
|
| 517 |
static void |
| 518 |
test_limiter_timeout_waiting_acquire (void) |
| 519 |
{ |
| 520 |
run_test_fiber (test_limiter_timeout_waiting_acquire_fiber, NULL); |
| 521 |
} |
| 522 |
|
| 523 |
static DexFuture * |
| 524 |
test_limiter_discard_run_item (gpointer user_data) |
| 525 |
{ |
| 526 |
int *completed = user_data; |
| 527 |
|
| 528 |
dex_await (dex_timeout_new_msec (10), NULL); |
| 529 |
g_atomic_int_inc (completed); |
| 530 |
|
| 531 |
return dex_future_new_true (); |
| 532 |
} |
| 533 |
|
| 534 |
static DexFuture * |
| 535 |
test_limiter_discard_run_fiber (gpointer user_data) |
| 536 |
{ |
| 537 |
DexLimiter * limiter = dex_limiter_new (1); |
| 538 |
GError * error = NULL; |
| 539 |
DexFuture *block; |
| 540 |
DexFuture *future; |
| 541 |
int completed = 0; |
| 542 |
|
| 543 |
future = dex_limiter_run (limiter, |
| 544 |
NULL, |
| 545 |
0, |
| 546 |
test_limiter_discard_run_item, |
| 547 |
&completed, |
| 548 |
NULL); |
| 549 |
block = dex_future_then (future, test_limiter_not_reached, NULL, NULL); |
| 550 |
|
| 551 |
dex_await (dex_timeout_new_msec (1), NULL); |
| 552 |
|
| 553 |
dex_clear (&block); |
| 554 |
|
| 555 |
dex_await (dex_timeout_new_msec (20), NULL); |
| 556 |
|
| 557 |
g_assert_cmpint (g_atomic_int_get (&completed), ==, 1); |
| 558 |
g_assert_true (dex_await (dex_limiter_acquire (limiter), &error)); |
| 559 |
g_assert_no_error (error); |
| 560 |
dex_limiter_release (limiter); |
| 561 |
|
| 562 |
return dex_future_new_true (); |
| 563 |
} |
| 564 |
|
| 565 |
static void |
| 566 |
test_limiter_discard_run (void) |
| 567 |
{ |
| 568 |
run_test_fiber (test_limiter_discard_run_fiber, NULL); |
| 569 |
} |
| 570 |
|
| 571 |
typedef struct |
| 572 |
{ |
| 573 |
int started; |
| 574 |
int completed; |
| 575 |
guint sleep_usec; |
| 576 |
} PoolDiscardState; |
| 577 |
|
| 578 |
static DexFuture * |
| 579 |
test_limiter_discard_run_on_pool_item (gpointer user_data) |
| 580 |
{ |
| 581 |
PoolDiscardState *state = user_data; |
| 582 |
|
| 583 |
g_atomic_int_inc (&state->started); |
| 584 |
g_usleep (state->sleep_usec); |
| 585 |
g_atomic_int_inc (&state->completed); |
| 586 |
|
| 587 |
return dex_future_new_true (); |
| 588 |
} |
| 589 |
|
| 590 |
static DexFuture * |
| 591 |
test_limiter_discard_run_on_pool_fiber (gpointer user_data) |
| 592 |
{ |
| 593 |
DexLimiter * limiter = dex_limiter_new (1); |
| 594 |
DexThreadPool * pool = dex_thread_pool_new (1); |
| 595 |
GError * error = NULL; |
| 596 |
DexFuture *block; |
| 597 |
DexFuture *future; |
| 598 |
PoolDiscardState state = { .sleep_usec = 10000 }; |
| 599 |
|
| 600 |
future = dex_limiter_run_on_pool (limiter, |
| 601 |
pool, |
| 602 |
test_limiter_discard_run_on_pool_item, |
| 603 |
&state, |
| 604 |
NULL); |
| 605 |
block = dex_future_then (future, test_limiter_not_reached, NULL, NULL); |
| 606 |
|
| 607 |
for (guint i = 0; i < 100 && g_atomic_int_get (&state.started) == 0; i++) |
| 608 |
dex_await (dex_timeout_new_msec (1), NULL); |
| 609 |
|
| 610 |
g_assert_cmpint (g_atomic_int_get (&state.started), ==, 1); |
| 611 |
|
| 612 |
dex_clear (&block); |
| 613 |
|
| 614 |
for (guint i = 0; i < 100 && g_atomic_int_get (&state.completed) == 0; i++) |
| 615 |
dex_await (dex_timeout_new_msec (1), NULL); |
| 616 |
|
| 617 |
g_assert_cmpint (g_atomic_int_get (&state.completed), ==, 1); |
| 618 |
g_assert_true (dex_await (dex_limiter_acquire (limiter), &error)); |
| 619 |
g_assert_no_error (error); |
| 620 |
dex_limiter_release (limiter); |
| 621 |
|
| 622 |
g_assert_true (dex_await (dex_thread_pool_close (pool, DEX_THREAD_POOL_SHUTDOWN_DRAIN), |
| 623 |
&error)); |
| 624 |
g_assert_no_error (error); |
| 625 |
|
| 626 |
return dex_future_new_true (); |
| 627 |
} |
| 628 |
|
| 629 |
static void |
| 630 |
test_limiter_discard_run_on_pool (void) |
| 631 |
{ |
| 632 |
run_test_fiber (test_limiter_discard_run_on_pool_fiber, NULL); |
| 633 |
} |
| 634 |
|
| 635 |
typedef struct |
| 636 |
{ |
| 637 |
int started; |
| 638 |
int completed; |
| 639 |
guint timeout_msec; |
| 640 |
} TimeoutRunState; |
| 641 |
|
| 642 |
static DexFuture * |
| 643 |
test_limiter_timeout_run_item (gpointer user_data) |
| 644 |
{ |
| 645 |
TimeoutRunState *state = user_data; |
| 646 |
|
| 647 |
g_atomic_int_inc (&state->started); |
| 648 |
dex_await (dex_timeout_new_msec (state->timeout_msec), NULL); |
| 649 |
g_atomic_int_inc (&state->completed); |
| 650 |
|
| 651 |
return dex_future_new_true (); |
| 652 |
} |
| 653 |
|
| 654 |
static DexFuture * |
| 655 |
test_limiter_timeout_waiting_run_fiber (gpointer user_data) |
| 656 |
{ |
| 657 |
DexLimiter * limiter = dex_limiter_new (1); |
| 658 |
GError * error = NULL; |
| 659 |
DexFuture *blocked; |
| 660 |
DexFuture *next; |
| 661 |
TimeoutRunState blocked_state = {0}; |
| 662 |
TimeoutRunState next_state = {0}; |
| 663 |
|
| 664 |
g_assert_true (dex_await (dex_limiter_acquire (limiter), &error)); |
| 665 |
g_assert_no_error (error); |
| 666 |
|
| 667 |
blocked = dex_future_with_timeout_msec (dex_limiter_run (limiter, |
| 668 |
NULL, |
| 669 |
0, |
| 670 |
test_limiter_timeout_run_item, |
| 671 |
&blocked_state, |
| 672 |
NULL), |
| 673 |
1); |
| 674 |
g_assert_false (dex_await (blocked, &error)); |
| 675 |
g_assert_error (error, DEX_ERROR, DEX_ERROR_TIMED_OUT); |
| 676 |
g_clear_error (&error); |
| 677 |
|
| 678 |
next = dex_limiter_run (limiter, |
| 679 |
NULL, |
| 680 |
0, |
| 681 |
test_limiter_timeout_run_item, |
| 682 |
&next_state, |
| 683 |
NULL); |
| 684 |
g_assert_true (dex_future_is_pending (next)); |
| 685 |
|
| 686 |
dex_limiter_release (limiter); |
| 687 |
|
| 688 |
g_assert_true (dex_await (dex_future_with_timeout_msec (next, 100), &error)); |
| 689 |
g_assert_no_error (error); |
| 690 |
g_assert_cmpint (g_atomic_int_get (&blocked_state.started), ==, 0); |
| 691 |
g_assert_cmpint (g_atomic_int_get (&blocked_state.completed), ==, 0); |
| 692 |
g_assert_cmpint (g_atomic_int_get (&next_state.started), ==, 1); |
| 693 |
g_assert_cmpint (g_atomic_int_get (&next_state.completed), ==, 1); |
| 694 |
|
| 695 |
return dex_future_new_true (); |
| 696 |
} |
| 697 |
|
| 698 |
static void |
| 699 |
test_limiter_timeout_waiting_run (void) |
| 700 |
{ |
| 701 |
run_test_fiber (test_limiter_timeout_waiting_run_fiber, NULL); |
| 702 |
} |
| 703 |
|
| 704 |
static DexFuture * |
| 705 |
test_limiter_timeout_running_run_fiber (gpointer user_data) |
| 706 |
{ |
| 707 |
DexLimiter * limiter = dex_limiter_new (1); |
| 708 |
GError * error = NULL; |
| 709 |
DexFuture *running; |
| 710 |
DexFuture *next; |
| 711 |
TimeoutRunState state = { .timeout_msec = 20 }; |
| 712 |
|
| 713 |
running = dex_future_with_timeout_msec (dex_limiter_run (limiter, |
| 714 |
NULL, |
| 715 |
0, |
| 716 |
test_limiter_timeout_run_item, |
| 717 |
&state, |
| 718 |
NULL), |
| 719 |
1); |
| 720 |
|
| 721 |
g_assert_false (dex_await (running, &error)); |
| 722 |
g_assert_error (error, DEX_ERROR, DEX_ERROR_TIMED_OUT); |
| 723 |
g_clear_error (&error); |
| 724 |
g_assert_cmpint (g_atomic_int_get (&state.started), ==, 1); |
| 725 |
|
| 726 |
next = dex_limiter_acquire (limiter); |
| 727 |
g_assert_true (dex_future_is_pending (next)); |
| 728 |
|
| 729 |
g_assert_true (dex_await (dex_future_with_timeout_msec (next, 100), &error)); |
| 730 |
g_assert_no_error (error); |
| 731 |
g_assert_cmpint (g_atomic_int_get (&state.completed), ==, 1); |
| 732 |
|
| 733 |
dex_limiter_release (limiter); |
| 734 |
|
| 735 |
return dex_future_new_true (); |
| 736 |
} |
| 737 |
|
| 738 |
static void |
| 739 |
test_limiter_timeout_running_run (void) |
| 740 |
{ |
| 741 |
run_test_fiber (test_limiter_timeout_running_run_fiber, NULL); |
| 742 |
} |
| 743 |
|
| 744 |
static DexFuture * |
| 745 |
test_limiter_close_fiber (gpointer user_data) |
| 746 |
{ |
| 747 |
DexLimiter * limiter = dex_limiter_new (1); |
| 748 |
GError * error = NULL; |
| 749 |
DexFuture *future; |
| 750 |
|
| 751 |
g_assert_true (dex_await (dex_limiter_acquire (limiter), &error)); |
| 752 |
g_assert_no_error (error); |
| 753 |
|
| 754 |
future = dex_limiter_acquire (limiter); |
| 755 |
dex_limiter_close (limiter); |
| 756 |
|
| 757 |
g_assert_false (dex_await (future, &error)); |
| 758 |
g_assert_error (error, DEX_ERROR, DEX_ERROR_SEMAPHORE_CLOSED); |
| 759 |
g_clear_error (&error); |
| 760 |
|
| 761 |
g_assert_false (dex_await (dex_limiter_acquire (limiter), &error)); |
| 762 |
g_assert_error (error, DEX_ERROR, DEX_ERROR_SEMAPHORE_CLOSED); |
| 763 |
|
| 764 |
dex_limiter_release (limiter); |
| 765 |
|
| 766 |
return dex_future_new_true (); |
| 767 |
} |
| 768 |
|
| 769 |
static void |
| 770 |
test_limiter_close (void) |
| 771 |
{ |
| 772 |
run_test_fiber (test_limiter_close_fiber, NULL); |
| 773 |
} |
| 774 |
|
| 775 |
static DexFuture * |
| 776 |
test_limiter_close_after_drain_fiber (gpointer user_data) |
| 777 |
{ |
| 778 |
DexLimiter *limiter = dex_limiter_new (1); |
| 779 |
GError *error = NULL; |
| 780 |
DexFuture *first; |
| 781 |
DexFuture *next; |
| 782 |
DexFuture *drain; |
| 783 |
DexFuture *drain_timeout; |
| 784 |
|
| 785 |
first = dex_limiter_acquire (limiter); |
| 786 |
g_assert_true (dex_await (first, &error)); |
| 787 |
g_assert_no_error (error); |
| 788 |
|
| 789 |
next = dex_limiter_acquire (limiter); |
| 790 |
g_assert_true (dex_future_is_pending (next)); |
| 791 |
|
| 792 |
drain = dex_limiter_close_after_drain (limiter); |
| 793 |
g_assert_true (dex_future_is_pending (drain)); |
| 794 |
|
| 795 |
drain_timeout = dex_future_with_timeout_msec (dex_ref (drain), 1); |
| 796 |
|
| 797 |
g_assert_false (dex_await (drain_timeout, &error)); |
| 798 |
g_assert_error (error, DEX_ERROR, DEX_ERROR_TIMED_OUT); |
| 799 |
g_clear_error (&error); |
| 800 |
|
| 801 |
dex_limiter_release (limiter); |
| 802 |
|
| 803 |
g_assert_false (dex_await (next, &error)); |
| 804 |
g_clear_error (&error); |
| 805 |
|
| 806 |
g_assert_true (dex_await (drain, &error)); |
| 807 |
g_assert_no_error (error); |
| 808 |
|
| 809 |
return dex_future_new_true (); |
| 810 |
} |
| 811 |
|
| 812 |
static void |
| 813 |
test_limiter_close_after_drain (void) |
| 814 |
{ |
| 815 |
run_test_fiber (test_limiter_close_after_drain_fiber, NULL); |
| 816 |
} |
| 817 |
|
| 818 |
int |
| 819 |
main (int argc, |
| 820 |
char *argv[]) |
| 821 |
{ |
| 822 |
dex_init (); |
| 823 |
g_test_init (&argc, &argv, NULL); |
| 824 |
|
| 825 |
g_test_add_func ("/Dex/TestSuite/Limiter/basic", test_limiter_basic); |
| 826 |
g_test_add_func ("/Dex/TestSuite/Limiter/run", test_limiter_run); |
| 827 |
g_test_add_func ("/Dex/TestSuite/Limiter/run_on_pool", test_limiter_run_on_pool); |
| 828 |
g_test_add_func ("/Dex/TestSuite/Limiter/run_coroutine", test_limiter_run_coroutine); |
| 829 |
g_test_add_func ("/Dex/TestSuite/Limiter/run_error", test_limiter_run_error); |
| 830 |
g_test_add_func ("/Dex/TestSuite/Limiter/run_coroutine_error", |
| 831 |
test_limiter_run_coroutine_error); |
| 832 |
g_test_add_func ("/Dex/TestSuite/Limiter/run_on_pool_error", |
| 833 |
test_limiter_run_on_pool_error); |
| 834 |
g_test_add_func ("/Dex/TestSuite/Limiter/discard_waiting", test_limiter_discard_waiting); |
| 835 |
g_test_add_func ("/Dex/TestSuite/Limiter/timeout_waiting_acquire", |
| 836 |
test_limiter_timeout_waiting_acquire); |
| 837 |
g_test_add_func ("/Dex/TestSuite/Limiter/discard_run", test_limiter_discard_run); |
| 838 |
g_test_add_func ("/Dex/TestSuite/Limiter/discard_run_on_pool", |
| 839 |
test_limiter_discard_run_on_pool); |
| 840 |
g_test_add_func ("/Dex/TestSuite/Limiter/timeout_waiting_run", |
| 841 |
test_limiter_timeout_waiting_run); |
| 842 |
g_test_add_func ("/Dex/TestSuite/Limiter/timeout_running_run", |
| 843 |
test_limiter_timeout_running_run); |
| 844 |
g_test_add_func ("/Dex/TestSuite/Limiter/close", test_limiter_close); |
| 845 |
g_test_add_func ("/Dex/TestSuite/Limiter/close_after_drain", |
| 846 |
test_limiter_close_after_drain); |
| 847 |
|
| 848 |
return g_test_run (); |
| 849 |
} |