Commit 9d80ea8e authored by Jason Madden's avatar Jason Madden

Fix test__ssl under leakchecks. Refactor test__ssl to share more with test__socket.

parent 74e04b49
......@@ -254,7 +254,9 @@ def wrap_refcount(method):
d = sum(hist_before.values())
self.setUp()
try:
method(self, *args, **kwargs)
finally:
self.tearDown()
# Grab post snapshot
......
......@@ -119,15 +119,12 @@ class Test(greentest.TestCase):
# Keeping raw sockets alive keeps SSL sockets
# from being closed too, at least on CPython, so we
# need to use weakrefs
if 'close_on_teardown' not in self.__dict__:
self.close_on_teardown = []
self.close_on_teardown.append(weakref.ref(resource))
return resource
def _tearDownCloseOnTearDown(self):
self.close_on_teardown = [r() for r in self.close_on_teardown if r() is not None]
super(Test, self)._tearDownCloseOnTearDown()
self.close_on_teardown = []
class TestSocket(Test):
......
......@@ -47,8 +47,8 @@ class TestTCP(greentest.TestCase):
def setUp(self):
super(TestTCP, self).setUp()
listener = socket.socket()
self._close_on_teardown(listener)
self.listener = self._close_on_teardown(self._setup_listener())
# XXX: On Windows (at least with libev), if we have a cleanup/tearDown method
# that does 'del self.listener' AND we haven't sometime
# previously closed the listener (while the test body was executing)
......@@ -64,10 +64,12 @@ class TestTCP(greentest.TestCase):
# Perhaps our logic is wrong in libev_vfd in the way we use
# _open_osfhandle and determine we can close it?
greentest.bind_and_listen(listener, ('127.0.0.1', 0))
self.listener = listener
self.port = listener.getsockname()[1]
self.port = self.listener.getsockname()[1]
def _setup_listener(self):
listener = socket.socket()
greentest.bind_and_listen(listener, ('127.0.0.1', 0))
return listener
def create_connection(self, host='127.0.0.1', port=None, timeout=None,
blocking=None):
......
......@@ -20,10 +20,10 @@ class TestSSL(test__socket.TestTCP):
# See https://bugs.python.org/issue10272
TIMEOUT_ERROR = getattr(socket, 'sslerror', socket.timeout)
def setUp(self):
greentest.TestCase.setUp(self)
self.listener, _raw_listener = ssl_listener(('127.0.0.1', 0), self.privfile, self.certfile)
self.port = self.listener.getsockname()[1]
def _setup_listener(self):
listener, raw_listener = ssl_listener(('127.0.0.1', 0), self.privfile, self.certfile)
self._close_on_teardown(raw_listener)
return listener
def create_connection(self, *args, **kwargs):
return ssl.wrap_socket(super(TestSSL, self).create_connection(*args, **kwargs))
......@@ -50,26 +50,33 @@ class TestSSL(test__socket.TestTCP):
# on non-blocking sockets because it's a simple loop around
# send(). Python 2.6 doesn't have SSLWantWriteError
expected = getattr(ssl, 'SSLWantWriteError', ssl.SSLError)
self.assertRaises(expected, client.sendall, self._test_sendall_data)
with self.assertRaises(expected):
client.sendall(self._test_sendall_data)
finally:
acceptor.join()
client.close()
server_sock[0][0].close()
@greentest.ignores_leakcheck
def test_empty_send(self):
# Issue 719
# Sending empty bytes with the 'send' method raises
# ssl.SSLEOFError in the stdlib. PyPy 4.0 and CPython 2.6
# both just raise the superclass, ssl.SSLError.
expected = ssl.SSLError
self.assertRaises(expected, self._test_sendall,
b'',
client_method='send')
# Ignored during leakchecks because the third or fourth iteration of the
# test hangs on CPython 2/posix for some reason, likely due to
# the use of _close_on_teardown keeping something alive longer than intended.
# cf test__makefile_ref
with self.assertRaises(ssl.SSLError):
super(TestSSL, self).test_empty_send()
@greentest.ignores_leakcheck
def test_sendall_nonblocking(self):
# Override; doesn't work with SSL sockets.
pass
@greentest.ignores_leakcheck
def test_connect_with_type_flags_ignored(self):
# Override; doesn't work with SSL sockets.
pass
......
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