Skip to content

Commit 98839f0

Browse files
authored
Handle connection error when reading frames (#12)
1 parent e208666 commit 98839f0

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

stompman/connection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ async def read_frames(self) -> AsyncGenerator[AnyServerFrame, None]:
124124
while True:
125125
try:
126126
raw_frames = await asyncio.wait_for(self._read_non_empty_bytes(), timeout=self.read_timeout)
127-
except TimeoutError as exception:
127+
except (TimeoutError, ConnectionError) as exception:
128128
raise ConnectionLostError(self.read_timeout) from exception
129129

130130
for frame in cast(Iterator[AnyServerFrame], parser.load_frames(raw_frames)):

tests/test_connection.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ async def test_connection_error(monkeypatch: pytest.MonkeyPatch, connection: Con
120120
assert not await connection.connect()
121121

122122

123-
async def test_read_timeout(monkeypatch: pytest.MonkeyPatch, connection: Connection) -> None:
123+
async def test_read_frames_timeout_error(monkeypatch: pytest.MonkeyPatch, connection: Connection) -> None:
124124
monkeypatch.setattr(
125125
"asyncio.open_connection",
126126
mock.AsyncMock(return_value=(mock.AsyncMock(read=partial(asyncio.sleep, 5)), mock.AsyncMock())),
@@ -129,3 +129,15 @@ async def test_read_timeout(monkeypatch: pytest.MonkeyPatch, connection: Connect
129129
mock_wait_for(monkeypatch)
130130
with pytest.raises(ConnectionLostError):
131131
[frame async for frame in connection.read_frames()]
132+
133+
134+
async def test_read_frames_connection_error(monkeypatch: pytest.MonkeyPatch, connection: Connection) -> None:
135+
monkeypatch.setattr(
136+
"asyncio.open_connection",
137+
mock.AsyncMock(
138+
return_value=(mock.AsyncMock(read=mock.AsyncMock(side_effect=BrokenPipeError)), mock.AsyncMock())
139+
),
140+
)
141+
await connection.connect()
142+
with pytest.raises(ConnectionLostError):
143+
[frame async for frame in connection.read_frames()]

0 commit comments

Comments
 (0)