Skip to content

Commit f7d9bab

Browse files
committed
More tests
1 parent a30acc9 commit f7d9bab

File tree

2 files changed

+89
-1
lines changed

2 files changed

+89
-1
lines changed

Lib/asyncio/unix_events.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ def _sock_sendfile_native_impl(self, fut, registered_fd, sock, fileno,
358358
self._sock_add_cancellation_callback(fut, sock)
359359
self.add_writer(fd, self._sock_sendfile_native_impl, fut,
360360
fd, sock, fileno,
361-
offset, count, blocksize. total_sent)
361+
offset, count, blocksize, total_sent)
362362
except OSError as exc:
363363
if total_sent == 0:
364364
# We can get here for different reasons, the main

Lib/test/test_asyncio/test_unix_events.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Tests for unix_events.py."""
22

33
import collections
4+
import contextlib
45
import errno
56
import io
67
import os
@@ -580,6 +581,93 @@ def test_mix_sendfile_and_regular_send(self):
580581
self.assertEqual(proto.data, expected)
581582
self.assertEqual(self.file.tell(), len(self.DATA))
582583

584+
def test_cancel1(self):
585+
sock, proto = self.prepare()
586+
587+
fut = self.loop.create_future()
588+
fileno = self.file.fileno()
589+
self.loop._sock_sendfile_native_impl(fut, None, sock, fileno,
590+
0, None, len(self.DATA), 0)
591+
fut.cancel()
592+
with contextlib.suppress(asyncio.CancelledError):
593+
self.run_loop(fut)
594+
with self.assertRaises(KeyError):
595+
self.loop._selector.get_key(sock)
596+
597+
def test_cancel2(self):
598+
sock, proto = self.prepare()
599+
600+
fut = self.loop.create_future()
601+
fileno = self.file.fileno()
602+
self.loop._sock_sendfile_native_impl(fut, None, sock, fileno,
603+
0, None, len(self.DATA), 0)
604+
fut.cancel()
605+
self.loop._sock_sendfile_native_impl(fut, sock.fileno(), sock, fileno,
606+
0, None, len(self.DATA), 0)
607+
with self.assertRaises(KeyError):
608+
self.loop._selector.get_key(sock)
609+
610+
def test_blocking_error(self):
611+
sock, proto = self.prepare()
612+
613+
fileno = self.file.fileno()
614+
fut = mock.Mock()
615+
fut.cancelled.return_value = False
616+
with mock.patch('os.sendfile', side_effect=BlockingIOError()):
617+
self.loop._sock_sendfile_native_impl(fut, None, sock, fileno,
618+
0, None, len(self.DATA), 0)
619+
key = self.loop._selector.get_key(sock)
620+
self.assertIsNotNone(key)
621+
fut.add_done_callback.assert_called_once_with(mock.ANY)
622+
623+
def test_os_error_first_call(self):
624+
sock, proto = self.prepare()
625+
626+
fileno = self.file.fileno()
627+
fut = self.loop.create_future()
628+
with mock.patch('os.sendfile', side_effect=OSError()):
629+
self.loop._sock_sendfile_native_impl(fut, None, sock, fileno,
630+
0, None, len(self.DATA), 0)
631+
with self.assertRaises(KeyError):
632+
self.loop._selector.get_key(sock)
633+
exc = fut.exception()
634+
self.assertIsInstance(exc, base_events._SendfileNotAvailable)
635+
self.assertEqual(0, self.file.tell())
636+
637+
def test_os_error_next_call(self):
638+
sock, proto = self.prepare()
639+
640+
fileno = self.file.fileno()
641+
fut = self.loop.create_future()
642+
err = OSError()
643+
with mock.patch('os.sendfile', side_effect=err):
644+
self.loop._sock_sendfile_native_impl(fut, sock.fileno(),
645+
sock, fileno,
646+
1000, None, len(self.DATA),
647+
1000)
648+
with self.assertRaises(KeyError):
649+
self.loop._selector.get_key(sock)
650+
exc = fut.exception()
651+
self.assertIs(exc, err)
652+
self.assertEqual(1000, self.file.tell())
653+
654+
def test_exception(self):
655+
sock, proto = self.prepare()
656+
657+
fileno = self.file.fileno()
658+
fut = self.loop.create_future()
659+
err = RuntimeError()
660+
with mock.patch('os.sendfile', side_effect=err):
661+
self.loop._sock_sendfile_native_impl(fut, sock.fileno(),
662+
sock, fileno,
663+
1000, None, len(self.DATA),
664+
1000)
665+
with self.assertRaises(KeyError):
666+
self.loop._selector.get_key(sock)
667+
exc = fut.exception()
668+
self.assertIs(exc, err)
669+
self.assertEqual(1000, self.file.tell())
670+
583671

584672
class UnixReadPipeTransportTests(test_utils.TestCase):
585673

0 commit comments

Comments
 (0)