Skip to content

Commit 446bbf5

Browse files
author
paskozdilar
committed
Updates
- fix client timeout - add client timeout test - add socket cleanup on garbage collection
1 parent df32be0 commit 446bbf5

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

tests/test_rpc.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44
import sys
55
import tempfile
66
import time
7+
8+
import pytest
9+
710
from zrpc.server import Server, rpc_method
8-
from zrpc.client import Client
11+
from zrpc.client import Client, RPCTimeoutError
912

1013

1114
def test_basic_rpc():
@@ -175,3 +178,23 @@ def run_client():
175178
multiprocessing.Process(target=run_client, daemon=True).start()
176179

177180
assert rpc_event.wait(1)
181+
182+
183+
def test_client_timeout():
184+
"""
185+
Test whether client raises RPCTimeoutError on `call()` timeout.
186+
"""
187+
188+
client_event = multiprocessing.Event()
189+
190+
def run_client():
191+
with tempfile.TemporaryDirectory() as tempdir:
192+
client = Client(socket_dir=tempdir)
193+
try:
194+
client.call('non_existant', 'non_existant', timeout=0.1)
195+
except RPCTimeoutError:
196+
client_event.set()
197+
198+
multiprocessing.Process(target=run_client, daemon=True).start()
199+
200+
assert client_event.wait(1)

zrpc/client.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ def __init__(self, socket_dir=None):
3939
except (OSError, zmq.ZMQError) as exc:
4040
raise ConnectError('Cannot connect client') from exc
4141

42+
def __del__(self):
43+
try:
44+
for socket in self._sockets:
45+
socket.close(linger=0)
46+
except AttributeError:
47+
pass
48+
4249
def __connect(self, socket_name):
4350
context = self._context
4451
sockets = self._sockets
@@ -89,12 +96,13 @@ def call(self, server, method, args=(), kwargs={}, timeout=None):
8996
start_time = time.monotonic()
9097
events = {}
9198

92-
current_time = time.monotonic()
99+
current_time = start_time
100+
elapsed_time = current_time - start_time
93101

94102
iter_timeout = 1
95-
while (current_time - start_time) < timeout:
103+
while elapsed_time <= timeout:
96104
socket.send(request)
97-
timeout_ms = 1000 * max(0, min(iter_timeout, timeout - (current_time - start_time)))
105+
timeout_ms = 1000 * max(0, min(iter_timeout, timeout - elapsed_time))
98106
logging.debug('Polling sockets with {}ms timeout'.format(timeout_ms))
99107
events = dict(self._poller.poll(timeout=timeout_ms))
100108

@@ -106,6 +114,9 @@ def call(self, server, method, args=(), kwargs={}, timeout=None):
106114
self.__connect(server)
107115
socket = sockets[server]
108116

117+
current_time = time.monotonic()
118+
elapsed_time = current_time - start_time
119+
109120
if socket not in events:
110121
raise RPCTimeoutError('Service "%s" not responding' % server)
111122

zrpc/server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ def stop(self):
136136
try:
137137
if not self._started:
138138
return
139-
self._context.term()
139+
self._socket.close(linger=0)
140140
os.unlink(self._socket_path)
141141
except (OSError, AttributeError):
142142
pass

0 commit comments

Comments
 (0)