Consistently include <config.h> in all C source files and never in header files.
[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   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   dbus_message_append_args (method, DBUS_TYPE_STRING, &echo, NULL);
29   dbus_connection_send_with_reply (conn, method, &pending, timeout_milliseconds);
30   dbus_message_unref (method);
31   
32   /* block on the message */
33   dbus_pending_call_block (pending);
34
35   /* check the reply only to make sure we
36      are not getting errors unrelated
37      to the block in poll bug */
38   reply = dbus_pending_call_steal_reply (pending);
39
40   if (reply == NULL)
41     {
42       printf ("Failed: Reply is NULL ***\n");
43       exit (1);
44     }
45
46   if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR)
47     {
48       printf ("Failed: Reply is error: %s ***\n", dbus_message_get_error_name (reply));
49       exit (1);
50     } 
51
52   dbus_message_unref (reply);
53   dbus_pending_call_unref (pending);
54 }
55
56 static void
57 _run_iteration (DBusConnection *conn)
58 {
59   _method_call (conn, -1);
60   _method_call (conn, 10000);
61   _method_call (conn, INT_MAX);
62 }
63
64 int
65 main (int argc, char *argv[])
66 {
67   long start_tv_sec, start_tv_usec;
68   long end_tv_sec, end_tv_usec;
69   int i;
70   DBusMessage *method;
71   DBusConnection *conn;
72   DBusError error;
73
74   printf ("*** Testing pending call timeouts\n");
75
76   dbus_error_init (&error);
77
78   conn = dbus_bus_get (DBUS_BUS_SESSION, &error);
79
80   /* run 100 times to make sure */
81   for (i = 0; i < 100; i++)
82     {
83       long delta;
84       
85       _dbus_get_current_time (&start_tv_sec, &start_tv_usec);
86       _run_iteration (conn);
87       _dbus_get_current_time (&end_tv_sec, &end_tv_usec);
88
89       /* we just care about seconds */
90       delta = end_tv_sec - start_tv_sec;
91       printf ("Iter %i: %lis\n", i, delta);
92     }
93  
94   method = dbus_message_new_method_call ("org.freedesktop.TestSuiteEchoService",
95                                          "/org/freedesktop/TestSuite",
96                                          "org.freedesktop.TestSuite",
97                                          "Exit");
98   dbus_connection_send (conn, method, NULL);
99   dbus_message_unref (method);
100
101   printf ("Success ***\n");
102   exit (0);
103 }