Skip to content
Snippets Groups Projects
Commit 3fd65ac9 authored by Julian Bouzas's avatar Julian Bouzas
Browse files

tests: config-endpoints: run the main loop in the same thread

parent fefa4b91
No related branches found
No related tags found
No related merge requests found
......@@ -16,38 +16,38 @@
typedef struct {
WpTestServer server;
GMutex mutex;
GCond cond;
gboolean created;
GThread *loop_thread;
GMainContext *context;
GMainLoop *loop;
GSource *timeout_source;
WpCore *core;
} TestConfigEndpointFixture;
void wait_for_created (TestConfigEndpointFixture *self)
static gboolean
timeout_callback (TestConfigEndpointFixture *self)
{
g_mutex_lock (&self->mutex);
while (!self->created)
g_cond_wait (&self->cond, &self->mutex);
self->created = FALSE;
g_mutex_unlock (&self->mutex);
g_message ("test timed out");
g_test_fail ();
g_main_loop_quit (self->loop);
return G_SOURCE_REMOVE;
}
void signal_created (TestConfigEndpointFixture *self)
static void
disconnected_callback (WpCore *core, TestConfigEndpointFixture *self)
{
g_mutex_lock (&self->mutex);
self->created = TRUE;
g_cond_signal (&self->cond);
g_mutex_unlock (&self->mutex);
g_message ("core disconnected");
g_test_fail ();
g_main_loop_quit (self->loop);
}
static void
create_audiotestsrc (TestConfigEndpointFixture *self)
config_endpoint_setup (TestConfigEndpointFixture *self, gconstpointer data)
{
g_autoptr (WpProperties) props = NULL;
/* Create the server and load audiotestsrc */
wp_test_server_setup (&self->server);
pw_thread_loop_lock (self->server.thread_loop);
pw_context_add_spa_lib (self->server.context, "audiotestsrc",
"audiotestsrc/libspa-audiotestsrc");
......@@ -58,82 +58,37 @@ create_audiotestsrc (TestConfigEndpointFixture *self)
return;
}
pw_thread_loop_unlock (self->server.thread_loop);
}
static void *
loop_thread_start (void *d)
{
TestConfigEndpointFixture *self = d;
/* Create the main loop using the default thread context */
self->context = g_main_context_get_thread_default ();
/* Create the main context and loop */
self->context = g_main_context_new ();
self->loop = g_main_loop_new (self->context, FALSE);
g_main_context_push_thread_default (self->context);
/* Create the server */
wp_test_server_setup (&self->server);
/* Set a timeout source */
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);
/* Create the core and connect to the server */
g_autoptr (WpProperties) props = NULL;
/* Create the core */
props = wp_properties_new (PW_KEY_REMOTE_NAME, self->server.name, NULL);
self->core = wp_core_new (self->context, props);
g_assert_true (wp_core_connect (self->core));
g_signal_connect (self->core, "disconnected",
(GCallback) disconnected_callback, self);
/* Register the wp-endpoint-audiotestsrc */
wp_factory_new (self->core, "wp-endpoint-audiotestsrc",
wp_endpoint_audiotestsrc_factory);
/* Create the audiotestsrc node */
create_audiotestsrc (self);
/* Signal we are done */
signal_created (self);
/* Run the main loop */
g_main_loop_run (self->loop);
/* Clean up */
g_clear_object (&self->core);
wp_test_server_teardown (&self->server);
g_clear_pointer (&self->loop, g_main_loop_unref);
return NULL;
}
static void
config_endpoint_setup (TestConfigEndpointFixture *self, gconstpointer data)
{
/* Data */
g_mutex_init (&self->mutex);
g_cond_init (&self->cond);
self->created = FALSE;
/* Initialize main loop, server and core in a thread */
self->loop_thread = g_thread_new("loop-thread", &loop_thread_start, self);
/* Wait for everything to be created */
wait_for_created (self);
}
static gboolean
loop_thread_stop (gpointer data)
{
TestConfigEndpointFixture *self = data;
g_main_loop_quit (self->loop);
return G_SOURCE_REMOVE;
}
static void
config_endpoint_teardown (TestConfigEndpointFixture *self, gconstpointer data)
{
/* Stop the main loop and wait until it is done */
g_autoptr (GSource) source = g_idle_source_new ();
g_source_set_callback (source, loop_thread_stop, self, NULL);
g_source_attach (source, self->context);
g_thread_join (self->loop_thread);
/* Data */
g_mutex_clear (&self->mutex);
g_cond_clear (&self->cond);
self->created = FALSE;
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);
wp_test_server_teardown (&self->server);
}
static void
......@@ -141,7 +96,7 @@ on_audiotestsrc_created (WpConfigEndpointContext *ctx, WpEndpoint *ep,
TestConfigEndpointFixture *f)
{
g_assert_nonnull (ep);
signal_created (f);
g_main_loop_quit (f->loop);
}
static void
......@@ -156,11 +111,20 @@ basic (TestConfigEndpointFixture *f, gconstpointer data)
g_autoptr (WpConfigEndpointContext) ctx =
wp_config_endpoint_context_new (f->core);
g_assert_nonnull (ctx);
g_assert_cmpint (wp_config_endpoint_context_get_length (ctx), ==, 0);
/* Add a handler to stop the main loop when the endpoint is created */
g_signal_connect (ctx, "endpoint-created",
(GCallback) on_audiotestsrc_created, f);
/* Wait for the endpoint to be created */
wait_for_created (f);
/* Connect */
g_assert_true (wp_core_connect (f->core));
/* Run the main loop */
g_main_loop_run (f->loop);
/* Check if the endpoint was created */
g_assert_cmpint (wp_config_endpoint_context_get_length (ctx), ==, 1);
}
int
......
......@@ -162,9 +162,10 @@ wp_endpoint_audiotestsrc_init_async (GAsyncInitable *initable, int io_priority,
cancellable, callback, data);
g_autoptr (WpCore) core = wp_base_endpoint_get_core (WP_BASE_ENDPOINT(self));
if (core)
wp_core_sync (core, NULL,
(GAsyncReadyCallback) wp_endpoint_audiotestsrc_finish_creation, self);
g_return_if_fail (core);
wp_core_sync (core, NULL,
(GAsyncReadyCallback) wp_endpoint_audiotestsrc_finish_creation, self);
}
static void
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment