1 /* GLib testing framework examples and tests
3 * Copyright (C) 2008-2010 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: David Zeuthen <davidz@redhat.com>
27 #include "gdbus-tests.h"
29 /* all tests rely on a global connection */
30 static GDBusConnection *c = NULL;
32 /* all tests rely on a shared mainloop */
33 static GMainLoop *loop = NULL;
35 /* ---------------------------------------------------------------------------------------------------- */
36 /* Ensure that signal and method replies are delivered in the right thread */
37 /* ---------------------------------------------------------------------------------------------------- */
41 GMainLoop *thread_loop;
46 msg_cb_expect_success (GDBusConnection *connection,
50 DeliveryData *data = user_data;
55 result = g_dbus_connection_call_finish (connection,
58 g_assert_no_error (error);
59 g_assert (result != NULL);
60 g_variant_unref (result);
62 g_assert (g_thread_self () == data->thread);
64 g_main_loop_quit (data->thread_loop);
68 msg_cb_expect_error_cancelled (GDBusConnection *connection,
72 DeliveryData *data = user_data;
77 result = g_dbus_connection_call_finish (connection,
80 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
81 g_assert (!g_dbus_error_is_remote_error (error));
83 g_assert (result == NULL);
85 g_assert (g_thread_self () == data->thread);
87 g_main_loop_quit (data->thread_loop);
91 signal_handler (GDBusConnection *connection,
92 const gchar *sender_name,
93 const gchar *object_path,
94 const gchar *interface_name,
95 const gchar *signal_name,
99 DeliveryData *data = user_data;
101 g_assert (g_thread_self () == data->thread);
103 data->signal_count++;
105 g_main_loop_quit (data->thread_loop);
109 test_delivery_in_thread_func (gpointer _data)
111 GMainLoop *thread_loop;
112 GMainContext *thread_context;
115 guint subscription_id;
116 GDBusConnection *priv_c;
121 thread_context = g_main_context_new ();
122 thread_loop = g_main_loop_new (thread_context, FALSE);
123 g_main_context_push_thread_default (thread_context);
125 data.thread = g_thread_self ();
126 data.thread_loop = thread_loop;
127 data.signal_count = 0;
129 /* ---------------------------------------------------------------------------------------------------- */
132 * Check that we get a reply to the GetId() method call.
134 g_dbus_connection_call (c,
135 "org.freedesktop.DBus", /* bus_name */
136 "/org/freedesktop/DBus", /* object path */
137 "org.freedesktop.DBus", /* interface name */
138 "GetId", /* method name */
140 G_DBUS_CALL_FLAGS_NONE,
143 (GAsyncReadyCallback) msg_cb_expect_success,
145 g_main_loop_run (thread_loop);
148 * Check that we never actually send a message if the GCancellable
149 * is already cancelled - i.e. we should get #G_IO_ERROR_CANCELLED
150 * when the actual connection is not up.
152 ca = g_cancellable_new ();
153 g_cancellable_cancel (ca);
154 g_dbus_connection_call (c,
155 "org.freedesktop.DBus", /* bus_name */
156 "/org/freedesktop/DBus", /* object path */
157 "org.freedesktop.DBus", /* interface name */
158 "GetId", /* method name */
160 G_DBUS_CALL_FLAGS_NONE,
163 (GAsyncReadyCallback) msg_cb_expect_error_cancelled,
165 g_main_loop_run (thread_loop);
169 * Check that cancellation works when the message is already in flight.
171 ca = g_cancellable_new ();
172 g_dbus_connection_call (c,
173 "org.freedesktop.DBus", /* bus_name */
174 "/org/freedesktop/DBus", /* object path */
175 "org.freedesktop.DBus", /* interface name */
176 "GetId", /* method name */
178 G_DBUS_CALL_FLAGS_NONE,
181 (GAsyncReadyCallback) msg_cb_expect_error_cancelled,
183 g_cancellable_cancel (ca);
184 g_main_loop_run (thread_loop);
188 * Check that signals are delivered to the correct thread.
190 * First we subscribe to the signal, then we create a a private
191 * connection. This should cause a NameOwnerChanged message from
194 subscription_id = g_dbus_connection_signal_subscribe (c,
195 "org.freedesktop.DBus", /* sender */
196 "org.freedesktop.DBus", /* interface */
197 "NameOwnerChanged", /* member */
198 "/org/freedesktop/DBus", /* path */
200 G_DBUS_SIGNAL_FLAGS_NONE,
204 g_assert (subscription_id != 0);
205 g_assert (data.signal_count == 0);
207 priv_c = _g_bus_get_priv (G_BUS_TYPE_SESSION, NULL, &error);
208 g_assert_no_error (error);
209 g_assert (priv_c != NULL);
211 g_main_loop_run (thread_loop);
212 g_assert (data.signal_count == 1);
214 g_object_unref (priv_c);
216 g_dbus_connection_signal_unsubscribe (c, subscription_id);
218 /* ---------------------------------------------------------------------------------------------------- */
220 g_main_context_pop_thread_default (thread_context);
221 g_main_loop_unref (thread_loop);
222 g_main_context_unref (thread_context);
224 g_main_loop_quit (loop);
230 test_delivery_in_thread (void)
236 thread = g_thread_new ("deliver",
237 test_delivery_in_thread_func,
241 g_assert_no_error (error);
242 g_assert (thread != NULL);
244 /* run the event loop - it is needed to dispatch D-Bus messages */
245 g_main_loop_run (loop);
247 g_thread_join (thread);
250 /* ---------------------------------------------------------------------------------------------------- */
258 GMainLoop *thread_loop;
265 sleep_cb (GDBusProxy *proxy,
269 SyncThreadData *data = user_data;
274 result = g_dbus_proxy_call_finish (proxy,
277 g_assert_no_error (error);
278 g_assert (result != NULL);
279 g_assert_cmpstr (g_variant_get_type_string (result), ==, "()");
280 g_variant_unref (result);
282 g_assert (data->thread == g_thread_self ());
284 g_main_loop_quit (data->thread_loop);
286 //g_debug ("async cb (%p)", g_thread_self ());
290 test_sleep_in_thread_func (gpointer _data)
292 SyncThreadData *data = _data;
293 GMainContext *thread_context;
296 thread_context = g_main_context_new ();
297 data->thread_loop = g_main_loop_new (thread_context, FALSE);
298 g_main_context_push_thread_default (thread_context);
300 data->thread = g_thread_self ();
302 for (n = 0; n < data->num; n++)
306 //g_debug ("invoking async (%p)", g_thread_self ());
307 g_dbus_proxy_call (data->proxy,
309 g_variant_new ("(i)", data->msec),
310 G_DBUS_CALL_FLAGS_NONE,
313 (GAsyncReadyCallback) sleep_cb,
315 g_main_loop_run (data->thread_loop);
317 //g_debug ("done invoking async (%p)", g_thread_self ());
325 //g_debug ("invoking sync (%p)", g_thread_self ());
326 result = g_dbus_proxy_call_sync (data->proxy,
328 g_variant_new ("(i)", data->msec),
329 G_DBUS_CALL_FLAGS_NONE,
334 //g_debug ("done invoking sync (%p)", g_thread_self ());
335 g_assert_no_error (error);
336 g_assert (result != NULL);
337 g_assert_cmpstr (g_variant_get_type_string (result), ==, "()");
338 g_variant_unref (result);
342 g_main_context_pop_thread_default (thread_context);
343 g_main_loop_unref (data->thread_loop);
344 g_main_context_unref (thread_context);
347 g_main_loop_quit (loop);
353 test_method_calls_on_proxy (GDBusProxy *proxy)
358 * Check that multiple threads can do calls without interferring with
359 * each other. We do this by creating three threads that call the
360 * Sleep() method on the server (which handles it asynchronously, e.g.
361 * it won't block other requests) with different sleep durations and
362 * a number of times. We do this so each set of calls add up to 4000
365 * The dbus test server that this code calls into uses glib timeouts
366 * to do the sleeping which have only a granularity of 1ms. It is
367 * therefore possible to lose as much as 40ms; the test could finish
368 * in slightly less than 4 seconds.
370 * We run this test twice - first with async calls in each thread, then
371 * again with sync calls
374 for (n = 0; n < 2; n++)
380 SyncThreadData data1;
381 SyncThreadData data2;
382 SyncThreadData data3;
391 g_get_current_time (&start_time);
396 data1.async = do_async;
398 thread1 = g_thread_new ("sleep",
399 test_sleep_in_thread_func,
403 g_assert_no_error (error);
404 g_assert (thread1 != NULL);
409 data2.async = do_async;
411 thread2 = g_thread_new ("sleep2",
412 test_sleep_in_thread_func,
416 g_assert_no_error (error);
417 g_assert (thread2 != NULL);
422 data3.async = do_async;
424 thread3 = g_thread_new ("sleep3",
425 test_sleep_in_thread_func,
429 g_assert_no_error (error);
430 g_assert (thread3 != NULL);
432 /* we handle messages in the main loop - threads will quit it when they are done */
433 while (!(data1.done && data2.done && data3.done))
434 g_main_loop_run (loop);
436 g_thread_join (thread1);
437 g_thread_join (thread2);
438 g_thread_join (thread3);
440 g_get_current_time (&end_time);
442 elapsed_msec = ((end_time.tv_sec * G_USEC_PER_SEC + end_time.tv_usec) -
443 (start_time.tv_sec * G_USEC_PER_SEC + start_time.tv_usec)) / 1000;
445 //g_debug ("Elapsed time for %s = %d msec", n == 0 ? "async" : "sync", elapsed_msec);
447 /* elapsed_msec should be 4000 msec +/- change for overhead/inaccuracy */
448 g_assert_cmpint (elapsed_msec, >=, 3950);
449 g_assert_cmpint (elapsed_msec, <, 6000);
454 g_main_loop_quit (loop);
458 test_method_calls_in_thread (void)
461 GDBusConnection *connection;
466 connection = g_bus_get_sync (G_BUS_TYPE_SESSION,
469 g_assert_no_error (error);
471 proxy = g_dbus_proxy_new_sync (connection,
472 G_DBUS_PROXY_FLAGS_NONE,
473 NULL, /* GDBusInterfaceInfo */
474 "com.example.TestService", /* name */
475 "/com/example/TestObject", /* object path */
476 "com.example.Frob", /* interface */
477 NULL, /* GCancellable */
479 g_assert_no_error (error);
481 name_owner = g_dbus_proxy_get_name_owner (proxy);
482 g_assert_cmpstr (name_owner, !=, NULL);
485 test_method_calls_on_proxy (proxy);
487 g_object_unref (proxy);
488 g_object_unref (connection);
491 /* ---------------------------------------------------------------------------------------------------- */
501 g_test_init (&argc, &argv, NULL);
503 /* all the tests rely on a shared main loop */
504 loop = g_main_loop_new (NULL, FALSE);
506 /* all the tests use a session bus with a well-known address that we can bring up and down
507 * using session_bus_up() and session_bus_down().
509 g_unsetenv ("DISPLAY");
510 g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_get_temporary_address (), TRUE);
514 /* TODO: wait a bit for the bus to come up.. ideally session_bus_up() won't return
515 * until one can connect to the bus but that's not how things work right now
519 /* this is safe; testserver will exit once the bus goes away */
520 g_assert (g_spawn_command_line_async (SRCDIR "/gdbus-testserver.py", NULL));
522 /* wait for the service to come up */
525 /* Create the connection in the main thread */
527 c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
528 g_assert_no_error (error);
529 g_assert (c != NULL);
531 g_test_add_func ("/gdbus/delivery-in-thread", test_delivery_in_thread);
532 g_test_add_func ("/gdbus/method-calls-in-thread", test_method_calls_in_thread);