Commit 0688a69a authored by Jason Madden's avatar Jason Madden

Fix test__example_echoserver.py. Text/binary issues in the server and client...

Fix test__example_echoserver.py. Text/binary issues in the server and client as well as a changed kwarg.
parent 5764ee11
......@@ -13,21 +13,20 @@ from gevent.server import StreamServer
# this handler will be run for each incoming connection in a dedicated greenlet
def echo(socket, address):
print('New connection from %s:%s' % address)
socket.sendall('Welcome to the echo server! Type quit to exit.\r\n')
socket.sendall(b'Welcome to the echo server! Type quit to exit.\r\n')
# using a makefile because we want to use readline()
fileobj = socket.makefile()
rfileobj = socket.makefile(mode='rb')
while True:
line = fileobj.readline()
line = rfileobj.readline()
if not line:
print("client disconnected")
break
if line.strip().lower() == 'quit':
if line.strip().lower() == b'quit':
print("client quit")
break
fileobj.write(line)
fileobj.flush()
socket.sendall(line)
print("echoed %r" % line)
rfileobj.close()
if __name__ == '__main__':
# to make the server use SSL, pass certfile and keyfile arguments to the constructor
......
from gevent.socket import create_connection, timeout
from unittest import main
import gevent
from gevent.hub import PY3
import util
......@@ -10,16 +11,29 @@ class Test(util.TestServer):
def _run_all_tests(self):
def test_client(message):
conn = create_connection(('127.0.0.1', 6000)).makefile(bufsize=1)
welcome = conn.readline()
assert 'Welcome' in welcome, repr(welcome)
conn.write(message)
received = conn.read(len(message))
if PY3:
kwargs = {'buffering': 1}
else:
kwargs = {'bufsize': 1}
kwargs['mode'] = 'rb'
conn = create_connection(('127.0.0.1', 6000))
conn.settimeout(0.1)
rfile = conn.makefile(**kwargs)
welcome = rfile.readline()
assert b'Welcome' in welcome, repr(welcome)
conn.sendall(message)
received = rfile.read(len(message))
self.assertEqual(received, message)
conn._sock.settimeout(0.1)
self.assertRaises(timeout, conn.read, 1)
client1 = gevent.spawn(test_client, 'hello\r\n')
client2 = gevent.spawn(test_client, 'world\r\n')
self.assertRaises(timeout, conn.recv, 1)
rfile.close()
conn.close()
client1 = gevent.spawn(test_client, b'hello\r\n')
client2 = gevent.spawn(test_client, b'world\r\n')
gevent.joinall([client1, client2], raise_error=True)
......
......@@ -88,7 +88,6 @@ test_threading_2.py
test__refcount.py
test__all__.py
test__pywsgi.py
test__example_echoserver.py
test__makefile_ref.py
test__server_pywsgi.py
test__core_stat.py
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment