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>
26 #include "gdbus-tests.h"
28 /* ---------------------------------------------------------------------------------------------------- */
37 on_property_notify (GObject *object,
41 PropertyNotifyData *data = user_data;
42 g_main_loop_quit (data->loop);
46 on_property_notify_timeout (gpointer user_data)
48 PropertyNotifyData *data = user_data;
49 data->timed_out = TRUE;
50 g_main_loop_quit (data->loop);
55 _g_assert_property_notify_run (gpointer object,
56 const gchar *property_name)
61 PropertyNotifyData data;
63 data.loop = g_main_loop_new (g_main_context_get_thread_default (), FALSE);
64 data.timed_out = FALSE;
65 s = g_strdup_printf ("notify::%s", property_name);
66 handler_id = g_signal_connect (object,
68 G_CALLBACK (on_property_notify),
71 timeout_id = g_timeout_add (30 * 1000,
72 on_property_notify_timeout,
74 g_main_loop_run (data.loop);
75 g_signal_handler_disconnect (object, handler_id);
76 g_source_remove (timeout_id);
77 g_main_loop_unref (data.loop);
79 return data.timed_out;
82 /* ---------------------------------------------------------------------------------------------------- */
91 on_signal_received (gpointer user_data)
93 SignalReceivedData *data = user_data;
94 g_main_loop_quit (data->loop);
98 on_signal_received_timeout (gpointer user_data)
100 SignalReceivedData *data = user_data;
101 data->timed_out = TRUE;
102 g_main_loop_quit (data->loop);
107 _g_assert_signal_received_run (gpointer object,
108 const gchar *signal_name)
112 SignalReceivedData data;
114 data.loop = g_main_loop_new (g_main_context_get_thread_default (), FALSE);
115 data.timed_out = FALSE;
116 handler_id = g_signal_connect_swapped (object,
118 G_CALLBACK (on_signal_received),
120 timeout_id = g_timeout_add (30 * 1000,
121 on_signal_received_timeout,
123 g_main_loop_run (data.loop);
124 g_signal_handler_disconnect (object, handler_id);
125 g_source_remove (timeout_id);
126 g_main_loop_unref (data.loop);
128 return data.timed_out;
131 /* ---------------------------------------------------------------------------------------------------- */
134 _g_bus_get_priv (GBusType bus_type,
135 GCancellable *cancellable,
139 GDBusConnection *ret;
143 address = g_dbus_address_get_for_bus_sync (bus_type, cancellable, error);
147 ret = g_dbus_connection_new_for_address_sync (address,
148 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
149 G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
150 NULL, /* GDBusAuthObserver */
159 /* ---------------------------------------------------------------------------------------------------- */
162 /* toggle refs are not easy to use (maybe not even safe) when multiple
163 * threads are involved so implement this by busy-waiting for now
166 _g_object_wait_for_single_ref_do (gpointer object)
168 guint num_ms_elapsed;
176 if (G_OBJECT (object)->ref_count == 1)
179 if (num_ms_elapsed > 30000)
186 num_ms_elapsed += 10;
202 on_wait_single_ref_timeout (gpointer user_data)
204 WaitSingleRefData *data = user_data;
205 data->timed_out = TRUE;
206 g_main_loop_quit (data->loop);
211 on_wait_for_single_ref_toggled (gpointer user_data,
213 gboolean is_last_ref)
215 WaitSingleRefData *data = user_data;
216 g_main_loop_quit (data->loop);
220 _g_object_wait_for_single_ref_do (gpointer object)
222 WaitSingleRefData data;
225 data.timed_out = FALSE;
227 if (G_OBJECT (object)->ref_count == 1)
230 data.loop = g_main_loop_new (NULL, FALSE);
231 timeout_id = g_timeout_add (30 * 1000,
232 on_wait_single_ref_timeout,
235 g_object_add_toggle_ref (G_OBJECT (object),
236 on_wait_for_single_ref_toggled,
238 /* the reference could have been removed between us checking the
239 * ref_count and the toggle ref being added
241 if (G_OBJECT (object)->ref_count == 2)
242 goto single_ref_already;
244 g_object_unref (object);
245 g_main_loop_run (data.loop);
246 g_object_ref (object);
249 g_object_remove_toggle_ref (object,
250 on_wait_for_single_ref_toggled,
253 g_source_remove (timeout_id);
254 g_main_loop_unref (data.loop);
259 g_printerr ("b ref_count is %d\n", G_OBJECT (object)->ref_count);
261 return data.timed_out;
265 /* ---------------------------------------------------------------------------------------------------- */