From 5ab6cdc200267095edd56645df536dac5d4dc986 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Tue, 31 Jul 2007 12:38:02 +0100 Subject: [PATCH] Measure async call timeout in seconds as intended, not in ms (blocking calls already used seconds). Add regression tests --- dbus/connection.py | 2 +- test/test-client.py | 40 ++++++++++++++++++++++++++++++++++++++++ test/test-service.py | 13 +++++++++++++ 3 files changed, 54 insertions(+), 1 deletion(-) diff --git a/dbus/connection.py b/dbus/connection.py index 8c06f2b..9ea02f6 100644 --- a/dbus/connection.py +++ b/dbus/connection.py @@ -564,7 +564,7 @@ class Connection(_Connection): error_handler(TypeError('Unexpected type for reply ' 'message: %r' % message)) return self.send_message_with_reply(message, msg_reply_handler, - timeout/1000.0, + timeout, require_main_loop=require_main_loop) def call_blocking(self, bus_name, object_path, dbus_interface, method, diff --git a/test/test-client.py b/test/test-client.py index 441d75c..b600d9d 100755 --- a/test/test-client.py +++ b/test/test-client.py @@ -424,6 +424,46 @@ class TestDBusBindings(unittest.TestCase): #self.assertEquals(rel, '/Badger/Mushroom') self.assertEquals(unique_name, obj.bus_name) + def testTimeoutSync(self): + self.assert_(self.iface.BlockFor500ms(timeout=1.0) is None) + self.assertRaises(dbus.DBusException, + lambda: self.iface.BlockFor500ms(timeout=0.25)) + + def testTimeoutAsyncClient(self): + loop = gobject.MainLoop() + passes = [] + fails = [] + def correctly_returned(): + passes.append('1000') + if len(passes) + len(fails) >= 2: + loop.quit() + def correctly_failed(exc): + passes.append('250') + if len(passes) + len(fails) >= 2: + loop.quit() + def incorrectly_returned(): + fails.append('250') + if len(passes) + len(fails) >= 2: + loop.quit() + def incorrectly_failed(exc): + fails.append('1000') + if len(passes) + len(fails) >= 2: + loop.quit() + self.iface.BlockFor500ms(timeout=1.0, + reply_handler=correctly_returned, + error_handler=incorrectly_failed) + self.iface.BlockFor500ms(timeout=0.25, + reply_handler=incorrectly_returned, + error_handler=correctly_failed) + loop.run() + self.assertEquals(passes, ['250', '1000']) + self.assertEquals(fails, []) + + def testTimeoutAsyncService(self): + self.assert_(self.iface.AsyncWait500ms(timeout=1.0) is None) + self.assertRaises(dbus.DBusException, + lambda: self.iface.AsyncWait500ms(timeout=0.25)) + """ Remove this for now class TestDBusPythonToGLibBindings(unittest.TestCase): def setUp(self): diff --git a/test/test-service.py b/test/test-service.py index dcd511e..ef4efe2 100755 --- a/test/test-service.py +++ b/test/test-service.py @@ -22,6 +22,7 @@ import sys import os import logging +from time import sleep builddir = os.path.normpath(os.environ["DBUS_TOP_BUILDDIR"]) pydir = os.path.normpath(os.environ["DBUS_TOP_SRCDIR"]) @@ -279,6 +280,18 @@ class TestObject(dbus.service.Object, TestInterface): # https://bugs.freedesktop.org/show_bug.cgi?id=10174 return dbus.String('abc'), dbus.Int32(123) + @dbus.service.method(IFACE, in_signature='', out_signature='') + def BlockFor500ms(self): + sleep(0.5) + + @dbus.service.method(IFACE, in_signature='', out_signature='', + async_callbacks=('return_cb', 'raise_cb')) + def AsyncWait500ms(self, return_cb, raise_cb): + def return_from_async_wait(): + return_cb() + return False + gobject.timeout_add(500, return_from_async_wait) + session_bus = dbus.SessionBus() global_name = dbus.service.BusName(NAME, bus=session_bus) object = TestObject(global_name) -- 2.11.4.GIT