Skip to content

Commit 474c1fb

Browse files
committed
MOD: Live iteration starts after session error
1 parent efe3c77 commit 474c1fb

File tree

3 files changed

+62
-8
lines changed

3 files changed

+62
-8
lines changed

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.0 - TBD
4+
5+
#### Breaking change
6+
- Iterating a `Live` client after the streaming session has started will now raise a `ValueError`. Calling `Live.start` is not necessary when iterating the `Live` client
7+
38
## 0.27.0 - 2024-01-23
49

510
#### Enhancements

databento/live/client.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,14 @@ async def __anext__(self) -> DBNRecord:
127127

128128
def __iter__(self) -> Live:
129129
logger.debug("starting iteration")
130-
self._dbn_queue._enabled.set()
131-
if not self._session.is_started() and self.is_connected():
130+
if self._session.is_started():
131+
logger.error("iteration started after session has started")
132+
raise ValueError(
133+
"Cannot start iteration after streaming has started, records may be missed. Don't call `Live.start` before iterating.",
134+
)
135+
elif self.is_connected():
132136
self.start()
137+
self._dbn_queue._enabled.set()
133138
return self
134139

135140
def __next__(self) -> DBNRecord:
@@ -358,12 +363,14 @@ def start(
358363
"""
359364
Start the live client session.
360365
366+
It is not necessary to call `Live.start` before iterating a `Live` client and doing so will result in an error.
367+
361368
Raises
362369
------
363370
ValueError
364-
If `start()` is called before a subscription has been made.
365-
If `start()` is called after streaming has already started.
366-
If `start()` is called after the live session has closed.
371+
If `Live.start` is called before a subscription has been made.
372+
If `Live.start` is called after streaming has already started.
373+
If `Live.start` is called after the live session has closed.
367374
368375
See Also
369376
--------
@@ -388,7 +395,7 @@ def stop(self) -> None:
388395
Raises
389396
------
390397
ValueError
391-
If `stop()` is called before a connection has been made.
398+
If `Live.stop` is called before a connection has been made.
392399
393400
See Also
394401
--------
@@ -505,7 +512,7 @@ def block_for_close(
505512
) -> None:
506513
"""
507514
Block until the session closes or a timeout is reached. A session will
508-
close after `stop()` is called or the remote gateway disconnects.
515+
close after `Live.stop` is called or the remote gateway disconnects.
509516
510517
Parameters
511518
----------
@@ -548,7 +555,7 @@ async def wait_for_close(
548555
) -> None:
549556
"""
550557
Coroutine to wait until the session closes or a timeout is reached. A
551-
session will close after `stop()` is called or the remote gateway
558+
session will close after `Live.stop` is called or the remote gateway
552559
disconnects.
553560
554561
Parameters

tests/test_live_client.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,48 @@ def test_live_start_before_subscribe(
345345
live_client.start()
346346

347347

348+
def test_live_iteration_after_start(
349+
live_client: client.Live,
350+
) -> None:
351+
"""
352+
Test that iterating the Live client after it is started raises a
353+
ValueError.
354+
"""
355+
# Arrange
356+
live_client.subscribe(
357+
dataset=Dataset.GLBX_MDP3,
358+
schema=Schema.MBO,
359+
)
360+
361+
# Act
362+
live_client.start()
363+
364+
# Assert
365+
with pytest.raises(ValueError):
366+
iter(live_client)
367+
368+
369+
def test_live_async_iteration_after_start(
370+
live_client: client.Live,
371+
) -> None:
372+
"""
373+
Test that async-iterating the Live client after it is started raises a
374+
ValueError.
375+
"""
376+
# Arrange
377+
live_client.subscribe(
378+
dataset=Dataset.GLBX_MDP3,
379+
schema=Schema.MBO,
380+
)
381+
382+
# Act
383+
live_client.start()
384+
385+
# Assert
386+
with pytest.raises(ValueError):
387+
live_client.__aiter__()
388+
389+
348390
@pytest.mark.parametrize(
349391
"schema",
350392
[pytest.param(schema, id=str(schema)) for schema in Schema.variants()],

0 commit comments

Comments
 (0)