Skip to content

Commit 0a5e71b

Browse files
authored
bpo-32410: Improve sock_sendfile tests (#5294)
* Rename sock_sendfile test methods * Make sock_sendfile tests more stable
1 parent d499031 commit 0a5e71b

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

Lib/test/test_asyncio/test_unix_events.py

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -484,8 +484,11 @@ def prepare(self):
484484
self.run_loop(self.loop.sock_connect(sock, (support.HOST, port)))
485485

486486
def cleanup():
487-
proto.transport.close()
488-
self.run_loop(proto.wait_closed())
487+
if proto.transport is not None:
488+
# can be None if the task was cancelled before
489+
# connection_made callback
490+
proto.transport.close()
491+
self.run_loop(proto.wait_closed())
489492

490493
server.close()
491494
self.run_loop(server.wait_closed())
@@ -494,7 +497,7 @@ def cleanup():
494497

495498
return sock, proto
496499

497-
def test_success(self):
500+
def test_sock_sendfile_success(self):
498501
sock, proto = self.prepare()
499502
ret = self.run_loop(self.loop.sock_sendfile(sock, self.file))
500503
sock.close()
@@ -504,7 +507,7 @@ def test_success(self):
504507
self.assertEqual(proto.data, self.DATA)
505508
self.assertEqual(self.file.tell(), len(self.DATA))
506509

507-
def test_with_offset_and_count(self):
510+
def test_sock_sendfile_with_offset_and_count(self):
508511
sock, proto = self.prepare()
509512
ret = self.run_loop(self.loop.sock_sendfile(sock, self.file,
510513
1000, 2000))
@@ -515,7 +518,7 @@ def test_with_offset_and_count(self):
515518
self.assertEqual(self.file.tell(), 3000)
516519
self.assertEqual(ret, 2000)
517520

518-
def test_sendfile_not_available(self):
521+
def test_sock_sendfile_not_available(self):
519522
sock, proto = self.prepare()
520523
with mock.patch('asyncio.unix_events.os', spec=[]):
521524
with self.assertRaisesRegex(events.SendfileNotAvailableError,
@@ -524,7 +527,7 @@ def test_sendfile_not_available(self):
524527
0, None))
525528
self.assertEqual(self.file.tell(), 0)
526529

527-
def test_sendfile_not_a_file(self):
530+
def test_sock_sendfile_not_a_file(self):
528531
sock, proto = self.prepare()
529532
f = object()
530533
with self.assertRaisesRegex(events.SendfileNotAvailableError,
@@ -533,7 +536,7 @@ def test_sendfile_not_a_file(self):
533536
0, None))
534537
self.assertEqual(self.file.tell(), 0)
535538

536-
def test_sendfile_iobuffer(self):
539+
def test_sock_sendfile_iobuffer(self):
537540
sock, proto = self.prepare()
538541
f = io.BytesIO()
539542
with self.assertRaisesRegex(events.SendfileNotAvailableError,
@@ -542,7 +545,7 @@ def test_sendfile_iobuffer(self):
542545
0, None))
543546
self.assertEqual(self.file.tell(), 0)
544547

545-
def test_sendfile_not_regular_file(self):
548+
def test_sock_sendfile_not_regular_file(self):
546549
sock, proto = self.prepare()
547550
f = mock.Mock()
548551
f.fileno.return_value = -1
@@ -552,7 +555,7 @@ def test_sendfile_not_regular_file(self):
552555
0, None))
553556
self.assertEqual(self.file.tell(), 0)
554557

555-
def test_sendfile_zero_size(self):
558+
def test_sock_sendfile_zero_size(self):
556559
sock, proto = self.prepare()
557560
fname = support.TESTFN + '.suffix'
558561
with open(fname, 'wb') as f:
@@ -568,7 +571,7 @@ def test_sendfile_zero_size(self):
568571
self.assertEqual(ret, 0)
569572
self.assertEqual(self.file.tell(), 0)
570573

571-
def test_mix_sendfile_and_regular_send(self):
574+
def test_sock_sendfile_mix_with_regular_send(self):
572575
buf = b'1234567890' * 1024 * 1024 # 10 MB
573576
sock, proto = self.prepare()
574577
self.run_loop(self.loop.sock_sendall(sock, buf))
@@ -582,7 +585,7 @@ def test_mix_sendfile_and_regular_send(self):
582585
self.assertEqual(proto.data, expected)
583586
self.assertEqual(self.file.tell(), len(self.DATA))
584587

585-
def test_cancel1(self):
588+
def test_sock_sendfile_cancel1(self):
586589
sock, proto = self.prepare()
587590

588591
fut = self.loop.create_future()
@@ -595,7 +598,7 @@ def test_cancel1(self):
595598
with self.assertRaises(KeyError):
596599
self.loop._selector.get_key(sock)
597600

598-
def test_cancel2(self):
601+
def test_sock_sendfile_cancel2(self):
599602
sock, proto = self.prepare()
600603

601604
fut = self.loop.create_future()
@@ -608,7 +611,7 @@ def test_cancel2(self):
608611
with self.assertRaises(KeyError):
609612
self.loop._selector.get_key(sock)
610613

611-
def test_blocking_error(self):
614+
def test_sock_sendfile_blocking_error(self):
612615
sock, proto = self.prepare()
613616

614617
fileno = self.file.fileno()
@@ -621,7 +624,7 @@ def test_blocking_error(self):
621624
self.assertIsNotNone(key)
622625
fut.add_done_callback.assert_called_once_with(mock.ANY)
623626

624-
def test_os_error_first_call(self):
627+
def test_sock_sendfile_os_error_first_call(self):
625628
sock, proto = self.prepare()
626629

627630
fileno = self.file.fileno()
@@ -635,7 +638,7 @@ def test_os_error_first_call(self):
635638
self.assertIsInstance(exc, events.SendfileNotAvailableError)
636639
self.assertEqual(0, self.file.tell())
637640

638-
def test_os_error_next_call(self):
641+
def test_sock_sendfile_os_error_next_call(self):
639642
sock, proto = self.prepare()
640643

641644
fileno = self.file.fileno()
@@ -652,7 +655,7 @@ def test_os_error_next_call(self):
652655
self.assertIs(exc, err)
653656
self.assertEqual(1000, self.file.tell())
654657

655-
def test_exception(self):
658+
def test_sock_sendfile_exception(self):
656659
sock, proto = self.prepare()
657660

658661
fileno = self.file.fileno()

0 commit comments

Comments
 (0)