Skip to content

Commit

Permalink
greenio: socket.recv() could return str; Thanks to jerzyk
Browse files Browse the repository at this point in the history
It must always return bytes.
#245
  • Loading branch information
temoto committed Sep 6, 2015
1 parent c3ce3ee commit 001f31f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
2 changes: 1 addition & 1 deletion eventlet/greenio/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ def recv(self, buflen, flags=0):
if get_errno(e) in SOCKET_BLOCKING:
pass
elif get_errno(e) in SOCKET_CLOSED:
return ''
return b''
else:
raise
try:
Expand Down
13 changes: 9 additions & 4 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,13 +292,15 @@ def get_database_auth():
return retval


def run_python(path):
def run_python(path, env=None):
if not path.endswith('.py'):
path += '.py'
path = os.path.abspath(path)
src_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
new_env = os.environ.copy()
new_env['PYTHONPATH'] = os.pathsep.join(sys.path + [src_dir])
if env:
new_env.update(env)
p = subprocess.Popen(
[sys.executable, path],
env=new_env,
Expand All @@ -310,15 +312,18 @@ def run_python(path):
return output


def run_isolated(path, prefix='tests/isolated/'):
output = run_python(prefix + path).rstrip()
def run_isolated(path, prefix='tests/isolated/', env=None):
output = run_python(prefix + path, env=env).rstrip()
if output.startswith(b'skip'):
parts = output.split(b':', 1)
skip_args = []
if len(parts) > 1:
skip_args.append(parts[1])
raise SkipTest(*skip_args)
assert output == b'pass', output
ok = output == b'pass'
if not ok:
sys.stderr.write('Isolated test {0} output:\n---\n{1}\n---\n'.format(path, output.decode()))
assert ok, 'Expected single line "pass" in stdout'


certificate_file = os.path.join(os.path.dirname(__file__), 'test_server.crt')
Expand Down
23 changes: 23 additions & 0 deletions tests/socket_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import eventlet
from eventlet.green import socket


Expand All @@ -6,3 +7,25 @@ def test_create_connection_error():
socket.create_connection(('192.0.2.1', 80), timeout=0.1)
except (IOError, OSError):
pass


def test_recv_type():
# https://github.com/eventlet/eventlet/issues/245
# socket recv returning multiple data types
# For this test to work, client and server have to be in separate
# processes or OS threads. Just running two greenthreads gives
# false test pass.
threading = eventlet.patcher.original('threading')
addr = []

def server():
sock = eventlet.listen(('127.0.0.1', 0))
addr[:] = sock.getsockname()
eventlet.sleep(0.2)

server_thread = threading.Thread(target=server)
server_thread.start()
eventlet.sleep(0.1)
sock = eventlet.connect(tuple(addr))
s = sock.recv(1)
assert isinstance(s, bytes)

0 comments on commit 001f31f

Please sign in to comment.