Add IgnoreOnIsolate=yes to dbus.service
[platform/upstream/dbus.git] / test / name-test / test-pending-call-dispatch.c
1 /*
2  * Copyright © 2006 Red Hat Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person
5  * obtaining a copy of this software and associated documentation files
6  * (the "Software"), to deal in the Software without restriction,
7  * including without limitation the rights to use, copy, modify, merge,
8  * publish, distribute, sublicense, and/or sell copies of the Software,
9  * and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 /**
25 * Test to make sure we don't get stuck polling a dbus connection
26 * which has no data on the socket.  This was an issue where
27 * one pending call would read all the data off the bus
28 * and the second pending call would not check to see
29 * if its data had already been read before polling the connection
30 * and blocking.
31 **/
32
33 #include <config.h>
34 #include <dbus/dbus.h>
35 #include <dbus/dbus-sysdeps.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38
39 static void
40 _run_iteration (DBusConnection *conn)
41 {
42   DBusPendingCall *echo_pending;
43   DBusPendingCall *dbus_pending;
44   DBusMessage *method;
45   DBusMessage *reply;
46   const char *echo = "echo";
47
48   /* send the first message */
49   method = dbus_message_new_method_call ("org.freedesktop.DBus.TestSuiteEchoService",
50                                          "/org/freedesktop/TestSuite",
51                                          "org.freedesktop.TestSuite",
52                                          "Echo");
53
54   if (!dbus_message_append_args (method, DBUS_TYPE_STRING, &echo, NULL))
55     {
56       fprintf (stderr, "Bail out! Failed to append arguments: OOM\n");
57       exit (1);
58     }
59
60   dbus_connection_send_with_reply (conn, method, &echo_pending, -1);
61   dbus_message_unref (method);
62   
63   /* send the second message */
64   method = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
65                                          DBUS_PATH_DBUS,
66                                          "org.freedesktop.Introspectable",
67                                          "Introspect");
68
69   dbus_connection_send_with_reply (conn, method, &dbus_pending, -1);
70   dbus_message_unref (method);
71
72   /* block on the second message (should return immediately) */
73   dbus_pending_call_block (dbus_pending);
74
75   /* block on the first message */
76   /* if it does not return immediately chances 
77      are we hit the block in poll bug */
78   dbus_pending_call_block (echo_pending);
79
80   /* check the reply only to make sure we
81      are not getting errors unrelated
82      to the block in poll bug */
83   reply = dbus_pending_call_steal_reply (echo_pending);
84
85   if (reply == NULL)
86     {
87       printf ("Bail out! Reply is NULL ***\n");
88       exit (1);
89     }
90
91   if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR)
92     {
93       printf ("Bail out! Reply is error: %s ***\n", dbus_message_get_error_name (reply));
94       exit (1);
95     } 
96
97   dbus_message_unref (reply);
98   dbus_pending_call_unref (dbus_pending);
99   dbus_pending_call_unref (echo_pending);
100   
101 }
102
103 /* This test outputs TAP syntax: http://testanything.org/ */
104 int
105 main (int argc, char *argv[])
106 {
107   long start_tv_sec, start_tv_usec;
108   long end_tv_sec, end_tv_usec;
109   int i;
110   DBusMessage *method;
111   DBusConnection *conn;
112   DBusError error;
113
114   /* Time each iteration and make sure it doesn't take more than 5 seconds
115      to complete.  Outside influences may cause connections to take longer
116      but if it does and we are stuck in a poll call then we know the 
117      stuck in poll bug has come back to haunt us */
118
119   printf ("# Testing stuck in poll\n");
120
121   dbus_error_init (&error);
122
123   conn = dbus_bus_get (DBUS_BUS_SESSION, &error);
124
125   /* run 100 times to make sure */
126   for (i = 0; i < 100; i++)
127     {
128       long delta;
129       
130       _dbus_get_monotonic_time (&start_tv_sec, &start_tv_usec);
131       _run_iteration (conn);
132       _dbus_get_monotonic_time (&end_tv_sec, &end_tv_usec);
133
134       /* we just care about seconds */
135       delta = end_tv_sec - start_tv_sec;
136       printf ("ok %d - %lis\n", i + 1, delta);
137       if (delta >= 5)
138         {
139           printf ("Bail out! Looks like we might have been be stuck in poll ***\n");
140           exit (1);
141         }
142     }
143  
144   method = dbus_message_new_method_call ("org.freedesktop.TestSuiteEchoService",
145                                          "/org/freedesktop/TestSuite",
146                                          "org.freedesktop.TestSuite",
147                                          "Exit");
148   dbus_connection_send (conn, method, NULL);
149   dbus_message_unref (method);
150
151   printf ("# Testing completed\n1..%d\n", i);
152   exit (0);
153 }