1 /* Test case for GNOME #651133
3 * Copyright (C) 2008-2010 Red Hat, Inc.
4 * Copyright (C) 2011 Nokia Corporation
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.
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.
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.
21 * Author: Simon McVittie <simon.mcvittie@collabora.co.uk>
31 #include "gdbus-tests.h"
34 # include <dbus/dbus-shared.h>
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
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.
48 #define N_THREADS_MAX 10
50 /* This many GDBusProxy instances are created by each thread. */
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.
58 #define N_RAPID_CYCLES 50
60 static GMainLoop *loop;
63 run_proxy_thread (gpointer data)
65 GDBusConnection *connection = data;
68 g_assert (g_main_context_get_thread_default () == NULL);
70 for (i = 0; i < N_REPEATS; i++)
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,
84 "/com/example/TestObject",
88 g_assert_no_error (error);
89 g_assert (proxy != NULL);
90 g_dbus_proxy_set_default_timeout (proxy, G_MAXINT);
92 ret = g_dbus_proxy_call_sync (proxy, "StupidMethod", NULL,
93 G_DBUS_CALL_FLAGS_NO_AUTO_START, -1,
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
99 g_assert (ret == NULL);
102 * this races with the NameOwnerChanged signal being emitted in an
105 g_object_unref (proxy);
108 g_main_loop_quit (loop);
112 static void release_name (GDBusConnection *connection, gboolean wait);
115 request_name_cb (GObject *source,
119 GDBusConnection *connection = G_DBUS_CONNECTION (source);
120 GError *error = NULL;
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);
128 release_name (connection, TRUE);
132 request_name (GDBusConnection *connection,
135 g_dbus_connection_call (connection,
140 g_variant_new ("(su)", MY_NAME, 0),
141 G_VARIANT_TYPE ("(u)"),
142 G_DBUS_CALL_FLAGS_NONE,
145 wait ? request_name_cb : NULL,
150 release_name_cb (GObject *source,
154 GDBusConnection *connection = G_DBUS_CONNECTION (source);
155 GError *error = NULL;
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);
164 /* generate some rapid NameOwnerChanged signals to try to trigger crashes */
165 for (i = 0; i < N_RAPID_CYCLES; i++)
167 request_name (connection, FALSE);
168 release_name (connection, FALSE);
171 /* wait for dbus-daemon to catch up */
172 request_name (connection, TRUE);
176 release_name (GDBusConnection *connection,
179 g_dbus_connection_call (connection,
184 g_variant_new ("(s)", MY_NAME),
185 G_VARIANT_TYPE ("(u)"),
186 G_DBUS_CALL_FLAGS_NONE,
189 wait ? release_name_cb : NULL,
196 GDBusConnection *connection;
197 GError *error = NULL;
198 GThread *proxy_threads[N_THREADS_MAX];
203 n_threads = N_THREADS_MAX;
205 n_threads = N_THREADS;
209 loop = g_main_loop_new (NULL, TRUE);
211 connection = g_bus_get_sync (G_BUS_TYPE_SESSION,
214 g_assert_no_error (error);
216 request_name (connection, TRUE);
218 for (i = 0; i < n_threads; i++)
220 proxy_threads[i] = g_thread_new ("run-proxy",
221 run_proxy_thread, connection);
224 g_main_loop_run (loop);
226 for (i = 0; i < n_threads; i++)
228 g_thread_join (proxy_threads[i]);
231 g_object_unref (connection);
232 g_main_loop_unref (loop);
234 /* TODO: should call session_bus_down() but that requires waiting
235 * for all the oustanding method calls to complete...
237 if (!g_test_quiet ())
245 g_test_init (&argc, &argv, NULL);
247 g_test_dbus_unset ();
249 g_test_add_func ("/gdbus/proxy/vs-threads", test_proxy);