Improve GSettings test coverage
[platform/upstream/glib.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       if (!g_test_quiet ())
77         g_print (".");
78
79       proxy = g_dbus_proxy_new_sync (connection,
80                                      G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START |
81                                      G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES,
82                                      NULL,
83                                      MY_NAME,
84                                      "/com/example/TestObject",
85                                      "com.example.Frob",
86                                      NULL,
87                                      &error);
88       g_assert_no_error (error);
89       g_assert (proxy != NULL);
90       g_dbus_proxy_set_default_timeout (proxy, G_MAXINT);
91
92       ret = g_dbus_proxy_call_sync (proxy, "StupidMethod", NULL,
93                                     G_DBUS_CALL_FLAGS_NO_AUTO_START, -1,
94                                     NULL, NULL);
95       /*
96        * we expect this to fail - if we have the name at the moment, we called
97        * an unimplemented method, and if not, there was nothing to call
98        */
99       g_assert (ret == NULL);
100
101       /*
102        * this races with the NameOwnerChanged signal being emitted in an
103        * idle
104        */
105       g_object_unref (proxy);
106     }
107
108   g_main_loop_quit (loop);
109   return NULL;
110 }
111
112 static void release_name (GDBusConnection *connection, gboolean wait);
113
114 static void
115 request_name_cb (GObject *source,
116                  GAsyncResult *res,
117                  gpointer user_data)
118 {
119   GDBusConnection *connection = G_DBUS_CONNECTION (source);
120   GError *error = NULL;
121   GVariant *var;
122
123   var = g_dbus_connection_call_finish (connection, res, &error);
124   g_assert_no_error (error);
125   g_assert_cmpuint (g_variant_get_uint32 (g_variant_get_child_value (var, 0)),
126                     ==, DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER);
127
128   release_name (connection, TRUE);
129 }
130
131 static void
132 request_name (GDBusConnection *connection,
133               gboolean         wait)
134 {
135   g_dbus_connection_call (connection,
136                           DBUS_SERVICE_DBUS,
137                           DBUS_PATH_DBUS,
138                           DBUS_INTERFACE_DBUS,
139                           "RequestName",
140                           g_variant_new ("(su)", MY_NAME, 0),
141                           G_VARIANT_TYPE ("(u)"),
142                           G_DBUS_CALL_FLAGS_NONE,
143                           -1,
144                           NULL,
145                           wait ? request_name_cb : NULL,
146                           NULL);
147 }
148
149 static void
150 release_name_cb (GObject *source,
151                  GAsyncResult *res,
152                  gpointer user_data)
153 {
154   GDBusConnection *connection = G_DBUS_CONNECTION (source);
155   GError *error = NULL;
156   GVariant *var;
157   int i;
158
159   var = g_dbus_connection_call_finish (connection, res, &error);
160   g_assert_no_error (error);
161   g_assert_cmpuint (g_variant_get_uint32 (g_variant_get_child_value (var, 0)),
162                     ==, DBUS_RELEASE_NAME_REPLY_RELEASED);
163
164   /* generate some rapid NameOwnerChanged signals to try to trigger crashes */
165   for (i = 0; i < N_RAPID_CYCLES; i++)
166     {
167       request_name (connection, FALSE);
168       release_name (connection, FALSE);
169     }
170
171   /* wait for dbus-daemon to catch up */
172   request_name (connection, TRUE);
173 }
174
175 static void
176 release_name (GDBusConnection *connection,
177               gboolean         wait)
178 {
179   g_dbus_connection_call (connection,
180                           DBUS_SERVICE_DBUS,
181                           DBUS_PATH_DBUS,
182                           DBUS_INTERFACE_DBUS,
183                           "ReleaseName",
184                           g_variant_new ("(s)", MY_NAME),
185                           G_VARIANT_TYPE ("(u)"),
186                           G_DBUS_CALL_FLAGS_NONE,
187                           -1,
188                           NULL,
189                           wait ? release_name_cb : NULL,
190                           NULL);
191 }
192
193 static void
194 test_proxy (void)
195 {
196   GDBusConnection *connection;
197   GError *error = NULL;
198   GThread *proxy_threads[N_THREADS_MAX];
199   int i;
200   int n_threads;
201
202   if (g_test_slow ())
203     n_threads = N_THREADS_MAX;
204   else
205     n_threads = N_THREADS;
206
207   session_bus_up ();
208
209   loop = g_main_loop_new (NULL, TRUE);
210
211   connection = g_bus_get_sync (G_BUS_TYPE_SESSION,
212                                NULL,
213                                &error);
214   g_assert_no_error (error);
215
216   request_name (connection, TRUE);
217
218   for (i = 0; i < n_threads; i++)
219     {
220       proxy_threads[i] = g_thread_new ("run-proxy",
221                                        run_proxy_thread, connection);
222     }
223
224   g_main_loop_run (loop);
225
226   for (i = 0; i < n_threads; i++)
227     {
228       g_thread_join (proxy_threads[i]);
229     }
230
231   g_object_unref (connection);
232   g_main_loop_unref (loop);
233
234   /* TODO: should call session_bus_down() but that requires waiting
235    * for all the oustanding method calls to complete...
236    */
237   if (!g_test_quiet ())
238     g_print ("\n");
239 }
240
241 int
242 main (int   argc,
243       char *argv[])
244 {
245   g_test_init (&argc, &argv, NULL);
246
247   g_test_dbus_unset ();
248
249   g_test_add_func ("/gdbus/proxy/vs-threads", test_proxy);
250
251   return g_test_run();
252 }