diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2014-06-03 22:13:31 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@gmail.com> | 2014-06-03 22:13:31 (GMT) |
commit | b470f0dd2a4a539bb8ba44c818f685fb9a260d38 (patch) | |
tree | c25572fef36655186c2e0f7e164d4b08e455125d /Lib/test | |
parent | ef7f14036635e76f1bca975662130047d39314eb (diff) | |
parent | a9fa2664ab98a1077e5b16d66c17aea0cd631ed7 (diff) | |
download | cpython-b470f0dd2a4a539bb8ba44c818f685fb9a260d38.zip cpython-b470f0dd2a4a539bb8ba44c818f685fb9a260d38.tar.gz cpython-b470f0dd2a4a539bb8ba44c818f685fb9a260d38.tar.bz2 |
Merge 3.4: Issue #21119, fix ResourceWarning in asyncio
* Make sure that socketpair() close sockets on error. Close the listening
socket if sock.bind() raises an exception.
* asyncio now closes sockets on errors. Fix ResourceWarning:
create_connection(), create_datagram_endpoint() and create_unix_server()
methods of event loop now close the newly created socket on error.
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/test_asyncio/test_base_events.py | 21 | ||||
-rw-r--r-- | Lib/test/test_asyncio/test_unix_events.py | 18 | ||||
-rw-r--r-- | Lib/test/test_asyncio/test_windows_utils.py | 9 |
3 files changed, 48 insertions, 0 deletions
diff --git a/Lib/test/test_asyncio/test_base_events.py b/Lib/test/test_asyncio/test_base_events.py index 4ba9556..dbcd590 100644 --- a/Lib/test/test_asyncio/test_base_events.py +++ b/Lib/test/test_asyncio/test_base_events.py @@ -583,6 +583,27 @@ class BaseEventLoopWithSelectorTests(unittest.TestCase): self.assertEqual(str(cm.exception), 'Multiple exceptions: err1, err2') + @mock.patch('asyncio.base_events.socket') + def test_create_connection_timeout(self, m_socket): + # Ensure that the socket is closed on timeout + sock = mock.Mock() + m_socket.socket.return_value = sock + + def getaddrinfo(*args, **kw): + fut = asyncio.Future(loop=self.loop) + addr = (socket.AF_INET, socket.SOCK_STREAM, 0, '', + ('127.0.0.1', 80)) + fut.set_result([addr]) + return fut + self.loop.getaddrinfo = getaddrinfo + + with mock.patch.object(self.loop, 'sock_connect', + side_effect=asyncio.TimeoutError): + coro = self.loop.create_connection(MyProto, '127.0.0.1', 80) + with self.assertRaises(asyncio.TimeoutError) as cm: + self.loop.run_until_complete(coro) + self.assertTrue(sock.close.called) + def test_create_connection_host_port_sock(self): coro = self.loop.create_connection( MyProto, 'example.com', 80, sock=object()) diff --git a/Lib/test/test_asyncio/test_unix_events.py b/Lib/test/test_asyncio/test_unix_events.py index 744c319..cec7a11 100644 --- a/Lib/test/test_asyncio/test_unix_events.py +++ b/Lib/test/test_asyncio/test_unix_events.py @@ -256,6 +256,24 @@ class SelectorEventLoopUnixSocketTests(unittest.TestCase): 'A UNIX Domain Socket was expected'): self.loop.run_until_complete(coro) + @mock.patch('asyncio.unix_events.socket') + def test_create_unix_server_bind_error(self, m_socket): + # Ensure that the socket is closed on any bind error + sock = mock.Mock() + m_socket.socket.return_value = sock + + sock.bind.side_effect = OSError + coro = self.loop.create_unix_server(lambda: None, path="/test") + with self.assertRaises(OSError): + self.loop.run_until_complete(coro) + self.assertTrue(sock.close.called) + + sock.bind.side_effect = MemoryError + coro = self.loop.create_unix_server(lambda: None, path="/test") + with self.assertRaises(MemoryError): + self.loop.run_until_complete(coro) + self.assertTrue(sock.close.called) + def test_create_unix_connection_path_sock(self): coro = self.loop.create_unix_connection( lambda: None, '/dev/null', sock=object()) diff --git a/Lib/test/test_asyncio/test_windows_utils.py b/Lib/test/test_asyncio/test_windows_utils.py index 9daf434..b1f81da 100644 --- a/Lib/test/test_asyncio/test_windows_utils.py +++ b/Lib/test/test_asyncio/test_windows_utils.py @@ -51,6 +51,15 @@ class WinsocketpairTests(unittest.TestCase): self.assertRaises(ValueError, windows_utils.socketpair, proto=1) + @mock.patch('asyncio.windows_utils.socket') + def test_winsocketpair_close(self, m_socket): + m_socket.AF_INET = socket.AF_INET + m_socket.SOCK_STREAM = socket.SOCK_STREAM + sock = mock.Mock() + m_socket.socket.return_value = sock + sock.bind.side_effect = OSError + self.assertRaises(OSError, windows_utils.socketpair) + self.assertTrue(sock.close.called) class PipeTests(unittest.TestCase): |