2 * Test to make sure late thread initialization works
7 #include <dbus/dbus-sysdeps.h>
11 #include <dbus/dbus-internals.h>
12 #include <dbus/dbus-connection-internal.h>
15 _run_iteration (DBusConnection *conn)
17 DBusPendingCall *echo_pending;
18 DBusPendingCall *dbus_pending;
21 const char *echo = "echo";
23 /* send the first message */
24 method = dbus_message_new_method_call ("org.freedesktop.DBus.TestSuiteEchoService",
25 "/org/freedesktop/TestSuite",
26 "org.freedesktop.TestSuite",
29 if (!dbus_message_append_args (method, DBUS_TYPE_STRING, &echo, NULL))
31 fprintf (stderr, "Bail out! Failed to append arguments: OOM\n");
35 dbus_connection_send_with_reply (conn, method, &echo_pending, -1);
36 dbus_message_unref (method);
38 /* send the second message */
39 method = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
41 "org.freedesktop.Introspectable",
44 dbus_connection_send_with_reply (conn, method, &dbus_pending, -1);
45 dbus_message_unref (method);
47 /* block on the second message (should return immediately) */
48 dbus_pending_call_block (dbus_pending);
50 /* block on the first message */
51 /* if it does not return immediately chances
52 are we hit the block in poll bug */
53 dbus_pending_call_block (echo_pending);
55 /* check the reply only to make sure we
56 are not getting errors unrelated
57 to the block in poll bug */
58 reply = dbus_pending_call_steal_reply (echo_pending);
62 printf ("Bail out! Reply is NULL ***\n");
66 if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR)
68 printf ("Bail out! Reply is error: %s ***\n", dbus_message_get_error_name (reply));
72 dbus_message_unref (reply);
73 dbus_pending_call_unref (dbus_pending);
74 dbus_pending_call_unref (echo_pending);
78 check_mutex_lock (DBusMutex *mutex1,
82 _dbus_assert (mutex1 != NULL);
83 _dbus_assert (mutex2 != NULL);
87 _dbus_assert (mutex1 == mutex2);
91 _dbus_assert (mutex1 != mutex2);
96 check_condvar_lock (DBusCondVar *condvar1,
97 DBusCondVar *condvar2,
100 _dbus_assert (condvar1 != NULL);
101 _dbus_assert (condvar2 != NULL);
105 _dbus_assert (condvar1 == condvar2);
109 _dbus_assert (condvar1 != condvar2);
113 /* This test outputs TAP syntax: http://testanything.org/ */
115 main (int argc, char *argv[])
118 DBusConnection *conn;
120 DBusMutex *mutex1, *dispatch_mutex1, *io_path_mutex1;
121 DBusCondVar *dispatch_cond1, *io_path_cond1;
122 DBusMutex *mutex2, *dispatch_mutex2, *io_path_mutex2;
123 DBusCondVar *dispatch_cond2, *io_path_cond2;
126 printf ("# Testing late thread init\n");
128 dbus_error_init (&error);
130 conn = dbus_bus_get (DBUS_BUS_SESSION, &error);
134 fprintf (stderr, "Bail out! Failed to open connection to session bus: %s\n",
136 dbus_error_free (&error);
140 _dbus_connection_test_get_locks (conn, &mutex1,
145 _run_iteration (conn);
146 _dbus_connection_test_get_locks (conn, &mutex2,
152 check_mutex_lock (mutex1, mutex2, TRUE);
153 check_mutex_lock (dispatch_mutex1, dispatch_mutex2, TRUE);
154 check_mutex_lock (io_path_mutex1, io_path_mutex2, TRUE);
155 check_condvar_lock (dispatch_cond1, dispatch_cond2, TRUE);
156 check_condvar_lock (io_path_cond1, io_path_cond2, TRUE);
157 printf ("ok %d\n", ++test_num);
159 if (!dbus_threads_init_default ())
161 fprintf (stderr, "Bail out! Failed to initialise threads: OOM\n");
165 _dbus_connection_test_get_locks (conn, &mutex1,
171 _run_iteration (conn);
172 _dbus_connection_test_get_locks (conn, &mutex2,
178 check_mutex_lock (mutex1, mutex2, TRUE);
179 check_mutex_lock (dispatch_mutex1, dispatch_mutex2, TRUE);
180 check_mutex_lock (io_path_mutex1, io_path_mutex2, TRUE);
181 check_condvar_lock (dispatch_cond1, dispatch_cond2, TRUE);
182 check_condvar_lock (io_path_cond1, io_path_cond2, TRUE);
183 printf ("ok %d\n", ++test_num);
185 method = dbus_message_new_method_call ("org.freedesktop.TestSuiteEchoService",
186 "/org/freedesktop/TestSuite",
187 "org.freedesktop.TestSuite",
189 dbus_connection_send (conn, method, NULL);
190 dbus_message_unref (method);
192 printf ("# Testing completed\n1..%d\n", test_num);