| 1 |
/* test-thread.c |
| 2 |
* |
| 3 |
* Copyright 2025 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 <libdex.h> |
| 22 |
|
| 23 |
typedef struct |
| 24 |
{ |
| 25 |
char *str1; |
| 26 |
char *str2; |
| 27 |
} State; |
| 28 |
|
| 29 |
static void |
| 30 |
state_free (State *state) |
| 31 |
{ |
| 32 |
g_free (state->str1); |
| 33 |
g_free (state->str2); |
| 34 |
g_free (state); |
| 35 |
} |
| 36 |
|
| 37 |
static DexFuture * |
| 38 |
thread_func (gpointer data) |
| 39 |
{ |
| 40 |
State *state = data; |
| 41 |
|
| 42 |
g_assert_cmpstr (state->str1, ==, "string1"); |
| 43 |
g_assert_cmpstr (state->str2, ==, "string2"); |
| 44 |
|
| 45 |
return dex_future_new_take_string (g_strdup ("string3")); |
| 46 |
} |
| 47 |
|
| 48 |
static DexFuture * |
| 49 |
after_thread_func (DexFuture *completed, |
| 50 |
gpointer user_data) |
| 51 |
{ |
| 52 |
GMainLoop *main_loop = user_data; |
| 53 |
GError *error = NULL; |
| 54 |
const GValue *value; |
| 55 |
|
| 56 |
value = dex_future_get_value (completed, &error); |
| 57 |
|
| 58 |
g_assert_no_error (error); |
| 59 |
g_assert_nonnull (value); |
| 60 |
g_assert_true (G_VALUE_HOLDS_STRING (value)); |
| 61 |
g_assert_cmpstr ("string3", ==, g_value_get_string (value)); |
| 62 |
|
| 63 |
g_main_loop_quit (main_loop); |
| 64 |
|
| 65 |
return dex_future_new_true (); |
| 66 |
} |
| 67 |
|
| 68 |
static void |
| 69 |
test_thread_spawn (void) |
| 70 |
{ |
| 71 |
GMainLoop *main_loop = g_main_loop_new (NULL, FALSE); |
| 72 |
State *state; |
| 73 |
|
| 74 |
state = g_new0 (State, 1); |
| 75 |
state->str1 = g_strdup ("string1"); |
| 76 |
state->str2 = g_strdup ("string2"); |
| 77 |
|
| 78 |
dex_future_disown (dex_future_finally (dex_thread_spawn ("[test-thread]", |
| 79 |
thread_func, |
| 80 |
state, |
| 81 |
(GDestroyNotify) state_free), |
| 82 |
after_thread_func, |
| 83 |
g_main_loop_ref (main_loop), |
| 84 |
(GDestroyNotify) g_main_loop_unref)); |
| 85 |
|
| 86 |
g_main_loop_run (main_loop); |
| 87 |
g_main_loop_unref (main_loop); |
| 88 |
} |
| 89 |
|
| 90 |
static DexFuture * |
| 91 |
test_thread_wait_for_thread (gpointer data) |
| 92 |
{ |
| 93 |
DexPromise *promise = data; |
| 94 |
GError *error = NULL; |
| 95 |
|
| 96 |
g_assert_true (dex_thread_wait_for (dex_ref (promise), &error)); |
| 97 |
g_assert_no_error (error); |
| 98 |
|
| 99 |
return dex_future_new_for_int (123); |
| 100 |
} |
| 101 |
|
| 102 |
static DexFuture * |
| 103 |
quit_main (DexFuture *future, |
| 104 |
gpointer user_data) |
| 105 |
{ |
| 106 |
GMainLoop *main_loop = user_data; |
| 107 |
g_main_loop_quit (main_loop); |
| 108 |
return dex_ref (future); |
| 109 |
} |
| 110 |
|
| 111 |
static gboolean |
| 112 |
do_resolve (gpointer data) |
| 113 |
{ |
| 114 |
DexPromise *promise = data; |
| 115 |
dex_promise_resolve_boolean (promise, TRUE); |
| 116 |
return G_SOURCE_REMOVE; |
| 117 |
} |
| 118 |
|
| 119 |
static void |
| 120 |
test_thread_wait_for (void) |
| 121 |
{ |
| 122 |
GMainLoop *main_loop = g_main_loop_new (NULL, FALSE); |
| 123 |
DexPromise *promise = dex_promise_new (); |
| 124 |
DexFuture *thread; |
| 125 |
const GValue *value; |
| 126 |
|
| 127 |
thread = dex_thread_spawn ("[test-wait-for]", |
| 128 |
test_thread_wait_for_thread, |
| 129 |
dex_ref (promise), |
| 130 |
dex_unref); |
| 131 |
|
| 132 |
dex_future_disown (dex_future_finally (dex_ref (thread), |
| 133 |
quit_main, |
| 134 |
g_main_loop_ref (main_loop), |
| 135 |
(GDestroyNotify) g_main_loop_unref)); |
| 136 |
|
| 137 |
g_idle_add_full (G_PRIORITY_DEFAULT, |
| 138 |
do_resolve, |
| 139 |
dex_ref (promise), |
| 140 |
dex_unref); |
| 141 |
|
| 142 |
g_main_loop_run (main_loop); |
| 143 |
|
| 144 |
g_assert_true (dex_future_is_resolved (thread)); |
| 145 |
value = dex_future_get_value (thread, NULL); |
| 146 |
g_assert_true (G_VALUE_HOLDS_INT (value)); |
| 147 |
g_assert_cmpint (123, ==, g_value_get_int (value)); |
| 148 |
|
| 149 |
dex_unref (promise); |
| 150 |
dex_unref (thread); |
| 151 |
|
| 152 |
while (g_main_context_pending (NULL)) |
| 153 |
g_main_context_iteration (NULL, FALSE); |
| 154 |
|
| 155 |
g_main_loop_unref (main_loop); |
| 156 |
} |
| 157 |
|
| 158 |
int |
| 159 |
main (int argc, |
| 160 |
char *argv[]) |
| 161 |
{ |
| 162 |
dex_init (); |
| 163 |
g_test_init (&argc, &argv, NULL); |
| 164 |
g_test_add_func ("/Dex/TestSuite/Thread/spawn", test_thread_spawn); |
| 165 |
g_test_add_func ("/Dex/TestSuite/Thread/wait_for", test_thread_wait_for); |
| 166 |
return g_test_run (); |
| 167 |
} |