From a53634cf1274fc28d5098d96326bc66a12746b72 Mon Sep 17 00:00:00 2001 From: Scott James Remnant Date: Mon, 11 May 2009 23:29:52 +0100 Subject: [PATCH 11/11] Add tests for pending call timeouts * test/test-service.c (handle_delay_echo, path_message_func): Add a variant of the Echo method which sleeps for a short time. * test/name-test/test-pending-call-timeout.c: Run tests with default, specified and infinite timeout to make sure we get the reply. * test/name-test/run-test.sh: Run the new test * test/name-test/Makefile.am: Build the new test Signed-off-by: Scott James Remnant --- test/name-test/.gitignore | 1 + test/name-test/Makefile.am | 8 ++- test/name-test/run-test.sh | 3 + test/name-test/test-pending-call-timeout.c | 102 ++++++++++++++++++++++++++++ test/test-service.c | 60 ++++++++++++++++ 5 files changed, 173 insertions(+), 1 deletions(-) create mode 100644 test/name-test/test-pending-call-timeout.c diff --git a/test/name-test/.gitignore b/test/name-test/.gitignore index 09930f1..7e4233e 100644 --- a/test/name-test/.gitignore +++ b/test/name-test/.gitignore @@ -4,6 +4,7 @@ Makefile Makefile.in test-names test-pending-call-dispatch +test-pending-call-timeout test-threads-init test-ids run-with-tmp-session-bus.conf diff --git a/test/name-test/Makefile.am b/test/name-test/Makefile.am index 10a2536..1c73b87 100644 --- a/test/name-test/Makefile.am +++ b/test/name-test/Makefile.am @@ -16,7 +16,7 @@ if DBUS_BUILD_TESTS ## we use noinst_PROGRAMS not check_PROGRAMS for TESTS so that we ## build even when not doing "make check" -noinst_PROGRAMS=test-names test-pending-call-dispatch test-threads-init test-ids test-shutdown test-privserver test-privserver-client +noinst_PROGRAMS=test-names test-pending-call-dispatch test-pending-call-timeout test-threads-init test-ids test-shutdown test-privserver test-privserver-client test_names_SOURCES= \ test-names.c @@ -30,6 +30,12 @@ test_pending_call_dispatch_SOURCES = \ test_pending_call_dispatch_LDADD=$(top_builddir)/dbus/libdbus-convenience.la $(DBUS_TEST_LIBS) test_pending_call_dispatch_LDFLAGS=@R_DYNAMIC_LDFLAG@ +test_pending_call_timeout_SOURCES = \ + test-pending-call-timeout.c + +test_pending_call_timeout_LDADD=$(top_builddir)/dbus/libdbus-convenience.la $(DBUS_TEST_LIBS) +test_pending_call_timeout_LDFLAGS=@R_DYNAMIC_LDFLAG@ + test_threads_init_SOURCES = \ test-threads-init.c diff --git a/test/name-test/run-test.sh b/test/name-test/run-test.sh index 3699bc9..fba4558 100755 --- a/test/name-test/run-test.sh +++ b/test/name-test/run-test.sh @@ -39,6 +39,9 @@ ${DBUS_TOP_BUILDDIR}/libtool --mode=execute $DEBUG $DBUS_TOP_BUILDDIR/test/name- echo "running test-pending-call-dispatch" ${DBUS_TOP_BUILDDIR}/libtool --mode=execute $DEBUG $DBUS_TOP_BUILDDIR/test/name-test/test-pending-call-dispatch || die "test-pending-call-dispatch failed" +echo "running test-pending-call-timeout" +${DBUS_TOP_BUILDDIR}/libtool --mode=execute $DEBUG $DBUS_TOP_BUILDDIR/test/name-test/test-pending-call-timeout || die "test-pending-call-timeout failed" + echo "running test-threads-init" ${DBUS_TOP_BUILDDIR}/libtool --mode=execute $DEBUG $DBUS_TOP_BUILDDIR/test/name-test/test-threads-init || die "test-threads-init failed" diff --git a/test/name-test/test-pending-call-timeout.c b/test/name-test/test-pending-call-timeout.c new file mode 100644 index 0000000..53a0c3a --- /dev/null +++ b/test/name-test/test-pending-call-timeout.c @@ -0,0 +1,102 @@ +/** +* Test to make sure that pending calls succeed when given a default, +* specific and infinite timeout. +**/ + +#include +#include +#include +#include +#include + +static void +_method_call (DBusConnection *conn, + int timeout_milliseconds) +{ + DBusPendingCall *pending; + DBusMessage *method; + DBusMessage *reply; + char *echo = "echo"; + + /* send the message */ + method = dbus_message_new_method_call ("org.freedesktop.DBus.TestSuiteEchoService", + "/org/freedesktop/TestSuite", + "org.freedesktop.TestSuite", + "DelayEcho"); + + dbus_message_append_args (method, DBUS_TYPE_STRING, &echo, NULL); + dbus_connection_send_with_reply (conn, method, &pending, timeout_milliseconds); + dbus_message_unref (method); + + /* block on the message */ + dbus_pending_call_block (pending); + + /* check the reply only to make sure we + are not getting errors unrelated + to the block in poll bug */ + reply = dbus_pending_call_steal_reply (pending); + + if (reply == NULL) + { + printf ("Failed: Reply is NULL ***\n"); + exit (1); + } + + if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR) + { + printf ("Failed: Reply is error: %s ***\n", dbus_message_get_error_name (reply)); + exit (1); + } + + dbus_message_unref (reply); + dbus_pending_call_unref (pending); +} + +static void +_run_iteration (DBusConnection *conn) +{ + _method_call (conn, -1); + _method_call (conn, 10000); + _method_call (conn, INT_MAX); +} + +int +main (int argc, char *argv[]) +{ + long start_tv_sec, start_tv_usec; + long end_tv_sec, end_tv_usec; + int i; + DBusMessage *method; + DBusConnection *conn; + DBusError error; + + printf ("*** Testing pending call timeouts\n"); + + dbus_error_init (&error); + + conn = dbus_bus_get (DBUS_BUS_SESSION, &error); + + /* run 100 times to make sure */ + for (i = 0; i < 100; i++) + { + long delta; + + _dbus_get_current_time (&start_tv_sec, &start_tv_usec); + _run_iteration (conn); + _dbus_get_current_time (&end_tv_sec, &end_tv_usec); + + /* we just care about seconds */ + delta = end_tv_sec - start_tv_sec; + printf ("Iter %i: %lis\n", i, delta); + } + + method = dbus_message_new_method_call ("org.freedesktop.TestSuiteEchoService", + "/org/freedesktop/TestSuite", + "org.freedesktop.TestSuite", + "Exit"); + dbus_connection_send (conn, method, NULL); + dbus_message_unref (method); + + printf ("Success ***\n"); + exit (0); +} diff --git a/test/test-service.c b/test/test-service.c index bd2a463..57ece0f 100644 --- a/test/test-service.c +++ b/test/test-service.c @@ -223,6 +223,62 @@ handle_echo (DBusConnection *connection, return DBUS_HANDLER_RESULT_HANDLED; } +static DBusHandlerResult +handle_delay_echo (DBusConnection *connection, + DBusMessage *message) +{ + DBusError error; + DBusMessage *reply; + char *s; + + _dbus_verbose ("sleeping for a short time\n"); + + usleep (50000); + + _dbus_verbose ("sending reply to DelayEcho method\n"); + + dbus_error_init (&error); + + if (!dbus_message_get_args (message, + &error, + DBUS_TYPE_STRING, &s, + DBUS_TYPE_INVALID)) + { + reply = dbus_message_new_error (message, + error.name, + error.message); + + if (reply == NULL) + die ("No memory\n"); + + if (!dbus_connection_send (connection, reply, NULL)) + die ("No memory\n"); + + dbus_message_unref (reply); + + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; + } + + reply = dbus_message_new_method_return (message); + if (reply == NULL) + die ("No memory\n"); + + if (!dbus_message_append_args (reply, + DBUS_TYPE_STRING, &s, + DBUS_TYPE_INVALID)) + die ("No memory"); + + if (!dbus_connection_send (connection, reply, NULL)) + die ("No memory\n"); + + fprintf (stderr, "DelayEcho service echoed string: \"%s\"\n", s); + + dbus_message_unref (reply); + + return DBUS_HANDLER_RESULT_HANDLED; +} + + static void path_unregistered_func (DBusConnection *connection, void *user_data) @@ -241,6 +297,10 @@ path_message_func (DBusConnection *connection, return handle_echo (connection, message); else if (dbus_message_is_method_call (message, "org.freedesktop.TestSuite", + "DelayEcho")) + return handle_delay_echo (connection, message); + else if (dbus_message_is_method_call (message, + "org.freedesktop.TestSuite", "Exit")) { quit (); -- 1.6.0.4