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