ofdbus/dbus/tsrc/testapps/dbus_test_cases/name_test/test-pending-call-dispatch.c
changeset 0 e4d67989cc36
child 18 47c74d1534e1
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 /* Portion Copyright © 2008 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.*/
       
     2 /**
       
     3 * Test to make sure we don't get stuck polling a dbus connection
       
     4 * which has no data on the socket.  This was an issue where
       
     5 * one pending call would read all the data off the bus
       
     6 * and the second pending call would not check to see
       
     7 * if its data had already been read before polling the connection
       
     8 * and blocking.
       
     9 **/
       
    10 
       
    11 #include <dbus/dbus.h>
       
    12 #ifndef __SYMBIAN32__
       
    13 #include <dbus/dbus-sysdeps.h>
       
    14 #else
       
    15 #include "dbus-sysdeps.h"
       
    16 #endif //__SYMBIAN32__
       
    17 #include <stdio.h>
       
    18 #include <stdlib.h>
       
    19 
       
    20 #define LOG_FILE "c:\\logs\\test_pending_call_dispatch_log1.txt"
       
    21 #include "std_log_result.h"
       
    22 #define LOG_FILENAME_LINE __FILE__, __LINE__
       
    23 
       
    24 void create_xml(int result)
       
    25 	{
       
    26 	if(result)
       
    27 		assert_failed = 1;
       
    28 	
       
    29 	testResultXml("test_pending_call_dispatch");
       
    30     close_log_file();
       
    31 	}
       
    32 
       
    33 
       
    34 static void
       
    35 _run_iteration (DBusConnection *conn)
       
    36 {
       
    37   DBusPendingCall *echo_pending;
       
    38   DBusPendingCall *dbus_pending;
       
    39   DBusMessage *method;
       
    40   DBusMessage *reply;
       
    41   char *echo = "echo";
       
    42 
       
    43   /* send the first message */
       
    44   method = dbus_message_new_method_call ("org.freedesktop.DBus.TestSuiteEchoService",
       
    45                                          "/org/freedesktop/TestSuite",
       
    46                                          "org.freedesktop.TestSuite",
       
    47                                          "Echo");
       
    48 
       
    49   dbus_message_append_args (method, DBUS_TYPE_STRING, &echo, NULL);
       
    50   dbus_connection_send_with_reply (conn, method, &echo_pending, -1);
       
    51   dbus_message_unref (method);
       
    52   
       
    53   /* send the second message */
       
    54   method = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
       
    55                                          DBUS_PATH_DBUS,
       
    56                                          "org.freedesktop.Introspectable",
       
    57                                          "Introspect");
       
    58 
       
    59   dbus_connection_send_with_reply (conn, method, &dbus_pending, -1);
       
    60   dbus_message_unref (method);
       
    61 
       
    62   /* block on the second message (should return immediately) */
       
    63   dbus_pending_call_block (dbus_pending);
       
    64 
       
    65   /* block on the first message */
       
    66   /* if it does not return immediately chances 
       
    67      are we hit the block in poll bug */
       
    68   dbus_pending_call_block (echo_pending);
       
    69 
       
    70   /* check the reply only to make sure we
       
    71      are not getting errors unrelated
       
    72      to the block in poll bug */
       
    73   reply = dbus_pending_call_steal_reply (echo_pending);
       
    74 
       
    75   if (reply == NULL)
       
    76     {
       
    77       printf ("Failed: Reply is NULL ***\n");
       
    78       std_log(LOG_FILENAME_LINE, "Failed: Reply is NULL ***\n");
       
    79       create_xml(1);
       
    80       exit (1);
       
    81     }
       
    82 
       
    83   if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR)
       
    84     {
       
    85       printf ("Failed: Reply is error: %s ***\n", dbus_message_get_error_name (reply));
       
    86       std_log(LOG_FILENAME_LINE, "Failed: Reply is error: %s ***\n", dbus_message_get_error_name (reply));
       
    87       create_xml(1);
       
    88       exit (1);
       
    89     } 
       
    90 
       
    91   dbus_message_unref (reply);
       
    92   dbus_pending_call_unref (dbus_pending);
       
    93   dbus_pending_call_unref (echo_pending);
       
    94   
       
    95 }
       
    96 
       
    97 int
       
    98 main ()
       
    99 {
       
   100   long start_tv_sec, start_tv_usec;
       
   101   long end_tv_sec, end_tv_usec;
       
   102   int i;
       
   103   DBusMessage *method;
       
   104   DBusConnection *conn;
       
   105   DBusError error;
       
   106   
       
   107   /* Time each iteration and make sure it doesn't take more than 5 seconds
       
   108      to complete.  Outside influences may cause connections to take longer
       
   109      but if it does and we are stuck in a poll call then we know the 
       
   110      stuck in poll bug has come back to haunt us */
       
   111 
       
   112   printf ("*** Testing stuck in poll\n");
       
   113   std_log(LOG_FILENAME_LINE, "*** Testing stuck in poll\n");
       
   114   
       
   115   dbus_error_init (&error);
       
   116 
       
   117   conn = dbus_bus_get (DBUS_BUS_SESSION, &error);
       
   118 
       
   119   /* run 100 times to make sure */
       
   120   for (i = 0; i < 100; i++)
       
   121     {
       
   122       long delta;
       
   123       
       
   124       _dbus_get_current_time (&start_tv_sec, &start_tv_usec);
       
   125       _run_iteration (conn);
       
   126       _dbus_get_current_time (&end_tv_sec, &end_tv_usec);
       
   127 
       
   128       /* we just care about seconds */
       
   129       delta = end_tv_sec - start_tv_sec;
       
   130       printf ("Iter %i: %lis\n", i, delta);
       
   131       std_log(LOG_FILENAME_LINE, "Iter %i: %lis\n", i, delta);
       
   132       if (delta >= 5)
       
   133         {
       
   134 	  printf ("Failed: looks like we might have been be stuck in poll ***\n");
       
   135       std_log(LOG_FILENAME_LINE, "Failed: looks like we might have been be stuck in poll ***\n");
       
   136 	  create_xml(1);
       
   137 	  exit (1);
       
   138 	}
       
   139     }
       
   140  
       
   141   method = dbus_message_new_method_call ("org.freedesktop.TestSuiteEchoService",
       
   142                                          "/org/freedesktop/TestSuite",
       
   143                                          "org.freedesktop.TestSuite",
       
   144                                          "Exit");
       
   145   dbus_connection_send (conn, method, NULL);
       
   146   dbus_message_unref (method);
       
   147 
       
   148   printf ("Success ***\n");
       
   149   std_log(LOG_FILENAME_LINE, "Test Successful.");
       
   150 //  ch=getchar();
       
   151   create_xml(0);
       
   152   exit (0);
       
   153 }