Skip to content

Commit 5578cae

Browse files
authored
Avoid raising RepeatedConnectionLostError when listening frames (#52)
1 parent c8d5e56 commit 5578cae

File tree

2 files changed

+8
-22
lines changed

2 files changed

+8
-22
lines changed

stompman/connection_manager.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,17 +120,13 @@ async def write_frame_reconnecting(self, frame: AnyClientFrame) -> None:
120120
raise RepeatedConnectionLostError(retry_attempts=self.connect_retry_attempts)
121121

122122
async def read_frames_reconnecting(self) -> AsyncGenerator[AnyServerFrame, None]:
123-
for _ in range(self.connect_retry_attempts):
123+
while True:
124124
connection_state = await self._get_active_connection_state()
125125
try:
126126
async for frame in connection_state.connection.read_frames():
127127
yield frame
128128
except ConnectionLostError:
129129
self._clear_active_connection_state()
130-
else:
131-
return
132-
133-
raise RepeatedConnectionLostError(retry_attempts=self.connect_retry_attempts)
134130

135131
async def maybe_write_frame(self, frame: AnyClientFrame) -> bool:
136132
if not self._active_connection_state:

tests/test_connection_manager.py

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import asyncio
2-
from collections.abc import AsyncGenerator
2+
from collections.abc import AsyncGenerator, AsyncIterable
33
from types import SimpleNamespace
44
from typing import Self
55
from unittest import mock
@@ -235,20 +235,6 @@ class MockConnection(BaseMockConnection):
235235
await manager.write_frame_reconnecting(build_dataclass(ConnectFrame))
236236

237237

238-
async def test_read_frames_reconnecting_raises() -> None:
239-
class MockConnection(BaseMockConnection):
240-
@staticmethod
241-
async def read_frames() -> AsyncGenerator[AnyServerFrame, None]:
242-
raise ConnectionLostError
243-
yield
244-
await asyncio.sleep(0)
245-
246-
manager = EnrichedConnectionManager(connection_class=MockConnection)
247-
248-
with pytest.raises(RepeatedConnectionLostError):
249-
[_ async for _ in manager.read_frames_reconnecting()]
250-
251-
252238
SIDE_EFFECTS = [(None,), (ConnectionLostError(), None), (ConnectionLostError(), ConnectionLostError(), None)]
253239

254240

@@ -299,11 +285,15 @@ async def read_frames() -> AsyncGenerator[AnyServerFrame, None]:
299285
raise ConnectionLostError
300286
for frame in frames:
301287
yield frame
302-
await asyncio.sleep(0)
303288

304289
manager = EnrichedConnectionManager(connection_class=MockConnection)
305290

306-
assert frames == [frame async for frame in manager.read_frames_reconnecting()]
291+
async def take_all_frames() -> AsyncIterable[AnyServerFrame]:
292+
iterator = manager.read_frames_reconnecting()
293+
for _ in frames:
294+
yield await anext(iterator)
295+
296+
assert frames == [frame async for frame in take_all_frames()]
307297

308298

309299
async def test_maybe_write_frame_connection_already_lost() -> None:

0 commit comments

Comments
 (0)