Skip to content

Commit

Permalink
add listener.status property
Browse files Browse the repository at this point in the history
minor refactor
closes #324
  • Loading branch information
liampauling committed Jul 27, 2020
1 parent cb47fcd commit 9871a82
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
11 changes: 5 additions & 6 deletions betfairlightweight/streaming/listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def __init__(self, max_latency: Optional[float] = 0.5):
self.max_latency = max_latency

self.connection_id = None
self.status = None
self.stream = None
self.stream_type = None # marketSubscription/orderSubscription
self.stream_unique_id = None
Expand Down Expand Up @@ -106,6 +107,7 @@ def on_data(self, raw_data: str) -> Optional[bool]:
logger.error("value error: %s" % raw_data)
return

self.status = data.get("status")
unique_id = data.get("id")

if self._error_handler(data, unique_id):
Expand Down Expand Up @@ -166,8 +168,7 @@ def _on_change_message(self, data: dict, unique_id: int) -> None:
elif change_type == "UPDATE":
self.stream.on_update(data)

@staticmethod
def _error_handler(data: dict, unique_id: int) -> Optional[bool]:
def _error_handler(self, data: dict, unique_id: int) -> Optional[bool]:
"""Called when data first received
:param data: Received data
Expand All @@ -181,9 +182,7 @@ def _error_handler(data: dict, unique_id: int) -> Optional[bool]:
)
if data.get("connectionClosed"):
return True
if data.get("status"):
if self.status:
# Clients shouldn't disconnect if status 503 is returned; when the stream
# recovers updates will be sent containing the latest data
logger.warning(
"[Subscription: %s] status: %s" % (unique_id, data["status"])
)
logger.warning("[Subscription: %s] status: %s" % (unique_id, self.status))
1 change: 1 addition & 0 deletions tests/resources/streaming_503.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"op":"mcm","id":2,"status":"503"}
21 changes: 21 additions & 0 deletions tests/unit/test_listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def setUp(self):

def test_init(self):
assert self.base_listener.connection_id is None
assert self.base_listener.status is None
assert self.base_listener.stream is None
assert self.base_listener.stream_unique_id is None
assert self.base_listener.stream_type is None
Expand Down Expand Up @@ -151,6 +152,21 @@ def test_on_data(
on_data = self.stream_listener.on_data(mock_response.text)
assert on_data is False

@mock.patch(
"betfairlightweight.streaming.listener.StreamListener._error_handler",
return_value=True,
)
def test_on_data_status_error(
self, mock_error_handler,
):
self.stream_listener.stream_unique_id = 2
mock_response = create_mock_json("tests/resources/streaming_503.json")
self.assertFalse(self.stream_listener.on_data(mock_response.text))
mock_error_handler.assert_called_with(
mock_response.json(), mock_response.json().get("id")
)
self.assertEqual(self.stream_listener.status, "503")

def test_on_connection(self):
self.stream_listener._on_connection({"connectionId": 1234}, 1)
assert self.stream_listener.connection_id == 1234
Expand Down Expand Up @@ -215,6 +231,11 @@ def test_error_handler(self):
return_value = self.stream_listener._error_handler(error_data, 1)
assert return_value is None

def test_error_handler_503(self):
self.stream_listener.status = "503"
mock_response = create_mock_json("tests/resources/streaming_connection.json")
self.stream_listener._error_handler(mock_response.json(), 1)

def test_str(self):
assert str(self.stream_listener) == "StreamListener"

Expand Down

0 comments on commit 9871a82

Please sign in to comment.