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, see <http://www.gnu.org/licenses/>.
19 * Author: Simon McVittie <simon.mcvittie@collabora.co.uk>
29 #include "gdbus-tests.h"
32 # include <dbus/dbus-shared.h>
34 # define DBUS_INTERFACE_DBUS "org.freedesktop.DBus"
35 # define DBUS_PATH_DBUS "/org/freedesktop/DBus"
36 # define DBUS_SERVICE_DBUS "org.freedesktop.DBus"
37 # define DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER 1
38 # define DBUS_RELEASE_NAME_REPLY_RELEASED 1
41 #define MY_NAME "com.example.Test.Myself"
42 /* This many threads create and destroy GDBusProxy instances, in addition
43 * to the main thread processing their NameOwnerChanged signals.
44 * N_THREADS_MAX is used with "-m slow", N_THREADS otherwise.
46 #define N_THREADS_MAX 10
48 /* This many GDBusProxy instances are created by each thread. */
50 /* The main thread requests/releases a name this many times as rapidly as
51 * possible, before performing one "slow" cycle that waits for each method
52 * call result (and therefore, due to D-Bus total ordering, all previous
53 * method calls) to prevent requests from piling up infinitely. The more calls
54 * are made rapidly, the better we reproduce bugs.
56 #define N_RAPID_CYCLES 50
58 static GMainLoop
*loop
;
61 run_proxy_thread (gpointer data
)
63 GDBusConnection
*connection
= data
;
66 g_assert (g_main_context_get_thread_default () == NULL
);
68 for (i
= 0; i
< N_REPEATS
; i
++)
74 if (g_test_verbose ())
77 proxy
= g_dbus_proxy_new_sync (connection
,
78 G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START
|
79 G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES
,
82 "/com/example/TestObject",
86 g_assert_no_error (error
);
87 g_assert (proxy
!= NULL
);
88 g_dbus_proxy_set_default_timeout (proxy
, G_MAXINT
);
90 ret
= g_dbus_proxy_call_sync (proxy
, "StupidMethod", NULL
,
91 G_DBUS_CALL_FLAGS_NO_AUTO_START
, -1,
94 * we expect this to fail - if we have the name at the moment, we called
95 * an unimplemented method, and if not, there was nothing to call
97 g_assert (ret
== NULL
);
100 * this races with the NameOwnerChanged signal being emitted in an
103 g_object_unref (proxy
);
106 g_main_loop_quit (loop
);
110 static void release_name (GDBusConnection
*connection
, gboolean wait
);
113 request_name_cb (GObject
*source
,
117 GDBusConnection
*connection
= G_DBUS_CONNECTION (source
);
118 GError
*error
= NULL
;
121 var
= g_dbus_connection_call_finish (connection
, res
, &error
);
122 g_assert_no_error (error
);
123 g_assert_cmpuint (g_variant_get_uint32 (g_variant_get_child_value (var
, 0)),
124 ==, DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER
);
126 release_name (connection
, TRUE
);
130 request_name (GDBusConnection
*connection
,
133 g_dbus_connection_call (connection
,
138 g_variant_new ("(su)", MY_NAME
, 0),
139 G_VARIANT_TYPE ("(u)"),
140 G_DBUS_CALL_FLAGS_NONE
,
143 wait
? request_name_cb
: NULL
,
148 release_name_cb (GObject
*source
,
152 GDBusConnection
*connection
= G_DBUS_CONNECTION (source
);
153 GError
*error
= NULL
;
157 var
= g_dbus_connection_call_finish (connection
, res
, &error
);
158 g_assert_no_error (error
);
159 g_assert_cmpuint (g_variant_get_uint32 (g_variant_get_child_value (var
, 0)),
160 ==, DBUS_RELEASE_NAME_REPLY_RELEASED
);
162 /* generate some rapid NameOwnerChanged signals to try to trigger crashes */
163 for (i
= 0; i
< N_RAPID_CYCLES
; i
++)
165 request_name (connection
, FALSE
);
166 release_name (connection
, FALSE
);
169 /* wait for dbus-daemon to catch up */
170 request_name (connection
, TRUE
);
174 release_name (GDBusConnection
*connection
,
177 g_dbus_connection_call (connection
,
182 g_variant_new ("(s)", MY_NAME
),
183 G_VARIANT_TYPE ("(u)"),
184 G_DBUS_CALL_FLAGS_NONE
,
187 wait
? release_name_cb
: NULL
,
194 GDBusConnection
*connection
;
195 GError
*error
= NULL
;
196 GThread
*proxy_threads
[N_THREADS_MAX
];
201 n_threads
= N_THREADS_MAX
;
203 n_threads
= N_THREADS
;
207 loop
= g_main_loop_new (NULL
, TRUE
);
209 connection
= g_bus_get_sync (G_BUS_TYPE_SESSION
,
212 g_assert_no_error (error
);
214 request_name (connection
, TRUE
);
216 for (i
= 0; i
< n_threads
; i
++)
218 proxy_threads
[i
] = g_thread_new ("run-proxy",
219 run_proxy_thread
, connection
);
222 g_main_loop_run (loop
);
224 for (i
= 0; i
< n_threads
; i
++)
226 g_thread_join (proxy_threads
[i
]);
229 g_object_unref (connection
);
230 g_main_loop_unref (loop
);
232 /* TODO: should call session_bus_down() but that requires waiting
233 * for all the oustanding method calls to complete...
235 if (g_test_verbose ())
243 g_test_init (&argc
, &argv
, NULL
);
245 g_test_dbus_unset ();
247 g_test_add_func ("/gdbus/proxy/vs-threads", test_proxy
);