Skip to content

Commit 2003372

Browse files
committed
Removed on_heartbeat callback handling from Client and tests
1 parent 830f6cf commit 2003372

File tree

3 files changed

+2
-69
lines changed

3 files changed

+2
-69
lines changed

packages/stompman/stompman/client.py

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ class Client:
3232

3333
servers: list[ConnectionParameters] = field(kw_only=False)
3434
on_error_frame: Callable[[ErrorFrame], Any] | None = None
35-
on_heartbeat: Callable[[], Any] | Callable[[], Awaitable[Any]] | None = None
3635

3736
heartbeat: Heartbeat = field(default=Heartbeat(1000, 1000))
3837
ssl: Literal[True] | SSLContext | None = None
@@ -57,7 +56,6 @@ class Client:
5756
_check_server_heartbeat_task: asyncio.Task[None] = field(init=False)
5857
_listen_task: asyncio.Task[None] = field(init=False)
5958
_task_group: asyncio.TaskGroup = field(init=False)
60-
_on_heartbeat_is_async: bool = field(init=False)
6159

6260
def __post_init__(self) -> None:
6361
self._connection_manager = ConnectionManager(
@@ -82,7 +80,6 @@ def __post_init__(self) -> None:
8280
check_server_alive_interval_factor=self.check_server_alive_interval_factor,
8381
ssl=self.ssl,
8482
)
85-
self._on_heartbeat_is_async = inspect.iscoroutinefunction(self.on_heartbeat) if self.on_heartbeat else False
8683

8784
async def __aenter__(self) -> Self:
8885
self._task_group = await self._exit_stack.enter_async_context(asyncio.TaskGroup())
@@ -157,14 +154,7 @@ async def _listen_to_frames(self) -> None:
157154
case ErrorFrame():
158155
if self.on_error_frame:
159156
self.on_error_frame(frame)
160-
case HeartbeatFrame():
161-
if self.on_heartbeat is None:
162-
pass
163-
elif self._on_heartbeat_is_async:
164-
task_group.create_task(self.on_heartbeat()) # type: ignore[arg-type]
165-
else:
166-
self.on_heartbeat()
167-
case ConnectedFrame() | ReceiptFrame():
157+
case HeartbeatFrame() | ConnectedFrame() | ReceiptFrame():
168158
pass
169159

170160
async def send(

packages/stompman/test_stompman/test_connection_lifespan.py

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import asyncio
22
from collections.abc import AsyncGenerator, Coroutine
3-
from functools import partial
43
from typing import Any
54
from unittest import mock
65

@@ -17,7 +16,6 @@
1716
DisconnectFrame,
1817
ErrorFrame,
1918
FailedAllConnectAttemptsError,
20-
HeartbeatFrame,
2119
ReceiptFrame,
2220
UnsupportedProtocolVersion,
2321
)
@@ -156,58 +154,6 @@ async def mock_sleep(delay: float) -> None:
156154
assert write_heartbeat_mock.mock_calls == [mock.call(), mock.call(), mock.call()]
157155

158156

159-
async def test_client_on_heartbeat_none(monkeypatch: pytest.MonkeyPatch) -> None:
160-
real_sleep = asyncio.sleep
161-
monkeypatch.setattr("asyncio.sleep", partial(asyncio.sleep, 0))
162-
connection_class, _ = create_spying_connection(
163-
*get_read_frames_with_lifespan(
164-
[build_dataclass(HeartbeatFrame), build_dataclass(HeartbeatFrame), build_dataclass(HeartbeatFrame)]
165-
)
166-
)
167-
168-
async with EnrichedClient(connection_class=connection_class, on_heartbeat=None):
169-
await real_sleep(0)
170-
await real_sleep(0)
171-
await real_sleep(0)
172-
173-
174-
async def test_client_on_heartbeat_sync(monkeypatch: pytest.MonkeyPatch) -> None:
175-
real_sleep = asyncio.sleep
176-
monkeypatch.setattr("asyncio.sleep", partial(asyncio.sleep, 0))
177-
connection_class, _ = create_spying_connection(
178-
*get_read_frames_with_lifespan(
179-
[build_dataclass(HeartbeatFrame), build_dataclass(HeartbeatFrame), build_dataclass(HeartbeatFrame)]
180-
)
181-
)
182-
on_heartbeat_mock = mock.Mock()
183-
184-
async with EnrichedClient(connection_class=connection_class, on_heartbeat=on_heartbeat_mock):
185-
await real_sleep(0)
186-
await real_sleep(0)
187-
await real_sleep(0)
188-
189-
assert on_heartbeat_mock.mock_calls == [mock.call(), mock.call(), mock.call()]
190-
191-
192-
async def test_client_on_heartbeat_async(monkeypatch: pytest.MonkeyPatch) -> None:
193-
real_sleep = asyncio.sleep
194-
monkeypatch.setattr("asyncio.sleep", partial(asyncio.sleep, 0))
195-
connection_class, _ = create_spying_connection(
196-
*get_read_frames_with_lifespan(
197-
[build_dataclass(HeartbeatFrame), build_dataclass(HeartbeatFrame), build_dataclass(HeartbeatFrame)]
198-
)
199-
)
200-
on_heartbeat_mock = mock.AsyncMock()
201-
202-
async with EnrichedClient(connection_class=connection_class, on_heartbeat=on_heartbeat_mock):
203-
await real_sleep(0)
204-
await real_sleep(0)
205-
await real_sleep(0)
206-
207-
assert on_heartbeat_mock.await_count == 3 # noqa: PLR2004
208-
assert on_heartbeat_mock.mock_calls == [mock.call.__bool__(), mock.call(), mock.call(), mock.call()]
209-
210-
211157
def test_make_receipt_id(monkeypatch: pytest.MonkeyPatch) -> None:
212158
monkeypatch.undo()
213159
stompman.connection_lifespan._make_receipt_id()

packages/stompman/test_stompman/test_subscription.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,7 @@ async def test_client_listen_routing_ok(monkeypatch: pytest.MonkeyPatch, faker:
165165
second_message_handler, second_error_handler = mock.AsyncMock(side_effect=SomeError), mock.Mock()
166166

167167
async with EnrichedClient(
168-
connection_class=connection_class,
169-
on_error_frame=(on_error_frame := mock.Mock()),
170-
on_heartbeat=(on_heartbeat := mock.Mock()),
168+
connection_class=connection_class, on_error_frame=(on_error_frame := mock.Mock())
171169
) as client:
172170
first_subscription = await client.subscribe(
173171
faker.pystr(), handler=first_message_handler, on_suppressed_exception=first_error_handler
@@ -187,7 +185,6 @@ async def test_client_listen_routing_ok(monkeypatch: pytest.MonkeyPatch, faker:
187185
second_error_handler.assert_called_once_with(SomeError(), third_message_frame)
188186

189187
on_error_frame.assert_called_once_with(error_frame)
190-
on_heartbeat.assert_called_once_with()
191188

192189

193190
@pytest.mark.parametrize("side_effect", [None, SomeError])

0 commit comments

Comments
 (0)