bus: Assign a serial number for messages from the driver
[platform/upstream/dbus.git] / test / name-test / test-pending-call-timeout.c
1 /**
2 * Test to make sure that pending calls succeed when given a default,
3 * specific and infinite timeout.
4 **/
5
6 #include <config.h>
7 #include <dbus/dbus.h>
8 #include <dbus/dbus-sysdeps.h>
9 #include <stdio.h>
10 #include <limits.h>
11 #include <stdlib.h>
12
13 static void
14 _method_call (DBusConnection *conn,
15               int             timeout_milliseconds)
16 {
17   DBusPendingCall *pending;
18   DBusMessage *method;
19   DBusMessage *reply;
20   const char *echo = "echo";
21
22   /* send the message */
23   method = dbus_message_new_method_call ("org.freedesktop.DBus.TestSuiteEchoService",
24                                          "/org/freedesktop/TestSuite",
25                                          "org.freedesktop.TestSuite",
26                                          "DelayEcho");
27
28   if (method == NULL ||
29       !dbus_message_append_args (method, DBUS_TYPE_STRING, &echo, NULL) ||
30       !dbus_connection_send_with_reply (conn, method, &pending, timeout_milliseconds))
31     {
32       printf ("Bail out! OOM when building and sending message ***\n");
33       exit (1);
34     }
35   dbus_message_unref (method);
36   
37   /* block on the message */
38   dbus_pending_call_block (pending);
39
40   /* check the reply only to make sure we
41      are not getting errors unrelated
42      to the block in poll bug */
43   reply = dbus_pending_call_steal_reply (pending);
44
45   if (reply == NULL)
46     {
47       printf ("Bail out! Reply is NULL ***\n");
48       exit (1);
49     }
50
51   if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR)
52     {
53       printf ("Bail out! Reply is error: %s ***\n", dbus_message_get_error_name (reply));
54       exit (1);
55     } 
56
57   dbus_message_unref (reply);
58   dbus_pending_call_unref (pending);
59 }
60
61 static void
62 _run_iteration (DBusConnection *conn)
63 {
64   _method_call (conn, -1);
65   _method_call (conn, 10000);
66   _method_call (conn, INT_MAX);
67 }
68
69 /* This test outputs TAP syntax: http://testanything.org/ */
70 int
71 main (int argc, char *argv[])
72 {
73   long start_tv_sec, start_tv_usec;
74   long end_tv_sec, end_tv_usec;
75   int i;
76   DBusMessage *method;
77   DBusConnection *conn;
78   DBusError error;
79
80   printf ("# Testing pending call timeouts\n");
81
82   dbus_error_init (&error);
83
84   conn = dbus_bus_get (DBUS_BUS_SESSION, &error);
85
86   /* run 100 times to make sure */
87   for (i = 0; i < 100; i++)
88     {
89       long delta;
90       
91       _dbus_get_monotonic_time (&start_tv_sec, &start_tv_usec);
92       _run_iteration (conn);
93       _dbus_get_monotonic_time (&end_tv_sec, &end_tv_usec);
94
95       /* we just care about seconds */
96       delta = end_tv_sec - start_tv_sec;
97       printf ("ok %d - %lis\n", i + 1, delta);
98     }
99  
100   method = dbus_message_new_method_call ("org.freedesktop.TestSuiteEchoService",
101                                          "/org/freedesktop/TestSuite",
102                                          "org.freedesktop.TestSuite",
103                                          "Exit");
104   dbus_connection_send (conn, method, NULL);
105   dbus_message_unref (method);
106
107   printf ("# Testing completed\n1..%d\n", i);
108   exit (0);
109 }