Skip to content

Commit

Permalink
Added DXLinkStreamer.get_event_nowait() (#150)
Browse files Browse the repository at this point in the history
* MarginReportEntry bug fix

* MarginReportEntry rework

* Added DXLinkStreamer.get_event_nowait

* Added tests for DXLinkStreamer.get_event_nowait

* Added tests for DXLinkStreamer.get_event_nowait
  • Loading branch information
Quenos authored May 26, 2024
1 parent e6cbb4b commit 40d5601
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
12 changes: 12 additions & 0 deletions tastytrade/streamer.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,18 @@ async def listen(self, event_type: EventType) -> AsyncIterator[Event]:
while True:
yield await self._queues[event_type].get()

def get_event_nowait(self, event_type: EventType) -> Optional[Event]:
"""
Using the existing subscriptions, pulls an event of the given type and
returns it. if the queue is empty None is returned.
:param event_type: the type of event to get
"""
if not self._queues[event_type].empty():
return self._queues[event_type].get_nowait()
else:
return None

async def get_event(self, event_type: EventType) -> Event:
"""
Using the existing subscription, pulls an event of the given type and
Expand Down
13 changes: 13 additions & 0 deletions tests/test_streamer.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,16 @@ async def test_dxlink_streamer(session):
break
await streamer.unsubscribe_candle(subs[0], '1d')
await streamer.unsubscribe(EventType.QUOTE, subs[1])


@pytest.mark.asyncio
async def test_dxlink_streamer_nowait(session):
async with DXLinkStreamer(session) as streamer:
subs_not_none = ['SPY']
subs_none = ['QQQQ']
await streamer.subscribe(EventType.TRADE, subs_not_none)
await streamer.subscribe(EventType.QUOTE, subs_none)
assert streamer.get_event_nowait(EventType.TRADE) is not None
assert streamer.get_event_nowait(EventType.QUOTE) is None
await streamer.unsubscribe(EventType.TRADE, subs_not_none)
await streamer.unsubscribe(EventType.QUOTE, subs_none)

0 comments on commit 40d5601

Please sign in to comment.