Add some more cases to the app-id unit tests
[glib.git] / gio / tests / gdbus-connection-slow.c
blob9c2f59a4b13ee8ce0b1fe84aec811b01c9ef8190
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>
21 #include <gio/gio.h>
22 #include <unistd.h>
23 #include <string.h>
25 #include <sys/types.h>
27 #include "gdbus-tests.h"
29 /* all tests rely on a shared mainloop */
30 static GMainLoop *loop = NULL;
32 /* ---------------------------------------------------------------------------------------------------- */
34 static void
35 test_connection_flush_signal_handler (GDBusConnection *connection,
36 const gchar *sender_name,
37 const gchar *object_path,
38 const gchar *interface_name,
39 const gchar *signal_name,
40 GVariant *parameters,
41 gpointer user_data)
43 g_main_loop_quit (loop);
46 static gboolean
47 test_connection_flush_on_timeout (gpointer user_data)
49 guint iteration = GPOINTER_TO_UINT (user_data);
50 g_printerr ("Timeout waiting 1000 msec on iteration %d\n", iteration);
51 g_assert_not_reached ();
52 return FALSE;
55 static void
56 test_connection_flush (void)
58 GDBusConnection *connection;
59 GError *error;
60 guint n;
61 guint signal_handler_id;
62 const gchar *flush_helper;
64 session_bus_up ();
66 error = NULL;
67 connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
68 g_assert_no_error (error);
69 g_assert (connection != NULL);
71 signal_handler_id = g_dbus_connection_signal_subscribe (connection,
72 NULL, /* sender */
73 "org.gtk.GDBus.FlushInterface",
74 "SomeSignal",
75 "/org/gtk/GDBus/FlushObject",
76 NULL,
77 G_DBUS_SIGNAL_FLAGS_NONE,
78 test_connection_flush_signal_handler,
79 NULL,
80 NULL);
81 g_assert_cmpint (signal_handler_id, !=, 0);
83 flush_helper = g_test_get_filename (G_TEST_BUILT, "gdbus-connection-flush-helper", NULL);
84 for (n = 0; n < 50; n++)
86 gboolean ret;
87 gint exit_status;
88 guint timeout_mainloop_id;
90 error = NULL;
91 ret = g_spawn_command_line_sync (flush_helper,
92 NULL, /* stdout */
93 NULL, /* stderr */
94 &exit_status,
95 &error);
96 g_assert_no_error (error);
97 g_spawn_check_exit_status (exit_status, &error);
98 g_assert_no_error (error);
99 g_assert (ret);
101 timeout_mainloop_id = g_timeout_add (1000, test_connection_flush_on_timeout, GUINT_TO_POINTER (n));
102 g_main_loop_run (loop);
103 g_source_remove (timeout_mainloop_id);
106 g_dbus_connection_signal_unsubscribe (connection, signal_handler_id);
107 g_object_unref (connection);
109 session_bus_down ();
112 /* ---------------------------------------------------------------------------------------------------- */
114 /* Message size > 20MiB ... should be enough to make sure the message
115 * is fragmented when shoved across any transport
117 #define LARGE_MESSAGE_STRING_LENGTH (20*1024*1024)
118 /* the test will fail if the service name has not appeared after this amount of seconds */
119 #define LARGE_MESSAGE_TIMEOUT_SECONDS 10
121 static gboolean
122 large_message_timeout_cb (gpointer data)
124 (void)data;
126 g_error ("Error: timeout waiting for dbus name to appear\n");
128 return FALSE;
131 static void
132 large_message_on_name_appeared (GDBusConnection *connection,
133 const gchar *name,
134 const gchar *name_owner,
135 gpointer user_data)
137 GError *error;
138 gchar *request;
139 const gchar *reply;
140 GVariant *result;
141 guint n;
143 g_assert (g_source_remove (GPOINTER_TO_UINT (user_data)));
145 request = g_new (gchar, LARGE_MESSAGE_STRING_LENGTH + 1);
146 for (n = 0; n < LARGE_MESSAGE_STRING_LENGTH; n++)
147 request[n] = '0' + (n%10);
148 request[n] = '\0';
150 error = NULL;
151 result = g_dbus_connection_call_sync (connection,
152 "com.example.TestService", /* bus name */
153 "/com/example/TestObject", /* object path */
154 "com.example.Frob", /* interface name */
155 "HelloWorld", /* method name */
156 g_variant_new ("(s)", request), /* parameters */
157 G_VARIANT_TYPE ("(s)"), /* return type */
158 G_DBUS_CALL_FLAGS_NONE,
159 G_MAXINT,
160 NULL,
161 &error);
162 g_assert_no_error (error);
163 g_assert (result != NULL);
164 g_variant_get (result, "(&s)", &reply);
165 g_assert_cmpint (strlen (reply), >, LARGE_MESSAGE_STRING_LENGTH);
166 g_assert (g_str_has_prefix (reply, "You greeted me with '01234567890123456789012"));
167 g_assert (g_str_has_suffix (reply, "6789'. Thanks!"));
168 g_variant_unref (result);
170 g_free (request);
172 g_main_loop_quit (loop);
175 static void
176 large_message_on_name_vanished (GDBusConnection *connection,
177 const gchar *name,
178 gpointer user_data)
182 static void
183 test_connection_large_message (void)
185 guint watcher_id;
186 guint timeout_id;
188 session_bus_up ();
190 /* this is safe; testserver will exit once the bus goes away */
191 g_assert (g_spawn_command_line_async (g_test_get_filename (G_TEST_BUILT, "gdbus-testserver", NULL), NULL));
193 timeout_id = g_timeout_add_seconds (LARGE_MESSAGE_TIMEOUT_SECONDS,
194 large_message_timeout_cb,
195 NULL);
197 watcher_id = g_bus_watch_name (G_BUS_TYPE_SESSION,
198 "com.example.TestService",
199 G_BUS_NAME_WATCHER_FLAGS_NONE,
200 large_message_on_name_appeared,
201 large_message_on_name_vanished,
202 GUINT_TO_POINTER (timeout_id), /* user_data */
203 NULL); /* GDestroyNotify */
204 g_main_loop_run (loop);
205 g_bus_unwatch_name (watcher_id);
207 session_bus_down ();
210 /* ---------------------------------------------------------------------------------------------------- */
213 main (int argc,
214 char *argv[])
216 gint ret;
218 g_test_init (&argc, &argv, NULL);
220 /* all the tests rely on a shared main loop */
221 loop = g_main_loop_new (NULL, FALSE);
223 g_test_dbus_unset ();
225 g_test_add_func ("/gdbus/connection/flush", test_connection_flush);
226 g_test_add_func ("/gdbus/connection/large_message", test_connection_large_message);
228 ret = g_test_run();
229 g_main_loop_unref (loop);
230 return ret;