Debian Codegraph @

src/dex-test.c

raw symbols SCIP
1 /*
2 * dex-test.c
3 *
4 * Copyright 2026 Christian Hergert
5 *
6 * This library is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of the
9 * License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * SPDX-License-Identifier: LGPL-2.1-or-later
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 * dex_test_add_func:
117 * @testpath: test case path
118 * @test_func: (scope async): test function to execute from a fiber
119 *
120 * Adds a test function like g_test_add_func(), but runs @test_func from a
121 * [[email protected]]. The calling thread is given a thread-default
122 * [[email protected]] if it does not already have one, allowing tests to use
123 * dex_await() and related APIs directly.
124 *
125 * After @test_func completes, the scheduler's main context is iterated until no
126 * immediately pending sources remain.
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 }