Commit 3db30e93 authored by Jason Madden's avatar Jason Madden

Fix test__ssl and test__socket under Py3.3; mostly a bytes/unicode thing....

Fix test__ssl and test__socket under Py3.3; mostly a bytes/unicode thing. These tests, however, now hang under Py3.4 (previously they weren't getting far enough to do so), so temporarily remove from the Travis test matrix while debugging.
parent a97ed09e
......@@ -3,7 +3,6 @@ python:
- "2.6"
- "2.7"
- "3.3"
- "3.4"
- "pypy"
script:
- if [[ $TRAVIS_PYTHON_VERSION == 'pypy' ]]; then NWORKERS=4 PYTHON=pypy make travis_pypy; fi
......
......@@ -44,9 +44,11 @@ class socket(_socket.socket):
self._write_event = io_class(fileno, 2)
self.timeout = _socket.getdefaulttimeout()
@property
def type(self):
return _socket.socket.type.__get__(self) & ~_socket.SOCK_NONBLOCK
if hasattr(_socket, 'SOCK_NONBLOCK'):
# Only defined under Linux
@property
def type(self):
return _socket.socket.type.__get__(self) & ~_socket.SOCK_NONBLOCK
def __enter__(self):
return self
......
......@@ -60,6 +60,7 @@ class SSLSocket(socket):
suppress_ragged_eofs=True, npn_protocols=None, ciphers=None,
server_hostname=None,
_context=None):
if _context:
self.context = _context
else:
......
......@@ -32,6 +32,7 @@ class Thread(_Thread):
target = kwargs.pop('target')
target = wrap_error(target)
_Thread.__init__(self, target=target, **kwargs)
self.daemon = True
self.start()
......@@ -40,6 +41,8 @@ class TestTCP(greentest.TestCase):
__timeout__ = None
TIMEOUT_ERROR = socket.timeout
long_data = ", ".join([str(x) for x in range(20000)])
if six.PY3:
long_data = long_data.encode('ascii')
def setUp(self):
greentest.TestCase.setUp(self)
......@@ -49,6 +52,10 @@ class TestTCP(greentest.TestCase):
self.port = listener.getsockname()[1]
def cleanup(self):
try:
self.listener.close()
except:
pass
del self.listener
def create_connection(self):
......@@ -62,7 +69,11 @@ class TestTCP(greentest.TestCase):
def accept_and_read():
try:
read_data.append(self.listener.accept()[0].makefile().read())
conn, _ = self.listener.accept()
r = conn.makefile(mode='rb')
read_data.append(r.read())
r.close()
conn.close()
except:
traceback.print_exc()
os._exit(1)
......@@ -72,13 +83,14 @@ class TestTCP(greentest.TestCase):
client.sendall(data)
client.close()
server.join()
assert read_data[0] == self.long_data, read_data
self.assertEqual(read_data[0], self.long_data)
def test_sendall_str(self):
self._test_sendall(self.long_data)
def test_sendall_unicode(self):
self._test_sendall(six.text_type(self.long_data))
if not six.PY3:
def test_sendall_unicode(self):
self._test_sendall(six.text_type(self.long_data))
def test_sendall_array(self):
data = array.array("B", self.long_data)
......@@ -89,25 +101,28 @@ class TestTCP(greentest.TestCase):
N = 100000
def server():
(client, addr) = self.listener.accept()
(remote_client, _) = self.listener.accept()
# start reading, then, while reading, start writing. the reader should not hang forever
def sendall():
client.sendall('t' * N)
remote_client.sendall(b't' * N)
sender = Thread(target=sendall)
result = client.recv(1000)
self.assertEqual(result, 'hello world')
result = remote_client.recv(1000)
self.assertEqual(result, b'hello world')
sender.join()
remote_client.close()
server_thread = Thread(target=server)
client = self.create_connection()
client_reader = Thread(target=client.makefile().read, args=(N, ))
client_file = client.makefile()
client_reader = Thread(target=client_file.read, args=(N, ))
time.sleep(0.1)
client.send('hello world')
client.sendall(b'hello world')
time.sleep(0.1)
# close() used to hang
client_file.close()
client.close()
# this tests "full duplex" bug;
......@@ -125,6 +140,8 @@ class TestTCP(greentest.TestCase):
took = time.time() - start
assert 1 - 0.1 <= took <= 1 + 0.1, (time.time() - start)
acceptor.join()
client.close()
client_sock[0][0].close()
# On Windows send() accepts whatever is thrown at it
if sys.platform != 'win32':
......@@ -136,28 +153,30 @@ class TestTCP(greentest.TestCase):
time.sleep(0.1)
assert client_sock
client.settimeout(0.1)
data_sent = 'h' * 1000000
data_sent = b'h' * 1000000
start = time.time()
self.assertRaises(self.TIMEOUT_ERROR, client.sendall, data_sent)
took = time.time() - start
assert 0.1 - 0.01 <= took <= 0.1 + 0.1, took
acceptor.join()
client.close()
client_sock[0][0].close()
def test_makefile(self):
def accept_once():
conn, addr = self.listener.accept()
fd = conn.makefile(mode='w')
fd.write('hello\n')
fd = conn.makefile(mode='wb')
fd.write(b'hello\n')
fd.close()
conn.close() # for pypy
acceptor = Thread(target=accept_once)
client = self.create_connection()
fd = client.makefile()
fd = client.makefile(mode='rb')
client.close()
assert fd.readline() == 'hello\n'
assert fd.read() == ''
assert fd.readline() == b'hello\n'
assert fd.read() == b''
fd.close()
acceptor.join()
......
......@@ -10,12 +10,14 @@ class TestSSL(TestTCP):
certfile = os.path.join(os.path.dirname(__file__), 'test_server.crt')
privfile = os.path.join(os.path.dirname(__file__), 'test_server.key')
TIMEOUT_ERROR = socket.sslerror
# Python 2.x has socket.sslerror, which we need to be sure is an alias for
# ssl.SSLError. That's gone in Py3 though.
TIMEOUT_ERROR = getattr(socket, 'sslerror', ssl.SSLError)
def setUp(self):
greentest.TestCase.setUp(self)
self.listener, r = ssl_listener(('127.0.0.1', 0), self.privfile, self.certfile)
self.port = r.getsockname()[1]
self.listener, raw_listener = ssl_listener(('127.0.0.1', 0), self.privfile, self.certfile)
self.port = self.listener.getsockname()[1]
def create_connection(self):
return ssl.wrap_socket(super(TestSSL, self).create_connection())
......@@ -27,10 +29,10 @@ del TestTCP
def ssl_listener(address, private_key, certificate):
r = socket.socket()
greentest.bind_and_listen(r, address)
sock = ssl.wrap_socket(r, private_key, certificate)
return sock, r
raw_listener = socket.socket()
greentest.bind_and_listen(raw_listener, address)
sock = ssl.wrap_socket(raw_listener, private_key, certificate)
return sock, raw_listener
if __name__ == '__main__':
......
......@@ -92,7 +92,6 @@ test__os.py
test__backdoor.py
test_threading_2.py
test__refcount.py
test__socket.py
test__subprocess.py
test__all__.py
test__fileobject.py
......@@ -100,7 +99,6 @@ test__pywsgi.py
test__socket_ex.py
test__example_echoserver.py
test__subprocess_poll.py
test__ssl.py
test__makefile_ref.py
test__socketpair.py
test__server_pywsgi.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