1 /* GLib testing framework examples and tests
3 * Copyright (C) 2008-2010 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 * Author: David Zeuthen <davidz@redhat.com>
25 #include "gdbus-tests.h"
27 /* all tests rely on a global connection */
28 static GDBusConnection *c = NULL;
30 /* all tests rely on a shared mainloop */
31 static GMainLoop *loop = NULL;
33 /* ---------------------------------------------------------------------------------------------------- */
34 /* Check that pending calls fail with G_IO_ERROR_CLOSED if the connection is closed */
35 /* See https://bugzilla.gnome.org/show_bug.cgi?id=660637 */
36 /* ---------------------------------------------------------------------------------------------------- */
39 sleep_cb (GObject *source_object,
43 GError **error = user_data;
46 result = g_dbus_proxy_call_finish (G_DBUS_PROXY (source_object), res, error);
47 g_assert (result == NULL);
48 g_main_loop_quit (loop);
52 on_timeout (gpointer user_data)
56 return FALSE; /* remove source */
60 test_connection_loss (void)
66 proxy = g_dbus_proxy_new_sync (c,
67 G_DBUS_PROXY_FLAGS_NONE,
68 NULL, /* GDBusInterfaceInfo */
69 "com.example.TestService", /* name */
70 "/com/example/TestObject", /* object path */
71 "com.example.Frob", /* interface */
72 NULL, /* GCancellable */
74 g_assert_no_error (error);
77 g_dbus_proxy_call (proxy,
79 g_variant_new ("(i)", 100 * 1000 /* msec */),
80 G_DBUS_CALL_FLAGS_NONE,
82 NULL, /* GCancellable */
86 /* Make sure we don't exit when the bus goes away */
87 g_dbus_connection_set_exit_on_close (c, FALSE);
89 /* Nuke the connection to the bus */
90 g_timeout_add (100 /* ms */, on_timeout, NULL);
92 g_main_loop_run (loop);
94 /* If we didn't act on connection-loss we'd be getting G_IO_ERROR_TIMEOUT
95 * generated locally. So if we get G_IO_ERROR_CLOSED it means that we
96 * are acting correctly on connection loss.
98 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED);
99 g_assert (!g_dbus_error_is_remote_error (error));
100 g_clear_error (&error);
102 g_object_unref (proxy);
105 /* ---------------------------------------------------------------------------------------------------- */
115 g_test_init (&argc, &argv, NULL);
117 /* all the tests rely on a shared main loop */
118 loop = g_main_loop_new (NULL, FALSE);
122 /* this is safe; testserver will exit once the bus goes away */
123 path = g_test_build_filename (G_TEST_BUILT, "gdbus-testserver", NULL);
124 g_assert (g_spawn_command_line_async (path, NULL));
127 /* wait for the service to come up */
130 /* Create the connection in the main thread */
132 c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
133 g_assert_no_error (error);
134 g_assert (c != NULL);
136 g_test_add_func ("/gdbus/connection-loss", test_connection_loss);