Skip to content

Commit eae7d03

Browse files
committed
FIX: Live.block_for_close to call stop
1 parent 4adeee9 commit eae7d03

3 files changed

Lines changed: 21 additions & 12 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## 0.28.1 - TBD
4+
5+
#### Bug fixes
6+
- `Live.block_for_close` and `Live.wait_for_close` will now call `Live.stop` when a timeout is reached instead of `Live.terminate` to close the stream more gracefully
7+
38
## 0.28.0 - 2024-02-01
49

510
#### Enhancements

databento/live/client.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,9 @@ def block_for_close(
538538
Block until the session closes or a timeout is reached. A session will
539539
close after `Live.stop` is called or the remote gateway disconnects.
540540
541+
If a `timeout` is specified, `Live.stop` will be called when the
542+
timeout is reached.
543+
541544
Parameters
542545
----------
543546
timeout : float, optional
@@ -562,8 +565,8 @@ def block_for_close(
562565
loop=Live._loop,
563566
).result(timeout=timeout)
564567
except (futures.TimeoutError, KeyboardInterrupt) as exc:
565-
logger.info("terminating session due to %s", type(exc).__name__)
566-
self.terminate()
568+
logger.info("closing session due to %s", type(exc).__name__)
569+
self.stop()
567570
if isinstance(exc, KeyboardInterrupt):
568571
raise
569572
except BentoError:
@@ -582,6 +585,9 @@ async def wait_for_close(
582585
session will close after `Live.stop` is called or the remote gateway
583586
disconnects.
584587
588+
If a `timeout` is specified, `Live.stop` will be called when the
589+
timeout is reached.
590+
585591
Parameters
586592
----------
587593
timeout : float, optional
@@ -610,8 +616,8 @@ async def wait_for_close(
610616
try:
611617
await asyncio.wait_for(waiter, timeout=timeout)
612618
except (asyncio.TimeoutError, KeyboardInterrupt) as exc:
613-
logger.info("terminating session due to %s", type(exc).__name__)
614-
self.terminate()
619+
logger.info("closing session due to %s", type(exc).__name__)
620+
self.stop()
615621
if isinstance(exc, KeyboardInterrupt):
616622
raise
617623
except BentoError:

tests/test_live_client.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -604,11 +604,10 @@ def test_live_block_for_close_timeout(
604604
monkeypatch: pytest.MonkeyPatch,
605605
) -> None:
606606
"""
607-
Test that block_for_close terminates the session when the timeout is
608-
reached.
607+
Test that block_for_close stops the session when the timeout is reached.
609608
"""
610609
# Arrange
611-
monkeypatch.setattr(live_client, "terminate", MagicMock())
610+
monkeypatch.setattr(live_client, "stop", MagicMock())
612611
live_client.subscribe(
613612
dataset=Dataset.GLBX_MDP3,
614613
schema=Schema.MBO,
@@ -619,7 +618,7 @@ def test_live_block_for_close_timeout(
619618

620619
# Act, Assert
621620
live_client.block_for_close(timeout=0)
622-
live_client.terminate.assert_called_once() # type: ignore
621+
live_client.stop.assert_called_once() # type: ignore
623622

624623

625624
@pytest.mark.usefixtures("mock_live_server")
@@ -679,11 +678,10 @@ async def test_live_wait_for_close_timeout(
679678
monkeypatch: pytest.MonkeyPatch,
680679
) -> None:
681680
"""
682-
Test that wait_for_close terminates the session when the timeout is
683-
reached.
681+
Test that wait_for_close stops the session when the timeout is reached.
684682
"""
685683
# Arrange
686-
monkeypatch.setattr(live_client, "terminate", MagicMock())
684+
monkeypatch.setattr(live_client, "stop", MagicMock())
687685

688686
# Act
689687
live_client.subscribe(
@@ -696,7 +694,7 @@ async def test_live_wait_for_close_timeout(
696694
await live_client.wait_for_close(timeout=0)
697695

698696
# Assert
699-
live_client.terminate.assert_called_once() # type: ignore
697+
live_client.stop.assert_called_once() # type: ignore
700698

701699

702700
@pytest.mark.usefixtures("mock_live_server")

0 commit comments

Comments
 (0)