bpo-32410: Improve sock_sendfile tests (#5294)

* Rename sock_sendfile test methods
* Make sock_sendfile tests more stable
This commit is contained in:
Andrew Svetlov 2018-01-24 21:40:35 +02:00 committed by GitHub
parent d499031f5f
commit 0a5e71b4c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 19 additions and 16 deletions

View File

@ -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()