Skip to content

Commit cbbb3d5

Browse files
authored
Increase stability (#77)
* Increasing stability of `true_sendall` function. * Improving the `flake8` usage. * Minor changes.
1 parent 4e7e59a commit cbbb3d5

File tree

7 files changed

+25
-18
lines changed

7 files changed

+25
-18
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ test-python:
1414

1515
lint-python:
1616
@echo "Linting Python files"
17-
flake8 --exit-zero --ignore=E501 --exclude=.git,compat.py mocket
17+
flake8 --ignore=E501,E731 --exclude=.git,compat.py mocket
1818
@echo ""
1919

2020
develop: install-test-requirements install-dev-requirements

mocket/mocket.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ def __init__(self, family=socket.AF_INET, type=socket.SOCK_STREAM, proto=0, deta
136136
self.true_socket = true_socket(family, type, proto)
137137
self.fd = MocketSocketCore()
138138
self._connected = False
139-
self._buflen = 65536
139+
self._buflen = 4096
140140
self._entry = None
141141
self.family = int(family)
142142
self.type = int(type)
@@ -303,14 +303,16 @@ def true_sendall(self, data, *args, **kwargs):
303303
except KeyError:
304304
self._connect()
305305
self.true_socket.sendall(data, *args, **kwargs)
306-
encoded_response = b''
306+
encoded_response = None
307307
# https://github.com/kennethreitz/requests/blob/master/tests/testserver/server.py#L13
308-
while select.select([self.true_socket], [], [], 0.5)[0]:
308+
while True:
309+
more_to_read = select.select([self.true_socket], [], [], 0.5)[0]
310+
if not more_to_read and encoded_response is not None:
311+
break
309312
recv = self.true_socket.recv(self._buflen)
310-
if recv:
311-
encoded_response += recv
312-
else:
313+
if not recv and encoded_response is not None:
313314
break
315+
encoded_response = encoded_response or b'' + recv
314316

315317
# dump the resulting dictionary to a JSON file
316318
if Mocket.get_truesocket_recording_dir():

mocket/mockredis.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,26 @@ class Redisizer(byte_type):
2020
@staticmethod
2121
def tokens(iterable):
2222
iterable = [encode_to_bytes(x) for x in iterable]
23-
return ['*{0}'.format(len(iterable)).encode('utf-8')] + list(chain(*zip(['${0}'.format(len(x)).encode('utf-8') for x in iterable], iterable)))
23+
return [
24+
'*{0}'.format(len(iterable)).encode('utf-8')
25+
] + list(
26+
chain(*zip(['${0}'.format(len(x)).encode('utf-8') for x in iterable], iterable))
27+
)
2428

2529
@staticmethod
2630
def redisize(data):
31+
def get_conversion(t):
32+
return {
33+
dict: lambda x: b'\r\n'.join(Redisizer.tokens(list(chain(*tuple(x.items()))))),
34+
int: lambda x: ':{0}'.format(x).encode('utf-8'),
35+
text_type: lambda x: '${0}\r\n{1}'.format(len(x.encode('utf-8')), x).encode('utf-8'),
36+
list: lambda x: b'\r\n'.join(Redisizer.tokens(x)),
37+
}[t]
2738
if isinstance(data, Redisizer):
2839
return data
2940
if isinstance(data, byte_type):
3041
data = decode_from_bytes(data)
31-
CONVERSION = {
32-
dict: lambda x: b'\r\n'.join(Redisizer.tokens(list(chain(*tuple(x.items()))))),
33-
int: lambda x: ':{0}'.format(x).encode('utf-8'),
34-
text_type: lambda x: '${0}\r\n{1}'.format(len(x.encode('utf-8')), x).encode('utf-8'),
35-
list: lambda x: b'\r\n'.join(Redisizer.tokens(x)),
36-
}
37-
return Redisizer(CONVERSION[type(data)](data) + b'\r\n')
42+
return Redisizer(get_conversion(data.__class__)(data) + b'\r\n')
3843

3944
@staticmethod
4045
def command(description, _type='+'):

mocket/plugins/httpretty/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
from mocket.compat import text_type, byte_type, decode_from_bytes
1+
from mocket.compat import decode_from_bytes
22

33
decode_utf8 = decode_from_bytes

mocket/plugins/pook_mock_engine.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,3 @@ def mocket_mock_fun(*args, **kwargs):
6767
# mocking pook.mock()
6868
self.pook_mock_fun = self.engine.mock
6969
self.engine.mock = mocket_mock_fun
70-

shippable.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ python:
55
- 3.4
66
- 3.5
77
- 3.6
8-
- 3.7
8+
#- 3.7
99
- pypy
1010
- pypy3
1111

tests/main/test_https.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ def test_json(response):
4141
recording_directory = tempfile.mkdtemp()
4242

4343

44+
@pytest.mark.skipif('os.getenv("SKIP_TRUE_HTTP", False)')
4445
@mocketize(truesocket_recording_dir=recording_directory)
4546
def test_truesendall_with_recording_https():
4647
url = 'https://httpbin.org/ip'

0 commit comments

Comments
 (0)