|
1 | 1 | import asyncio
|
2 | 2 | from collections.abc import AsyncGenerator, Coroutine
|
| 3 | +from functools import partial |
3 | 4 | from typing import Any
|
4 | 5 | from unittest import mock
|
5 | 6 |
|
|
17 | 18 | DisconnectFrame,
|
18 | 19 | ErrorFrame,
|
19 | 20 | FailedAllConnectAttemptsError,
|
| 21 | + HeartbeatFrame, |
20 | 22 | ReceiptFrame,
|
21 | 23 | UnsupportedProtocolVersion,
|
22 | 24 | )
|
@@ -154,6 +156,58 @@ async def mock_sleep(delay: float) -> None:
|
154 | 156 | assert write_heartbeat_mock.mock_calls == [mock.call(), mock.call(), mock.call()]
|
155 | 157 |
|
156 | 158 |
|
| 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 | + |
157 | 211 | def test_make_receipt_id(monkeypatch: pytest.MonkeyPatch) -> None:
|
158 | 212 | monkeypatch.undo()
|
159 | 213 | stompman.connection_lifespan._make_receipt_id()
|
0 commit comments