Skip to content

Commit a3199a1

Browse files
committed
Raise exception on socket disconnect
1 parent 0bfe172 commit a3199a1

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

pynats/client.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,9 @@ def _readline(self, *, size: int = None) -> bytes:
272272
line = cast(bytes, self._socket_file.readline())
273273
read.write(line)
274274

275+
if len(line) == 0:
276+
raise ConnectionResetError(self)
277+
275278
if size is not None:
276279
if read.tell() == size + len(_CRLF_):
277280
break

tests/test_client.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,3 +179,14 @@ def test_request_timeout(nats_url):
179179
with NATSClient(nats_url, socket_timeout=2) as client:
180180
with pytest.raises(socket.timeout):
181181
client.request("test-subject")
182+
183+
def test_exception_on_disconnect(nats_url):
184+
with NATSClient(nats_url, socket_timeout=2) as client:
185+
sub = client.subscribe(
186+
"test-subject", callback=lambda x: x, queue="test-queue", max_messages=2
187+
)
188+
189+
client._socket_file.readline = lambda: b""
190+
191+
with pytest.raises(ConnectionResetError):
192+
client.wait()

0 commit comments

Comments
 (0)