Skip to content

Commit

Permalink
Merge 047d4ab into 2450dec
Browse files Browse the repository at this point in the history
  • Loading branch information
liampauling committed Mar 9, 2020
2 parents 2450dec + 047d4ab commit 2f1f7e2
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 4 deletions.
4 changes: 2 additions & 2 deletions betfairlightweight/streaming/listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


class BaseListener:
def __init__(self, max_latency: float = 0.5):
def __init__(self, max_latency: Optional[float] = 0.5):
self.max_latency = max_latency

self.connection_id = None
Expand Down Expand Up @@ -80,7 +80,7 @@ class StreamListener(BaseListener):
def __init__(
self,
output_queue: queue.Queue = None,
max_latency: float = 0.5,
max_latency: Optional[float] = 0.5,
lightweight: bool = False,
):
"""
Expand Down
2 changes: 1 addition & 1 deletion betfairlightweight/streaming/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def on_update(self, data: dict) -> None:

publish_time = data["pt"]
latency = self._calc_latency(publish_time)
if latency > self._max_latency:
if self._max_latency and latency > self._max_latency:
logger.warning("[Stream: %s]: Latency high: %s" % (self.unique_id, latency))

if self._lookup in data:
Expand Down
3 changes: 3 additions & 0 deletions docs/streaming.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,6 @@ import logging

logging.basicConfig(level=logging.DEBUG)
```

!!! tip
By default `max_latency` is set to 0.5, this means a warning will be logged if the latency between the publishTime and your machines time is greater than this number. Often you will need to check that your clock is up to date, however this can be removed by setting `max_latency=None` when initializing the listener.
2 changes: 1 addition & 1 deletion examples/examplestreaminghistorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def _add_stream(self, unique_id, stream_type):


# create listener
listener = HistoricalListener(max_latency=1e100)
listener = HistoricalListener(max_latency=None)

# create historical stream, update directory to file location
stream = trading.streaming.create_historical_stream(
Expand Down
16 changes: 16 additions & 0 deletions tests/unit/test_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,22 @@ def test_on_update(self, mock_update_clk, mock_calc_latency, mock_process):
mock_calc_latency.return_value = 10
self.stream.on_update(mock_response.json())

@mock.patch("betfairlightweight.streaming.stream.BaseStream._process")
@mock.patch(
"betfairlightweight.streaming.stream.BaseStream._calc_latency", return_value=0.1
)
@mock.patch("betfairlightweight.streaming.stream.BaseStream._update_clk")
def test_on_update_no_latency(
self, mock_update_clk, mock_calc_latency, mock_process
):
data = {"pt": 12345, "mc": "trainer"}
self.listener.max_latency = None
self.stream.on_update(data)

mock_update_clk.assert_called_with(data)
mock_calc_latency.assert_called_with(data.get("pt"))
mock_process.assert_called_with(data.get("mc"), data.get("pt"))

def test_clear_cache(self):
self.stream._caches = {1: "abc"}
self.stream.clear_cache()
Expand Down

0 comments on commit 2f1f7e2

Please sign in to comment.