From 415a8d81f6fc52d7314517f36bd03f0cbad27dd5 Mon Sep 17 00:00:00 2001 From: Xavier Claessens Date: Wed, 18 Apr 2012 23:28:17 +0200 Subject: [PATCH] Use GTestDBus in all GDBus unit tests To make port easier, this rewrites dbus-sessionbus.c using a GTestDBus singleton internally. https://bugzilla.gnome.org/show_bug.cgi?id=672985 --- gio/tests/actions.c | 2 - gio/tests/gapplication.c | 23 +-- gio/tests/gdbus-auth.c | 5 - gio/tests/gdbus-bz627724.c | 6 +- gio/tests/gdbus-connection-loss.c | 13 +- gio/tests/gdbus-connection-slow.c | 7 +- gio/tests/gdbus-connection.c | 23 +-- gio/tests/gdbus-exit-on-close.c | 12 +- gio/tests/gdbus-export.c | 13 +- gio/tests/gdbus-introspection.c | 21 +-- gio/tests/gdbus-names.c | 14 +- gio/tests/gdbus-peer.c | 2 - gio/tests/gdbus-proxy-threads.c | 14 +- gio/tests/gdbus-proxy-well-known-name.c | 11 +- gio/tests/gdbus-proxy.c | 15 +- gio/tests/gdbus-sessionbus.c | 325 ++------------------------------ gio/tests/gdbus-sessionbus.h | 12 +- gio/tests/gdbus-test-codegen.c | 11 -- gio/tests/gdbus-tests.c | 106 ----------- gio/tests/gdbus-tests.h | 25 --- gio/tests/gdbus-threading.c | 11 -- gio/tests/gmenumodel.c | 3 - 22 files changed, 65 insertions(+), 609 deletions(-) diff --git a/gio/tests/actions.c b/gio/tests/actions.c index 148f656..f82515c 100644 --- a/gio/tests/actions.c +++ b/gio/tests/actions.c @@ -712,8 +712,6 @@ main (int argc, char **argv) g_type_init (); g_test_init (&argc, &argv, NULL); - g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_get_temporary_address (), TRUE); - g_test_add_func ("/actions/basic", test_basic); g_test_add_func ("/actions/simplegroup", test_simple_group); g_test_add_func ("/actions/stateful", test_stateful); diff --git a/gio/tests/gapplication.c b/gio/tests/gapplication.c index a1faa5c..4dc239f 100644 --- a/gio/tests/gapplication.c +++ b/gio/tests/gapplication.c @@ -116,11 +116,8 @@ basic (void) g_main_loop_run (main_loop); - session_bus_down (); - _g_object_wait_for_single_ref_do (c); g_object_unref (c); - - g_assert (g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL) == NULL); + session_bus_down (); } @@ -252,14 +249,11 @@ properties (void) g_application_quit (G_APPLICATION (app)); + g_object_unref (c); g_object_unref (app); g_free (id); session_bus_down (); - _g_object_wait_for_single_ref (c); - g_object_unref (c); - - g_assert (g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL) == NULL); } static void @@ -357,15 +351,11 @@ test_quit (void) g_signal_connect (app, "activate", G_CALLBACK (quit_activate), NULL); g_application_run (app, 1, argv); g_object_unref (app); + g_object_unref (c); g_assert (quit_activated); session_bus_down (); - - _g_object_wait_for_single_ref (c); - g_object_unref (c); - - g_assert (g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL) == NULL); } static void @@ -423,12 +413,7 @@ main (int argc, char **argv) g_test_init (&argc, &argv, NULL); - /* all the tests use a session bus with a well-known address - * that we can bring up and down using session_bus_up() and - * session_bus_down(). - */ - g_unsetenv ("DISPLAY"); - g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_get_temporary_address (), TRUE); + g_test_dbus_unset (); g_test_add_func ("/gapplication/no-dbus", test_nodbus); g_test_add_func ("/gapplication/basic", basic); diff --git a/gio/tests/gdbus-auth.c b/gio/tests/gdbus-auth.c index 3b24b09..737770e 100644 --- a/gio/tests/gdbus-auth.c +++ b/gio/tests/gdbus-auth.c @@ -269,11 +269,6 @@ main (int argc, g_type_init (); g_test_init (&argc, &argv, NULL); - /* all the tests use a session bus with a well-known address that we can bring up and down - * using session_bus_up() and session_bus_down(). - */ - g_unsetenv ("DISPLAY"); - g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_get_temporary_address (), TRUE); session_bus_up (); g_test_add_func ("/gdbus/auth/client/EXTERNAL", auth_client_external); diff --git a/gio/tests/gdbus-bz627724.c b/gio/tests/gdbus-bz627724.c index 03d196e..b9ea9c9 100644 --- a/gio/tests/gdbus-bz627724.c +++ b/gio/tests/gdbus-bz627724.c @@ -83,11 +83,7 @@ main (int argc, g_type_init (); g_test_init (&argc, &argv, NULL); - /* all the tests use a session bus with a well-known address that we can bring up and down - * using session_bus_up() and session_bus_down(). - */ - g_unsetenv ("DISPLAY"); - g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_get_temporary_address (), TRUE); + g_test_dbus_unset (); g_test_add_func ("/gdbus/bz627724", test_bz627724); return g_test_run(); diff --git a/gio/tests/gdbus-connection-loss.c b/gio/tests/gdbus-connection-loss.c index 3fdd367..504383e 100644 --- a/gio/tests/gdbus-connection-loss.c +++ b/gio/tests/gdbus-connection-loss.c @@ -54,7 +54,7 @@ static gboolean on_timeout (gpointer user_data) { /* tear down bus */ - session_bus_down (); + session_bus_stop (); return FALSE; /* remove source */ } @@ -119,19 +119,8 @@ main (int argc, /* all the tests rely on a shared main loop */ loop = g_main_loop_new (NULL, FALSE); - /* all the tests use a session bus with a well-known address that we can bring up and down - * using session_bus_up() and session_bus_down(). - */ - g_unsetenv ("DISPLAY"); - g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_get_temporary_address (), TRUE); - session_bus_up (); - /* TODO: wait a bit for the bus to come up.. ideally session_bus_up() won't return - * until one can connect to the bus but that's not how things work right now - */ - usleep (500 * 1000); - /* this is safe; testserver will exit once the bus goes away */ g_assert (g_spawn_command_line_async (SRCDIR "/gdbus-testserver.py", NULL)); diff --git a/gio/tests/gdbus-connection-slow.c b/gio/tests/gdbus-connection-slow.c index 39a9eb8..c9f4411 100644 --- a/gio/tests/gdbus-connection-slow.c +++ b/gio/tests/gdbus-connection-slow.c @@ -105,7 +105,6 @@ test_connection_flush (void) } g_dbus_connection_signal_unsubscribe (connection, signal_handler_id); - _g_object_wait_for_single_ref (connection); g_object_unref (connection); session_bus_down (); @@ -202,11 +201,7 @@ main (int argc, /* all the tests rely on a shared main loop */ loop = g_main_loop_new (NULL, FALSE); - /* all the tests use a session bus with a well-known address that we can bring up and down - * using session_bus_up() and session_bus_down(). - */ - g_unsetenv ("DISPLAY"); - g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_get_temporary_address (), TRUE); + g_test_dbus_unset (); g_test_add_func ("/gdbus/connection/flush", test_connection_flush); g_test_add_func ("/gdbus/connection/large_message", test_connection_large_message); diff --git a/gio/tests/gdbus-connection.c b/gio/tests/gdbus-connection.c index 94c4d9d..6753b08 100644 --- a/gio/tests/gdbus-connection.c +++ b/gio/tests/gdbus-connection.c @@ -267,12 +267,12 @@ test_connection_life_cycle (void) */ g_assert (!g_dbus_connection_is_closed (c)); g_dbus_connection_set_exit_on_close (c, FALSE); - session_bus_down (); + session_bus_stop (); _g_assert_signal_received (c, "closed"); g_assert (g_dbus_connection_is_closed (c)); - - _g_object_wait_for_single_ref (c); g_object_unref (c); + + session_bus_down (); } /* ---------------------------------------------------------------------------------------------------- */ @@ -469,7 +469,7 @@ test_connection_send (void) * Check that we get an error when sending to a connection that is disconnected. */ g_dbus_connection_set_exit_on_close (c, FALSE); - session_bus_down (); + session_bus_stop (); _g_assert_signal_received (c, "closed"); g_assert (g_dbus_connection_is_closed (c)); @@ -486,8 +486,9 @@ test_connection_send (void) NULL); g_main_loop_run (loop); - _g_object_wait_for_single_ref (c); g_object_unref (c); + + session_bus_down (); } /* ---------------------------------------------------------------------------------------------------- */ @@ -705,10 +706,6 @@ test_connection_signals (void) g_dbus_connection_signal_unsubscribe (c1, s3); g_dbus_connection_signal_unsubscribe (c1, s1b); - _g_object_wait_for_single_ref (c1); - _g_object_wait_for_single_ref (c2); - _g_object_wait_for_single_ref (c3); - g_object_unref (c1); g_object_unref (c2); g_object_unref (c3); @@ -992,7 +989,6 @@ test_connection_filter (void) g_dbus_connection_remove_filter (c, filter_id); - _g_object_wait_for_single_ref (c); g_object_unref (c); g_object_unref (m); @@ -1046,7 +1042,6 @@ test_connection_basic (void) g_free (name); g_free (guid); - _g_object_wait_for_single_ref (connection); g_object_unref (connection); session_bus_down (); @@ -1064,11 +1059,7 @@ main (int argc, /* all the tests rely on a shared main loop */ loop = g_main_loop_new (NULL, FALSE); - /* all the tests use a session bus with a well-known address that we can bring up and down - * using session_bus_up() and session_bus_down(). - */ - g_unsetenv ("DISPLAY"); - g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_get_temporary_address (), TRUE); + g_test_dbus_unset (); g_test_add_func ("/gdbus/connection/basic", test_connection_basic); g_test_add_func ("/gdbus/connection/life-cycle", test_connection_life_cycle); diff --git a/gio/tests/gdbus-exit-on-close.c b/gio/tests/gdbus-exit-on-close.c index 14d3a8c..f97e5da 100644 --- a/gio/tests/gdbus-exit-on-close.c +++ b/gio/tests/gdbus-exit-on-close.c @@ -117,11 +117,7 @@ test_exit_on_close (gconstpointer test_data) /* all the tests rely on a shared main loop */ loop = g_main_loop_new (NULL, FALSE); - /* all the tests use a session bus with a well-known address that we can bring up and down - * using session_bus_up() and session_bus_down(). - */ - g_unsetenv ("DISPLAY"); - g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_get_temporary_address (), TRUE); + g_test_dbus_unset (); if (g_test_verbose ()) silence = 0; @@ -173,12 +169,16 @@ test_exit_on_close (gconstpointer test_data) } else { - session_bus_down (); + session_bus_stop (); } g_main_loop_run (loop); /* this is only reached when we turn off exit-on-close */ g_main_loop_unref (loop); + g_object_unref (c); + + session_bus_down (); + exit (0); } diff --git a/gio/tests/gdbus-export.c b/gio/tests/gdbus-export.c index 21e9fda..e015ed9 100644 --- a/gio/tests/gdbus-export.c +++ b/gio/tests/gdbus-export.c @@ -1341,7 +1341,7 @@ test_object_registration (void) /* To prevent from exiting and attaching a D-Bus tool like D-Feet; uncomment: */ #if 0 - g_debug ("Point D-feet or other tool at: %s", session_bus_get_temporary_address()); + g_debug ("Point D-feet or other tool at: %s", g_test_dbus_get_temporary_address()); g_main_loop_run (loop); #endif @@ -1537,19 +1537,8 @@ main (int argc, /* all the tests rely on a shared main loop */ loop = g_main_loop_new (NULL, FALSE); - /* all the tests use a session bus with a well-known address that we can bring up and down - * using session_bus_up() and session_bus_down(). - */ - g_unsetenv ("DISPLAY"); - g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_get_temporary_address (), TRUE); - session_bus_up (); - /* TODO: wait a bit for the bus to come up.. ideally session_bus_up() won't return - * until one can connect to the bus but that's not how things work right now - */ - usleep (500 * 1000); - g_test_add_func ("/gdbus/object-registration", test_object_registration); g_test_add_func ("/gdbus/registered-interfaces", test_registered_interfaces); diff --git a/gio/tests/gdbus-introspection.c b/gio/tests/gdbus-introspection.c index 8a3fed2..17a1870 100644 --- a/gio/tests/gdbus-introspection.c +++ b/gio/tests/gdbus-introspection.c @@ -108,13 +108,6 @@ test_introspection_parser (void) GDBusConnection *connection; GError *error; - session_bus_up (); - - /* TODO: wait a bit for the bus to come up.. ideally session_bus_up() won't return - * until one can connect to the bus but that's not how things work right now - */ - usleep (500 * 1000); - error = NULL; connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, @@ -308,17 +301,15 @@ int main (int argc, char *argv[]) { + gint ret; + g_type_init (); g_test_init (&argc, &argv, NULL); /* all the tests rely on a shared main loop */ loop = g_main_loop_new (NULL, FALSE); - /* all the tests use a session bus with a well-known address that we can bring up and down - * using session_bus_up() and session_bus_down(). - */ - g_unsetenv ("DISPLAY"); - g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_get_temporary_address (), TRUE); + session_bus_up (); g_test_add_func ("/gdbus/introspection-parser", test_introspection_parser); g_test_add_func ("/gdbus/introspection-generate", test_generate); @@ -328,5 +319,9 @@ main (int argc, g_test_add_func ("/gdbus/introspection-extra-data", test_extra_data); #endif - return g_test_run(); + ret = g_test_run (); + + session_bus_down (); + + return ret; } diff --git a/gio/tests/gdbus-names.c b/gio/tests/gdbus-names.c index 4af7ecb..246a261 100644 --- a/gio/tests/gdbus-names.c +++ b/gio/tests/gdbus-names.c @@ -462,7 +462,7 @@ test_bus_own_name (void) * */ data.expect_null_connection = TRUE; - session_bus_down (); + session_bus_stop (); while (data.num_lost != 2) g_main_loop_run (loop); g_assert_cmpint (data.num_acquired, ==, 2); @@ -470,9 +470,10 @@ test_bus_own_name (void) g_bus_unown_name (id); g_assert_cmpint (data.num_free_func, ==, 5); - _g_object_wait_for_single_ref (c); g_object_unref (c); g_object_unref (c2); + + session_bus_down (); } /* ---------------------------------------------------------------------------------------------------- */ @@ -696,7 +697,7 @@ test_bus_watch_name (void) * Nuke the bus and check that the name vanishes and is lost. */ data.expect_null_connection = TRUE; - session_bus_down (); + session_bus_stop (); g_main_loop_run (loop); g_assert_cmpint (data.num_lost, ==, 1); g_assert_cmpint (data.num_vanished, ==, 2); @@ -707,6 +708,7 @@ test_bus_watch_name (void) g_bus_unown_name (owner_id); g_assert_cmpint (data.num_free_func, ==, 2); + session_bus_down (); } /* ---------------------------------------------------------------------------------------------------- */ @@ -772,11 +774,7 @@ main (int argc, loop = g_main_loop_new (NULL, FALSE); - /* all the tests use a session bus with a well-known address that we can bring up and down - * using session_bus_up() and session_bus_down(). - */ - g_unsetenv ("DISPLAY"); - g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_get_temporary_address (), TRUE); + g_test_dbus_unset (); g_test_add_func ("/gdbus/validate-names", test_validate_names); g_test_add_func ("/gdbus/bus-own-name", test_bus_own_name); diff --git a/gio/tests/gdbus-peer.c b/gio/tests/gdbus-peer.c index 926010a..934ebda 100644 --- a/gio/tests/gdbus-peer.c +++ b/gio/tests/gdbus-peer.c @@ -1804,8 +1804,6 @@ main (int argc, g_type_init (); g_test_init (&argc, &argv, NULL); - g_unsetenv ("DBUS_SESSION_BUS_ADDRESS"); - introspection_data = g_dbus_node_info_new_for_xml (test_interface_introspection_xml, NULL); g_assert (introspection_data != NULL); test_interface_introspection_data = introspection_data->interfaces[0]; diff --git a/gio/tests/gdbus-proxy-threads.c b/gio/tests/gdbus-proxy-threads.c index 325c68b..14131ee 100644 --- a/gio/tests/gdbus-proxy-threads.c +++ b/gio/tests/gdbus-proxy-threads.c @@ -205,11 +205,6 @@ test_proxy (void) session_bus_up (); - /* TODO: wait a bit for the bus to come up.. ideally session_bus_up() won't return - * until one can connect to the bus but that's not how things work right now - */ - usleep (500 * 1000); - loop = g_main_loop_new (NULL, TRUE); connection = g_bus_get_sync (G_BUS_TYPE_SESSION, @@ -234,18 +229,21 @@ test_proxy (void) g_object_unref (connection); g_main_loop_unref (loop); + + /* TODO: should call session_bus_down() but that requires waiting + * for all the oustanding method calls to complete... + */ } int main (int argc, char *argv[]) { - g_unsetenv ("DISPLAY"); - g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_get_temporary_address (), TRUE); - g_type_init (); g_test_init (&argc, &argv, NULL); + g_test_dbus_unset (); + g_test_add_func ("/gdbus/proxy/vs-threads", test_proxy); return g_test_run(); diff --git a/gio/tests/gdbus-proxy-well-known-name.c b/gio/tests/gdbus-proxy-well-known-name.c index a599ef6..b2d204d 100644 --- a/gio/tests/gdbus-proxy-well-known-name.c +++ b/gio/tests/gdbus-proxy-well-known-name.c @@ -63,11 +63,6 @@ test_proxy_well_known_name (void) session_bus_up (); - /* TODO: wait a bit for the bus to come up.. ideally session_bus_up() won't return - * until one can connect to the bus but that's not how things work right now - */ - usleep (500 * 1000); - error = NULL; c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error); g_assert_no_error (error); @@ -270,11 +265,7 @@ main (int argc, /* all the tests rely on a shared main loop */ loop = g_main_loop_new (NULL, FALSE); - /* all the tests use a session bus with a well-known address that we can bring up and down - * using session_bus_up() and session_bus_down(). - */ - g_unsetenv ("DISPLAY"); - g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_get_temporary_address (), TRUE); + g_test_dbus_unset (); g_test_add_func ("/gdbus/proxy-well-known-name", test_proxy_well_known_name); diff --git a/gio/tests/gdbus-proxy.c b/gio/tests/gdbus-proxy.c index ba750ff..31ee1f0 100644 --- a/gio/tests/gdbus-proxy.c +++ b/gio/tests/gdbus-proxy.c @@ -739,13 +739,6 @@ test_proxy (void) GDBusConnection *connection; GError *error; - session_bus_up (); - - /* TODO: wait a bit for the bus to come up.. ideally session_bus_up() won't return - * until one can connect to the bus but that's not how things work right now - */ - usleep (500 * 1000); - error = NULL; connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, @@ -894,11 +887,7 @@ main (int argc, /* all the tests rely on a shared main loop */ loop = g_main_loop_new (NULL, FALSE); - /* all the tests use a session bus with a well-known address that we can bring up and down - * using session_bus_up() and session_bus_down(). - */ - g_unsetenv ("DISPLAY"); - g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_get_temporary_address (), TRUE); + session_bus_up (); g_test_add_func ("/gdbus/proxy", test_proxy); g_test_add_func ("/gdbus/proxy/no-properties", test_no_properties); @@ -908,5 +897,7 @@ main (int argc, ret = g_test_run(); g_dbus_node_info_unref (introspection_data); + + session_bus_down (); return ret; } diff --git a/gio/tests/gdbus-sessionbus.c b/gio/tests/gdbus-sessionbus.c index f39583c..68c4449 100644 --- a/gio/tests/gdbus-sessionbus.c +++ b/gio/tests/gdbus-sessionbus.c @@ -1,6 +1,6 @@ /* GLib testing framework examples and tests * - * Copyright (C) 2008-2010 Red Hat, Inc. + * Copyright (C) 2012 Collabora Ltd. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -17,328 +17,33 @@ * Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, MA 02111-1307, USA. * - * Author: David Zeuthen + * Author: Xavier Claessens */ -#include -#include -#include -#include -#include -#include -#include -#include - -#include - #include "gdbus-sessionbus.h" -/* ---------------------------------------------------------------------------------------------------- */ -/* Utilities for bringing up and tearing down session message bus instances */ - -static void -watch_parent (gint fd) -{ - GPollFD fds[1]; - gint num_events; - gchar buf[512]; - gssize bytes_read; - GArray *buses_to_kill_array; - - fds[0].fd = fd; - fds[0].events = G_IO_HUP | G_IO_IN; - fds[0].revents = 0; - - buses_to_kill_array = g_array_new (FALSE, TRUE, sizeof (guint)); - - do - { - guint pid; - guint n; - - num_events = g_poll (fds, 1, -1); - if (num_events == 0) - continue; - - if (fds[0].revents == G_IO_HUP) - { - for (n = 0; n < buses_to_kill_array->len; n++) - { - pid = g_array_index (buses_to_kill_array, guint, n); - g_print ("cleaning up bus with pid %d\n", pid); - kill (pid, SIGTERM); - } - g_array_free (buses_to_kill_array, TRUE); - exit (0); - } - - //g_debug ("data from parent"); - - memset (buf, '\0', sizeof buf); - again: - bytes_read = read (fds[0].fd, buf, sizeof buf); - if (bytes_read < 0 && (errno == EAGAIN || errno == EINTR)) - goto again; - - if (sscanf (buf, "add %d\n", &pid) == 1) - { - g_array_append_val (buses_to_kill_array, pid); - } - else if (sscanf (buf, "remove %d\n", &pid) == 1) - { - for (n = 0; n < buses_to_kill_array->len; n++) - { - if (g_array_index (buses_to_kill_array, guint, n) == pid) - { - g_array_remove_index (buses_to_kill_array, n); - pid = 0; - break; - } - } - if (pid != 0) - { - g_warning ("unknown pid %d to remove", pid); - } - } - else - { - g_warning ("unknown command from parent '%s'", buf); - } - } - while (TRUE); - -} - -static GHashTable *session_bus_address_to_pid = NULL; -static gint pipe_fds[2]; - -const gchar * -session_bus_up_with_address (const gchar *given_address) -{ - gchar *address; - int stdout_fd; - GError *error; - gchar *argv[] = {"dbus-daemon", "--print-address", "--config-file=foo", NULL}; - GPid pid; - gchar buf[512]; - ssize_t bytes_read; - gchar *config_file_name; - gint config_file_fd; - GString *config_file_contents; - - address = NULL; - error = NULL; - config_file_name = NULL; - config_file_fd = -1; - argv[2] = NULL; - - config_file_fd = g_file_open_tmp ("g-dbus-tests-XXXXXX", - &config_file_name, - &error); - if (config_file_fd < 0) - { - g_warning ("Error creating temporary config file: %s", error->message); - g_error_free (error); - goto out; - } - - config_file_contents = g_string_new (NULL); - g_string_append (config_file_contents, "\n"); - g_string_append (config_file_contents, " session\n"); - g_string_append_printf (config_file_contents, " %s\n", given_address); - g_string_append (config_file_contents, - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n"); - g_string_append (config_file_contents, "\n"); - - if (write (config_file_fd, config_file_contents->str, config_file_contents->len) != (gssize) config_file_contents->len) - { - g_warning ("Error writing %d bytes to config file: %m", (gint) config_file_contents->len); - g_string_free (config_file_contents, TRUE); - goto out; - } - g_string_free (config_file_contents, TRUE); - - argv[2] = g_strdup_printf ("--config-file=%s", config_file_name); - - if (session_bus_address_to_pid == NULL) - { - /* keep a mapping from session bus address to the pid */ - session_bus_address_to_pid = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL); - - /* fork a child to clean up session buses when we are killed */ - if (pipe (pipe_fds) != 0) - { - g_warning ("pipe() failed: %m"); - g_assert_not_reached (); - } - switch (fork ()) - { - case -1: - g_warning ("fork() failed: %m"); - g_assert_not_reached (); - break; - - case 0: - /* child */ - close (pipe_fds[1]); - watch_parent (pipe_fds[0]); - break; - - default: - /* parent */ - close (pipe_fds[0]); - break; - } - - //atexit (cleanup_session_buses); - /* TODO: need to handle the cases where we crash */ - } - else - { - /* check if we already have a bus running for this address */ - if (g_hash_table_lookup (session_bus_address_to_pid, given_address) != NULL) - { - g_warning ("Already have a bus instance for the given address %s", given_address); - goto out; - } - } - - if (!g_spawn_async_with_pipes (NULL, - argv, - NULL, - G_SPAWN_SEARCH_PATH, - NULL, - NULL, - &pid, - NULL, - &stdout_fd, - NULL, - &error)) - { - g_warning ("Error spawning dbus-daemon: %s", error->message); - g_error_free (error); - goto out; - } - - memset (buf, '\0', sizeof buf); - again: - bytes_read = read (stdout_fd, buf, sizeof buf); - if (bytes_read < 0 && (errno == EAGAIN || errno == EINTR)) - goto again; - close (stdout_fd); - - if (bytes_read == 0 || bytes_read == sizeof buf) - { - g_warning ("Error reading address from dbus daemon, %d bytes read", (gint) bytes_read); - kill (SIGTERM, pid); - goto out; - } - - address = g_strdup (buf); - g_strstrip (address); - - /* write the pid to the child so it can kill it when we die */ - g_snprintf (buf, sizeof buf, "add %d\n", (guint) pid); - write (pipe_fds[1], buf, strlen (buf)); - - /* start dbus-monitor */ - if (g_getenv ("G_DBUS_MONITOR") != NULL) - { - g_spawn_command_line_async ("dbus-monitor --session", NULL); - usleep (500 * 1000); - } - - g_hash_table_insert (session_bus_address_to_pid, address, GUINT_TO_POINTER (pid)); - - out: - if (config_file_fd > 0) - { - if (close (config_file_fd) != 0) - { - g_warning ("Error closing fd for config file %s: %m", config_file_name); - } - g_assert (config_file_name != NULL); - if (unlink (config_file_name) != 0) - { - g_warning ("Error unlinking config file %s: %m", config_file_name); - } - } - g_free (argv[2]); - g_free (config_file_name); - return address; -} +static GTestDBus *singleton = NULL; void -session_bus_down_with_address (const gchar *address) -{ - gpointer value; - GPid pid; - gchar buf[512]; - - g_assert (address != NULL); - g_assert (session_bus_address_to_pid != NULL); - - value = g_hash_table_lookup (session_bus_address_to_pid, address); - g_assert (value != NULL); - - pid = GPOINTER_TO_UINT (g_hash_table_lookup (session_bus_address_to_pid, address)); - - kill (pid, SIGTERM); - - /* write the pid to the child so it won't kill it when we die */ - g_snprintf (buf, sizeof buf, "remove %d\n", (guint) pid); - write (pipe_fds[1], buf, strlen (buf)); - - g_hash_table_remove (session_bus_address_to_pid, address); -} - -static gchar *temporary_address = NULL; -static gchar *temporary_address_used_by_bus = NULL; - -const gchar * -session_bus_get_temporary_address (void) +session_bus_up (void) { - if (temporary_address == NULL) - { - temporary_address = g_strdup_printf ("unix:path=/tmp/g-dbus-tests-pid-%d", getpid ()); - } - - return temporary_address; + g_assert (singleton == NULL); + singleton = g_test_dbus_new (G_TEST_DBUS_NONE); + g_test_dbus_up (singleton); } -const gchar * -session_bus_up (void) +void +session_bus_stop (void) { - if (temporary_address_used_by_bus != NULL) - { - g_warning ("There is already a session bus up"); - goto out; - } - - temporary_address_used_by_bus = g_strdup (session_bus_up_with_address (session_bus_get_temporary_address ())); - - out: - return temporary_address_used_by_bus; + g_assert (singleton != NULL); + g_test_dbus_stop (singleton); } void session_bus_down (void) { - if (temporary_address_used_by_bus == NULL) - { - g_warning ("There is not a session bus up"); - } - else - { - session_bus_down_with_address (temporary_address_used_by_bus); - g_free (temporary_address_used_by_bus); - temporary_address_used_by_bus = NULL; - } + g_assert (singleton != NULL); + g_test_dbus_down (singleton); + g_clear_object (&singleton); } + diff --git a/gio/tests/gdbus-sessionbus.h b/gio/tests/gdbus-sessionbus.h index e107e06..7ef3abd 100644 --- a/gio/tests/gdbus-sessionbus.h +++ b/gio/tests/gdbus-sessionbus.h @@ -1,6 +1,6 @@ /* GLib testing framework examples and tests * - * Copyright (C) 2008-2009 Red Hat, Inc. + * Copyright (C) 2012 Collabora Ltd. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -17,7 +17,7 @@ * Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, MA 02111-1307, USA. * - * Author: David Zeuthen + * Author: Xavier Claessens */ #ifndef __SESSION_BUS_H__ @@ -27,11 +27,9 @@ G_BEGIN_DECLS -const gchar *session_bus_up_with_address (const gchar *given_address); -void session_bus_down_with_address (const gchar *address); -const gchar *session_bus_get_temporary_address (void); -const gchar *session_bus_up (void); -void session_bus_down (void); +void session_bus_up (void); +void session_bus_stop (void); +void session_bus_down (void); G_END_DECLS diff --git a/gio/tests/gdbus-test-codegen.c b/gio/tests/gdbus-test-codegen.c index 363aa85..5eb175f 100644 --- a/gio/tests/gdbus-test-codegen.c +++ b/gio/tests/gdbus-test-codegen.c @@ -2299,19 +2299,8 @@ main (int argc, g_type_init (); g_test_init (&argc, &argv, NULL); - /* all the tests use a session bus with a well-known address that we can bring up and down - * using session_bus_up() and session_bus_down(). - */ - g_unsetenv ("DISPLAY"); - g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_get_temporary_address (), TRUE); - session_bus_up (); - /* TODO: wait a bit for the bus to come up.. ideally session_bus_up() won't return - * until one can connect to the bus but that's not how things work right now - */ - usleep (500 * 1000); - g_test_add_func ("/gdbus/codegen/annotations", test_annotations); g_test_add_func ("/gdbus/codegen/interface_stability", test_interface_stability); g_test_add_func ("/gdbus/codegen/object-manager", test_object_manager); diff --git a/gio/tests/gdbus-tests.c b/gio/tests/gdbus-tests.c index 58b1e60..20ec171 100644 --- a/gio/tests/gdbus-tests.c +++ b/gio/tests/gdbus-tests.c @@ -157,109 +157,3 @@ _g_bus_get_priv (GBusType bus_type, } /* ---------------------------------------------------------------------------------------------------- */ - -#if 0 -/* toggle refs are not easy to use (maybe not even safe) when multiple - * threads are involved so implement this by busy-waiting for now - */ -gboolean -_g_object_wait_for_single_ref_do (gpointer object) -{ - guint num_ms_elapsed; - gboolean timed_out; - - timed_out = FALSE; - num_ms_elapsed = 0; - - while (TRUE) - { - if (G_OBJECT (object)->ref_count == 1) - goto out; - - if (num_ms_elapsed > 30000) - { - timed_out = TRUE; - goto out; - } - - usleep (10 * 1000); - num_ms_elapsed += 10; - } - - out: - return timed_out; -} - -#else - -typedef struct -{ - GMainLoop *loop; - gboolean timed_out; -} WaitSingleRefData; - -static gboolean -on_wait_single_ref_timeout (gpointer user_data) -{ - WaitSingleRefData *data = user_data; - data->timed_out = TRUE; - g_main_loop_quit (data->loop); - return TRUE; -} - -static void -on_wait_for_single_ref_toggled (gpointer user_data, - GObject *object, - gboolean is_last_ref) -{ - WaitSingleRefData *data = user_data; - g_main_loop_quit (data->loop); -} - -gboolean -_g_object_wait_for_single_ref_do (gpointer object) -{ - WaitSingleRefData data; - guint timeout_id; - - data.timed_out = FALSE; - - if (G_OBJECT (object)->ref_count == 1) - goto out; - - data.loop = g_main_loop_new (NULL, FALSE); - timeout_id = g_timeout_add (30 * 1000, - on_wait_single_ref_timeout, - &data); - - g_object_add_toggle_ref (G_OBJECT (object), - on_wait_for_single_ref_toggled, - &data); - /* the reference could have been removed between us checking the - * ref_count and the toggle ref being added - */ - if (G_OBJECT (object)->ref_count == 2) - goto single_ref_already; - - g_object_unref (object); - g_main_loop_run (data.loop); - g_object_ref (object); - -single_ref_already: - g_object_remove_toggle_ref (object, - on_wait_for_single_ref_toggled, - &data); - - g_source_remove (timeout_id); - g_main_loop_unref (data.loop); - - out: - if (data.timed_out) - { - g_printerr ("b ref_count is %d\n", G_OBJECT (object)->ref_count); - } - return data.timed_out; -} -#endif - -/* ---------------------------------------------------------------------------------------------------- */ diff --git a/gio/tests/gdbus-tests.h b/gio/tests/gdbus-tests.h index ffcc57c..b2782bf 100644 --- a/gio/tests/gdbus-tests.h +++ b/gio/tests/gdbus-tests.h @@ -116,31 +116,6 @@ GDBusConnection *_g_bus_get_priv (GBusType bus_type, GCancellable *cancellable, GError **error); - -#define _g_object_wait_for_single_ref(object) \ - do \ - { \ - if (!G_IS_OBJECT (object)) \ - { \ - g_assertion_message (G_LOG_DOMAIN, \ - __FILE__, \ - __LINE__, \ - G_STRFUNC, \ - "Not a GObject instance"); \ - } \ - if (_g_object_wait_for_single_ref_do (object)) \ - { \ - g_assertion_message (G_LOG_DOMAIN, \ - __FILE__, \ - __LINE__, \ - G_STRFUNC, \ - "Timed out waiting for single ref"); \ - } \ - } \ - while (FALSE) - -gboolean _g_object_wait_for_single_ref_do (gpointer object); - G_END_DECLS #endif /* __TESTS_H__ */ diff --git a/gio/tests/gdbus-threading.c b/gio/tests/gdbus-threading.c index b46af0b..c1fc97e 100644 --- a/gio/tests/gdbus-threading.c +++ b/gio/tests/gdbus-threading.c @@ -585,19 +585,8 @@ main (int argc, g_type_init (); g_test_init (&argc, &argv, NULL); - /* all the tests use a session bus with a well-known address that we can bring up and down - * using session_bus_up() and session_bus_down(). - */ - g_unsetenv ("DISPLAY"); - g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_get_temporary_address (), TRUE); - session_bus_up (); - /* TODO: wait a bit for the bus to come up.. ideally session_bus_up() won't return - * until one can connect to the bus but that's not how things work right now - */ - usleep (500 * 1000); - /* this is safe; testserver will exit once the bus goes away */ g_assert (g_spawn_command_line_async (SRCDIR "/gdbus-testserver.py", NULL)); diff --git a/gio/tests/gmenumodel.c b/gio/tests/gmenumodel.c index 3d8cefa..c882c49 100644 --- a/gio/tests/gmenumodel.c +++ b/gio/tests/gmenumodel.c @@ -1027,9 +1027,6 @@ main (int argc, char **argv) g_test_init (&argc, &argv, NULL); g_type_init (); - g_unsetenv ("DISPLAY"); - g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_get_temporary_address (), TRUE); - session_bus_up (); g_test_add_func ("/gmenu/equality", test_equality); -- 2.7.4