|
13 | 13 | from databento import SType |
14 | 14 | from databento.common.enums import ReconnectPolicy |
15 | 15 | from databento.live import client |
| 16 | +from databento.live import session |
16 | 17 | from databento.live.gateway import AuthenticationRequest |
17 | 18 | from databento.live.gateway import SessionStart |
18 | 19 | from databento.live.gateway import SubscriptionRequest |
| 20 | +from databento.live.protocol import DatabentoLiveProtocol |
19 | 21 | from tests.mockliveserver.fixture import MockLiveServerInterface |
20 | 22 |
|
21 | 23 |
|
@@ -241,3 +243,54 @@ async def test_reconnect_callback( |
241 | 243 | gap_start, gap_end = args |
242 | 244 | assert isinstance(gap_start, pd.Timestamp) |
243 | 245 | assert isinstance(gap_end, pd.Timestamp) |
| 246 | + |
| 247 | + |
| 248 | +async def test_reconnect_after_heartbeat_timeout( |
| 249 | + test_live_api_key: str, |
| 250 | + mock_live_server: MockLiveServerInterface, |
| 251 | + monkeypatch: pytest.MonkeyPatch, |
| 252 | + reconnect_policy: ReconnectPolicy = ReconnectPolicy.RECONNECT, |
| 253 | +) -> None: |
| 254 | + """ |
| 255 | + Test that the heartbeat monitor reconnects the client every time the |
| 256 | + gateway times out, not just the first time. |
| 257 | + """ |
| 258 | + # Arrange |
| 259 | + # this mock will make the connection go silent instead of disconnecting |
| 260 | + monkeypatch.setattr(DatabentoLiveProtocol, "eof_received", lambda _: True) |
| 261 | + monkeypatch.setattr(session, "CLIENT_TIMEOUT_MARGIN_SECONDS", 0) |
| 262 | + |
| 263 | + live_client = client.Live( |
| 264 | + key=test_live_api_key, |
| 265 | + gateway=mock_live_server.host, |
| 266 | + port=mock_live_server.port, |
| 267 | + heartbeat_interval_s=1, |
| 268 | + reconnect_policy=reconnect_policy, |
| 269 | + ) |
| 270 | + |
| 271 | + live_client.subscribe( |
| 272 | + dataset=Dataset.GLBX_MDP3, |
| 273 | + schema=Schema.MBO, |
| 274 | + stype_in=SType.RAW_SYMBOL, |
| 275 | + symbols="TEST", |
| 276 | + ) |
| 277 | + |
| 278 | + await mock_live_server.wait_for_message_of_type(AuthenticationRequest) |
| 279 | + |
| 280 | + # Act |
| 281 | + live_client.start() |
| 282 | + |
| 283 | + await mock_live_server.wait_for_message_of_type(SessionStart) |
| 284 | + |
| 285 | + # Assert |
| 286 | + for _ in range(2): |
| 287 | + await mock_live_server.wait_for_message_of_type( |
| 288 | + AuthenticationRequest, |
| 289 | + timeout=10, |
| 290 | + ) |
| 291 | + await mock_live_server.wait_for_message_of_type( |
| 292 | + SessionStart, |
| 293 | + timeout=10, |
| 294 | + ) |
| 295 | + |
| 296 | + live_client.stop() |
0 commit comments