Debian Codegraph @

tests/common/base-test-fixture.h

raw
1 /* WirePlumber
2 *
3 * Copyright © 2020 Collabora Ltd.
4 * @author George Kiagiadakis <[email protected]>
5 *
6 * SPDX-License-Identifier: MIT
7 */
8
9 #include "test-server.h"
10 #include "test-log.h"
11 #include <wp/wp.h>
12
13 typedef enum {
14 WP_BASE_TEST_FLAG_CLIENT_CORE = (1<<0),
15 WP_BASE_TEST_FLAG_DONT_CONNECT = (1<<1),
16 } WpBaseTestFlags;
17
18 typedef struct {
19 /* the local pipewire server */
20 WpTestServer server;
21
22 /* the main loop */
23 GMainContext *context;
24 GMainLoop *loop;
25
26 /* watchdog */
27 GSource *timeout_source;
28
29 /* our session manager core */
30 WpCore *core;
31
32 /* the "client" core, which receives proxies
33 (second client to our internal server) */
34 WpCore *client_core;
35
36 /* Custom JSON config file if any,
37 * this overrides the default conf file that is picked
38 */
39 gchar *conf_file;
40
41 } WpBaseTestFixture;
42
43 static gboolean
44 timeout_callback (WpBaseTestFixture * self)
45 {
46 wp_critical ("test timed out");
47 g_test_fail ();
48 g_main_loop_quit (self->loop);
49
50 return G_SOURCE_REMOVE;
51 }
52
53 static void
54 disconnected_callback (WpCore *core, WpBaseTestFixture * self)
55 {
56 wp_critical_object (core, "%s core disconnected",
57 (core == self->client_core) ? "client" : "sm");
58 g_test_fail ();
59 g_main_loop_quit (self->loop);
60 }
61
62 static void
63 on_core_connected (WpObject * core, GAsyncResult * res, WpBaseTestFixture * self)
64 {
65 g_autoptr (GError) error = NULL;
66 wp_object_activate_finish (core, res, &error);
67 g_assert_no_error (error);
68 g_main_loop_quit (self->loop);
69 }
70
71 static void
72 wp_base_test_fixture_setup (WpBaseTestFixture * self, WpBaseTestFlags flags)
73 {
74 g_autoptr (WpProperties) props = NULL;
75 g_autoptr (WpConf) conf = NULL;
76
77 /* init test server */
78 wp_test_server_setup (&self->server);
79
80 /* init our main loop */
81 self->context = g_main_context_new ();
82 self->loop = g_main_loop_new (self->context, FALSE);
83 g_main_context_push_thread_default (self->context);
84
85 /* watchdog */
86 self->timeout_source = g_timeout_source_new_seconds (8);
87 g_source_set_callback (self->timeout_source, (GSourceFunc) timeout_callback,
88 self, NULL);
89 g_source_attach (self->timeout_source, self->context);
90
91 /* init our core */
92 props = wp_properties_new (PW_KEY_REMOTE_NAME, self->server.name, NULL);
93
94 if (self->conf_file) {
95 g_autoptr (GError) error = NULL;
96 conf = wp_conf_new_open (self->conf_file, NULL, &error);
97 g_assert_no_error (error);
98 g_assert_nonnull (conf);
99 }
100
101 self->core = wp_core_new (self->context, g_steal_pointer (&conf),
102 wp_properties_ref (props));
103 g_assert_true (self->core);
104
105 g_signal_connect (self->core, "disconnected",
106 (GCallback) disconnected_callback, self);
107
108 if (!(flags & WP_BASE_TEST_FLAG_DONT_CONNECT)) {
109 wp_object_activate (WP_OBJECT (self->core), WP_CORE_FEATURE_CONNECTED, NULL,
110 (GAsyncReadyCallback) on_core_connected, self);
111 g_main_loop_run (self->loop);
112 g_assert_true (wp_core_is_connected (self->core));
113 }
114
115 /* init the second client's core */
116 if (flags & WP_BASE_TEST_FLAG_CLIENT_CORE) {
117 self->client_core = wp_core_new (self->context, NULL,
118 wp_properties_ref (props));
119 g_signal_connect (self->client_core, "disconnected",
120 (GCallback) disconnected_callback, self);
121
122 if (!(flags & WP_BASE_TEST_FLAG_DONT_CONNECT)) {
123 wp_object_activate (WP_OBJECT (self->client_core), WP_CORE_FEATURE_CONNECTED,
124 NULL, (GAsyncReadyCallback) on_core_connected, self);
125 g_main_loop_run (self->loop);
126 g_assert_true (wp_core_is_connected (self->client_core));
127 }
128 }
129 }
130
131 static void
132 test_core_done_cb (WpCore *core, GAsyncResult *res, WpBaseTestFixture *self)
133 {
134 g_autoptr (GError) error = NULL;
135 g_assert_true (wp_core_sync_finish (core, res, &error));
136 g_assert_null (error);
137 g_main_loop_quit (self->loop);
138 }
139
140 static void
141 wp_base_test_fixture_teardown (WpBaseTestFixture * self)
142 {
143 /* wait for all client core pending tasks to be done */
144 if (self->client_core && wp_core_is_connected (self->client_core)) {
145 wp_core_sync (self->client_core, NULL,
146 (GAsyncReadyCallback) test_core_done_cb, self);
147 g_main_loop_run (self->loop);
148 g_signal_handlers_disconnect_by_data (self->client_core, self);
149 wp_core_disconnect (self->client_core);
150 }
151
152 /* wait for all core pending tasks to be done */
153 if (self->core && wp_core_is_connected (self->core)) {
154 wp_core_sync (self->core, NULL, (GAsyncReadyCallback) test_core_done_cb,
155 self);
156 g_main_loop_run (self->loop);
157 g_signal_handlers_disconnect_by_data (self->core, self);
158 wp_core_disconnect (self->core);
159 }
160
161 /* double check and ensure that there is no event pending */
162 while (g_main_context_pending (self->context))
163 g_main_context_iteration (self->context, TRUE);
164
165 g_main_context_pop_thread_default (self->context);
166 g_clear_pointer (&self->conf_file, g_free);
167 g_clear_object (&self->client_core);
168 g_clear_object (&self->core);
169 g_clear_pointer (&self->timeout_source, g_source_unref);
170 g_clear_pointer (&self->loop, g_main_loop_unref);
171 g_clear_pointer (&self->context, g_main_context_unref);
172 wp_test_server_teardown (&self->server);
173 }
174
175 static G_GNUC_UNUSED void
176 test_object_activate_finish_cb (WpObject * object, GAsyncResult * res,
177 WpBaseTestFixture * f)
178 {
179 g_autoptr (GError) error = NULL;
180 gboolean augment_ret = wp_object_activate_finish (object, res, &error);
181 g_assert_no_error (error);
182 g_assert_true (augment_ret);
183
184 g_main_loop_quit (f->loop);
185 }
186
187 static G_GNUC_UNUSED void
188 test_ensure_object_manager_is_installed (WpObjectManager * om, WpCore * core,
189 GMainLoop * loop)
190 {
191 gulong id = g_signal_connect_swapped (om, "installed",
192 G_CALLBACK (g_main_loop_quit), loop);
193 wp_core_install_object_manager (core, om);
194 if (!wp_object_manager_is_installed (om))
195 g_main_loop_run (loop);
196 g_signal_handler_disconnect (om, id);
197 }
198
199 static G_GNUC_UNUSED gboolean
200 test_is_spa_lib_installed (WpBaseTestFixture *f, const gchar *factory_name) {
201 struct spa_handle *handle;
202
203 handle = pw_context_load_spa_handle (f->server.context, factory_name, NULL);
204 if (!handle)
205 return FALSE;
206
207 pw_unload_spa_handle (handle);
208 return TRUE;
209 }