tests: Use GMainContext instead of GMainLoop in gdbus-threading

This is equivalent, but makes the loop exit conditions a little clearer,
since they’re actually in a `while` statement, rather than being a
`g_main_loop_quit()` call in a callback somewhere else in the file.

Signed-off-by: Philip Withnall <withnall@endlessm.com>

Helps: #1515
This commit is contained in:
Philip Withnall 2020-02-21 14:12:52 +00:00
parent 79ef610f03
commit cd0ab355cd

View File

@ -80,54 +80,24 @@ assert_connection_has_one_ref (GDBusConnection *connection,
typedef struct { typedef struct {
GThread *thread; GThread *thread;
GMainLoop *thread_loop; GMainContext *context;
guint signal_count; guint signal_count;
gboolean unsubscribe_complete; gboolean unsubscribe_complete;
GAsyncResult *async_result;
} DeliveryData; } DeliveryData;
static void static void
msg_cb_expect_success (GDBusConnection *connection, async_result_cb (GDBusConnection *connection,
GAsyncResult *res, GAsyncResult *res,
gpointer user_data) gpointer user_data)
{ {
DeliveryData *data = user_data; DeliveryData *data = user_data;
GError *error;
GVariant *result;
error = NULL; data->async_result = g_object_ref (res);
result = g_dbus_connection_call_finish (connection,
res,
&error);
g_assert_no_error (error);
g_assert_nonnull (result);
g_variant_unref (result);
g_assert_true (g_thread_self () == data->thread); g_assert_true (g_thread_self () == data->thread);
g_main_loop_quit (data->thread_loop); g_main_context_wakeup (data->context);
}
static void
msg_cb_expect_error_cancelled (GDBusConnection *connection,
GAsyncResult *res,
gpointer user_data)
{
DeliveryData *data = user_data;
GError *error;
GVariant *result;
error = NULL;
result = g_dbus_connection_call_finish (connection,
res,
&error);
g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
g_assert_false (g_dbus_error_is_remote_error (error));
g_error_free (error);
g_assert_null (result);
g_assert_true (g_thread_self () == data->thread);
g_main_loop_quit (data->thread_loop);
} }
static void static void
@ -145,7 +115,7 @@ signal_handler (GDBusConnection *connection,
data->signal_count++; data->signal_count++;
g_main_loop_quit (data->thread_loop); g_main_context_wakeup (data->context);
} }
static void static void
@ -157,30 +127,28 @@ signal_data_free_cb (gpointer user_data)
data->unsubscribe_complete = TRUE; data->unsubscribe_complete = TRUE;
g_main_loop_quit (data->thread_loop); g_main_context_wakeup (data->context);
} }
static gpointer static gpointer
test_delivery_in_thread_func (gpointer _data) test_delivery_in_thread_func (gpointer _data)
{ {
GMainLoop *thread_loop;
GMainContext *thread_context; GMainContext *thread_context;
DeliveryData data; DeliveryData data;
GCancellable *ca; GCancellable *ca;
guint subscription_id; guint subscription_id;
GDBusConnection *priv_c; GDBusConnection *priv_c;
GError *error; GError *error = NULL;
GVariant *result_variant = NULL;
error = NULL;
thread_context = g_main_context_new (); thread_context = g_main_context_new ();
thread_loop = g_main_loop_new (thread_context, FALSE);
g_main_context_push_thread_default (thread_context); g_main_context_push_thread_default (thread_context);
data.thread = g_thread_self (); data.thread = g_thread_self ();
data.thread_loop = thread_loop; data.context = thread_context;
data.signal_count = 0; data.signal_count = 0;
data.unsubscribe_complete = FALSE; data.unsubscribe_complete = FALSE;
data.async_result = NULL;
/* ---------------------------------------------------------------------------------------------------- */ /* ---------------------------------------------------------------------------------------------------- */
@ -196,9 +164,16 @@ test_delivery_in_thread_func (gpointer _data)
G_DBUS_CALL_FLAGS_NONE, G_DBUS_CALL_FLAGS_NONE,
-1, -1,
NULL, NULL,
(GAsyncReadyCallback) msg_cb_expect_success, (GAsyncReadyCallback) async_result_cb,
&data); &data);
g_main_loop_run (thread_loop); while (data.async_result == NULL)
g_main_context_iteration (thread_context, TRUE);
result_variant = g_dbus_connection_call_finish (c, data.async_result, &error);
g_assert_no_error (error);
g_assert_nonnull (result_variant);
g_clear_pointer (&result_variant, g_variant_unref);
g_clear_object (&data.async_result);
/* /*
* Check that we never actually send a message if the GCancellable * Check that we never actually send a message if the GCancellable
@ -216,9 +191,18 @@ test_delivery_in_thread_func (gpointer _data)
G_DBUS_CALL_FLAGS_NONE, G_DBUS_CALL_FLAGS_NONE,
-1, -1,
ca, ca,
(GAsyncReadyCallback) msg_cb_expect_error_cancelled, (GAsyncReadyCallback) async_result_cb,
&data); &data);
g_main_loop_run (thread_loop); while (data.async_result == NULL)
g_main_context_iteration (thread_context, TRUE);
result_variant = g_dbus_connection_call_finish (c, data.async_result, &error);
g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
g_assert_false (g_dbus_error_is_remote_error (error));
g_clear_error (&error);
g_assert_null (result_variant);
g_clear_object (&data.async_result);
g_object_unref (ca); g_object_unref (ca);
/* /*
@ -234,10 +218,20 @@ test_delivery_in_thread_func (gpointer _data)
G_DBUS_CALL_FLAGS_NONE, G_DBUS_CALL_FLAGS_NONE,
-1, -1,
ca, ca,
(GAsyncReadyCallback) msg_cb_expect_error_cancelled, (GAsyncReadyCallback) async_result_cb,
&data); &data);
g_cancellable_cancel (ca); g_cancellable_cancel (ca);
g_main_loop_run (thread_loop);
while (data.async_result == NULL)
g_main_context_iteration (thread_context, TRUE);
result_variant = g_dbus_connection_call_finish (c, data.async_result, &error);
g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
g_assert_false (g_dbus_error_is_remote_error (error));
g_clear_error (&error);
g_assert_null (result_variant);
g_clear_object (&data.async_result);
g_object_unref (ca); g_object_unref (ca);
/* /*
@ -264,7 +258,8 @@ test_delivery_in_thread_func (gpointer _data)
g_assert_no_error (error); g_assert_no_error (error);
g_assert_nonnull (priv_c); g_assert_nonnull (priv_c);
g_main_loop_run (thread_loop); while (data.signal_count < 1)
g_main_context_iteration (thread_context, TRUE);
g_assert_cmpuint (data.signal_count, ==, 1); g_assert_cmpuint (data.signal_count, ==, 1);
g_object_unref (priv_c); g_object_unref (priv_c);
@ -272,13 +267,13 @@ test_delivery_in_thread_func (gpointer _data)
g_dbus_connection_signal_unsubscribe (c, subscription_id); g_dbus_connection_signal_unsubscribe (c, subscription_id);
subscription_id = 0; subscription_id = 0;
g_main_loop_run (thread_loop); while (!data.unsubscribe_complete)
g_main_context_iteration (thread_context, TRUE);
g_assert_true (data.unsubscribe_complete); g_assert_true (data.unsubscribe_complete);
/* ---------------------------------------------------------------------------------------------------- */ /* ---------------------------------------------------------------------------------------------------- */
g_main_context_pop_thread_default (thread_context); g_main_context_pop_thread_default (thread_context);
g_main_loop_unref (thread_loop);
g_main_context_unref (thread_context); g_main_context_unref (thread_context);
return NULL; return NULL;