Merge branch '976-disable-assert-checks' into 'master'
[glib.git] / gio / tests / gdbus-connection-loss.c
blob9dbbeb2a47d456b82cfc9ec2876e64387b51f51f
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.1 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>
21 #include <gio/gio.h>
22 #include <unistd.h>
23 #include <string.h>
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 /* ---------------------------------------------------------------------------------------------------- */
38 static void
39 sleep_cb (GObject *source_object,
40 GAsyncResult *res,
41 gpointer user_data)
43 GError **error = user_data;
44 GVariant *result;
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);
51 static gboolean
52 on_timeout (gpointer user_data)
54 /* tear down bus */
55 session_bus_stop ();
56 return FALSE; /* remove source */
59 static void
60 test_connection_loss (void)
62 GDBusProxy *proxy;
63 GError *error;
65 error = NULL;
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 */
73 &error);
74 g_assert_no_error (error);
76 error = NULL;
77 g_dbus_proxy_call (proxy,
78 "Sleep",
79 g_variant_new ("(i)", 100 * 1000 /* msec */),
80 G_DBUS_CALL_FLAGS_NONE,
81 10 * 1000 /* msec */,
82 NULL, /* GCancellable */
83 sleep_cb,
84 &error);
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 /* ---------------------------------------------------------------------------------------------------- */
108 main (int argc,
109 char *argv[])
111 GError *error;
112 gint ret;
113 gchar *path;
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);
120 session_bus_up ();
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));
125 g_free (path);
127 ensure_gdbus_testserver_up ();
129 /* Create the connection in the main thread */
130 error = NULL;
131 c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
132 g_assert_no_error (error);
133 g_assert (c != NULL);
135 g_test_add_func ("/gdbus/connection-loss", test_connection_loss);
137 ret = g_test_run();
139 session_bus_down ();
141 g_object_unref (c);
142 g_main_loop_unref (loop);
144 return ret;