diff --git a/ros2cli/ros2cli/node/daemon.py b/ros2cli/ros2cli/node/daemon.py index bfa4f8fd2..dd8327770 100644 --- a/ros2cli/ros2cli/node/daemon.py +++ b/ros2cli/ros2cli/node/daemon.py @@ -27,15 +27,17 @@ from ros2cli.helpers import wait_for from ros2cli.xmlrpc.client import ServerProxy +from ros2cli.xmlrpc.client import TimeoutTransport class DaemonNode: - def __init__(self, args): + def __init__(self, args, *, timeout=None): self._args = args self._proxy = ServerProxy( daemon.get_xmlrpc_server_url(), - allow_none=True) + allow_none=True, + transport=TimeoutTransport(timeout=timeout)) self._methods = [] @property @@ -65,13 +67,16 @@ def __exit__(self, exc_type, exc_value, traceback): self._proxy.__exit__(exc_type, exc_value, traceback) -def is_daemon_running(args): +def is_daemon_running(args, timeout=None): """ Check if the daemon node is running. :param args: `DaemonNode` arguments namespace. + :param timeout: option duration, in seconds, to wait + for the daemon node to respond. If it is not given, + the global default timeout setting is used. """ - with DaemonNode(args) as node: + with DaemonNode(args, timeout=timeout) as node: return node.connected diff --git a/ros2cli/ros2cli/xmlrpc/client.py b/ros2cli/ros2cli/xmlrpc/client.py index 138569106..90383de4f 100644 --- a/ros2cli/ros2cli/xmlrpc/client.py +++ b/ros2cli/ros2cli/xmlrpc/client.py @@ -15,9 +15,24 @@ # Alias xmlrpc.client module objects to ensure client code uses ros2cli.xmlrpc from xmlrpc.client import ProtocolError from xmlrpc.client import ServerProxy +from xmlrpc.client import Transport __all__ = [ 'ProtocolError', - 'ServerProxy' + 'ServerProxy', + 'TimeoutTransport', ] + + +class TimeoutTransport(Transport): + + def __init__(self, *args, timeout=None, **kwargs): + super().__init__(*args, **kwargs) + self._timeout = timeout + + def make_connection(self, *args, **kwargs): + connection = super().make_connection(*args, **kwargs) + if self._timeout is not None: + connection.timeout = self._timeout + return connection diff --git a/ros2cli/test/test_strategy.py b/ros2cli/test/test_strategy.py index baaa414b4..28e360ad1 100644 --- a/ros2cli/test/test_strategy.py +++ b/ros2cli/test/test_strategy.py @@ -24,14 +24,14 @@ @pytest.fixture def enforce_no_daemon_is_running(): - if is_daemon_running(args=[]): + if is_daemon_running(args=[], timeout=5.0): assert shutdown_daemon(args=[], timeout=5.0) yield @pytest.fixture def enforce_daemon_is_running(): - if not is_daemon_running(args=[]): + if not is_daemon_running(args=[], timeout=5.0): assert spawn_daemon(args=[], timeout=5.0) yield