Timeout the test if dbus sevice has not appeared in due time.
[platform/upstream/glib.git] / gio / tests / gdbus-connection-slow.c
1 /* GLib testing framework examples and tests
2  *
3  * Copyright (C) 2008-2010 Red Hat, Inc.
4  *
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.
9  *
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.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Author: David Zeuthen <davidz@redhat.com>
21  */
22
23 #include <gio/gio.h>
24 #include <unistd.h>
25 #include <string.h>
26
27 #include <sys/types.h>
28
29 #include "gdbus-tests.h"
30
31 static const gchar *datapath;
32
33 /* all tests rely on a shared mainloop */
34 static GMainLoop *loop = NULL;
35
36 /* ---------------------------------------------------------------------------------------------------- */
37
38 static void
39 test_connection_flush_signal_handler (GDBusConnection  *connection,
40                                       const gchar      *sender_name,
41                                       const gchar      *object_path,
42                                       const gchar      *interface_name,
43                                       const gchar      *signal_name,
44                                       GVariant         *parameters,
45                                       gpointer         user_data)
46 {
47   g_main_loop_quit (loop);
48 }
49
50 static gboolean
51 test_connection_flush_on_timeout (gpointer user_data)
52 {
53   guint iteration = GPOINTER_TO_UINT (user_data);
54   g_printerr ("Timeout waiting 1000 msec on iteration %d\n", iteration);
55   g_assert_not_reached ();
56   return FALSE;
57 }
58
59 static void
60 test_connection_flush (void)
61 {
62   GDBusConnection *connection;
63   GError *error;
64   guint n;
65   guint signal_handler_id;
66
67   session_bus_up ();
68
69   error = NULL;
70   connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
71   g_assert_no_error (error);
72   g_assert (connection != NULL);
73
74   signal_handler_id = g_dbus_connection_signal_subscribe (connection,
75                                                           NULL, /* sender */
76                                                           "org.gtk.GDBus.FlushInterface",
77                                                           "SomeSignal",
78                                                           "/org/gtk/GDBus/FlushObject",
79                                                           NULL,
80                                                           G_DBUS_SIGNAL_FLAGS_NONE,
81                                                           test_connection_flush_signal_handler,
82                                                           NULL,
83                                                           NULL);
84   g_assert_cmpint (signal_handler_id, !=, 0);
85
86   for (n = 0; n < 50; n++)
87     {
88       gboolean ret;
89       gint exit_status;
90       guint timeout_mainloop_id;
91       gchar *path;
92
93       error = NULL;
94       path = g_build_filename (datapath, "gdbus-connection-flush-helper", NULL);
95       ret = g_spawn_command_line_sync (path,
96                                        NULL, /* stdout */
97                                        NULL, /* stderr */
98                                        &exit_status,
99                                        &error);
100       g_assert_no_error (error);
101       g_spawn_check_exit_status (exit_status, &error);
102       g_assert_no_error (error);
103       g_assert (ret);
104       g_free (path);
105
106       timeout_mainloop_id = g_timeout_add (1000, test_connection_flush_on_timeout, GUINT_TO_POINTER (n));
107       g_main_loop_run (loop);
108       g_source_remove (timeout_mainloop_id);
109     }
110
111   g_dbus_connection_signal_unsubscribe (connection, signal_handler_id);
112   g_object_unref (connection);
113
114   session_bus_down ();
115 }
116
117 /* ---------------------------------------------------------------------------------------------------- */
118
119 /* Message size > 20MiB ... should be enough to make sure the message
120  * is fragmented when shoved across any transport
121  */
122 #define LARGE_MESSAGE_STRING_LENGTH (20*1024*1024)
123 /* the test will fail if the service name has not appeared after this amount of seconds */
124 #define LARGE_MESSAGE_TIMEOUT_SECONDS 10
125
126 static gboolean
127 large_message_timeout_cb (gpointer data)
128 {
129   (void)data;
130
131   g_error ("Error: timeout waiting for dbus name to appear\n");
132
133   return FALSE;
134 }
135
136 static void
137 large_message_on_name_appeared (GDBusConnection *connection,
138                                 const gchar *name,
139                                 const gchar *name_owner,
140                                 gpointer user_data)
141 {
142   GError *error;
143   gchar *request;
144   const gchar *reply;
145   GVariant *result;
146   guint n;
147
148   g_assert (g_source_remove (GPOINTER_TO_UINT (user_data)));
149
150   request = g_new (gchar, LARGE_MESSAGE_STRING_LENGTH + 1);
151   for (n = 0; n < LARGE_MESSAGE_STRING_LENGTH; n++)
152     request[n] = '0' + (n%10);
153   request[n] = '\0';
154
155   error = NULL;
156   result = g_dbus_connection_call_sync (connection,
157                                         "com.example.TestService",      /* bus name */
158                                         "/com/example/TestObject",      /* object path */
159                                         "com.example.Frob",             /* interface name */
160                                         "HelloWorld",                   /* method name */
161                                         g_variant_new ("(s)", request), /* parameters */
162                                         G_VARIANT_TYPE ("(s)"),         /* return type */
163                                         G_DBUS_CALL_FLAGS_NONE,
164                                         G_MAXINT,
165                                         NULL,
166                                         &error);
167   g_assert_no_error (error);
168   g_assert (result != NULL);
169   g_variant_get (result, "(&s)", &reply);
170   g_assert_cmpint (strlen (reply), >, LARGE_MESSAGE_STRING_LENGTH);
171   g_assert (g_str_has_prefix (reply, "You greeted me with '01234567890123456789012"));
172   g_assert (g_str_has_suffix (reply, "6789'. Thanks!"));
173   g_variant_unref (result);
174
175   g_free (request);
176
177   g_main_loop_quit (loop);
178 }
179
180 static void
181 large_message_on_name_vanished (GDBusConnection *connection,
182                                 const gchar *name,
183                                 gpointer user_data)
184 {
185 }
186
187 static void
188 test_connection_large_message (void)
189 {
190   guint watcher_id;
191   gchar *path;
192   guint timeout_id;
193
194   session_bus_up ();
195
196   /* this is safe; testserver will exit once the bus goes away */
197   path = g_build_filename (datapath, "gdbus-testserver", NULL);
198   g_assert (g_spawn_command_line_async (path, NULL));
199   g_free (path);
200
201   timeout_id = g_timeout_add_seconds (LARGE_MESSAGE_TIMEOUT_SECONDS,
202                                       large_message_timeout_cb,
203                                       NULL);
204
205   watcher_id = g_bus_watch_name (G_BUS_TYPE_SESSION,
206                                  "com.example.TestService",
207                                  G_BUS_NAME_WATCHER_FLAGS_NONE,
208                                  large_message_on_name_appeared,
209                                  large_message_on_name_vanished,
210                                  GUINT_TO_POINTER (timeout_id),  /* user_data */
211                                  NULL); /* GDestroyNotify */
212   g_main_loop_run (loop);
213   g_bus_unwatch_name (watcher_id);
214
215   session_bus_down ();
216 }
217
218 /* ---------------------------------------------------------------------------------------------------- */
219
220 int
221 main (int   argc,
222       char *argv[])
223 {
224   if (g_getenv ("G_TEST_DATA"))
225     datapath = g_getenv ("G_TEST_DATA");
226   else
227     datapath = SRCDIR;
228
229   g_test_init (&argc, &argv, NULL);
230
231   /* all the tests rely on a shared main loop */
232   loop = g_main_loop_new (NULL, FALSE);
233
234   g_test_dbus_unset ();
235
236   g_test_add_func ("/gdbus/connection/flush", test_connection_flush);
237   g_test_add_func ("/gdbus/connection/large_message", test_connection_large_message);
238   return g_test_run();
239 }