bus: Assign a serial number for messages from the driver
[platform/upstream/dbus.git] / test / name-test / test-threads-init.c
1 /**
2  * Test to make sure late thread initialization works
3  */
4
5 #include <config.h>
6 #include <dbus/dbus.h>
7 #include <dbus/dbus-sysdeps.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10
11 #include <dbus/dbus-internals.h>
12 #include <dbus/dbus-connection-internal.h>
13
14 static void
15 _run_iteration (DBusConnection *conn)
16 {
17   DBusPendingCall *echo_pending;
18   DBusPendingCall *dbus_pending;
19   DBusMessage *method;
20   DBusMessage *reply;
21   const char *echo = "echo";
22
23   /* send the first message */
24   method = dbus_message_new_method_call ("org.freedesktop.DBus.TestSuiteEchoService",
25                                          "/org/freedesktop/TestSuite",
26                                          "org.freedesktop.TestSuite",
27                                          "Echo");
28
29   if (!dbus_message_append_args (method, DBUS_TYPE_STRING, &echo, NULL))
30     {
31       fprintf (stderr, "Bail out! Failed to append arguments: OOM\n");
32       exit (1);
33     }
34
35   dbus_connection_send_with_reply (conn, method, &echo_pending, -1);
36   dbus_message_unref (method);
37   
38   /* send the second message */
39   method = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
40                                          DBUS_PATH_DBUS,
41                                          "org.freedesktop.Introspectable",
42                                          "Introspect");
43
44   dbus_connection_send_with_reply (conn, method, &dbus_pending, -1);
45   dbus_message_unref (method);
46
47   /* block on the second message (should return immediately) */
48   dbus_pending_call_block (dbus_pending);
49
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);
54
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);
59
60   if (reply == NULL)
61     {
62       printf ("Bail out! Reply is NULL ***\n");
63       exit (1);
64     }
65
66   if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR)
67     {
68       printf ("Bail out! Reply is error: %s ***\n", dbus_message_get_error_name (reply));
69       exit (1);
70     } 
71
72   dbus_message_unref (reply);
73   dbus_pending_call_unref (dbus_pending);
74   dbus_pending_call_unref (echo_pending);
75   
76 }
77 static void
78 check_mutex_lock (DBusMutex *mutex1, 
79                   DBusMutex *mutex2, 
80                   dbus_bool_t is_same)
81 {
82   _dbus_assert (mutex1 != NULL);
83   _dbus_assert (mutex2 != NULL);
84   
85   if (is_same)
86     {
87       _dbus_assert (mutex1 == mutex2);
88     }
89   else
90     {
91       _dbus_assert (mutex1 != mutex2);
92     }
93 }
94
95 static void
96 check_condvar_lock (DBusCondVar *condvar1,  
97                     DBusCondVar *condvar2,   
98                     dbus_bool_t is_same)
99 {
100   _dbus_assert (condvar1 != NULL);
101   _dbus_assert (condvar2 != NULL);
102
103   if (is_same)
104     {
105       _dbus_assert (condvar1 == condvar2);
106     }
107   else
108     {
109       _dbus_assert (condvar1 != condvar2);
110     }
111 }
112
113 /* This test outputs TAP syntax: http://testanything.org/ */
114 int
115 main (int argc, char *argv[])
116 {
117   DBusMessage *method;
118   DBusConnection *conn;
119   DBusError error;
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;
124   int test_num = 0;
125
126   printf ("# Testing late thread init\n");
127
128   dbus_error_init (&error);
129
130   conn = dbus_bus_get (DBUS_BUS_SESSION, &error);
131
132   if (conn == NULL)
133     {
134       fprintf (stderr, "Bail out! Failed to open connection to session bus: %s\n",
135                error.message);
136       dbus_error_free (&error);
137       return 1;
138     }
139
140   _dbus_connection_test_get_locks (conn, &mutex1,
141                                          &dispatch_mutex1, 
142                                          &io_path_mutex1,
143                                          &dispatch_cond1,
144                                          &io_path_cond1);
145   _run_iteration (conn);
146   _dbus_connection_test_get_locks (conn, &mutex2,
147                                          &dispatch_mutex2,
148                                          &io_path_mutex2,
149                                          &dispatch_cond2,
150                                          &io_path_cond2);
151
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);
158
159   if (!dbus_threads_init_default ())
160     {
161       fprintf (stderr, "Bail out! Failed to initialise threads: OOM\n");
162       return 1;
163     }
164
165   _dbus_connection_test_get_locks (conn, &mutex1,
166                                          &dispatch_mutex1,
167                                          &io_path_mutex1,
168                                          &dispatch_cond1,
169                                          &io_path_cond1);
170
171   _run_iteration (conn);
172   _dbus_connection_test_get_locks (conn, &mutex2,
173                                          &dispatch_mutex2,
174                                          &io_path_mutex2,
175                                          &dispatch_cond2,
176                                          &io_path_cond2);
177
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);
184
185   method = dbus_message_new_method_call ("org.freedesktop.TestSuiteEchoService",
186                                          "/org/freedesktop/TestSuite",
187                                          "org.freedesktop.TestSuite",
188                                          "Exit");
189   dbus_connection_send (conn, method, NULL);
190   dbus_message_unref (method);
191
192   printf ("# Testing completed\n1..%d\n", test_num);
193   exit (0);
194 }