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>
This commit is contained in:
Philip Withnall 2021-06-21 14:07:21 +01:00
parent bd8d139a77
commit 32e8e4ad91

View File

@ -25,9 +25,6 @@
#include "gdbus-tests.h" #include "gdbus-tests.h"
/* all tests rely on a shared mainloop */
static GMainLoop *loop;
/* ---------------------------------------------------------------------------------------------------- */ /* ---------------------------------------------------------------------------------------------------- */
/* Test that g_bus_own_name() works correctly */ /* Test that g_bus_own_name() works correctly */
/* ---------------------------------------------------------------------------------------------------- */ /* ---------------------------------------------------------------------------------------------------- */
@ -39,14 +36,14 @@ typedef struct
guint num_acquired; guint num_acquired;
guint num_lost; guint num_lost;
guint num_free_func; guint num_free_func;
GMainContext *main_context; /* (unowned) */
} OwnNameData; } OwnNameData;
static void static void
own_name_data_free_func (OwnNameData *data) own_name_data_free_func (OwnNameData *data)
{ {
data->num_free_func++; data->num_free_func++;
g_main_loop_quit (loop); g_main_context_wakeup (data->main_context);
g_main_context_wakeup (g_main_loop_get_context (loop));
} }
static void static void
@ -57,8 +54,7 @@ bus_acquired_handler (GDBusConnection *connection,
OwnNameData *data = user_data; OwnNameData *data = user_data;
g_dbus_connection_set_exit_on_close (connection, FALSE); g_dbus_connection_set_exit_on_close (connection, FALSE);
data->num_bus_acquired += 1; data->num_bus_acquired += 1;
g_main_loop_quit (loop); g_main_context_wakeup (data->main_context);
g_main_context_wakeup (g_main_loop_get_context (loop));
} }
static void static void
@ -68,8 +64,7 @@ name_acquired_handler (GDBusConnection *connection,
{ {
OwnNameData *data = user_data; OwnNameData *data = user_data;
data->num_acquired += 1; data->num_acquired += 1;
g_main_loop_quit (loop); g_main_context_wakeup (data->main_context);
g_main_context_wakeup (g_main_loop_get_context (loop));
} }
static void static void
@ -88,8 +83,7 @@ name_lost_handler (GDBusConnection *connection,
g_dbus_connection_set_exit_on_close (connection, FALSE); g_dbus_connection_set_exit_on_close (connection, FALSE);
} }
data->num_lost += 1; data->num_lost += 1;
g_main_loop_quit (loop); g_main_context_wakeup (data->main_context);
g_main_context_wakeup (g_main_loop_get_context (loop));
} }
static void static void
@ -105,6 +99,7 @@ test_bus_own_name (void)
gboolean name_has_owner_reply; gboolean name_has_owner_reply;
GDBusConnection *c2; GDBusConnection *c2;
GVariant *result; GVariant *result;
GMainContext *main_context = NULL; /* use the global default for now */
error = NULL; error = NULL;
name = "org.gtk.GDBus.Name1"; name = "org.gtk.GDBus.Name1";
@ -119,6 +114,7 @@ test_bus_own_name (void)
data.num_acquired = 0; data.num_acquired = 0;
data.num_lost = 0; data.num_lost = 0;
data.expect_null_connection = TRUE; data.expect_null_connection = TRUE;
data.main_context = main_context;
id = g_bus_own_name (G_BUS_TYPE_SESSION, id = g_bus_own_name (G_BUS_TYPE_SESSION,
name, name,
G_BUS_NAME_OWNER_FLAGS_NONE, 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_bus_acquired, ==, 0);
g_assert_cmpint (data.num_acquired, ==, 0); g_assert_cmpint (data.num_acquired, ==, 0);
g_assert_cmpint (data.num_lost, ==, 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_bus_acquired, ==, 0);
g_assert_cmpint (data.num_acquired, ==, 0); g_assert_cmpint (data.num_acquired, ==, 0);
g_assert_cmpint (data.num_lost, ==, 1); 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_bus_acquired, ==, 0);
g_assert_cmpint (data.num_acquired, ==, 0); g_assert_cmpint (data.num_acquired, ==, 0);
g_assert_cmpint (data.num_lost, ==, 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_bus_acquired, ==, 1);
g_assert_cmpint (data.num_acquired, ==, 0); g_assert_cmpint (data.num_acquired, ==, 0);
g_assert_cmpint (data.num_lost, ==, 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_bus_acquired, ==, 1);
g_assert_cmpint (data.num_acquired, ==, 1); g_assert_cmpint (data.num_acquired, ==, 1);
g_assert_cmpint (data.num_lost, ==, 0); 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 * Stop owning the name - this should invoke our free func
*/ */
g_bus_unown_name (id); 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); 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_acquired, ==, 1);
g_assert_cmpint (data.num_lost, ==, 0); g_assert_cmpint (data.num_lost, ==, 0);
g_assert_cmpint (data.num_free_func, ==, 2); 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); 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_bus_acquired, ==, 0);
g_assert_cmpint (data.num_acquired, ==, 0); g_assert_cmpint (data.num_acquired, ==, 0);
g_assert_cmpint (data.num_lost, ==, 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_bus_acquired, ==, 1);
g_assert_cmpint (data.num_acquired, ==, 0); g_assert_cmpint (data.num_acquired, ==, 0);
g_assert_cmpint (data.num_lost, ==, 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_bus_acquired, ==, 1);
g_assert_cmpint (data.num_acquired, ==, 1); g_assert_cmpint (data.num_acquired, ==, 1);
g_assert_cmpint (data.num_lost, ==, 0); g_assert_cmpint (data.num_lost, ==, 0);
@ -282,6 +298,7 @@ test_bus_own_name (void)
data2.num_acquired = 0; data2.num_acquired = 0;
data2.num_lost = 0; data2.num_lost = 0;
data2.expect_null_connection = FALSE; data2.expect_null_connection = FALSE;
data2.main_context = main_context;
id2 = g_bus_own_name (G_BUS_TYPE_SESSION, id2 = g_bus_own_name (G_BUS_TYPE_SESSION,
name, name,
G_BUS_NAME_OWNER_FLAGS_NONE, 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_bus_acquired, ==, 0);
g_assert_cmpint (data2.num_acquired, ==, 0); g_assert_cmpint (data2.num_acquired, ==, 0);
g_assert_cmpint (data2.num_lost, ==, 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_bus_acquired, ==, 1);
g_assert_cmpint (data2.num_acquired, ==, 0); g_assert_cmpint (data2.num_acquired, ==, 0);
g_assert_cmpint (data2.num_lost, ==, 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_bus_acquired, ==, 1);
g_assert_cmpint (data2.num_acquired, ==, 0); g_assert_cmpint (data2.num_acquired, ==, 0);
g_assert_cmpint (data2.num_lost, ==, 1); g_assert_cmpint (data2.num_lost, ==, 1);
g_bus_unown_name (id2); 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_bus_acquired, ==, 1);
g_assert_cmpint (data2.num_acquired, ==, 0); g_assert_cmpint (data2.num_acquired, ==, 0);
g_assert_cmpint (data2.num_lost, ==, 1); 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_bus_acquired, ==, 0);
g_assert_cmpint (data2.num_acquired, ==, 0); g_assert_cmpint (data2.num_acquired, ==, 0);
g_assert_cmpint (data2.num_lost, ==, 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_bus_acquired, ==, 0);
g_assert_cmpint (data2.num_acquired, ==, 0); g_assert_cmpint (data2.num_acquired, ==, 0);
g_assert_cmpint (data2.num_lost, ==, 1); g_assert_cmpint (data2.num_lost, ==, 1);
g_bus_unown_name (id2); 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_bus_acquired, ==, 0);
g_assert_cmpint (data2.num_acquired, ==, 0); g_assert_cmpint (data2.num_acquired, ==, 0);
g_assert_cmpint (data2.num_lost, ==, 1); 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_bus_acquired, ==, 0);
g_assert_cmpint (data2.num_acquired, ==, 0); g_assert_cmpint (data2.num_acquired, ==, 0);
g_assert_cmpint (data2.num_lost, ==, 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_bus_acquired, ==, 0);
g_assert_cmpint (data2.num_acquired, ==, 0); g_assert_cmpint (data2.num_acquired, ==, 0);
g_assert_cmpint (data2.num_lost, ==, 1); g_assert_cmpint (data2.num_lost, ==, 1);
g_bus_unown_name (id2); 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_bus_acquired, ==, 0);
g_assert_cmpint (data2.num_acquired, ==, 0); g_assert_cmpint (data2.num_acquired, ==, 0);
g_assert_cmpint (data2.num_lost, ==, 1); g_assert_cmpint (data2.num_lost, ==, 1);
@ -374,7 +412,9 @@ test_bus_own_name (void)
*/ */
data.expect_null_connection = FALSE; data.expect_null_connection = FALSE;
g_bus_unown_name (id); 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_bus_acquired, ==, 1);
g_assert_cmpint (data.num_acquired, ==, 1); g_assert_cmpint (data.num_acquired, ==, 1);
g_assert_cmpint (data.num_free_func, ==, 4); 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_bus_acquired, ==, 0);
g_assert_cmpint (data.num_acquired, ==, 0); g_assert_cmpint (data.num_acquired, ==, 0);
g_assert_cmpint (data.num_lost, ==, 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_bus_acquired, ==, 1);
g_assert_cmpint (data.num_acquired, ==, 0); g_assert_cmpint (data.num_acquired, ==, 0);
g_assert_cmpint (data.num_lost, ==, 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_bus_acquired, ==, 1);
g_assert_cmpint (data.num_acquired, ==, 1); g_assert_cmpint (data.num_acquired, ==, 1);
g_assert_cmpint (data.num_lost, ==, 0); 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_bus_acquired, ==, 0);
g_assert_cmpint (data2.num_acquired, ==, 0); g_assert_cmpint (data2.num_acquired, ==, 0);
g_assert_cmpint (data2.num_lost, ==, 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_bus_acquired, ==, 0);
g_assert_cmpint (data2.num_acquired, ==, 0); g_assert_cmpint (data2.num_acquired, ==, 0);
g_assert_cmpint (data2.num_lost, ==, 1); g_assert_cmpint (data2.num_lost, ==, 1);
g_bus_unown_name (id2); 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_bus_acquired, ==, 0);
g_assert_cmpint (data2.num_acquired, ==, 0); g_assert_cmpint (data2.num_acquired, ==, 0);
g_assert_cmpint (data2.num_lost, ==, 1); 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 (data.num_lost, ==, 0);
g_assert_cmpint (data2.num_acquired, ==, 0); g_assert_cmpint (data2.num_acquired, ==, 0);
g_assert_cmpint (data2.num_lost, ==, 0); g_assert_cmpint (data2.num_lost, ==, 0);
/* wait for handlers for both owner and owner2 to fire */ /* wait for handlers for both owner and owner2 to fire */
while (data.num_lost == 0 || data2.num_acquired == 0) 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_acquired, ==, 1);
g_assert_cmpint (data.num_lost, ==, 1); g_assert_cmpint (data.num_lost, ==, 1);
g_assert_cmpint (data2.num_acquired, ==, 1); g_assert_cmpint (data2.num_acquired, ==, 1);
g_assert_cmpint (data2.num_lost, ==, 0); g_assert_cmpint (data2.num_lost, ==, 0);
g_assert_cmpint (data2.num_bus_acquired, ==, 0); g_assert_cmpint (data2.num_bus_acquired, ==, 0);
/* ok, make owner2 release the name - then wait for owner to automagically reacquire it */ /* ok, make owner2 release the name - then wait for owner to automagically reacquire it */
g_bus_unown_name (id2); g_bus_unown_name (id2);
g_main_loop_run (loop); while (data.num_acquired < 2 || data2.num_free_func < 1)
g_main_loop_run (loop); g_main_context_iteration (main_context, TRUE);
g_assert_cmpint (data2.num_free_func, ==, 1); g_assert_cmpint (data2.num_free_func, ==, 1);
g_assert_cmpint (data.num_acquired, ==, 2); g_assert_cmpint (data.num_acquired, ==, 2);
g_assert_cmpint (data.num_lost, ==, 1); g_assert_cmpint (data.num_lost, ==, 1);
@ -474,11 +530,15 @@ test_bus_own_name (void)
data.expect_null_connection = TRUE; data.expect_null_connection = TRUE;
session_bus_stop (); session_bus_stop ();
while (data.num_lost != 2) 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_acquired, ==, 2);
g_assert_cmpint (data.num_lost, ==, 2); g_assert_cmpint (data.num_lost, ==, 2);
g_bus_unown_name (id); 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_assert_cmpint (data.num_free_func, ==, 5);
g_object_unref (c); g_object_unref (c);
@ -499,6 +559,7 @@ typedef struct
guint num_appeared; guint num_appeared;
guint num_vanished; guint num_vanished;
guint num_free_func; guint num_free_func;
GMainContext *main_context; /* (unowned), for the main test thread */
} WatchNameData; } WatchNameData;
typedef struct typedef struct
@ -520,8 +581,7 @@ static void
watch_name_data_free_func (WatchNameData *data) watch_name_data_free_func (WatchNameData *data)
{ {
data->num_free_func++; data->num_free_func++;
g_main_loop_quit (loop); g_main_context_wakeup (data->main_context);
g_main_context_wakeup (g_main_loop_get_context (loop));
} }
static void static void
@ -538,8 +598,7 @@ w_name_acquired_handler (GDBusConnection *connection,
{ {
OwnNameData *data = user_data; OwnNameData *data = user_data;
data->num_acquired += 1; data->num_acquired += 1;
g_main_loop_quit (loop); g_main_context_wakeup (data->main_context);
g_main_context_wakeup (g_main_loop_get_context (loop));
} }
static void static void
@ -549,8 +608,7 @@ w_name_lost_handler (GDBusConnection *connection,
{ {
OwnNameData *data = user_data; OwnNameData *data = user_data;
data->num_lost += 1; data->num_lost += 1;
g_main_loop_quit (loop); g_main_context_wakeup (data->main_context);
g_main_context_wakeup (g_main_loop_get_context (loop));
} }
static void static void
@ -571,8 +629,7 @@ name_appeared_handler (GDBusConnection *connection,
g_dbus_connection_set_exit_on_close (connection, FALSE); g_dbus_connection_set_exit_on_close (connection, FALSE);
} }
data->num_appeared += 1; data->num_appeared += 1;
g_main_loop_quit (loop); g_main_context_wakeup (data->main_context);
g_main_context_wakeup (g_main_loop_get_context (loop));
} }
static void static void
@ -592,8 +649,7 @@ name_vanished_handler (GDBusConnection *connection,
g_dbus_connection_set_exit_on_close (connection, FALSE); g_dbus_connection_set_exit_on_close (connection, FALSE);
} }
data->num_vanished += 1; data->num_vanished += 1;
g_main_loop_quit (loop); g_main_context_wakeup (data->main_context);
g_main_context_wakeup (g_main_loop_get_context (loop));
} }
typedef struct typedef struct
@ -640,6 +696,7 @@ stop_service (GDBusConnection *connection,
GError *error = NULL; GError *error = NULL;
GDBusProxy *proxy = NULL; GDBusProxy *proxy = NULL;
GVariant *result = NULL; GVariant *result = NULL;
GMainContext *main_context = NULL; /* use the global default for now */
data->num_vanished = 0; data->num_vanished = 0;
@ -665,7 +722,7 @@ stop_service (GDBusConnection *connection,
if (result) if (result)
g_variant_unref (result); g_variant_unref (result);
while (data->num_vanished == 0) while (data->num_vanished == 0)
g_main_loop_run (loop); g_main_context_iteration (main_context, TRUE);
} }
static void static void
@ -678,6 +735,7 @@ test_bus_watch_name (gconstpointer d)
GDBusConnection *connection; GDBusConnection *connection;
const WatchNameTest *watch_name_test; const WatchNameTest *watch_name_test;
const gchar *name; const gchar *name;
GMainContext *main_context = NULL; /* use the global default for now */
watch_name_test = (WatchNameTest *) d; watch_name_test = (WatchNameTest *) d;
@ -699,6 +757,7 @@ test_bus_watch_name (gconstpointer d)
data.num_appeared = 0; data.num_appeared = 0;
data.num_vanished = 0; data.num_vanished = 0;
data.expect_null_connection = TRUE; data.expect_null_connection = TRUE;
data.main_context = main_context;
id = g_bus_watch_name (G_BUS_TYPE_SESSION, id = g_bus_watch_name (G_BUS_TYPE_SESSION,
name, name,
watch_name_test->watcher_flags, watch_name_test->watcher_flags,
@ -708,7 +767,10 @@ test_bus_watch_name (gconstpointer d)
(GDestroyNotify) watch_name_data_free_func); (GDestroyNotify) watch_name_data_free_func);
g_assert_cmpint (data.num_appeared, ==, 0); g_assert_cmpint (data.num_appeared, ==, 0);
g_assert_cmpint (data.num_vanished, ==, 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_appeared, ==, 0);
g_assert_cmpint (data.num_vanished, ==, 1); g_assert_cmpint (data.num_vanished, ==, 1);
g_bus_unwatch_name (id); g_bus_unwatch_name (id);
@ -726,6 +788,7 @@ test_bus_watch_name (gconstpointer d)
own_data.num_acquired = 0; own_data.num_acquired = 0;
own_data.num_lost = 0; own_data.num_lost = 0;
data.expect_null_connection = FALSE; data.expect_null_connection = FALSE;
own_data.main_context = main_context;
owner_id = g_bus_own_name (G_BUS_TYPE_SESSION, owner_id = g_bus_own_name (G_BUS_TYPE_SESSION,
name, name,
G_BUS_NAME_OWNER_FLAGS_NONE, G_BUS_NAME_OWNER_FLAGS_NONE,
@ -734,7 +797,10 @@ test_bus_watch_name (gconstpointer d)
w_name_lost_handler, w_name_lost_handler,
&own_data, &own_data,
(GDestroyNotify) own_name_data_free_func); (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_acquired, ==, 1);
g_assert_cmpint (own_data.num_lost, ==, 0); 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_appeared, ==, 0);
g_assert_cmpint (data.num_vanished, ==, 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_appeared, ==, 1);
g_assert_cmpint (data.num_vanished, ==, 0); g_assert_cmpint (data.num_vanished, ==, 0);
@ -780,7 +849,9 @@ test_bus_watch_name (gconstpointer d)
/* unown the name */ /* unown the name */
g_bus_unown_name (owner_id); 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_acquired, ==, 1);
g_assert_cmpint (own_data.num_free_func, ==, 1); g_assert_cmpint (own_data.num_free_func, ==, 1);
own_data.num_free_func = 0; 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_appeared, ==, 0);
g_assert_cmpint (data.num_vanished, ==, 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) if (watch_name_test->existing_service)
{ {
g_assert_cmpint (data.num_appeared, ==, 1); 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_acquired = 0;
own_data.num_lost = 0; own_data.num_lost = 0;
own_data.expect_null_connection = FALSE; own_data.expect_null_connection = FALSE;
own_data.main_context = main_context;
owner_id = g_bus_own_name (G_BUS_TYPE_SESSION, owner_id = g_bus_own_name (G_BUS_TYPE_SESSION,
name, name,
G_BUS_NAME_OWNER_FLAGS_NONE, G_BUS_NAME_OWNER_FLAGS_NONE,
@ -844,8 +919,10 @@ test_bus_watch_name (gconstpointer d)
w_name_lost_handler, w_name_lost_handler,
&own_data, &own_data,
(GDestroyNotify) own_name_data_free_func); (GDestroyNotify) own_name_data_free_func);
while (own_data.num_acquired == 0 || data.num_appeared == 0) 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_acquired, ==, 1);
g_assert_cmpint (own_data.num_lost, ==, 0); g_assert_cmpint (own_data.num_lost, ==, 0);
g_assert_cmpint (data.num_appeared, ==, 1); g_assert_cmpint (data.num_appeared, ==, 1);
@ -865,7 +942,8 @@ test_bus_watch_name (gconstpointer d)
session_bus_stop (); session_bus_stop ();
if (!watch_name_test->existing_service) 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 (own_data.num_lost, ==, 1);
g_assert_cmpint (data.num_vanished, ==, 2); g_assert_cmpint (data.num_vanished, ==, 2);
} }
@ -880,7 +958,9 @@ test_bus_watch_name (gconstpointer d)
if (!watch_name_test->existing_service) if (!watch_name_test->existing_service)
{ {
g_bus_unown_name (owner_id); 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); g_assert_cmpint (own_data.num_free_func, ==, 1);
} }
session_bus_down (); session_bus_down ();
@ -1057,6 +1137,7 @@ watch_with_different_context (gboolean unwatch_early)
GDBusConnection *connection; GDBusConnection *connection;
GThread *watcher; GThread *watcher;
guint id; guint id;
GMainContext *main_context = NULL; /* use the global default for now */
session_bus_up (); session_bus_up ();
@ -1083,6 +1164,7 @@ watch_with_different_context (gboolean unwatch_early)
own_data.num_lost = 0; own_data.num_lost = 0;
own_data.num_free_func = 0; own_data.num_free_func = 0;
own_data.expect_null_connection = FALSE; own_data.expect_null_connection = FALSE;
own_data.main_context = main_context;
// Own the name to avoid direct name vanished in watcher thread // Own the name to avoid direct name vanished in watcher thread
id = g_bus_own_name_on_connection (connection, id = g_bus_own_name_on_connection (connection,
"org.gtk.GDBus.Name1", "org.gtk.GDBus.Name1",
@ -1092,7 +1174,7 @@ watch_with_different_context (gboolean unwatch_early)
&own_data, &own_data,
(GDestroyNotify) own_name_data_free_func); (GDestroyNotify) own_name_data_free_func);
while (own_data.num_acquired == 0) 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_acquired, ==, 1);
g_assert_cmpint (own_data.num_lost, ==, 0); 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 // Iterate the loop until thread is waking us up
while (!thread_data.ended) while (!thread_data.ended)
g_main_context_iteration (NULL, TRUE); g_main_context_iteration (main_context, TRUE);
g_thread_join (watcher); g_thread_join (watcher);
g_bus_unown_name (id); g_bus_unown_name (id);
while (own_data.num_free_func == 0) 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_assert_cmpint (own_data.num_free_func, ==, 1);
g_mutex_clear (&thread_data.mutex); g_mutex_clear (&thread_data.mutex);
@ -1241,8 +1323,6 @@ main (int argc,
g_test_init (&argc, &argv, NULL); g_test_init (&argc, &argv, NULL);
loop = g_main_loop_new (NULL, FALSE);
g_test_dbus_unset (); g_test_dbus_unset ();
g_test_add_func ("/gdbus/validate-names", test_validate_names); 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); g_test_add_func ("/gdbus/escape-object-path", test_escape_object_path);
ret = g_test_run(); ret = g_test_run();
g_main_loop_unref (loop);
return ret; return ret;
} }