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 ...@@ -13,21 +13,20 @@ from gevent.server import StreamServer
# this handler will be run for each incoming connection in a dedicated greenlet # this handler will be run for each incoming connection in a dedicated greenlet
def echo(socket, address): def echo(socket, address):
print('New connection from %s:%s' % 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() # using a makefile because we want to use readline()
fileobj = socket.makefile() rfileobj = socket.makefile(mode='rb')
while True: while True:
line = fileobj.readline() line = rfileobj.readline()
if not line: if not line:
print("client disconnected") print("client disconnected")
break break
if line.strip().lower() == 'quit': if line.strip().lower() == b'quit':
print("client quit") print("client quit")
break break
fileobj.write(line) socket.sendall(line)
fileobj.flush()
print("echoed %r" % line) print("echoed %r" % line)
rfileobj.close()
if __name__ == '__main__': if __name__ == '__main__':
# to make the server use SSL, pass certfile and keyfile arguments to the constructor # to make the server use SSL, pass certfile and keyfile arguments to the constructor
......
from gevent.socket import create_connection, timeout from gevent.socket import create_connection, timeout
from unittest import main from unittest import main
import gevent import gevent
from gevent.hub import PY3
import util import util
...@@ -10,16 +11,29 @@ class Test(util.TestServer): ...@@ -10,16 +11,29 @@ class Test(util.TestServer):
def _run_all_tests(self): def _run_all_tests(self):
def test_client(message): def test_client(message):
conn = create_connection(('127.0.0.1', 6000)).makefile(bufsize=1) if PY3:
welcome = conn.readline() kwargs = {'buffering': 1}
assert 'Welcome' in welcome, repr(welcome) else:
conn.write(message) kwargs = {'bufsize': 1}
received = conn.read(len(message)) 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) self.assertEqual(received, message)
conn._sock.settimeout(0.1)
self.assertRaises(timeout, conn.read, 1) self.assertRaises(timeout, conn.recv, 1)
client1 = gevent.spawn(test_client, 'hello\r\n')
client2 = gevent.spawn(test_client, 'world\r\n') 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) gevent.joinall([client1, client2], raise_error=True)
......
...@@ -88,7 +88,6 @@ test_threading_2.py ...@@ -88,7 +88,6 @@ test_threading_2.py
test__refcount.py test__refcount.py
test__all__.py test__all__.py
test__pywsgi.py test__pywsgi.py
test__example_echoserver.py
test__makefile_ref.py test__makefile_ref.py
test__server_pywsgi.py test__server_pywsgi.py
test__core_stat.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