Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call stop before raising SocketError in _read_loop #52

Merged
merged 5 commits into from
Apr 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions betfairlightweight/streaming/betfairstream.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,17 @@ def start(self, async=False):
self._read_loop()

def stop(self):
"""Stops read loop which closes socket
"""Stops read loop and closes socket if it has been created.
"""
self._running = False

if self._socket is not None and not self._socket._closed:
try:
self._socket.shutdown(2)
except OSError:
pass
self._socket.close()

def authenticate(self, unique_id=None):
"""Authentication request.

Expand Down Expand Up @@ -146,17 +153,9 @@ def _read_loop(self):
for received_data in received_data_split:
if received_data:
self._data(received_data)
except socket.timeout as e:
raise SocketError('[Connect: %s]: Socket timeout, %s' % (self.unique_id, e))
except socket.error as e:
raise SocketError('[Connect: %s]: Socket error, %s' % (self.unique_id, e))

if not self._socket._closed:
try:
self._socket.shutdown(2)
except OSError:
pass
self._socket.close()
except (socket.timeout, socket.error) as e:
self.stop()
raise SocketError('[Connect: %s]: Socket %s' % (self.unique_id, e))

def _receive_all(self):
"""Whilst socket is running receives data from socket,
Expand Down
3 changes: 2 additions & 1 deletion tests/test_betfairstream.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,9 @@ def test_read_loop(self, mock_receive_all, mock_data):
assert self.betfair_stream.datetime_last_received is not None
assert self.betfair_stream.receive_count > 0

@mock.patch('betfairlightweight.streaming.betfairstream.BetfairStream.stop')
@mock.patch('betfairlightweight.streaming.betfairstream.BetfairStream._receive_all')
def test_read_loop_error(self, mock_receive_all):
def test_read_loop_error(self, mock_receive_all, mock_stop):
mock_socket = mock.Mock()
self.betfair_stream._socket = mock_socket
self.betfair_stream._running = True
Expand Down