From d3d977cbe4bcfcfb16c42a0d208786f0e3fd261d Mon Sep 17 00:00:00 2001 From: Julian Bouzas Date: Fri, 27 Dec 2019 08:32:28 -0500 Subject: [PATCH] config-policy: add timeout when waiting on conditional variables --- tests/modules/config-policy.c | 9 ++++++++- tests/modules/config-policy/context.c | 8 +++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/tests/modules/config-policy.c b/tests/modules/config-policy.c index 6ab71a69..babc536e 100644 --- a/tests/modules/config-policy.c +++ b/tests/modules/config-policy.c @@ -71,6 +71,8 @@ loop_thread_start (void *d) static void config_policy_setup (TestConfigPolicyFixture *self, gconstpointer user_data) { + gint64 end_time; + /* Data */ g_mutex_init (&self->mutex); g_cond_init (&self->cond); @@ -81,8 +83,13 @@ config_policy_setup (TestConfigPolicyFixture *self, gconstpointer user_data) /* Wait for everything to be created */ g_mutex_lock (&self->mutex); + end_time = g_get_monotonic_time () + 3 * G_TIME_SPAN_SECOND; while (!self->created) - g_cond_wait (&self->cond, &self->mutex); + if (!g_cond_wait_until (&self->cond, &self->mutex, end_time)) { + /* Abort when timeout has passed */ + g_warning ("Aborting due to timeout when waiting for connection"); + abort(); + } g_mutex_unlock (&self->mutex); } diff --git a/tests/modules/config-policy/context.c b/tests/modules/config-policy/context.c index e286a4ed..def2d545 100644 --- a/tests/modules/config-policy/context.c +++ b/tests/modules/config-policy/context.c @@ -39,11 +39,17 @@ G_DEFINE_TYPE (WpConfigPolicyContext, wp_config_policy_context, G_TYPE_OBJECT); static WpBaseEndpoint * wait_for_endpoint (WpConfigPolicyContext *self, WpBaseEndpointLink **link) { + gint64 end_time; g_mutex_lock (&self->mutex); /* Wait for endpoint to be set */ + end_time = g_get_monotonic_time () + 3 * G_TIME_SPAN_SECOND; while (!self->endpoint) - g_cond_wait (&self->cond, &self->mutex); + if (!g_cond_wait_until (&self->cond, &self->mutex, end_time)) { + /* Abort when timeout has passed */ + g_warning ("Aborting due to timeout when waiting for endpoint"); + abort(); + } /* Set endpoint to a local value and clear global value */ WpBaseEndpoint *endpoint = g_object_ref (self->endpoint); -- GitLab