Skip to content

Commit e228f08

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

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,3 +179,15 @@ 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+
184+
def test_exception_on_disconnect(nats_url):
185+
with NATSClient(nats_url, socket_timeout=2) as client:
186+
client.subscribe(
187+
"test-subject", callback=lambda x: x, queue="test-queue", max_messages=2
188+
)
189+
190+
client._socket_file.readline = lambda: b""
191+
192+
with pytest.raises(ConnectionResetError):
193+
client.wait()

0 commit comments

Comments
 (0)