[glib/wip/pwithnall/gdbus-names-livelock] tests: Port gdbus-names test entirely to GMainContext
- From: Philip Withnall <pwithnall src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [glib/wip/pwithnall/gdbus-names-livelock] tests: Port gdbus-names test entirely to GMainContext
- Date: Mon, 21 Jun 2021 13:18:02 +0000 (UTC)
commit 32e8e4ad91890b97fde924038743af7faa245f65
Author: Philip Withnall <pwithnall endlessos org>
Date: Mon Jun 21 14:07:21 2021 +0100
tests: Port gdbus-names test entirely to GMainContext
It makes combination exit conditions a lot easier than when using
`g_main_loop_quit()` from different callbacks.
Signed-off-by: Philip Withnall <pwithnall endlessos org>
gio/tests/gdbus-names.c | 200 +++++++++++++++++++++++++++++++++---------------
1 file changed, 139 insertions(+), 61 deletions(-)
---
diff --git a/gio/tests/gdbus-names.c b/gio/tests/gdbus-names.c
index 195e762dd..81f70f4eb 100644
--- a/gio/tests/gdbus-names.c
+++ b/gio/tests/gdbus-names.c
@@ -25,9 +25,6 @@
#include "gdbus-tests.h"
-/* all tests rely on a shared mainloop */
-static GMainLoop *loop;
-
/* ---------------------------------------------------------------------------------------------------- */
/* Test that g_bus_own_name() works correctly */
/* ---------------------------------------------------------------------------------------------------- */
@@ -39,14 +36,14 @@ typedef struct
guint num_acquired;
guint num_lost;
guint num_free_func;
+ GMainContext *main_context; /* (unowned) */
} OwnNameData;
static void
own_name_data_free_func (OwnNameData *data)
{
data->num_free_func++;
- g_main_loop_quit (loop);
- g_main_context_wakeup (g_main_loop_get_context (loop));
+ g_main_context_wakeup (data->main_context);
}
static void
@@ -57,8 +54,7 @@ bus_acquired_handler (GDBusConnection *connection,
OwnNameData *data = user_data;
g_dbus_connection_set_exit_on_close (connection, FALSE);
data->num_bus_acquired += 1;
- g_main_loop_quit (loop);
- g_main_context_wakeup (g_main_loop_get_context (loop));
+ g_main_context_wakeup (data->main_context);
}
static void
@@ -68,8 +64,7 @@ name_acquired_handler (GDBusConnection *connection,
{
OwnNameData *data = user_data;
data->num_acquired += 1;
- g_main_loop_quit (loop);
- g_main_context_wakeup (g_main_loop_get_context (loop));
+ g_main_context_wakeup (data->main_context);
}
static void
@@ -88,8 +83,7 @@ name_lost_handler (GDBusConnection *connection,
g_dbus_connection_set_exit_on_close (connection, FALSE);
}
data->num_lost += 1;
- g_main_loop_quit (loop);
- g_main_context_wakeup (g_main_loop_get_context (loop));
+ g_main_context_wakeup (data->main_context);
}
static void
@@ -105,6 +99,7 @@ test_bus_own_name (void)
gboolean name_has_owner_reply;
GDBusConnection *c2;
GVariant *result;
+ GMainContext *main_context = NULL; /* use the global default for now */
error = NULL;
name = "org.gtk.GDBus.Name1";
@@ -119,6 +114,7 @@ test_bus_own_name (void)
data.num_acquired = 0;
data.num_lost = 0;
data.expect_null_connection = TRUE;
+ data.main_context = main_context;
id = g_bus_own_name (G_BUS_TYPE_SESSION,
name,
G_BUS_NAME_OWNER_FLAGS_NONE,
@@ -130,7 +126,10 @@ test_bus_own_name (void)
g_assert_cmpint (data.num_bus_acquired, ==, 0);
g_assert_cmpint (data.num_acquired, ==, 0);
g_assert_cmpint (data.num_lost, ==, 0);
- g_main_loop_run (loop);
+
+ while (data.num_lost < 1)
+ g_main_context_iteration (main_context, TRUE);
+
g_assert_cmpint (data.num_bus_acquired, ==, 0);
g_assert_cmpint (data.num_acquired, ==, 0);
g_assert_cmpint (data.num_lost, ==, 1);
@@ -158,11 +157,17 @@ test_bus_own_name (void)
g_assert_cmpint (data.num_bus_acquired, ==, 0);
g_assert_cmpint (data.num_acquired, ==, 0);
g_assert_cmpint (data.num_lost, ==, 0);
- g_main_loop_run (loop);
+
+ while (data.num_bus_acquired < 1)
+ g_main_context_iteration (main_context, TRUE);
+
g_assert_cmpint (data.num_bus_acquired, ==, 1);
g_assert_cmpint (data.num_acquired, ==, 0);
g_assert_cmpint (data.num_lost, ==, 0);
- g_main_loop_run (loop);
+
+ while (data.num_acquired < 1)
+ g_main_context_iteration (main_context, TRUE);
+
g_assert_cmpint (data.num_bus_acquired, ==, 1);
g_assert_cmpint (data.num_acquired, ==, 1);
g_assert_cmpint (data.num_lost, ==, 0);
@@ -194,7 +199,8 @@ test_bus_own_name (void)
* Stop owning the name - this should invoke our free func
*/
g_bus_unown_name (id);
- g_main_loop_run (loop);
+ while (data.num_free_func < 2)
+ g_main_context_iteration (main_context, TRUE);
g_assert_cmpint (data.num_free_func, ==, 2);
/*
@@ -239,7 +245,11 @@ test_bus_own_name (void)
g_assert_cmpint (data.num_acquired, ==, 1);
g_assert_cmpint (data.num_lost, ==, 0);
g_assert_cmpint (data.num_free_func, ==, 2);
- g_main_loop_run (loop); /* the GDestroyNotify is called in idle because the bus is acquired in idle */
+
+ /* the GDestroyNotify is called in idle because the bus is acquired in idle */
+ while (data.num_free_func < 3)
+ g_main_context_iteration (main_context, TRUE);
+
g_assert_cmpint (data.num_free_func, ==, 3);
/*
@@ -264,11 +274,17 @@ test_bus_own_name (void)
g_assert_cmpint (data.num_bus_acquired, ==, 0);
g_assert_cmpint (data.num_acquired, ==, 0);
g_assert_cmpint (data.num_lost, ==, 0);
- g_main_loop_run (loop);
+
+ while (data.num_bus_acquired < 1)
+ g_main_context_iteration (main_context, TRUE);
+
g_assert_cmpint (data.num_bus_acquired, ==, 1);
g_assert_cmpint (data.num_acquired, ==, 0);
g_assert_cmpint (data.num_lost, ==, 0);
- g_main_loop_run (loop);
+
+ while (data.num_acquired < 1)
+ g_main_context_iteration (main_context, TRUE);
+
g_assert_cmpint (data.num_bus_acquired, ==, 1);
g_assert_cmpint (data.num_acquired, ==, 1);
g_assert_cmpint (data.num_lost, ==, 0);
@@ -282,6 +298,7 @@ test_bus_own_name (void)
data2.num_acquired = 0;
data2.num_lost = 0;
data2.expect_null_connection = FALSE;
+ data2.main_context = main_context;
id2 = g_bus_own_name (G_BUS_TYPE_SESSION,
name,
G_BUS_NAME_OWNER_FLAGS_NONE,
@@ -293,16 +310,25 @@ test_bus_own_name (void)
g_assert_cmpint (data2.num_bus_acquired, ==, 0);
g_assert_cmpint (data2.num_acquired, ==, 0);
g_assert_cmpint (data2.num_lost, ==, 0);
- g_main_loop_run (loop);
+
+ while (data2.num_bus_acquired < 1)
+ g_main_context_iteration (main_context, TRUE);
+
g_assert_cmpint (data2.num_bus_acquired, ==, 1);
g_assert_cmpint (data2.num_acquired, ==, 0);
g_assert_cmpint (data2.num_lost, ==, 0);
- g_main_loop_run (loop);
+
+ while (data2.num_lost < 1)
+ g_main_context_iteration (main_context, TRUE);
+
g_assert_cmpint (data2.num_bus_acquired, ==, 1);
g_assert_cmpint (data2.num_acquired, ==, 0);
g_assert_cmpint (data2.num_lost, ==, 1);
+
g_bus_unown_name (id2);
- g_main_loop_run (loop);
+ while (data2.num_free_func < 1)
+ g_main_context_iteration (main_context, TRUE);
+
g_assert_cmpint (data2.num_bus_acquired, ==, 1);
g_assert_cmpint (data2.num_acquired, ==, 0);
g_assert_cmpint (data2.num_lost, ==, 1);
@@ -332,12 +358,18 @@ test_bus_own_name (void)
g_assert_cmpint (data2.num_bus_acquired, ==, 0);
g_assert_cmpint (data2.num_acquired, ==, 0);
g_assert_cmpint (data2.num_lost, ==, 0);
- g_main_loop_run (loop);
+
+ while (data2.num_lost < 1)
+ g_main_context_iteration (main_context, TRUE);
+
g_assert_cmpint (data2.num_bus_acquired, ==, 0);
g_assert_cmpint (data2.num_acquired, ==, 0);
g_assert_cmpint (data2.num_lost, ==, 1);
+
g_bus_unown_name (id2);
- g_main_loop_run (loop);
+ while (data2.num_free_func < 1)
+ g_main_context_iteration (main_context, TRUE);
+
g_assert_cmpint (data2.num_bus_acquired, ==, 0);
g_assert_cmpint (data2.num_acquired, ==, 0);
g_assert_cmpint (data2.num_lost, ==, 1);
@@ -358,12 +390,18 @@ test_bus_own_name (void)
g_assert_cmpint (data2.num_bus_acquired, ==, 0);
g_assert_cmpint (data2.num_acquired, ==, 0);
g_assert_cmpint (data2.num_lost, ==, 0);
- g_main_loop_run (loop);
+
+ while (data2.num_lost < 1)
+ g_main_context_iteration (main_context, TRUE);
+
g_assert_cmpint (data2.num_bus_acquired, ==, 0);
g_assert_cmpint (data2.num_acquired, ==, 0);
g_assert_cmpint (data2.num_lost, ==, 1);
+
g_bus_unown_name (id2);
- g_main_loop_run (loop);
+ while (data2.num_free_func < 1)
+ g_main_context_iteration (main_context, TRUE);
+
g_assert_cmpint (data2.num_bus_acquired, ==, 0);
g_assert_cmpint (data2.num_acquired, ==, 0);
g_assert_cmpint (data2.num_lost, ==, 1);
@@ -374,7 +412,9 @@ test_bus_own_name (void)
*/
data.expect_null_connection = FALSE;
g_bus_unown_name (id);
- g_main_loop_run (loop);
+ while (data.num_bus_acquired < 1 || data.num_free_func < 4)
+ g_main_context_iteration (main_context, TRUE);
+
g_assert_cmpint (data.num_bus_acquired, ==, 1);
g_assert_cmpint (data.num_acquired, ==, 1);
g_assert_cmpint (data.num_free_func, ==, 4);
@@ -394,11 +434,17 @@ test_bus_own_name (void)
g_assert_cmpint (data.num_bus_acquired, ==, 0);
g_assert_cmpint (data.num_acquired, ==, 0);
g_assert_cmpint (data.num_lost, ==, 0);
- g_main_loop_run (loop);
+
+ while (data.num_bus_acquired < 1)
+ g_main_context_iteration (main_context, TRUE);
+
g_assert_cmpint (data.num_bus_acquired, ==, 1);
g_assert_cmpint (data.num_acquired, ==, 0);
g_assert_cmpint (data.num_lost, ==, 0);
- g_main_loop_run (loop);
+
+ while (data.num_acquired < 1)
+ g_main_context_iteration (main_context, TRUE);
+
g_assert_cmpint (data.num_bus_acquired, ==, 1);
g_assert_cmpint (data.num_acquired, ==, 1);
g_assert_cmpint (data.num_lost, ==, 0);
@@ -423,12 +469,18 @@ test_bus_own_name (void)
g_assert_cmpint (data2.num_bus_acquired, ==, 0);
g_assert_cmpint (data2.num_acquired, ==, 0);
g_assert_cmpint (data2.num_lost, ==, 0);
- g_main_loop_run (loop);
+
+ while (data2.num_lost < 1)
+ g_main_context_iteration (main_context, TRUE);
+
g_assert_cmpint (data2.num_bus_acquired, ==, 0);
g_assert_cmpint (data2.num_acquired, ==, 0);
g_assert_cmpint (data2.num_lost, ==, 1);
+
g_bus_unown_name (id2);
- g_main_loop_run (loop);
+ while (data2.num_free_func < 1)
+ g_main_context_iteration (main_context, TRUE);
+
g_assert_cmpint (data2.num_bus_acquired, ==, 0);
g_assert_cmpint (data2.num_acquired, ==, 0);
g_assert_cmpint (data2.num_lost, ==, 1);
@@ -451,18 +503,22 @@ test_bus_own_name (void)
g_assert_cmpint (data.num_lost, ==, 0);
g_assert_cmpint (data2.num_acquired, ==, 0);
g_assert_cmpint (data2.num_lost, ==, 0);
+
/* wait for handlers for both owner and owner2 to fire */
while (data.num_lost == 0 || data2.num_acquired == 0)
- g_main_loop_run (loop);
+ g_main_context_iteration (main_context, TRUE);
+
g_assert_cmpint (data.num_acquired, ==, 1);
g_assert_cmpint (data.num_lost, ==, 1);
g_assert_cmpint (data2.num_acquired, ==, 1);
g_assert_cmpint (data2.num_lost, ==, 0);
g_assert_cmpint (data2.num_bus_acquired, ==, 0);
+
/* ok, make owner2 release the name - then wait for owner to automagically reacquire it */
g_bus_unown_name (id2);
- g_main_loop_run (loop);
- g_main_loop_run (loop);
+ while (data.num_acquired < 2 || data2.num_free_func < 1)
+ g_main_context_iteration (main_context, TRUE);
+
g_assert_cmpint (data2.num_free_func, ==, 1);
g_assert_cmpint (data.num_acquired, ==, 2);
g_assert_cmpint (data.num_lost, ==, 1);
@@ -474,11 +530,15 @@ test_bus_own_name (void)
data.expect_null_connection = TRUE;
session_bus_stop ();
while (data.num_lost != 2)
- g_main_loop_run (loop);
+ g_main_context_iteration (main_context, TRUE);
+
g_assert_cmpint (data.num_acquired, ==, 2);
g_assert_cmpint (data.num_lost, ==, 2);
+
g_bus_unown_name (id);
- g_main_loop_run (loop);
+ while (data.num_free_func < 5)
+ g_main_context_iteration (main_context, TRUE);
+
g_assert_cmpint (data.num_free_func, ==, 5);
g_object_unref (c);
@@ -499,6 +559,7 @@ typedef struct
guint num_appeared;
guint num_vanished;
guint num_free_func;
+ GMainContext *main_context; /* (unowned), for the main test thread */
} WatchNameData;
typedef struct
@@ -520,8 +581,7 @@ static void
watch_name_data_free_func (WatchNameData *data)
{
data->num_free_func++;
- g_main_loop_quit (loop);
- g_main_context_wakeup (g_main_loop_get_context (loop));
+ g_main_context_wakeup (data->main_context);
}
static void
@@ -538,8 +598,7 @@ w_name_acquired_handler (GDBusConnection *connection,
{
OwnNameData *data = user_data;
data->num_acquired += 1;
- g_main_loop_quit (loop);
- g_main_context_wakeup (g_main_loop_get_context (loop));
+ g_main_context_wakeup (data->main_context);
}
static void
@@ -549,8 +608,7 @@ w_name_lost_handler (GDBusConnection *connection,
{
OwnNameData *data = user_data;
data->num_lost += 1;
- g_main_loop_quit (loop);
- g_main_context_wakeup (g_main_loop_get_context (loop));
+ g_main_context_wakeup (data->main_context);
}
static void
@@ -571,8 +629,7 @@ name_appeared_handler (GDBusConnection *connection,
g_dbus_connection_set_exit_on_close (connection, FALSE);
}
data->num_appeared += 1;
- g_main_loop_quit (loop);
- g_main_context_wakeup (g_main_loop_get_context (loop));
+ g_main_context_wakeup (data->main_context);
}
static void
@@ -592,8 +649,7 @@ name_vanished_handler (GDBusConnection *connection,
g_dbus_connection_set_exit_on_close (connection, FALSE);
}
data->num_vanished += 1;
- g_main_loop_quit (loop);
- g_main_context_wakeup (g_main_loop_get_context (loop));
+ g_main_context_wakeup (data->main_context);
}
typedef struct
@@ -640,6 +696,7 @@ stop_service (GDBusConnection *connection,
GError *error = NULL;
GDBusProxy *proxy = NULL;
GVariant *result = NULL;
+ GMainContext *main_context = NULL; /* use the global default for now */
data->num_vanished = 0;
@@ -665,7 +722,7 @@ stop_service (GDBusConnection *connection,
if (result)
g_variant_unref (result);
while (data->num_vanished == 0)
- g_main_loop_run (loop);
+ g_main_context_iteration (main_context, TRUE);
}
static void
@@ -678,6 +735,7 @@ test_bus_watch_name (gconstpointer d)
GDBusConnection *connection;
const WatchNameTest *watch_name_test;
const gchar *name;
+ GMainContext *main_context = NULL; /* use the global default for now */
watch_name_test = (WatchNameTest *) d;
@@ -699,6 +757,7 @@ test_bus_watch_name (gconstpointer d)
data.num_appeared = 0;
data.num_vanished = 0;
data.expect_null_connection = TRUE;
+ data.main_context = main_context;
id = g_bus_watch_name (G_BUS_TYPE_SESSION,
name,
watch_name_test->watcher_flags,
@@ -708,7 +767,10 @@ test_bus_watch_name (gconstpointer d)
(GDestroyNotify) watch_name_data_free_func);
g_assert_cmpint (data.num_appeared, ==, 0);
g_assert_cmpint (data.num_vanished, ==, 0);
- g_main_loop_run (loop);
+
+ while (data.num_vanished < 1)
+ g_main_context_iteration (main_context, TRUE);
+
g_assert_cmpint (data.num_appeared, ==, 0);
g_assert_cmpint (data.num_vanished, ==, 1);
g_bus_unwatch_name (id);
@@ -726,6 +788,7 @@ test_bus_watch_name (gconstpointer d)
own_data.num_acquired = 0;
own_data.num_lost = 0;
data.expect_null_connection = FALSE;
+ own_data.main_context = main_context;
owner_id = g_bus_own_name (G_BUS_TYPE_SESSION,
name,
G_BUS_NAME_OWNER_FLAGS_NONE,
@@ -734,7 +797,10 @@ test_bus_watch_name (gconstpointer d)
w_name_lost_handler,
&own_data,
(GDestroyNotify) own_name_data_free_func);
- g_main_loop_run (loop);
+
+ while (own_data.num_acquired < 1)
+ g_main_context_iteration (main_context, TRUE);
+
g_assert_cmpint (own_data.num_acquired, ==, 1);
g_assert_cmpint (own_data.num_lost, ==, 0);
@@ -768,7 +834,10 @@ test_bus_watch_name (gconstpointer d)
}
g_assert_cmpint (data.num_appeared, ==, 0);
g_assert_cmpint (data.num_vanished, ==, 0);
- g_main_loop_run (loop);
+
+ while (data.num_appeared < 1)
+ g_main_context_iteration (main_context, TRUE);
+
g_assert_cmpint (data.num_appeared, ==, 1);
g_assert_cmpint (data.num_vanished, ==, 0);
@@ -780,7 +849,9 @@ test_bus_watch_name (gconstpointer d)
/* unown the name */
g_bus_unown_name (owner_id);
- g_main_loop_run (loop);
+ while (own_data.num_free_func < 1)
+ g_main_context_iteration (main_context, TRUE);
+
g_assert_cmpint (own_data.num_acquired, ==, 1);
g_assert_cmpint (own_data.num_free_func, ==, 1);
own_data.num_free_func = 0;
@@ -818,7 +889,10 @@ test_bus_watch_name (gconstpointer d)
g_assert_cmpint (data.num_appeared, ==, 0);
g_assert_cmpint (data.num_vanished, ==, 0);
- g_main_loop_run (loop);
+
+ while (data.num_appeared == 0 && data.num_vanished == 0)
+ g_main_context_iteration (main_context, TRUE);
+
if (watch_name_test->existing_service)
{
g_assert_cmpint (data.num_appeared, ==, 1);
@@ -836,6 +910,7 @@ test_bus_watch_name (gconstpointer d)
own_data.num_acquired = 0;
own_data.num_lost = 0;
own_data.expect_null_connection = FALSE;
+ own_data.main_context = main_context;
owner_id = g_bus_own_name (G_BUS_TYPE_SESSION,
name,
G_BUS_NAME_OWNER_FLAGS_NONE,
@@ -844,8 +919,10 @@ test_bus_watch_name (gconstpointer d)
w_name_lost_handler,
&own_data,
(GDestroyNotify) own_name_data_free_func);
+
while (own_data.num_acquired == 0 || data.num_appeared == 0)
- g_main_loop_run (loop);
+ g_main_context_iteration (main_context, TRUE);
+
g_assert_cmpint (own_data.num_acquired, ==, 1);
g_assert_cmpint (own_data.num_lost, ==, 0);
g_assert_cmpint (data.num_appeared, ==, 1);
@@ -865,7 +942,8 @@ test_bus_watch_name (gconstpointer d)
session_bus_stop ();
if (!watch_name_test->existing_service)
{
- g_main_loop_run (loop);
+ while (own_data.num_lost < 1 || data.num_vanished < 2)
+ g_main_context_iteration (main_context, TRUE);
g_assert_cmpint (own_data.num_lost, ==, 1);
g_assert_cmpint (data.num_vanished, ==, 2);
}
@@ -880,7 +958,9 @@ test_bus_watch_name (gconstpointer d)
if (!watch_name_test->existing_service)
{
g_bus_unown_name (owner_id);
- g_main_loop_run (loop);
+ while (own_data.num_free_func < 1)
+ g_main_context_iteration (main_context, TRUE);
+
g_assert_cmpint (own_data.num_free_func, ==, 1);
}
session_bus_down ();
@@ -1057,6 +1137,7 @@ watch_with_different_context (gboolean unwatch_early)
GDBusConnection *connection;
GThread *watcher;
guint id;
+ GMainContext *main_context = NULL; /* use the global default for now */
session_bus_up ();
@@ -1083,6 +1164,7 @@ watch_with_different_context (gboolean unwatch_early)
own_data.num_lost = 0;
own_data.num_free_func = 0;
own_data.expect_null_connection = FALSE;
+ own_data.main_context = main_context;
// Own the name to avoid direct name vanished in watcher thread
id = g_bus_own_name_on_connection (connection,
"org.gtk.GDBus.Name1",
@@ -1092,7 +1174,7 @@ watch_with_different_context (gboolean unwatch_early)
&own_data,
(GDestroyNotify) own_name_data_free_func);
while (own_data.num_acquired == 0)
- g_main_context_iteration (NULL, TRUE);
+ g_main_context_iteration (main_context, TRUE);
g_assert_cmpint (own_data.num_acquired, ==, 1);
g_assert_cmpint (own_data.num_lost, ==, 0);
@@ -1104,13 +1186,13 @@ watch_with_different_context (gboolean unwatch_early)
// Iterate the loop until thread is waking us up
while (!thread_data.ended)
- g_main_context_iteration (NULL, TRUE);
+ g_main_context_iteration (main_context, TRUE);
g_thread_join (watcher);
g_bus_unown_name (id);
while (own_data.num_free_func == 0)
- g_main_context_iteration (NULL, TRUE);
+ g_main_context_iteration (main_context, TRUE);
g_assert_cmpint (own_data.num_free_func, ==, 1);
g_mutex_clear (&thread_data.mutex);
@@ -1241,8 +1323,6 @@ main (int argc,
g_test_init (&argc, &argv, NULL);
- loop = g_main_loop_new (NULL, FALSE);
-
g_test_dbus_unset ();
g_test_add_func ("/gdbus/validate-names", test_validate_names);
@@ -1267,7 +1347,5 @@ main (int argc,
g_test_add_func ("/gdbus/escape-object-path", test_escape_object_path);
ret = g_test_run();
- g_main_loop_unref (loop);
-
return ret;
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]