diff options
author | Julian Bouzas <julian.bouzas@collabora.com> | 2021-06-25 22:55:44 -0400 |
---|---|---|
committer | George Kiagiadakis <george.kiagiadakis@collabora.com> | 2021-07-28 13:19:02 +0300 |
commit | 3fc57c841ff9bdae445b086755024b2fa2630808 (patch) | |
tree | c74f61639085ed9359a73fa76067f9c40cf00261 /tests/sender-receiver.c | |
parent | 9948c10fc6594ce1076550a32067a17dd463eb84 (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/sender-receiver.c')
-rw-r--r-- | tests/sender-receiver.c | 24 |
1 files changed, 16 insertions, 8 deletions
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]); |