From 0a5e71b4c70aab87125a54d7a59765e18d7583a4 Mon Sep 17 00:00:00 2001 From: Andrew Svetlov Date: Wed, 24 Jan 2018 21:40:35 +0200 Subject: [PATCH] bpo-32410: Improve sock_sendfile tests (#5294) * Rename sock_sendfile test methods * Make sock_sendfile tests more stable --- Lib/test/test_asyncio/test_unix_events.py | 35 ++++++++++++----------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/Lib/test/test_asyncio/test_unix_events.py b/Lib/test/test_asyncio/test_unix_events.py index 8646bf765e9..5bd76d30d2d 100644 --- a/Lib/test/test_asyncio/test_unix_events.py +++ b/Lib/test/test_asyncio/test_unix_events.py @@ -484,8 +484,11 @@ class SelectorEventLoopUnixSockSendfileTests(test_utils.TestCase): self.run_loop(self.loop.sock_connect(sock, (support.HOST, port))) def cleanup(): - proto.transport.close() - self.run_loop(proto.wait_closed()) + if proto.transport is not None: + # can be None if the task was cancelled before + # connection_made callback + proto.transport.close() + self.run_loop(proto.wait_closed()) server.close() self.run_loop(server.wait_closed()) @@ -494,7 +497,7 @@ class SelectorEventLoopUnixSockSendfileTests(test_utils.TestCase): return sock, proto - def test_success(self): + def test_sock_sendfile_success(self): sock, proto = self.prepare() ret = self.run_loop(self.loop.sock_sendfile(sock, self.file)) sock.close() @@ -504,7 +507,7 @@ class SelectorEventLoopUnixSockSendfileTests(test_utils.TestCase): self.assertEqual(proto.data, self.DATA) self.assertEqual(self.file.tell(), len(self.DATA)) - def test_with_offset_and_count(self): + def test_sock_sendfile_with_offset_and_count(self): sock, proto = self.prepare() ret = self.run_loop(self.loop.sock_sendfile(sock, self.file, 1000, 2000)) @@ -515,7 +518,7 @@ class SelectorEventLoopUnixSockSendfileTests(test_utils.TestCase): self.assertEqual(self.file.tell(), 3000) self.assertEqual(ret, 2000) - def test_sendfile_not_available(self): + def test_sock_sendfile_not_available(self): sock, proto = self.prepare() with mock.patch('asyncio.unix_events.os', spec=[]): with self.assertRaisesRegex(events.SendfileNotAvailableError, @@ -524,7 +527,7 @@ class SelectorEventLoopUnixSockSendfileTests(test_utils.TestCase): 0, None)) self.assertEqual(self.file.tell(), 0) - def test_sendfile_not_a_file(self): + def test_sock_sendfile_not_a_file(self): sock, proto = self.prepare() f = object() with self.assertRaisesRegex(events.SendfileNotAvailableError, @@ -533,7 +536,7 @@ class SelectorEventLoopUnixSockSendfileTests(test_utils.TestCase): 0, None)) self.assertEqual(self.file.tell(), 0) - def test_sendfile_iobuffer(self): + def test_sock_sendfile_iobuffer(self): sock, proto = self.prepare() f = io.BytesIO() with self.assertRaisesRegex(events.SendfileNotAvailableError, @@ -542,7 +545,7 @@ class SelectorEventLoopUnixSockSendfileTests(test_utils.TestCase): 0, None)) self.assertEqual(self.file.tell(), 0) - def test_sendfile_not_regular_file(self): + def test_sock_sendfile_not_regular_file(self): sock, proto = self.prepare() f = mock.Mock() f.fileno.return_value = -1 @@ -552,7 +555,7 @@ class SelectorEventLoopUnixSockSendfileTests(test_utils.TestCase): 0, None)) self.assertEqual(self.file.tell(), 0) - def test_sendfile_zero_size(self): + def test_sock_sendfile_zero_size(self): sock, proto = self.prepare() fname = support.TESTFN + '.suffix' with open(fname, 'wb') as f: @@ -568,7 +571,7 @@ class SelectorEventLoopUnixSockSendfileTests(test_utils.TestCase): self.assertEqual(ret, 0) self.assertEqual(self.file.tell(), 0) - def test_mix_sendfile_and_regular_send(self): + def test_sock_sendfile_mix_with_regular_send(self): buf = b'1234567890' * 1024 * 1024 # 10 MB sock, proto = self.prepare() self.run_loop(self.loop.sock_sendall(sock, buf)) @@ -582,7 +585,7 @@ class SelectorEventLoopUnixSockSendfileTests(test_utils.TestCase): self.assertEqual(proto.data, expected) self.assertEqual(self.file.tell(), len(self.DATA)) - def test_cancel1(self): + def test_sock_sendfile_cancel1(self): sock, proto = self.prepare() fut = self.loop.create_future() @@ -595,7 +598,7 @@ class SelectorEventLoopUnixSockSendfileTests(test_utils.TestCase): with self.assertRaises(KeyError): self.loop._selector.get_key(sock) - def test_cancel2(self): + def test_sock_sendfile_cancel2(self): sock, proto = self.prepare() fut = self.loop.create_future() @@ -608,7 +611,7 @@ class SelectorEventLoopUnixSockSendfileTests(test_utils.TestCase): with self.assertRaises(KeyError): self.loop._selector.get_key(sock) - def test_blocking_error(self): + def test_sock_sendfile_blocking_error(self): sock, proto = self.prepare() fileno = self.file.fileno() @@ -621,7 +624,7 @@ class SelectorEventLoopUnixSockSendfileTests(test_utils.TestCase): self.assertIsNotNone(key) fut.add_done_callback.assert_called_once_with(mock.ANY) - def test_os_error_first_call(self): + def test_sock_sendfile_os_error_first_call(self): sock, proto = self.prepare() fileno = self.file.fileno() @@ -635,7 +638,7 @@ class SelectorEventLoopUnixSockSendfileTests(test_utils.TestCase): self.assertIsInstance(exc, events.SendfileNotAvailableError) self.assertEqual(0, self.file.tell()) - def test_os_error_next_call(self): + def test_sock_sendfile_os_error_next_call(self): sock, proto = self.prepare() fileno = self.file.fileno() @@ -652,7 +655,7 @@ class SelectorEventLoopUnixSockSendfileTests(test_utils.TestCase): self.assertIs(exc, err) self.assertEqual(1000, self.file.tell()) - def test_exception(self): + def test_sock_sendfile_exception(self): sock, proto = self.prepare() fileno = self.file.fileno()