From 31a7537110b5d059a43a4fc248e131e573a3ca9f Mon Sep 17 00:00:00 2001 From: George Kiagiadakis <george.kiagiadakis@collabora.com> Date: Tue, 27 Aug 2019 17:37:06 +0300 Subject: [PATCH] tests: proxy: improvements in the fixture and basic test * move the watchdogs in the fixture setup * use a new GMainContext on each fixture to avoid test dependencies * test wp_proxy_sync --- tests/proxy.c | 85 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 56 insertions(+), 29 deletions(-) diff --git a/tests/proxy.c b/tests/proxy.c index 4665c2e0..805ad3f3 100644 --- a/tests/proxy.c +++ b/tests/proxy.c @@ -16,7 +16,9 @@ typedef struct { WpTestServer server; /* the main loop */ + GMainContext *context; GMainLoop *loop; + GSource *timeout_source; /* the client wireplumber objects */ WpCore *core; @@ -24,42 +26,79 @@ typedef struct { } TestProxyFixture; +static gboolean +timeout_callback (TestProxyFixture *fixture) +{ + g_message ("test timed out"); + g_test_fail (); + g_main_loop_quit (fixture->loop); + + return G_SOURCE_REMOVE; +} + +static void +test_proxy_state_changed (WpRemote *remote, WpRemoteState state, + TestProxyFixture *fixture) +{ + g_autofree gchar * msg = NULL; + + switch (state) { + case WP_REMOTE_STATE_ERROR: + g_object_get (remote, "error-message", &msg, NULL); + g_message ("remote error: %s", msg); + g_test_fail (); + g_main_loop_quit (fixture->loop); + break; + default: + break; + } +} + static void test_proxy_setup (TestProxyFixture *self, gconstpointer user_data) { wp_test_server_setup (&self->server); g_setenv ("PIPEWIRE_REMOTE", self->server.name, TRUE); - self->loop = g_main_loop_new (NULL, FALSE); + self->context = g_main_context_new (); + self->loop = g_main_loop_new (self->context, FALSE); self->core = wp_core_new (); - self->remote = wp_remote_pipewire_new (self->core, NULL); + self->remote = wp_remote_pipewire_new (self->core, self->context); + + g_main_context_push_thread_default (self->context); + + /* watchdogs */ + g_signal_connect (self->remote, "state-changed", + (GCallback) test_proxy_state_changed, self); + + self->timeout_source = g_timeout_source_new_seconds (3); + g_source_set_callback (self->timeout_source, (GSourceFunc) timeout_callback, + self, NULL); + g_source_attach (self->timeout_source, self->context); } static void test_proxy_teardown (TestProxyFixture *self, gconstpointer user_data) { + g_main_context_pop_thread_default (self->context); + g_clear_object (&self->remote); g_clear_object (&self->core); + g_clear_pointer (&self->timeout_source, g_source_unref); g_clear_pointer (&self->loop, g_main_loop_unref); + g_clear_pointer (&self->context, g_main_context_unref); g_unsetenv ("PIPEWIRE_REMOTE"); wp_test_server_teardown (&self->server); } static void -test_proxy_state_changed (WpRemote *remote, WpRemoteState state, +test_proxy_basic_done (WpProxy *proxy, GAsyncResult *res, TestProxyFixture *fixture) { - g_autofree gchar * msg = NULL; + g_autoptr (GError) error = NULL; + g_assert_true (wp_proxy_sync_finish (proxy, res, &error)); + g_assert_no_error (error); - switch (state) { - case WP_REMOTE_STATE_ERROR: - g_object_get (remote, "error-message", &msg, NULL); - g_message ("remote error: %s", msg); - g_test_fail (); - g_main_loop_quit (fixture->loop); - break; - default: - break; - } + g_main_loop_quit (fixture->loop); } static void @@ -68,11 +107,13 @@ test_proxy_basic_augmented (WpProxy *proxy, GAsyncResult *res, { g_autoptr (GError) error = NULL; g_assert_true (wp_proxy_augment_finish (proxy, res, &error)); + g_assert_no_error (error); g_assert_true (wp_proxy_get_features (proxy) & WP_PROXY_FEATURE_PW_PROXY); g_assert_nonnull (wp_proxy_get_pw_proxy (proxy)); - g_main_loop_quit (fixture->loop); + wp_proxy_sync (proxy, NULL, (GAsyncReadyCallback) test_proxy_basic_done, + fixture); } static void @@ -108,29 +149,15 @@ test_proxy_basic_global_added (WpRemote *remote, WpProxy *proxy, (GAsyncReadyCallback) test_proxy_basic_augmented, fixture); } -static gboolean -timeout_callback (TestProxyFixture *fixture) -{ - g_message ("test timed out"); - g_test_fail (); - g_main_loop_quit (fixture->loop); - - return G_SOURCE_REMOVE; -} - static void test_proxy_basic (TestProxyFixture *fixture, gconstpointer data) { - g_signal_connect (fixture->remote, "state-changed", - (GCallback) test_proxy_state_changed, fixture); - /* our test server should advertise exactly one * client: our WpRemote; use this to test WpProxy */ g_signal_connect (fixture->remote, "global-added::client", (GCallback) test_proxy_basic_global_added, fixture); g_assert_true (wp_remote_connect (fixture->remote)); - g_timeout_add_seconds (3, (GSourceFunc) timeout_callback, fixture); g_main_loop_run (fixture->loop); } -- GitLab