Tizen 2.1 base
[platform/upstream/glib2.0.git] / gio / tests / gdbus-proxy-threads.c
1 /* Test case for GNOME #651133
2  *
3  * Copyright (C) 2008-2010 Red Hat, Inc.
4  * Copyright (C) 2011 Nokia Corporation
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General
17  * Public License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
19  * Boston, MA 02111-1307, USA.
20  *
21  * Author: Simon McVittie <simon.mcvittie@collabora.co.uk>
22  */
23
24 #include <config.h>
25
26 #include <unistd.h>
27 #include <string.h>
28
29 #include <gio/gio.h>
30
31 #include "gdbus-tests.h"
32
33 #ifdef HAVE_DBUS1
34 # include <dbus/dbus-shared.h>
35 #else
36 # define DBUS_INTERFACE_DBUS "org.freedesktop.DBus"
37 # define DBUS_PATH_DBUS "/org/freedesktop/DBus"
38 # define DBUS_SERVICE_DBUS "org.freedesktop.DBus"
39 # define DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER 1
40 # define DBUS_RELEASE_NAME_REPLY_RELEASED 1
41 #endif
42
43 #define MY_NAME "com.example.Test.Myself"
44 /* This many threads create and destroy GDBusProxy instances, in addition
45  * to the main thread processing their NameOwnerChanged signals.
46  * N_THREADS_MAX is used with "-m slow", N_THREADS otherwise.
47  */
48 #define N_THREADS_MAX 10
49 #define N_THREADS 2
50 /* This many GDBusProxy instances are created by each thread. */
51 #define N_REPEATS 100
52 /* The main thread requests/releases a name this many times as rapidly as
53  * possible, before performing one "slow" cycle that waits for each method
54  * call result (and therefore, due to D-Bus total ordering, all previous
55  * method calls) to prevent requests from piling up infinitely. The more calls
56  * are made rapidly, the better we reproduce bugs.
57  */
58 #define N_RAPID_CYCLES 50
59
60 static GMainLoop *loop;
61
62 static gpointer
63 run_proxy_thread (gpointer data)
64 {
65   GDBusConnection *connection = data;
66   int i;
67
68   g_assert (g_main_context_get_thread_default () == NULL);
69
70   for (i = 0; i < N_REPEATS; i++)
71     {
72       GDBusProxy *proxy;
73       GError *error = NULL;
74       GVariant *ret;
75
76       g_print (".");
77
78       proxy = g_dbus_proxy_new_sync (connection,
79                                      G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START |
80                                      G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES,
81                                      NULL,
82                                      MY_NAME,
83                                      "/com/example/TestObject",
84                                      "com.example.Frob",
85                                      NULL,
86                                      &error);
87       g_assert_no_error (error);
88       g_assert (proxy != NULL);
89       g_dbus_proxy_set_default_timeout (proxy, G_MAXINT);
90
91       ret = g_dbus_proxy_call_sync (proxy, "StupidMethod", NULL,
92                                     G_DBUS_CALL_FLAGS_NO_AUTO_START, -1,
93                                     NULL, NULL);
94       /*
95        * we expect this to fail - if we have the name at the moment, we called
96        * an unimplemented method, and if not, there was nothing to call
97        */
98       g_assert (ret == NULL);
99
100       /*
101        * this races with the NameOwnerChanged signal being emitted in an
102        * idle
103        */
104       g_object_unref (proxy);
105     }
106
107   g_main_loop_quit (loop);
108   return NULL;
109 }
110
111 static void release_name (GDBusConnection *connection, gboolean wait);
112
113 static void
114 request_name_cb (GObject *source,
115                  GAsyncResult *res,
116                  gpointer user_data)
117 {
118   GDBusConnection *connection = G_DBUS_CONNECTION (source);
119   GError *error = NULL;
120   GVariant *var;
121
122   var = g_dbus_connection_call_finish (connection, res, &error);
123   g_assert_no_error (error);
124   g_assert_cmpuint (g_variant_get_uint32 (g_variant_get_child_value (var, 0)),
125                     ==, DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER);
126
127   release_name (connection, TRUE);
128 }
129
130 static void
131 request_name (GDBusConnection *connection,
132               gboolean         wait)
133 {
134   g_dbus_connection_call (connection,
135                           DBUS_SERVICE_DBUS,
136                           DBUS_PATH_DBUS,
137                           DBUS_INTERFACE_DBUS,
138                           "RequestName",
139                           g_variant_new ("(su)", MY_NAME, 0),
140                           G_VARIANT_TYPE ("(u)"),
141                           G_DBUS_CALL_FLAGS_NONE,
142                           -1,
143                           NULL,
144                           wait ? request_name_cb : NULL,
145                           NULL);
146 }
147
148 static void
149 release_name_cb (GObject *source,
150                  GAsyncResult *res,
151                  gpointer user_data)
152 {
153   GDBusConnection *connection = G_DBUS_CONNECTION (source);
154   GError *error = NULL;
155   GVariant *var;
156   int i;
157
158   var = g_dbus_connection_call_finish (connection, res, &error);
159   g_assert_no_error (error);
160   g_assert_cmpuint (g_variant_get_uint32 (g_variant_get_child_value (var, 0)),
161                     ==, DBUS_RELEASE_NAME_REPLY_RELEASED);
162
163   /* generate some rapid NameOwnerChanged signals to try to trigger crashes */
164   for (i = 0; i < N_RAPID_CYCLES; i++)
165     {
166       request_name (connection, FALSE);
167       release_name (connection, FALSE);
168     }
169
170   /* wait for dbus-daemon to catch up */
171   request_name (connection, TRUE);
172 }
173
174 static void
175 release_name (GDBusConnection *connection,
176               gboolean         wait)
177 {
178   g_dbus_connection_call (connection,
179                           DBUS_SERVICE_DBUS,
180                           DBUS_PATH_DBUS,
181                           DBUS_INTERFACE_DBUS,
182                           "ReleaseName",
183                           g_variant_new ("(s)", MY_NAME),
184                           G_VARIANT_TYPE ("(u)"),
185                           G_DBUS_CALL_FLAGS_NONE,
186                           -1,
187                           NULL,
188                           wait ? release_name_cb : NULL,
189                           NULL);
190 }
191
192 static void
193 test_proxy (void)
194 {
195   GDBusConnection *connection;
196   GError *error = NULL;
197   GThread *proxy_threads[N_THREADS_MAX];
198   int i;
199   int n_threads;
200
201   if (g_test_slow ())
202     n_threads = N_THREADS_MAX;
203   else
204     n_threads = N_THREADS;
205
206   session_bus_up ();
207
208   /* TODO: wait a bit for the bus to come up.. ideally session_bus_up() won't return
209    * until one can connect to the bus but that's not how things work right now
210    */
211   usleep (500 * 1000);
212
213   loop = g_main_loop_new (NULL, TRUE);
214
215   connection = g_bus_get_sync (G_BUS_TYPE_SESSION,
216                                NULL,
217                                &error);
218   g_assert_no_error (error);
219
220   request_name (connection, TRUE);
221
222   for (i = 0; i < n_threads; i++)
223     {
224       proxy_threads[i] = g_thread_new ("run-proxy",
225                                        run_proxy_thread, connection);
226     }
227
228   g_main_loop_run (loop);
229
230   for (i = 0; i < n_threads; i++)
231     {
232       g_thread_join (proxy_threads[i]);
233     }
234
235   g_object_unref (connection);
236   g_main_loop_unref (loop);
237 }
238
239 int
240 main (int   argc,
241       char *argv[])
242 {
243   g_unsetenv ("DISPLAY");
244   g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_get_temporary_address (), TRUE);
245
246   g_type_init ();
247   g_test_init (&argc, &argv, NULL);
248
249   g_test_add_func ("/gdbus/proxy/vs-threads", test_proxy);
250
251   return g_test_run();
252 }