Skip to content

Commit

Permalink
Adding socket.recv_into() plus its test (#71).
Browse files Browse the repository at this point in the history
  • Loading branch information
mindflayer committed Jul 3, 2018
1 parent fec4fcf commit 760bc2f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
4 changes: 4 additions & 0 deletions mocket/mocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,10 @@ def sendall(self, data, entry=None, *args, **kwargs):
def read(self, buffersize):
return self.fd.read(buffersize)

def recv_into(self, buffer, buffersize, flags=None):
print(buffer.read(), buffersize)
return buffer.write(self.fd.read(buffersize))

def recv(self, buffersize, flags=None):
if Mocket.r_fd and Mocket.w_fd:
return os.read(Mocket.r_fd, buffersize)
Expand Down
4 changes: 2 additions & 2 deletions runtests.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#!/usr/bin/env python
import sys
import os


def runtests(args=None):
import pytest
import pip

if not args:
args = []
Expand All @@ -17,7 +17,7 @@ def runtests(args=None):
if major == 3 and minor >= 5:
python35 = True

pip.main(['install', 'aiohttp', 'async_timeout'])
os.system('pip install aiohttp async_timeout')

if not any(a for a in args[1:] if not a.startswith('-')):
args.append('tests/main')
Expand Down
3 changes: 1 addition & 2 deletions test_requirements.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
pip<10
pep8
pytest
pytest-cov
mock
requests
redis
gevent
sure
sure
23 changes: 23 additions & 0 deletions tests/main/test_mocket.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import unicode_literals
import socket
from unittest import TestCase
import io

import pytest

Expand Down Expand Up @@ -94,6 +95,28 @@ def test_subsequent_recv_requests_have_correct_length(self):
assert _so.recv(4096) == b'Short'
_so.close()

def test_recv_into(self):
Mocket.register(
MocketEntry(
('localhost', 80),
[
b'Long payload',
b'Short'
]
)
)
buffer = io.BytesIO()
with Mocketizer():
_so = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
_so.connect(('localhost', 80))
_so.sendall(b'first\r\n')
assert _so.recv_into(buffer, 4096) == 12
_so.sendall(b'second\r\n')
assert _so.recv_into(buffer, 4096) == 5
_so.close()
buffer.seek(0)
assert buffer.read() == b'Long payloadShort'


class MocketizeTestCase(TestCase):
def mocketize_setup(self):
Expand Down

0 comments on commit 760bc2f

Please sign in to comment.