1 /* Test case for GNOME #651133
3 * Copyright (C) 2008-2010 Red Hat, Inc.
4 * Copyright (C) 2011 Nokia Corporation
6 * SPDX-License-Identifier: LGPL-2.1-or-later
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General
19 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
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++)
76 if (g_test_verbose ())
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;
124 var = g_dbus_connection_call_finish (connection, res, &error);
125 g_assert_no_error (error);
126 child = g_variant_get_child_value (var, 0);
127 g_assert_cmpuint (g_variant_get_uint32 (child),
128 ==, DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER);
130 release_name (connection, TRUE);
131 g_variant_unref (child);
132 g_variant_unref (var);
136 request_name (GDBusConnection *connection,
139 g_dbus_connection_call (connection,
144 g_variant_new ("(su)", MY_NAME, 0),
145 G_VARIANT_TYPE ("(u)"),
146 G_DBUS_CALL_FLAGS_NONE,
149 wait ? request_name_cb : NULL,
154 release_name_cb (GObject *source,
158 GDBusConnection *connection = G_DBUS_CONNECTION (source);
159 GError *error = NULL;
164 var = g_dbus_connection_call_finish (connection, res, &error);
165 g_assert_no_error (error);
166 child = g_variant_get_child_value (var, 0);
167 g_assert_cmpuint (g_variant_get_uint32 (child),
168 ==, DBUS_RELEASE_NAME_REPLY_RELEASED);
170 /* generate some rapid NameOwnerChanged signals to try to trigger crashes */
171 for (i = 0; i < N_RAPID_CYCLES; i++)
173 request_name (connection, FALSE);
174 release_name (connection, FALSE);
177 /* wait for dbus-daemon to catch up */
178 request_name (connection, TRUE);
179 g_variant_unref (child);
180 g_variant_unref (var);
184 release_name (GDBusConnection *connection,
187 g_dbus_connection_call (connection,
192 g_variant_new ("(s)", MY_NAME),
193 G_VARIANT_TYPE ("(u)"),
194 G_DBUS_CALL_FLAGS_NONE,
197 wait ? release_name_cb : NULL,
204 GDBusConnection *connection;
205 GError *error = NULL;
206 GThread *proxy_threads[N_THREADS_MAX];
211 n_threads = N_THREADS_MAX;
213 n_threads = N_THREADS;
217 loop = g_main_loop_new (NULL, TRUE);
219 connection = g_bus_get_sync (G_BUS_TYPE_SESSION,
222 g_assert_no_error (error);
224 request_name (connection, TRUE);
226 for (i = 0; i < n_threads; i++)
228 proxy_threads[i] = g_thread_new ("run-proxy",
229 run_proxy_thread, connection);
232 g_main_loop_run (loop);
234 for (i = 0; i < n_threads; i++)
236 g_thread_join (proxy_threads[i]);
239 g_object_unref (connection);
240 g_main_loop_unref (loop);
242 /* TODO: should call session_bus_down() but that requires waiting
243 * for all the outstanding method calls to complete...
245 if (g_test_verbose ())
253 g_test_init (&argc, &argv, G_TEST_OPTION_ISOLATE_DIRS, NULL);
255 g_test_dbus_unset ();
257 g_test_add_func ("/gdbus/proxy/vs-threads", test_proxy);