| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
#include "config.h" |
| 23 |
|
| 24 |
#include "dex-test.h" |
| 25 |
|
| 26 |
#include "dex-future.h" |
| 27 |
#include "dex-init.h" |
| 28 |
#include "dex-main-scheduler-private.h" |
| 29 |
#include "dex-scheduler-private.h" |
| 30 |
#include "dex-thread-storage-private.h" |
| 31 |
|
| 32 |
typedef struct _DexTest |
| 33 |
{ |
| 34 |
GTestFunc test_func; |
| 35 |
} DexTest; |
| 36 |
|
| 37 |
static DexFuture * |
| 38 |
dex_test_runner_fiber (gpointer user_data) |
| 39 |
{ |
| 40 |
DexTest *test = user_data; |
| 41 |
|
| 42 |
g_assert (test != NULL); |
| 43 |
g_assert (test->test_func != NULL); |
| 44 |
|
| 45 |
test->test_func (); |
| 46 |
|
| 47 |
return dex_future_new_true (); |
| 48 |
} |
| 49 |
|
| 50 |
static DexScheduler * |
| 51 |
dex_test_ensure_thread_scheduler (GMainContext *main_context, |
| 52 |
gboolean *created_scheduler) |
| 53 |
{ |
| 54 |
DexScheduler *scheduler; |
| 55 |
|
| 56 |
g_assert (main_context != NULL); |
| 57 |
g_assert (created_scheduler != NULL); |
| 58 |
|
| 59 |
*created_scheduler = FALSE; |
| 60 |
|
| 61 |
if ((scheduler = dex_scheduler_get_thread_default ())) |
| 62 |
return dex_ref (scheduler); |
| 63 |
|
| 64 |
*created_scheduler = TRUE; |
| 65 |
|
| 66 |
return DEX_SCHEDULER (dex_main_scheduler_new (main_context)); |
| 67 |
} |
| 68 |
|
| 69 |
static void |
| 70 |
dex_test_runner (gconstpointer user_data) |
| 71 |
{ |
| 72 |
DexTest *test = (gpointer)user_data; |
| 73 |
DexScheduler *scheduler = NULL; |
| 74 |
DexFuture *future = NULL; |
| 75 |
GMainContext *main_context = NULL; |
| 76 |
GError *error = NULL; |
| 77 |
const GValue *value; |
| 78 |
gboolean created_scheduler; |
| 79 |
|
| 80 |
dex_init (); |
| 81 |
|
| 82 |
main_context = g_main_context_ref_thread_default (); |
| 83 |
scheduler = dex_test_ensure_thread_scheduler (main_context, &created_scheduler); |
| 84 |
|
| 85 |
future = dex_scheduler_spawn (scheduler, |
| 86 |
8 * 1024 * 1024, |
| 87 |
dex_test_runner_fiber, |
| 88 |
test, |
| 89 |
NULL); |
| 90 |
|
| 91 |
while (dex_future_is_pending (future)) |
| 92 |
g_main_context_iteration (main_context, TRUE); |
| 93 |
|
| 94 |
value = dex_future_get_value (future, &error); |
| 95 |
g_assert_no_error (error); |
| 96 |
g_assert_nonnull (value); |
| 97 |
|
| 98 |
while (g_main_context_pending (main_context)) |
| 99 |
g_main_context_iteration (main_context, FALSE); |
| 100 |
|
| 101 |
if (created_scheduler) |
| 102 |
{ |
| 103 |
DexThreadStorage *storage = dex_thread_storage_get (); |
| 104 |
|
| 105 |
storage->scheduler = NULL; |
| 106 |
storage->aio_context = NULL; |
| 107 |
} |
| 108 |
|
| 109 |
g_clear_error (&error); |
| 110 |
g_clear_pointer (&future, dex_unref); |
| 111 |
g_clear_pointer (&scheduler, dex_unref); |
| 112 |
g_clear_pointer (&main_context, g_main_context_unref); |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
void |
| 129 |
dex_test_add_func (const char *testpath, |
| 130 |
GTestFunc test_func) |
| 131 |
{ |
| 132 |
DexTest *test; |
| 133 |
|
| 134 |
g_return_if_fail (testpath != NULL); |
| 135 |
g_return_if_fail (test_func != NULL); |
| 136 |
|
| 137 |
test = g_new0 (DexTest, 1); |
| 138 |
test->test_func = test_func; |
| 139 |
|
| 140 |
g_test_add_data_func_full (testpath, test, dex_test_runner, g_free); |
| 141 |
} |