| 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 struct |
| 24 |
{ |
| 25 |
GMainLoop *main_loop; |
| 26 |
DexThreadPool *pool; |
| 27 |
DexFuture *close1; |
| 28 |
DexFuture *close2; |
| 29 |
} CloseState; |
| 30 |
|
| 31 |
static DexFuture * |
| 32 |
sleep_thread_func (gpointer data) |
| 33 |
{ |
| 34 |
g_usleep (100000); |
| 35 |
return dex_future_new_true (); |
| 36 |
} |
| 37 |
|
| 38 |
static gboolean |
| 39 |
quit_main_loop (gpointer data) |
| 40 |
{ |
| 41 |
GMainLoop *main_loop = data; |
| 42 |
|
| 43 |
g_main_loop_quit (main_loop); |
| 44 |
return G_SOURCE_REMOVE; |
| 45 |
} |
| 46 |
|
| 47 |
static DexFuture * |
| 48 |
close_future_cb (DexFuture *future, |
| 49 |
gpointer user_data) |
| 50 |
{ |
| 51 |
CloseState *state = user_data; |
| 52 |
GError *error = NULL; |
| 53 |
const GValue *value; |
| 54 |
|
| 55 |
value = dex_future_get_value (future, &error); |
| 56 |
g_assert_no_error (error); |
| 57 |
g_assert_nonnull (value); |
| 58 |
g_assert_true (G_VALUE_HOLDS_BOOLEAN (value)); |
| 59 |
g_assert_true (g_value_get_boolean (value)); |
| 60 |
|
| 61 |
g_main_context_invoke (NULL, quit_main_loop, state->main_loop); |
| 62 |
|
| 63 |
return NULL; |
| 64 |
} |
| 65 |
|
| 66 |
static void |
| 67 |
test_thread_pool_close_is_single_flight (void) |
| 68 |
{ |
| 69 |
CloseState state = {0}; |
| 70 |
|
| 71 |
state.main_loop = g_main_loop_new (NULL, FALSE); |
| 72 |
state.pool = dex_thread_pool_new (2); |
| 73 |
|
| 74 |
dex_future_disown (dex_thread_pool_submit (state.pool, |
| 75 |
"[test-thread-pool]", |
| 76 |
sleep_thread_func, |
| 77 |
NULL, |
| 78 |
NULL)); |
| 79 |
|
| 80 |
state.close1 = dex_thread_pool_close (state.pool, |
| 81 |
DEX_THREAD_POOL_SHUTDOWN_DRAIN); |
| 82 |
state.close2 = dex_thread_pool_close (state.pool, |
| 83 |
DEX_THREAD_POOL_SHUTDOWN_DRAIN); |
| 84 |
|
| 85 |
g_assert_true (state.close1 == state.close2); |
| 86 |
|
| 87 |
dex_future_disown (dex_future_finally (dex_ref (state.close1), |
| 88 |
close_future_cb, |
| 89 |
&state, |
| 90 |
NULL)); |
| 91 |
|
| 92 |
g_main_loop_run (state.main_loop); |
| 93 |
|
| 94 |
g_assert_true (dex_future_is_resolved (state.close1)); |
| 95 |
g_assert_true (dex_future_is_resolved (state.close2)); |
| 96 |
|
| 97 |
dex_unref (state.close1); |
| 98 |
dex_unref (state.close2); |
| 99 |
dex_unref (state.pool); |
| 100 |
g_main_loop_unref (state.main_loop); |
| 101 |
} |
| 102 |
|
| 103 |
int |
| 104 |
main (int argc, |
| 105 |
char *argv[]) |
| 106 |
{ |
| 107 |
dex_init (); |
| 108 |
g_test_init (&argc, &argv, NULL); |
| 109 |
g_test_add_func ("/Dex/TestSuite/ThreadPool/close_is_single_flight", |
| 110 |
test_thread_pool_close_is_single_flight); |
| 111 |
return g_test_run (); |
| 112 |
} |