Skip to content

Commit 1a0f95e

Browse files
committed
Add timeout fundamentals for daemon client communication
Signed-off-by: Scott K Logan <[email protected]>
1 parent d3cfbd7 commit 1a0f95e

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

ros2cli/ros2cli/node/daemon.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,17 @@
2727
from ros2cli.helpers import wait_for
2828

2929
from ros2cli.xmlrpc.client import ServerProxy
30+
from ros2cli.xmlrpc.client import TimeoutTransport
3031

3132

3233
class DaemonNode:
3334

34-
def __init__(self, args):
35+
def __init__(self, args, *, timeout=None):
3536
self._args = args
3637
self._proxy = ServerProxy(
3738
daemon.get_xmlrpc_server_url(),
38-
allow_none=True)
39+
allow_none=True,
40+
transport=TimeoutTransport(timeout=timeout))
3941
self._methods = []
4042

4143
@property
@@ -65,13 +67,16 @@ def __exit__(self, exc_type, exc_value, traceback):
6567
self._proxy.__exit__(exc_type, exc_value, traceback)
6668

6769

68-
def is_daemon_running(args):
70+
def is_daemon_running(args, timeout=None):
6971
"""
7072
Check if the daemon node is running.
7173
7274
:param args: `DaemonNode` arguments namespace.
75+
:param timeout: option duration, in seconds, to wait
76+
for the daemon node to respond. If it is not given,
77+
the global default timeout setting is used.
7378
"""
74-
with DaemonNode(args) as node:
79+
with DaemonNode(args, timeout=timeout) as node:
7580
return node.connected
7681

7782

ros2cli/ros2cli/xmlrpc/client.py

+16-1
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,24 @@
1515
# Alias xmlrpc.client module objects to ensure client code uses ros2cli.xmlrpc
1616
from xmlrpc.client import ProtocolError
1717
from xmlrpc.client import ServerProxy
18+
from xmlrpc.client import Transport
1819

1920

2021
__all__ = [
2122
'ProtocolError',
22-
'ServerProxy'
23+
'ServerProxy',
24+
'TimeoutTransport',
2325
]
26+
27+
28+
class TimeoutTransport(Transport):
29+
30+
def __init__(self, *args, timeout=None, **kwargs):
31+
super().__init__(*args, **kwargs)
32+
self._timeout = timeout
33+
34+
def make_connection(self, *args, **kwargs):
35+
connection = super().make_connection(*args, **kwargs)
36+
if self._timeout is not None:
37+
connection.timeout = self._timeout
38+
return connection

ros2cli/test/test_strategy.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@
2424

2525
@pytest.fixture
2626
def enforce_no_daemon_is_running():
27-
if is_daemon_running(args=[]):
27+
if is_daemon_running(args=[], timeout=5.0):
2828
assert shutdown_daemon(args=[], timeout=5.0)
2929
yield
3030

3131

3232
@pytest.fixture
3333
def enforce_daemon_is_running():
34-
if not is_daemon_running(args=[]):
34+
if not is_daemon_running(args=[], timeout=5.0):
3535
assert spawn_daemon(args=[], timeout=5.0)
3636
yield
3737

0 commit comments

Comments
 (0)