summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorJulian Bouzas <julian.bouzas@collabora.com>2021-06-25 22:55:44 -0400
committerGeorge Kiagiadakis <george.kiagiadakis@collabora.com>2021-07-28 13:19:02 +0300
commit3fc57c841ff9bdae445b086755024b2fa2630808 (patch)
treec74f61639085ed9359a73fa76067f9c40cf00261 /tests
parent9948c10fc6594ce1076550a32067a17dd463eb84 (diff)
tests: wpipc: use GCond instead of while loop to wait for events
Fixes hanging issues when running test with valgrind. Signed-off-by: George Kiagiadakis <george.kiagiadakis@collabora.com>
Diffstat (limited to 'tests')
-rw-r--r--tests/client-server.c16
-rw-r--r--tests/sender-receiver.c24
2 files changed, 24 insertions, 16 deletions
diff --git a/tests/client-server.c b/tests/client-server.c
index 241804c..24f8cc1 100644
--- a/tests/client-server.c
+++ b/tests/client-server.c
@@ -36,19 +36,16 @@ struct reply_data {
const char *error;
int n_replies;
GMutex mutex;
+ GCond cond;
};
static void
wait_for_reply (struct reply_data *data, int n_replies)
{
- while (true) {
- g_mutex_lock (&data->mutex);
- if (data->n_replies == n_replies) {
- g_mutex_unlock (&data->mutex);
- break;
- }
- g_mutex_unlock (&data->mutex);
- }
+ g_mutex_lock (&data->mutex);
+ while (data->n_replies < n_replies)
+ g_cond_wait (&data->cond, &data->mutex);
+ g_mutex_unlock (&data->mutex);
}
static void
@@ -65,6 +62,7 @@ reply_handler (struct icipc_sender *self, const uint8_t *buffer, size_t size, vo
g_assert_true (spa_pod_get_int (pod, &data->incremented) == 0);
}
data->n_replies++;
+ g_cond_signal (&data->cond);
g_mutex_unlock (&data->mutex);
}
@@ -78,6 +76,7 @@ test_icipc_server_client ()
g_assert_nonnull (c);
struct reply_data data;
g_mutex_init (&data.mutex);
+ g_cond_init (&data.cond);
/* add request handlers */
g_assert_true (icipc_server_set_request_handler (s, "INCREMENT", increment_request_handler, NULL));
@@ -108,6 +107,7 @@ test_icipc_server_client ()
g_assert_cmpstr (data.error, ==, "request handler not found");
/* clean up */
+ g_cond_clear (&data.cond);
g_mutex_clear (&data.mutex);
icipc_client_free (c);
icipc_server_free (s);
diff --git a/tests/sender-receiver.c b/tests/sender-receiver.c
index 8ec7736..9c47ac3 100644
--- a/tests/sender-receiver.c
+++ b/tests/sender-receiver.c
@@ -17,19 +17,16 @@ struct event_data {
int connections;
int n_events;
GMutex mutex;
+ GCond cond;
};
static void
wait_for_event (struct event_data *data, int n_events)
{
- while (true) {
- g_mutex_lock (&data->mutex);
- if (data->n_events == n_events) {
- g_mutex_unlock (&data->mutex);
- break;
- }
- g_mutex_unlock (&data->mutex);
- }
+ g_mutex_lock (&data->mutex);
+ while (data->n_events < n_events)
+ g_cond_wait (&data->cond, &data->mutex);
+ g_mutex_unlock (&data->mutex);
}
static void
@@ -52,6 +49,7 @@ sender_state_callback (struct icipc_receiver *self, int sender_fd,
break;
}
data->n_events++;
+ g_cond_signal (&data->cond);
g_mutex_unlock (&data->mutex);
}
@@ -65,6 +63,7 @@ reply_callback (struct icipc_sender *self, const uint8_t *buffer, size_t size, v
g_mutex_lock (&data->mutex);
g_assert_cmpmem (buffer, size, data->expected_data, data->expected_size);
data->n_events++;
+ g_cond_signal (&data->cond);
g_mutex_unlock (&data->mutex);
}
@@ -104,6 +103,7 @@ test_icipc_sender_connect ()
};
struct event_data data;
g_mutex_init (&data.mutex);
+ g_cond_init (&data.cond);
data.n_events = 0;
data.connections = 0;
struct icipc_receiver *r = icipc_receiver_new (TEST_ADDRESS, 16, &events, &data, 0);
@@ -130,6 +130,7 @@ test_icipc_sender_connect ()
icipc_receiver_stop (r);
/* clean up */
+ g_cond_clear (&data.cond);
g_mutex_clear (&data.mutex);
icipc_sender_free (s);
icipc_receiver_free (r);
@@ -143,6 +144,7 @@ lost_connection_handler (struct icipc_sender *self, int receiver_fd, void *p)
g_mutex_lock (&data->mutex);
data->n_events++;
+ g_cond_signal (&data->cond);
g_mutex_unlock (&data->mutex);
}
@@ -151,6 +153,7 @@ test_icipc_sender_lost_connection ()
{
struct event_data data;
g_mutex_init (&data.mutex);
+ g_cond_init (&data.cond);
struct icipc_receiver *r = icipc_receiver_new (TEST_ADDRESS, 16, NULL, NULL, 0);
g_assert_nonnull (r);
struct icipc_sender *s = icipc_sender_new (TEST_ADDRESS, 16, lost_connection_handler, &data, 0);
@@ -166,6 +169,7 @@ test_icipc_sender_lost_connection ()
wait_for_event (&data, 1);
/* clean up */
+ g_cond_clear (&data.cond);
g_mutex_clear (&data.mutex);
icipc_sender_free (s);
}
@@ -179,6 +183,7 @@ test_icipc_sender_send ()
g_assert_nonnull (s);
struct event_data data;
g_mutex_init (&data.mutex);
+ g_cond_init (&data.cond);
data.n_events = 0;
/* start receiver */
@@ -224,6 +229,7 @@ test_icipc_sender_send ()
icipc_receiver_stop (r);
/* clean up */
+ g_cond_clear (&data.cond);
g_mutex_clear (&data.mutex);
icipc_sender_free (s);
icipc_receiver_free (r);
@@ -237,6 +243,7 @@ test_icipc_multiple_senders_send ()
struct icipc_sender *senders[50];
struct event_data data;
g_mutex_init (&data.mutex);
+ g_cond_init (&data.cond);
data.n_events = 0;
/* start receiver */
@@ -262,6 +269,7 @@ test_icipc_multiple_senders_send ()
icipc_receiver_stop (r);
/* clean up */
+ g_cond_clear (&data.cond);
g_mutex_clear (&data.mutex);
for (int i = 0; i < 50; i++)
icipc_sender_free (senders[i]);