8000 Connection class unit tests #1 · blynkkk/lib-python@2096977 · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on May 7, 2025. It is now read-only.

Commit 2096977

Browse files
committed
Connection class unit tests #1
1 parent 54e400e commit 2096977

File tree

4 files changed

+95
-11
lines changed

4 files changed

+95
-11
lines changed

blynklib.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,10 @@ def _set_socket_timeout(self, timeout):
158158
p.poll(int(timeout * 1000))
159159

160160
def send(self, data):
161-
retry_num = 0
162-
while retry_num <= self.RETRIES_TX_MAX_NUM:
161+
retries = self.RETRIES_TX_MAX_NUM
162+
while retries > 0:
163163
try:
164-
retry_num += 1
164+
retries -= 1
165165
self._last_send_time = ticks_ms()
166166
return self._socket.send(data)
167167
except (IOError, OSError):

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
author='Anton Morozenko',
1515
author_email='antoha.ua@gmail.com',
1616
setup_requires=['pytest-runner', ],
17-
tests_require=['pytest', ],
18-
py_modules = ['blynklib'],
17+
tests_require=['pytest', 'pytest-mock', ],
18+
py_modules=['blynklib'],
1919
classifiers=[
2020
"Programming Language :: Python :: 2.7",
2121
"Programming Language :: Python :: 3",

test/test_blynk_connection.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# -*- coding: utf-8 -*-
22
from __future__ import print_function
3+
import time
34
import pytest
45
import socket
56
from blynklib import Connection
@@ -22,3 +23,90 @@ def test_set_socket_timeout_via_poll(self, cb):
2223
in_timeout = 10
2324
cb._socket = 2222
2425
cb._set_socket_timeout(in_timeout)
26+
27+
def test_send(self, cb, mocker):
28+
cb._socket = socket.socket()
29+
with mocker.patch('socket.socket.send', return_value=5):
30+
result = cb.send('1234')
31+
assert result == 5
32+
33+
def test_send_ioerror(self, cb, mocker):
34+
cb._socket = socket.socket()
35+
with mocker.patch('socket.socket.send', side_effect=IOError('IO')):
36+
result = cb.send('1234')
37+
assert result is None
38+
39+
def test_send_oserror(self, cb, mocker):
40+
cb._socket = socket.socket()
41+
with mocker.patch('socket.socket.send', side_effect=OSError('OS')):
42+
result = cb.send('1234')
43+
assert result is None
44+
45+
# todo spy modify - AttributeError: 'socket' object attribute 'send' is read-only
46+
# def test_send_error_retry_count(self, cb, mocker):
47+
# cb._socket = socket.socket()
48+
# with mocker.patch('socket.socket.send', side_effect=OSError('OS')):
49+
# mocker.spy(cb._socket, 'send')
50+
# cb.send('1234')
51+
# assert cb._socket.send.call_count == 3
52+
53+
def test_receive(self, cb, mocker):
54+
cb._socket = socket.socket()
55+
with mocker.patch.object(cb, '_set_socket_timeout', return_value=None):
56+
with mocker.patch('socket.socket.recv', return_value=b'12345'):
57+
result = cb.receive(10, 1)
58+
assert result == b'12345'
59+
60+
def test_receive_timeout(self, cb, mocker):
61+
cb._socket = socket.socket()
62+
with mocker.patch.object(cb, '_set_socket_timeout', return_value=None):
63+
with mocker.patch('socket.socket.recv', side_effect=OSError('timed out')):
64+
result = cb.receive(10, 1)
65+
assert result == b''
66+
67+
def test_receive_eagain(self, cb, mocker):
68+
cb._socket = socket.socket()
69+
with mocker.patch.object(cb, '_set_socket_timeout', return_value=None):
70+
# with mocker.patch.object(cb._socket, 'recv', side_effect=IOError('[Errno 11]')):
71+
with mocker.patch('socket.socket.recv', side_effect=IOError('[Errno 11]')):
72+
result = cb.receive(10, 1)
73+
assert result == b''
74+
75+
def test_receive_etimeout(self, cb, mocker):
76+
cb._socket = socket.socket()
77+
with mocker.patch.object(cb, '_set_socket_timeout', return_value=None):
78+
# with mocker.patch.object(cb._socket, 'recv', side_effect=OSError('[Errno 60]')):
79+
with mocker.patch('socket.socket.recv', side_effect=OSError('[Errno 60]')):
80+
result = cb.receive(10, 1)
81+
assert result == b''
82+
83+
def test_receive_raise_other_oserror(self, cb, mocker):
84+
cb._socket = socket.socket()
85+
with mocker.patch.object(cb, '_set_socket_timeout', return_value=None):
86+
# with mocker.patch.object(cb._socket, 'recv', side_effect=OSError('[Errno 13]')):
87+
with mocker.patch('socket.socket.recv', side_effect=OSError('[Errno 13]')):
88+
with pytest.raises(OSError) as os_err:
89+
cb.receive(10, 1)
90+
assert '[Errno 13]' in str(os_err)
91+
92+
def test_is_server_alive_negative(self, cb):
93+
result = cb.is_server_alive()
94+
assert result is False
95+
96+
def test_is_server_alive_positive_ping(self, cb, mocker):
97+
cb._last_rcv_time = int(time.time() * 1000)
98+
with mocker.patch.object(cb, 'send', return_value=None):
99+
result = cb.is_server_alive()
100+
assert result is True
101+
102+
def test_is_server_alive_positive_no_ping_1(self, cb):
103+
cb._last_rcv_time = int(time.time() * 1000)
104+
cb._last_ping_time = int(time.time() * 1000)
105+
result = cb.is_server_alive()
106+
assert result is True
107+
108+
def test_is_server_alive_positive_no_ping_2(self, cb):
109+
cb._last_rcv_time = int(time.time() * 1000)
110+
cb._last_send_time = int(time.time() * 1000)
111+
result = cb.is_server_alive()
112+
assert result is True

test/test_blynk_protocol.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,8 @@ def test_parse_response_msg_hw_unicode(self, pb):
9595
assert result == (20, 2, 4, [u'ёж'])
9696

9797
def test_heartbeat_msg(self, pb):
98-
ver = b'0.1.1'
99-
buf = b'2048'
100-
hb = b'20'
101-
result = pb.heartbeat_msg(int(hb), int(buf))
102-
assert result == b'\x11\x00\x02\x00+ver\x00{}\x00buff-in\x00{}\x00h-beat\x00{}\x00dev\x00python'.format(ver,
103-
buf, hb)
98+
result = pb.heartbeat_msg(20, 2048)
99+
assert result == b'\x11\x00\x02\x00+ver\x000.1.1\x00buff-in\x002048\x00h-beat\x0020\x00dev\x00python'
104100

105101
def test_login_msg(self, pb):
106102
result = pb.login_msg('1234')

0 commit comments

Comments
 (0)
0