|
1 | 1 | import asyncio
|
| 2 | +import time |
2 | 3 | from collections.abc import AsyncGenerator, AsyncIterable
|
3 | 4 | from ssl import SSLContext
|
4 | 5 | from typing import Literal, Self
|
|
7 | 8 | import pytest
|
8 | 9 | from stompman import (
|
9 | 10 | AnyServerFrame,
|
| 11 | + Client, |
10 | 12 | ConnectedFrame,
|
11 | 13 | ConnectFrame,
|
12 | 14 | ConnectionLostError,
|
|
16 | 18 | FailedAllWriteAttemptsError,
|
17 | 19 | Heartbeat,
|
18 | 20 | MessageFrame,
|
| 21 | + ReceiptFrame, |
19 | 22 | )
|
20 | 23 | from stompman.connection_lifespan import EstablishedConnectionResult
|
21 | 24 | from stompman.connection_manager import ActiveConnectionState
|
22 | 25 |
|
23 |
| -from test_stompman.conftest import BaseMockConnection, EnrichedConnectionManager, build_dataclass |
| 26 | +from test_stompman.conftest import ( |
| 27 | + BaseMockConnection, |
| 28 | + EnrichedConnectionManager, |
| 29 | + build_dataclass, |
| 30 | + create_spying_connection, |
| 31 | +) |
24 | 32 |
|
25 | 33 | pytestmark = [pytest.mark.anyio, pytest.mark.usefixtures("mock_sleep")]
|
26 | 34 |
|
@@ -347,3 +355,16 @@ class MockConnection(BaseMockConnection):
|
347 | 355 | async def test_maybe_write_frame_ok() -> None:
|
348 | 356 | async with EnrichedConnectionManager(connection_class=BaseMockConnection) as manager:
|
349 | 357 | assert await manager.maybe_write_frame(build_dataclass(ConnectFrame))
|
| 358 | + |
| 359 | + |
| 360 | +@pytest.mark.parametrize("is_alive", [True, False]) |
| 361 | +async def test_connection_alive(is_alive: bool) -> None: # noqa: FBT001 |
| 362 | + connection_class, _ = create_spying_connection( |
| 363 | + [ConnectedFrame(headers={"version": Client.PROTOCOL_VERSION, "heart-beat": "1000,1000"})], |
| 364 | + [], |
| 365 | + [build_dataclass(ReceiptFrame)], |
| 366 | + ) |
| 367 | + connection_manager = await EnrichedConnectionManager(connection_class=connection_class).__aenter__() |
| 368 | + assert connection_manager._active_connection_state |
| 369 | + connection_manager._active_connection_state.connection.last_read_time = time.time() - 1 + is_alive |
| 370 | + assert connection_manager._active_connection_state.is_alive() == is_alive |
0 commit comments