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