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