Skip to content

Commit

Permalink
_last_rcv_time fixes for correct disconnect/connect handling
Browse files Browse the repository at this point in the history
  • Loading branch information
antohaUa committed Sep 18, 2019
1 parent f7ea75d commit 1db89d4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
3 changes: 2 additions & 1 deletion blynklib.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ def connect(self, timeout=_CONNECT_TIMEOUT):
self._get_socket()
self._authenticate()
self._set_heartbeat()
self._last_rcv_time = ticks_ms()
self.log('Registered events: {}\n'.format(list(self._events.keys())))
self.call_handler(self._CONNECT)
return True
Expand Down Expand Up @@ -361,8 +362,8 @@ def read_response(self, timeout=0.5):
end_time = time.time() + timeout
while time.time() <= end_time:
rsp_data = self.receive(self.rcv_buffer, self.SOCK_TIMEOUT)
self._last_rcv_time = ticks_ms()
if rsp_data:
self._last_rcv_time = ticks_ms()
msg_type, msg_id, h_data, msg_args = self.parse_response(rsp_data, self.rcv_buffer)
self.process(msg_type, msg_id, h_data, msg_args)

Expand Down
14 changes: 8 additions & 6 deletions test/test_blynk_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
from __future__ import print_function
import socket
import pytest
from blynklib import Blynk, BlynkError, RedirectError
import blynklib


class TestBlynk:
@pytest.fixture
def bl(self):
blynk = Blynk('1234', log=print)
blynk = blynklib.Blynk('1234', log=print)
yield blynk

def test_connect(self, bl, mocker):
Expand All @@ -17,13 +17,15 @@ def test_connect(self, bl, mocker):
with mocker.patch.object(bl, '_authenticate', return_value=None):
with mocker.patch.object(bl, '_set_heartbeat', return_value=None):
with mocker.patch.object(bl, 'call_handler', return_value=None):
result = bl.connect()
assert result is True
with mocker.patch.object(blynklib, 'ticks_ms', return_value=42):
result = bl.connect()
assert result is True
assert bl._last_rcv_time == 42

def test_connect_exception(self, bl, mocker):
with mocker.patch.object(bl, 'connected', return_value=False):
with mocker.patch.object(bl, '_get_socket', return_value=None):
with mocker.patch.object(bl, '_authenticate', side_effect=BlynkError()):
with mocker.patch.object(bl, '_authenticate', side_effect=blynklib.BlynkError()):
with mocker.patch.object(bl, 'disconnect', return_value=None):
with mocker.patch('time.sleep', return_value=None):
mocker.spy(bl, 'disconnect')
Expand All @@ -34,7 +36,7 @@ def test_connect_exception(self, bl, mocker):
def test_connect_redirect_exception(self, bl, mocker):
with mocker.patch.object(bl, 'connected', return_value=False):
with mocker.patch.object(bl, '_get_socket', return_value=None):
with mocker.patch.object(bl, '_authenticate', side_effect=RedirectError('127.0.0.1', 4444)):
with mocker.patch.object(bl, '_authenticate', side_effect=blynklib.RedirectError('127.0.0.1', 4444)):
with mocker.patch.object(bl, 'disconnect', return_value=None):
with mocker.patch('time.sleep', return_value=None):
mocker.spy(bl, 'disconnect')
Expand Down

0 comments on commit 1db89d4

Please sign in to comment.