Skip to content

Commit

Permalink
Raise exception on socket disconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
insideable committed Sep 5, 2019
1 parent 0bfe172 commit b43cecc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pynats/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,9 @@ def _readline(self, *, size: int = None) -> bytes:
line = cast(bytes, self._socket_file.readline())
read.write(line)

if len(line) == 0:
raise ConnectionResetError(self)

if size is not None:
if read.tell() == size + len(_CRLF_):
break
Expand Down
12 changes: 12 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,15 @@ def test_request_timeout(nats_url):
with NATSClient(nats_url, socket_timeout=2) as client:
with pytest.raises(socket.timeout):
client.request("test-subject")


def test_exception_on_disconnect(nats_url):
with NATSClient(nats_url, socket_timeout=2) as client:
sub = client.subscribe(
"test-subject", callback=lambda x: x, queue="test-queue", max_messages=2
)

client._socket_file.readline = lambda: b""

with pytest.raises(ConnectionResetError):
client.wait()

0 comments on commit b43cecc

Please sign in to comment.