summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@gmail.com>2015-01-15 12:23:36 (GMT)
committerVictor Stinner <victor.stinner@gmail.com>2015-01-15 12:23:36 (GMT)
commit7d78d0d91c1765cfdf6aa990d008c8a093f3ce4a (patch)
tree268cb7401048d627c38ea7cb5294ed36e9ebdd31 /Lib/test
parentfe47e66792dd32420c705796bc7e3445b0fc6292 (diff)
parent02392c92820d8633ccf906e8c3d0fdb2fe16e5ba (diff)
downloadcpython-7d78d0d91c1765cfdf6aa990d008c8a093f3ce4a.zip
cpython-7d78d0d91c1765cfdf6aa990d008c8a093f3ce4a.tar.gz
cpython-7d78d0d91c1765cfdf6aa990d008c8a093f3ce4a.tar.bz2
Merge 3.4 (asyncio)
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_asyncio/test_base_events.py1
-rw-r--r--Lib/test/test_asyncio/test_proactor_events.py91
-rw-r--r--Lib/test/test_asyncio/test_selector_events.py278
-rw-r--r--Lib/test/test_asyncio/test_unix_events.py157
4 files changed, 249 insertions, 278 deletions
diff --git a/Lib/test/test_asyncio/test_base_events.py b/Lib/test/test_asyncio/test_base_events.py
index bd6c0d8..9e7c50c 100644
--- a/Lib/test/test_asyncio/test_base_events.py
+++ b/Lib/test/test_asyncio/test_base_events.py
@@ -590,6 +590,7 @@ class BaseEventLoopTests(test_utils.TestCase):
raise ValueError('spam')
loop = Loop()
+ self.addCleanup(loop.close)
asyncio.set_event_loop(loop)
def run_loop():
diff --git a/Lib/test/test_asyncio/test_proactor_events.py b/Lib/test/test_asyncio/test_proactor_events.py
index 8258238..dee147e 100644
--- a/Lib/test/test_asyncio/test_proactor_events.py
+++ b/Lib/test/test_asyncio/test_proactor_events.py
@@ -12,26 +12,41 @@ from asyncio.proactor_events import _ProactorDuplexPipeTransport
from asyncio import test_utils
+def close_transport(transport):
+ # Don't call transport.close() because the event loop and the IOCP proactor
+ # are mocked
+ if transport._sock is None:
+ return
+ transport._sock.close()
+ transport._sock = None
+
+
class ProactorSocketTransportTests(test_utils.TestCase):
def setUp(self):
self.loop = self.new_test_loop()
+ self.addCleanup(self.loop.close)
self.proactor = mock.Mock()
self.loop._proactor = self.proactor
self.protocol = test_utils.make_test_protocol(asyncio.Protocol)
self.sock = mock.Mock(socket.socket)
+ def socket_transport(self, waiter=None):
+ transport = _ProactorSocketTransport(self.loop, self.sock,
+ self.protocol, waiter=waiter)
+ self.addCleanup(close_transport, transport)
+ return transport
+
def test_ctor(self):
fut = asyncio.Future(loop=self.loop)
- tr = _ProactorSocketTransport(
- self.loop, self.sock, self.protocol, fut)
+ tr = self.socket_transport(waiter=fut)
test_utils.run_briefly(self.loop)
self.assertIsNone(fut.result())
self.protocol.connection_made(tr)
self.proactor.recv.assert_called_with(self.sock, 4096)
def test_loop_reading(self):
- tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
+ tr = self.socket_transport()
tr._loop_reading()
self.loop._proactor.recv.assert_called_with(self.sock, 4096)
self.assertFalse(self.protocol.data_received.called)
@@ -41,8 +56,7 @@ class ProactorSocketTransportTests(test_utils.TestCase):
res = asyncio.Future(loop=self.loop)
res.set_result(b'data')
- tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
-
+ tr = self.socket_transport()
tr._read_fut = res
tr._loop_reading(res)
self.loop._proactor.recv.assert_called_with(self.sock, 4096)
@@ -52,8 +66,7 @@ class ProactorSocketTransportTests(test_utils.TestCase):
res = asyncio.Future(loop=self.loop)
res.set_result(b'')
- tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
-
+ tr = self.socket_transport()
self.assertRaises(AssertionError, tr._loop_reading, res)
tr.close = mock.Mock()
@@ -66,7 +79,7 @@ class ProactorSocketTransportTests(test_utils.TestCase):
def test_loop_reading_aborted(self):
err = self.loop._proactor.recv.side_effect = ConnectionAbortedError()
- tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
+ tr = self.socket_transport()
tr._fatal_error = mock.Mock()
tr._loop_reading()
tr._fatal_error.assert_called_with(
@@ -76,7 +89,7 @@ class ProactorSocketTransportTests(test_utils.TestCase):
def test_loop_reading_aborted_closing(self):
self.loop._proactor.recv.side_effect = ConnectionAbortedError()
- tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
+ tr = self.socket_transport()
tr._closing = True
tr._fatal_error = mock.Mock()
tr._loop_reading()
@@ -84,7 +97,7 @@ class ProactorSocketTransportTests(test_utils.TestCase):
def test_loop_reading_aborted_is_fatal(self):
self.loop._proactor.recv.side_effect = ConnectionAbortedError()
- tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
+ tr = self.socket_transport()
tr._closing = False
tr._fatal_error = mock.Mock()
tr._loop_reading()
@@ -93,7 +106,7 @@ class ProactorSocketTransportTests(test_utils.TestCase):
def test_loop_reading_conn_reset_lost(self):
err = self.loop._proactor.recv.side_effect = ConnectionResetError()
- tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
+ tr = self.socket_transport()
tr._closing = False
tr._fatal_error = mock.Mock()
tr._force_close = mock.Mock()
@@ -104,7 +117,7 @@ class ProactorSocketTransportTests(test_utils.TestCase):
def test_loop_reading_exception(self):
err = self.loop._proactor.recv.side_effect = (OSError())
- tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
+ tr = self.socket_transport()
tr._fatal_error = mock.Mock()
tr._loop_reading()
tr._fatal_error.assert_called_with(
@@ -112,19 +125,19 @@ class ProactorSocketTransportTests(test_utils.TestCase):
'Fatal read error on pipe transport')
def test_write(self):
- tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
+ tr = self.socket_transport()
tr._loop_writing = mock.Mock()
tr.write(b'data')
self.assertEqual(tr._buffer, None)
tr._loop_writing.assert_called_with(data=b'data')
def test_write_no_data(self):
- tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
+ tr = self.socket_transport()
tr.write(b'')
self.assertFalse(tr._buffer)
def test_write_more(self):
- tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
+ tr = self.socket_transport()
tr._write_fut = mock.Mock()
tr._loop_writing = mock.Mock()
tr.write(b'data')
@@ -132,7 +145,7 @@ class ProactorSocketTransportTests(test_utils.TestCase):
self.assertFalse(tr._loop_writing.called)
def test_loop_writing(self):
- tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
+ tr = self.socket_transport()
tr._buffer = bytearray(b'data')
tr._loop_writing()
self.loop._proactor.send.assert_called_with(self.sock, b'data')
@@ -142,7 +155,7 @@ class ProactorSocketTransportTests(test_utils.TestCase):
@mock.patch('asyncio.proactor_events.logger')
def test_loop_writing_err(self, m_log):
err = self.loop._proactor.send.side_effect = OSError()
- tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
+ tr = self.socket_transport()
tr._fatal_error = mock.Mock()
tr._buffer = [b'da', b'ta']
tr._loop_writing()
@@ -163,7 +176,7 @@ class ProactorSocketTransportTests(test_utils.TestCase):
fut = asyncio.Future(loop=self.loop)
fut.set_result(b'data')
- tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
+ tr = self.socket_transport()
tr._write_fut = fut
tr._loop_writing(fut)
self.assertIsNone(tr._write_fut)
@@ -172,7 +185,7 @@ class ProactorSocketTransportTests(test_utils.TestCase):
fut = asyncio.Future(loop=self.loop)
fut.set_result(1)
- tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
+ tr = self.socket_transport()
tr._write_fut = fut
tr.close()
tr._loop_writing(fut)
@@ -181,13 +194,13 @@ class ProactorSocketTransportTests(test_utils.TestCase):
self.protocol.connection_lost.assert_called_with(None)
def test_abort(self):
- tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
+ tr = self.socket_transport()
tr._force_close = mock.Mock()
tr.abort()
tr._force_close.assert_called_with(None)
def test_close(self):
- tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
+ tr = self.socket_transport()
tr.close()
test_utils.run_briefly(self.loop)
self.protocol.connection_lost.assert_called_with(None)
@@ -200,14 +213,14 @@ class ProactorSocketTransportTests(test_utils.TestCase):
self.assertFalse(self.protocol.connection_lost.called)
def test_close_write_fut(self):
- tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
+ tr = self.socket_transport()
tr._write_fut = mock.Mock()
tr.close()
test_utils.run_briefly(self.loop)
self.assertFalse(self.protocol.connection_lost.called)
def test_close_buffer(self):
- tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
+ tr = self.socket_transport()
tr._buffer = [b'data']
tr.close()
test_utils.run_briefly(self.loop)
@@ -215,14 +228,14 @@ class ProactorSocketTransportTests(test_utils.TestCase):
@mock.patch('asyncio.base_events.logger')
def test_fatal_error(self, m_logging):
- tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
+ tr = self.socket_transport()
tr._force_close = mock.Mock()
tr._fatal_error(None)
self.assertTrue(tr._force_close.called)
self.assertTrue(m_logging.error.called)
def test_force_close(self):
- tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
+ tr = self.socket_transport()
tr._buffer = [b'data']
read_fut = tr._read_fut = mock.Mock()
write_fut = tr._write_fut = mock.Mock()
@@ -236,14 +249,14 @@ class ProactorSocketTransportTests(test_utils.TestCase):
self.assertEqual(tr._conn_lost, 1)
def test_force_close_idempotent(self):
- tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
+ tr = self.socket_transport()
tr._closing = True
tr._force_close(None)
test_utils.run_briefly(self.loop)
self.assertFalse(self.protocol.connection_lost.called)
def test_fatal_error_2(self):
- tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
+ tr = self.socket_transport()
tr._buffer = [b'data']
tr._force_close(None)
@@ -252,14 +265,13 @@ class ProactorSocketTransportTests(test_utils.TestCase):
self.assertEqual(None, tr._buffer)
def test_call_connection_lost(self):
- tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
+ tr = self.socket_transport()
tr._call_connection_lost(None)
self.assertTrue(self.protocol.connection_lost.called)
self.assertTrue(self.sock.close.called)
def test_write_eof(self):
- tr = _ProactorSocketTransport(
- self.loop, self.sock, self.protocol)
+ tr = self.socket_transport()
self.assertTrue(tr.can_write_eof())
tr.write_eof()
self.sock.shutdown.assert_called_with(socket.SHUT_WR)
@@ -268,7 +280,7 @@ class ProactorSocketTransportTests(test_utils.TestCase):
tr.close()
def test_write_eof_buffer(self):
- tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
+ tr = self.socket_transport()
f = asyncio.Future(loop=self.loop)
tr._loop._proactor.send.return_value = f
tr.write(b'data')
@@ -312,11 +324,10 @@ class ProactorSocketTransportTests(test_utils.TestCase):
self.assertFalse(tr.can_write_eof())
with self.assertRaises(NotImplementedError):
tr.write_eof()
- tr.close()
+ close_transport(tr)
def test_pause_resume_reading(self):
- tr = _ProactorSocketTransport(
- self.loop, self.sock, self.protocol)
+ tr = self.socket_transport()
futures = []
for msg in [b'data1', b'data2', b'data3', b'data4', b'']:
f = asyncio.Future(loop=self.loop)
@@ -344,10 +355,7 @@ class ProactorSocketTransportTests(test_utils.TestCase):
def pause_writing_transport(self, high):
- tr = _ProactorSocketTransport(
- self.loop, self.sock, self.protocol)
- self.addCleanup(tr.close)
-
+ tr = self.socket_transport()
tr.set_write_buffer_limits(high=high)
self.assertEqual(tr.get_write_buffer_size(), 0)
@@ -438,7 +446,7 @@ class BaseProactorEventLoopTests(test_utils.TestCase):
return (self.ssock, self.csock)
self.loop = EventLoop(self.proactor)
- self.set_event_loop(self.loop, cleanup=False)
+ self.set_event_loop(self.loop)
@mock.patch.object(BaseProactorEventLoop, 'call_soon')
@mock.patch.object(BaseProactorEventLoop, '_socketpair')
@@ -450,6 +458,7 @@ class BaseProactorEventLoopTests(test_utils.TestCase):
self.assertIs(loop._csock, csock)
self.assertEqual(loop._internal_fds, 1)
call_soon.assert_called_with(loop._loop_self_reading)
+ loop.close()
def test_close_self_pipe(self):
self.loop._close_self_pipe()
@@ -459,6 +468,9 @@ class BaseProactorEventLoopTests(test_utils.TestCase):
self.assertIsNone(self.loop._ssock)
self.assertIsNone(self.loop._csock)
+ # Don't call close(): _close_self_pipe() cannot be called twice
+ self.loop._closed = True
+
def test_close(self):
self.loop._close_self_pipe = mock.Mock()
self.loop.close()
@@ -493,6 +505,7 @@ class BaseProactorEventLoopTests(test_utils.TestCase):
def test_make_socket_transport(self):
tr = self.loop._make_socket_transport(self.sock, asyncio.Protocol())
self.assertIsInstance(tr, _ProactorSocketTransport)
+ close_transport(tr)
def test_loop_self_reading(self):
self.loop._loop_self_reading()
diff --git a/Lib/test/test_asyncio/test_selector_events.py b/Lib/test/test_asyncio/test_selector_events.py
index 3e8392e..f99d04d 100644
--- a/Lib/test/test_asyncio/test_selector_events.py
+++ b/Lib/test/test_asyncio/test_selector_events.py
@@ -24,6 +24,11 @@ MOCK_ANY = mock.ANY
class TestBaseSelectorEventLoop(BaseSelectorEventLoop):
+ def close(self):
+ # Don't call the close() method of the parent class, because the
+ # selector is mocked
+ self._closed = True
+
def _make_self_pipe(self):
self._ssock = mock.Mock()
self._csock = mock.Mock()
@@ -34,19 +39,29 @@ def list_to_buffer(l=()):
return bytearray().join(l)
+def close_transport(transport):
+ # Don't call transport.close() because the event loop and the selector
+ # are mocked
+ if transport._sock is None:
+ return
+ transport._sock.close()
+ transport._sock = None
+
+
class BaseSelectorEventLoopTests(test_utils.TestCase):
def setUp(self):
self.selector = mock.Mock()
self.selector.select.return_value = []
self.loop = TestBaseSelectorEventLoop(self.selector)
- self.set_event_loop(self.loop, cleanup=False)
+ self.set_event_loop(self.loop)
def test_make_socket_transport(self):
m = mock.Mock()
self.loop.add_reader = mock.Mock()
transport = self.loop._make_socket_transport(m, asyncio.Protocol())
self.assertIsInstance(transport, _SelectorSocketTransport)
+ close_transport(transport)
@unittest.skipIf(ssl is None, 'No ssl module')
def test_make_ssl_transport(self):
@@ -59,11 +74,19 @@ class BaseSelectorEventLoopTests(test_utils.TestCase):
with test_utils.disable_logger():
transport = self.loop._make_ssl_transport(
m, asyncio.Protocol(), m, waiter)
+ # execute the handshake while the logger is disabled
+ # to ignore SSL handshake failure
+ test_utils.run_briefly(self.loop)
+
# Sanity check
class_name = transport.__class__.__name__
self.assertIn("ssl", class_name.lower())
self.assertIn("transport", class_name.lower())
+ transport.close()
+ # execute pending callbacks to close the socket transport
+ test_utils.run_briefly(self.loop)
+
@mock.patch('asyncio.selector_events.ssl', None)
@mock.patch('asyncio.sslproto.ssl', None)
def test_make_ssl_transport_without_ssl_error(self):
@@ -76,6 +99,15 @@ class BaseSelectorEventLoopTests(test_utils.TestCase):
self.loop._make_ssl_transport(m, m, m, m)
def test_close(self):
+ class EventLoop(BaseSelectorEventLoop):
+ def _make_self_pipe(self):
+ self._ssock = mock.Mock()
+ self._csock = mock.Mock()
+ self._internal_fds += 1
+
+ self.loop = EventLoop(self.selector)
+ self.set_event_loop(self.loop)
+
ssock = self.loop._ssock
ssock.fileno.return_value = 7
csock = self.loop._csock
@@ -636,21 +668,27 @@ class SelectorTransportTests(test_utils.TestCase):
self.sock = mock.Mock(socket.socket)
self.sock.fileno.return_value = 7
+ def create_transport(self):
+ transport = _SelectorTransport(self.loop, self.sock, self.protocol,
+ None)
+ self.addCleanup(close_transport, transport)
+ return transport
+
def test_ctor(self):
- tr = _SelectorTransport(self.loop, self.sock, self.protocol, None)
+ tr = self.create_transport()
self.assertIs(tr._loop, self.loop)
self.assertIs(tr._sock, self.sock)
self.assertIs(tr._sock_fd, 7)
def test_abort(self):
- tr = _SelectorTransport(self.loop, self.sock, self.protocol, None)
+ tr = self.create_transport()
tr._force_close = mock.Mock()
tr.abort()
tr._force_close.assert_called_with(None)
def test_close(self):
- tr = _SelectorTransport(self.loop, self.sock, self.protocol, None)
+ tr = self.create_transport()
tr.close()
self.assertTrue(tr._closing)
@@ -663,7 +701,7 @@ class SelectorTransportTests(test_utils.TestCase):
self.assertEqual(1, self.loop.remove_reader_count[7])
def test_close_write_buffer(self):
- tr = _SelectorTransport(self.loop, self.sock, self.protocol, None)
+ tr = self.create_transport()
tr._buffer.extend(b'data')
tr.close()
@@ -672,7 +710,7 @@ class SelectorTransportTests(test_utils.TestCase):
self.assertFalse(self.protocol.connection_lost.called)
def test_force_close(self):
- tr = _SelectorTransport(self.loop, self.sock, self.protocol, None)
+ tr = self.create_transport()
tr._buffer.extend(b'1')
self.loop.add_reader(7, mock.sentinel)
self.loop.add_writer(7, mock.sentinel)
@@ -691,7 +729,7 @@ class SelectorTransportTests(test_utils.TestCase):
@mock.patch('asyncio.log.logger.error')
def test_fatal_error(self, m_exc):
exc = OSError()
- tr = _SelectorTransport(self.loop, self.sock, self.protocol, None)
+ tr = self.create_transport()
tr._force_close = mock.Mock()
tr._fatal_error(exc)
@@ -704,7 +742,7 @@ class SelectorTransportTests(test_utils.TestCase):
def test_connection_lost(self):
exc = OSError()
- tr = _SelectorTransport(self.loop, self.sock, self.protocol, None)
+ tr = self.create_transport()
self.assertIsNotNone(tr._protocol)
self.assertIsNotNone(tr._loop)
tr._call_connection_lost(exc)
@@ -725,9 +763,14 @@ class SelectorSocketTransportTests(test_utils.TestCase):
self.sock = mock.Mock(socket.socket)
self.sock_fd = self.sock.fileno.return_value = 7
+ def socket_transport(self, waiter=None):
+ transport = _SelectorSocketTransport(self.loop, self.sock,
+ self.protocol, waiter=waiter)
+ self.addCleanup(close_transport, transport)
+ return transport
+
def test_ctor(self):
- tr = _SelectorSocketTransport(
- self.loop, self.sock, self.protocol)
+ tr = self.socket_transport()
self.loop.assert_reader(7, tr._read_ready)
test_utils.run_briefly(self.loop)
self.protocol.connection_made.assert_called_with(tr)
@@ -735,14 +778,12 @@ class SelectorSocketTransportTests(test_utils.TestCase):
def test_ctor_with_waiter(self):
fut = asyncio.Future(loop=self.loop)
- _SelectorSocketTransport(
- self.loop, self.sock, self.protocol, fut)
+ self.socket_transport(waiter=fut)
test_utils.run_briefly(self.loop)
self.assertIsNone(fut.result())
def test_pause_resume_reading(self):
- tr = _SelectorSocketTransport(
- self.loop, self.sock, self.protocol)
+ tr = self.socket_transport()
self.assertFalse(tr._paused)
self.loop.assert_reader(7, tr._read_ready)
tr.pause_reading()
@@ -755,8 +796,7 @@ class SelectorSocketTransportTests(test_utils.TestCase):
tr.resume_reading()
def test_read_ready(self):
- transport = _SelectorSocketTransport(
- self.loop, self.sock, self.protocol)
+ transport = self.socket_transport()
self.sock.recv.return_value = b'data'
transport._read_ready()
@@ -764,8 +804,7 @@ class SelectorSocketTransportTests(test_utils.TestCase):
self.protocol.data_received.assert_called_with(b'data')
def test_read_ready_eof(self):
- transport = _SelectorSocketTransport(
- self.loop, self.sock, self.protocol)
+ transport = self.socket_transport()
transport.close = mock.Mock()
self.sock.recv.return_value = b''
@@ -775,8 +814,7 @@ class SelectorSocketTransportTests(test_utils.TestCase):
transport.close.assert_called_with()
def test_read_ready_eof_keep_open(self):
- transport = _SelectorSocketTransport(
- self.loop, self.sock, self.protocol)
+ transport = self.socket_transport()
transport.close = mock.Mock()
self.sock.recv.return_value = b''
@@ -790,8 +828,7 @@ class SelectorSocketTransportTests(test_utils.TestCase):
def test_read_ready_tryagain(self, m_exc):
self.sock.recv.side_effect = BlockingIOError
- transport = _SelectorSocketTransport(
- self.loop, self.sock, self.protocol)
+ transport = self.socket_transport()
transport._fatal_error = mock.Mock()
transport._read_ready()
@@ -801,8 +838,7 @@ class SelectorSocketTransportTests(test_utils.TestCase):
def test_read_ready_tryagain_interrupted(self, m_exc):
self.sock.recv.side_effect = InterruptedError
- transport = _SelectorSocketTransport(
- self.loop, self.sock, self.protocol)
+ transport = self.socket_transport()
transport._fatal_error = mock.Mock()
transport._read_ready()
@@ -812,8 +848,7 @@ class SelectorSocketTransportTests(test_utils.TestCase):
def test_read_ready_conn_reset(self, m_exc):
err = self.sock.recv.side_effect = ConnectionResetError()
- transport = _SelectorSocketTransport(
- self.loop, self.sock, self.protocol)
+ transport = self.socket_transport()
transport._force_close = mock.Mock()
with test_utils.disable_logger():
transport._read_ready()
@@ -823,8 +858,7 @@ class SelectorSocketTransportTests(test_utils.TestCase):
def test_read_ready_err(self, m_exc):
err = self.sock.recv.side_effect = OSError()
- transport = _SelectorSocketTransport(
- self.loop, self.sock, self.protocol)
+ transport = self.socket_transport()
transport._fatal_error = mock.Mock()
transport._read_ready()
@@ -836,8 +870,7 @@ class SelectorSocketTransportTests(test_utils.TestCase):
data = b'data'
self.sock.send.return_value = len(data)
- transport = _SelectorSocketTransport(
- self.loop, self.sock, self.protocol)
+ transport = self.socket_transport()
transport.write(data)
self.sock.send.assert_called_with(data)
@@ -845,8 +878,7 @@ class SelectorSocketTransportTests(test_utils.TestCase):
data = bytearray(b'data')
self.sock.send.return_value = len(data)
- transport = _SelectorSocketTransport(
- self.loop, self.sock, self.protocol)
+ transport = self.socket_transport()
transport.write(data)
self.sock.send.assert_called_with(data)
self.assertEqual(data, bytearray(b'data')) # Hasn't been mutated.
@@ -855,22 +887,19 @@ class SelectorSocketTransportTests(test_utils.TestCase):
data = memoryview(b'data')
self.sock.send.return_value = len(data)
- transport = _SelectorSocketTransport(
- self.loop, self.sock, self.protocol)
+ transport = self.socket_transport()
transport.write(data)
self.sock.send.assert_called_with(data)
def test_write_no_data(self):
- transport = _SelectorSocketTransport(
- self.loop, self.sock, self.protocol)
+ transport = self.socket_transport()
transport._buffer.extend(b'data')
transport.write(b'')
self.assertFalse(self.sock.send.called)
self.assertEqual(list_to_buffer([b'data']), transport._buffer)
def test_write_buffer(self):
- transport = _SelectorSocketTransport(
- self.loop, self.sock, self.protocol)
+ transport = self.socket_transport()
transport._buffer.extend(b'data1')
transport.write(b'data2')
self.assertFalse(self.sock.send.called)
@@ -881,8 +910,7 @@ class SelectorSocketTransportTests(test_utils.TestCase):
data = b'data'
self.sock.send.return_value = 2
- transport = _SelectorSocketTransport(
- self.loop, self.sock, self.protocol)
+ transport = self.socket_transport()
transport.write(data)
self.loop.assert_writer(7, transport._write_ready)
@@ -892,8 +920,7 @@ class SelectorSocketTransportTests(test_utils.TestCase):
data = bytearray(b'data')
self.sock.send.return_value = 2
- transport = _SelectorSocketTransport(
- self.loop, self.sock, self.protocol)
+ transport = self.socket_transport()
transport.write(data)
self.loop.assert_writer(7, transport._write_ready)
@@ -904,8 +931,7 @@ class SelectorSocketTransportTests(test_utils.TestCase):
data = memoryview(b'data')
self.sock.send.return_value = 2
- transport = _SelectorSocketTransport(
- self.loop, self.sock, self.protocol)
+ transport = self.socket_transport()
transport.write(data)
self.loop.assert_writer(7, transport._write_ready)
@@ -916,8 +942,7 @@ class SelectorSocketTransportTests(test_utils.TestCase):
self.sock.send.return_value = 0
self.sock.fileno.return_value = 7
- transport = _SelectorSocketTransport(
- self.loop, self.sock, self.protocol)
+ transport = self.socket_transport()
transport.write(data)
self.loop.assert_writer(7, transport._write_ready)
@@ -927,8 +952,7 @@ class SelectorSocketTransportTests(test_utils.TestCase):
self.sock.send.side_effect = BlockingIOError
data = b'data'
- transport = _SelectorSocketTransport(
- self.loop, self.sock, self.protocol)
+ transport = self.socket_transport()
transport.write(data)
self.loop.assert_writer(7, transport._write_ready)
@@ -939,8 +963,7 @@ class SelectorSocketTransportTests(test_utils.TestCase):
err = self.sock.send.side_effect = OSError()
data = b'data'
- transport = _SelectorSocketTransport(
- self.loop, self.sock, self.protocol)
+ transport = self.socket_transport()
transport._fatal_error = mock.Mock()
transport.write(data)
transport._fatal_error.assert_called_with(
@@ -959,13 +982,11 @@ class SelectorSocketTransportTests(test_utils.TestCase):
m_log.warning.assert_called_with('socket.send() raised exception.')
def test_write_str(self):
- transport = _SelectorSocketTransport(
- self.loop, self.sock, self.protocol)
+ transport = self.socket_transport()
self.assertRaises(TypeError, transport.write, 'str')
def test_write_closing(self):
- transport = _SelectorSocketTransport(
- self.loop, self.sock, self.protocol)
+ transport = self.socket_transport()
transport.close()
self.assertEqual(transport._conn_lost, 1)
transport.write(b'data')
@@ -975,8 +996,7 @@ class SelectorSocketTransportTests(test_utils.TestCase):
data = b'data'
self.sock.send.return_value = len(data)
- transport = _SelectorSocketTransport(
- self.loop, self.sock, self.protocol)
+ transport = self.socket_transport()
transport._buffer.extend(data)
self.loop.add_writer(7, transport._write_ready)
transport._write_ready()
@@ -987,8 +1007,7 @@ class SelectorSocketTransportTests(test_utils.TestCase):
data = b'data'
self.sock.send.return_value = len(data)
- transport = _SelectorSocketTransport(
- self.loop, self.sock, self.protocol)
+ transport = self.socket_transport()
transport._closing = True
transport._buffer.extend(data)
self.loop.add_writer(7, transport._write_ready)
@@ -999,8 +1018,7 @@ class SelectorSocketTransportTests(test_utils.TestCase):
self.protocol.connection_lost.assert_called_with(None)
def test_write_ready_no_data(self):
- transport = _SelectorSocketTransport(
- self.loop, self.sock, self.protocol)
+ transport = self.socket_transport()
# This is an internal error.
self.assertRaises(AssertionError, transport._write_ready)
@@ -1008,8 +1026,7 @@ class SelectorSocketTransportTests(test_utils.TestCase):
data = b'data'
self.sock.send.return_value = 2
- transport = _SelectorSocketTransport(
- self.loop, self.sock, self.protocol)
+ transport = self.socket_transport()
transport._buffer.extend(data)
self.loop.add_writer(7, transport._write_ready)
transport._write_ready()
@@ -1020,8 +1037,7 @@ class SelectorSocketTransportTests(test_utils.TestCase):
data = b'data'
self.sock.send.return_value = 0
- transport = _SelectorSocketTransport(
- self.loop, self.sock, self.protocol)
+ transport = self.socket_transport()
transport._buffer.extend(data)
self.loop.add_writer(7, transport._write_ready)
transport._write_ready()
@@ -1031,8 +1047,7 @@ class SelectorSocketTransportTests(test_utils.TestCase):
def test_write_ready_tryagain(self):
self.sock.send.side_effect = BlockingIOError
- transport = _SelectorSocketTransport(
- self.loop, self.sock, self.protocol)
+ transport = self.socket_transport()
transport._buffer = list_to_buffer([b'data1', b'data2'])
self.loop.add_writer(7, transport._write_ready)
transport._write_ready()
@@ -1043,8 +1058,7 @@ class SelectorSocketTransportTests(test_utils.TestCase):
def test_write_ready_exception(self):
err = self.sock.send.side_effect = OSError()
- transport = _SelectorSocketTransport(
- self.loop, self.sock, self.protocol)
+ transport = self.socket_transport()
transport._fatal_error = mock.Mock()
transport._buffer.extend(b'data')
transport._write_ready()
@@ -1057,16 +1071,14 @@ class SelectorSocketTransportTests(test_utils.TestCase):
self.sock.send.side_effect = OSError()
remove_writer = self.loop.remove_writer = mock.Mock()
- transport = _SelectorSocketTransport(
- self.loop, self.sock, self.protocol)
+ transport = self.socket_transport()
transport.close()
transport._buffer.extend(b'data')
transport._write_ready()
remove_writer.assert_called_with(self.sock_fd)
def test_write_eof(self):
- tr = _SelectorSocketTransport(
- self.loop, self.sock, self.protocol)
+ tr = self.socket_transport()
self.assertTrue(tr.can_write_eof())
tr.write_eof()
self.sock.shutdown.assert_called_with(socket.SHUT_WR)
@@ -1075,8 +1087,7 @@ class SelectorSocketTransportTests(test_utils.TestCase):
tr.close()
def test_write_eof_buffer(self):
- tr = _SelectorSocketTransport(
- self.loop, self.sock, self.protocol)
+ tr = self.socket_transport()
self.sock.send.side_effect = BlockingIOError
tr.write(b'data')
tr.write_eof()
@@ -1103,9 +1114,15 @@ class SelectorSslTransportTests(test_utils.TestCase):
self.sslcontext = mock.Mock()
self.sslcontext.wrap_socket.return_value = self.sslsock
+ def ssl_transport(self, waiter=None, server_hostname=None):
+ transport = _SelectorSslTransport(self.loop, self.sock, self.protocol,
+ self.sslcontext, waiter=waiter,
+ server_hostname=server_hostname)
+ self.addCleanup(close_transport, transport)
+ return transport
+
def _make_one(self, create_waiter=None):
- transport = _SelectorSslTransport(
- self.loop, self.sock, self.protocol, self.sslcontext)
+ transport = self.ssl_transport()
self.sock.reset_mock()
self.sslsock.reset_mock()
self.sslcontext.reset_mock()
@@ -1114,9 +1131,7 @@ class SelectorSslTransportTests(test_utils.TestCase):
def test_on_handshake(self):
waiter = asyncio.Future(loop=self.loop)
- tr = _SelectorSslTransport(
- self.loop, self.sock, self.protocol, self.sslcontext,
- waiter=waiter)
+ tr = self.ssl_transport(waiter=waiter)
self.assertTrue(self.sslsock.do_handshake.called)
self.loop.assert_reader(1, tr._read_ready)
test_utils.run_briefly(self.loop)
@@ -1125,15 +1140,13 @@ class SelectorSslTransportTests(test_utils.TestCase):
def test_on_handshake_reader_retry(self):
self.loop.set_debug(False)
self.sslsock.do_handshake.side_effect = ssl.SSLWantReadError
- transport = _SelectorSslTransport(
- self.loop, self.sock, self.protocol, self.sslcontext)
+ transport = self.ssl_transport()
self.loop.assert_reader(1, transport._on_handshake, None)
def test_on_handshake_writer_retry(self):
self.loop.set_debug(False)
self.sslsock.do_handshake.side_effect = ssl.SSLWantWriteError
- transport = _SelectorSslTransport(
- self.loop, self.sock, self.protocol, self.sslcontext)
+ transport = self.ssl_transport()
self.loop.assert_writer(1, transport._on_handshake, None)
def test_on_handshake_exc(self):
@@ -1141,16 +1154,14 @@ class SelectorSslTransportTests(test_utils.TestCase):
self.sslsock.do_handshake.side_effect = exc
with test_utils.disable_logger():
waiter = asyncio.Future(loop=self.loop)
- transport = _SelectorSslTransport(
- self.loop, self.sock, self.protocol, self.sslcontext, waiter)
+ transport = self.ssl_transport(waiter=waiter)
self.assertTrue(waiter.done())
self.assertIs(exc, waiter.exception())
self.assertTrue(self.sslsock.close.called)
def test_on_handshake_base_exc(self):
waiter = asyncio.Future(loop=self.loop)
- transport = _SelectorSslTransport(
- self.loop, self.sock, self.protocol, self.sslcontext, waiter)
+ transport = self.ssl_transport(waiter=waiter)
exc = BaseException()
self.sslsock.do_handshake.side_effect = exc
with test_utils.disable_logger():
@@ -1163,8 +1174,7 @@ class SelectorSslTransportTests(test_utils.TestCase):
# Python issue #23197: cancelling an handshake must not raise an
# exception or log an error, even if the handshake failed
waiter = asyncio.Future(loop=self.loop)
- transport = _SelectorSslTransport(
- self.loop, self.sock, self.protocol, self.sslcontext, waiter)
+ transport = self.ssl_transport(waiter=waiter)
waiter.cancel()
exc = ValueError()
self.sslsock.do_handshake.side_effect = exc
@@ -1423,9 +1433,7 @@ class SelectorSslTransportTests(test_utils.TestCase):
@unittest.skipIf(ssl is None, 'No SSL support')
def test_server_hostname(self):
- _SelectorSslTransport(
- self.loop, self.sock, self.protocol, self.sslcontext,
- server_hostname='localhost')
+ self.ssl_transport(server_hostname='localhost')
self.sslcontext.wrap_socket.assert_called_with(
self.sock, do_handshake_on_connect=False, server_side=False,
server_hostname='localhost')
@@ -1448,9 +1456,15 @@ class SelectorDatagramTransportTests(test_utils.TestCase):
self.sock = mock.Mock(spec_set=socket.socket)
self.sock.fileno.return_value = 7
+ def datagram_transport(self, address=None):
+ transport = _SelectorDatagramTransport(self.loop, self.sock,
+ self.protocol,
+ address=address)
+ self.addCleanup(close_transport, transport)
+ return transport
+
def test_read_ready(self):
- transport = _SelectorDatagramTransport(
- self.loop, self.sock, self.protocol)
+ transport = self.datagram_transport()
self.sock.recvfrom.return_value = (b'data', ('0.0.0.0', 1234))
transport._read_ready()
@@ -1459,8 +1473,7 @@ class SelectorDatagramTransportTests(test_utils.TestCase):
b'data', ('0.0.0.0', 1234))
def test_read_ready_tryagain(self):
- transport = _SelectorDatagramTransport(
- self.loop, self.sock, self.protocol)
+ transport = self.datagram_transport()
self.sock.recvfrom.side_effect = BlockingIOError
transport._fatal_error = mock.Mock()
@@ -1469,8 +1482,7 @@ class SelectorDatagramTransportTests(test_utils.TestCase):
self.assertFalse(transport._fatal_error.called)
def test_read_ready_err(self):
- transport = _SelectorDatagramTransport(
- self.loop, self.sock, self.protocol)
+ transport = self.datagram_transport()
err = self.sock.recvfrom.side_effect = RuntimeError()
transport._fatal_error = mock.Mock()
@@ -1481,8 +1493,7 @@ class SelectorDatagramTransportTests(test_utils.TestCase):
'Fatal read error on datagram transport')
def test_read_ready_oserr(self):
- transport = _SelectorDatagramTransport(
- self.loop, self.sock, self.protocol)
+ transport = self.datagram_transport()
err = self.sock.recvfrom.side_effect = OSError()
transport._fatal_error = mock.Mock()
@@ -1493,8 +1504,7 @@ class SelectorDatagramTransportTests(test_utils.TestCase):
def test_sendto(self):
data = b'data'
- transport = _SelectorDatagramTransport(
- self.loop, self.sock, self.protocol)
+ transport = self.datagram_transport()
transport.sendto(data, ('0.0.0.0', 1234))
self.assertTrue(self.sock.sendto.called)
self.assertEqual(
@@ -1502,8 +1512,7 @@ class SelectorDatagramTransportTests(test_utils.TestCase):
def test_sendto_bytearray(self):
data = bytearray(b'data')
- transport = _SelectorDatagramTransport(
- self.loop, self.sock, self.protocol)
+ transport = self.datagram_transport()
transport.sendto(data, ('0.0.0.0', 1234))
self.assertTrue(self.sock.sendto.called)
self.assertEqual(
@@ -1511,16 +1520,14 @@ class SelectorDatagramTransportTests(test_utils.TestCase):
def test_sendto_memoryview(self):
data = memoryview(b'data')
- transport = _SelectorDatagramTransport(
- self.loop, self.sock, self.protocol)
+ transport = self.datagram_transport()
transport.sendto(data, ('0.0.0.0', 1234))
self.assertTrue(self.sock.sendto.called)
self.assertEqual(
self.sock.sendto.call_args[0], (data, ('0.0.0.0', 1234)))
def test_sendto_no_data(self):
- transport = _SelectorDatagramTransport(
- self.loop, self.sock, self.protocol)
+ transport = self.datagram_transport()
transport._buffer.append((b'data', ('0.0.0.0', 12345)))
transport.sendto(b'', ())
self.assertFalse(self.sock.sendto.called)
@@ -1528,8 +1535,7 @@ class SelectorDatagramTransportTests(test_utils.TestCase):
[(b'data', ('0.0.0.0', 12345))], list(transport._buffer))
def test_sendto_buffer(self):
- transport = _SelectorDatagramTransport(
- self.loop, self.sock, self.protocol)
+ transport = self.datagram_transport()
transport._buffer.append((b'data1', ('0.0.0.0', 12345)))
transport.sendto(b'data2', ('0.0.0.0', 12345))
self.assertFalse(self.sock.sendto.called)
@@ -1540,8 +1546,7 @@ class SelectorDatagramTransportTests(test_utils.TestCase):
def test_sendto_buffer_bytearray(self):
data2 = bytearray(b'data2')
- transport = _SelectorDatagramTransport(
- self.loop, self.sock, self.protocol)
+ transport = self.datagram_transport()
transport._buffer.append((b'data1', ('0.0.0.0', 12345)))
transport.sendto(data2, ('0.0.0.0', 12345))
self.assertFalse(self.sock.sendto.called)
@@ -1553,8 +1558,7 @@ class SelectorDatagramTransportTests(test_utils.TestCase):
def test_sendto_buffer_memoryview(self):
data2 = memoryview(b'data2')
- transport = _SelectorDatagramTransport(
- self.loop, self.sock, self.protocol)
+ transport = self.datagram_transport()
transport._buffer.append((b'data1', ('0.0.0.0', 12345)))
transport.sendto(data2, ('0.0.0.0', 12345))
self.assertFalse(self.sock.sendto.called)
@@ -1569,8 +1573,7 @@ class SelectorDatagramTransportTests(test_utils.TestCase):
self.sock.sendto.side_effect = BlockingIOError
- transport = _SelectorDatagramTransport(
- self.loop, self.sock, self.protocol)
+ transport = self.datagram_transport()
transport.sendto(data, ('0.0.0.0', 12345))
self.loop.assert_writer(7, transport._sendto_ready)
@@ -1582,8 +1585,7 @@ class SelectorDatagramTransportTests(test_utils.TestCase):
data = b'data'
err = self.sock.sendto.side_effect = RuntimeError()
- transport = _SelectorDatagramTransport(
- self.loop, self.sock, self.protocol)
+ transport = self.datagram_transport()
transport._fatal_error = mock.Mock()
transport.sendto(data, ())
@@ -1606,8 +1608,7 @@ class SelectorDatagramTransportTests(test_utils.TestCase):
self.sock.sendto.side_effect = ConnectionRefusedError
- transport = _SelectorDatagramTransport(
- self.loop, self.sock, self.protocol)
+ transport = self.datagram_transport()
transport._fatal_error = mock.Mock()
transport.sendto(data, ())
@@ -1619,8 +1620,7 @@ class SelectorDatagramTransportTests(test_utils.TestCase):
self.sock.send.side_effect = ConnectionRefusedError
- transport = _SelectorDatagramTransport(
- self.loop, self.sock, self.protocol, ('0.0.0.0', 1))
+ transport = self.datagram_transport(address=('0.0.0.0', 1))
transport._fatal_error = mock.Mock()
transport.sendto(data)
@@ -1628,19 +1628,16 @@ class SelectorDatagramTransportTests(test_utils.TestCase):
self.assertTrue(self.protocol.error_received.called)
def test_sendto_str(self):
- transport = _SelectorDatagramTransport(
- self.loop, self.sock, self.protocol)
+ transport = self.datagram_transport()
self.assertRaises(TypeError, transport.sendto, 'str', ())
def test_sendto_connected_addr(self):
- transport = _SelectorDatagramTransport(
- self.loop, self.sock, self.protocol, ('0.0.0.0', 1))
+ transport = self.datagram_transport(address=('0.0.0.0', 1))
self.assertRaises(
ValueError, transport.sendto, b'str', ('0.0.0.0', 2))
def test_sendto_closing(self):
- transport = _SelectorDatagramTransport(
- self.loop, self.sock, self.protocol, address=(1,))
+ transport = self.datagram_transport(address=(1,))
transport.close()
self.assertEqual(transport._conn_lost, 1)
transport.sendto(b'data', (1,))
@@ -1650,8 +1647,7 @@ class SelectorDatagramTransportTests(test_utils.TestCase):
data = b'data'
self.sock.sendto.return_value = len(data)
- transport = _SelectorDatagramTransport(
- self.loop, self.sock, self.protocol)
+ transport = self.datagram_transport()
transport._buffer.append((data, ('0.0.0.0', 12345)))
self.loop.add_writer(7, transport._sendto_ready)
transport._sendto_ready()
@@ -1664,8 +1660,7 @@ class SelectorDatagramTransportTests(test_utils.TestCase):
data = b'data'
self.sock.send.return_value = len(data)
- transport = _SelectorDatagramTransport(
- self.loop, self.sock, self.protocol)
+ transport = self.datagram_transport()
transport._closing = True
transport._buffer.append((data, ()))
self.loop.add_writer(7, transport._sendto_ready)
@@ -1676,8 +1671,7 @@ class SelectorDatagramTransportTests(test_utils.TestCase):
self.protocol.connection_lost.assert_called_with(None)
def test_sendto_ready_no_data(self):
- transport = _SelectorDatagramTransport(
- self.loop, self.sock, self.protocol)
+ transport = self.datagram_transport()
self.loop.add_writer(7, transport._sendto_ready)
transport._sendto_ready()
self.assertFalse(self.sock.sendto.called)
@@ -1686,8 +1680,7 @@ class SelectorDatagramTransportTests(test_utils.TestCase):
def test_sendto_ready_tryagain(self):
self.sock.sendto.side_effect = BlockingIOError
- transport = _SelectorDatagramTransport(
- self.loop, self.sock, self.protocol)
+ transport = self.datagram_transport()
transport._buffer.extend([(b'data1', ()), (b'data2', ())])
self.loop.add_writer(7, transport._sendto_ready)
transport._sendto_ready()
@@ -1700,8 +1693,7 @@ class SelectorDatagramTransportTests(test_utils.TestCase):
def test_sendto_ready_exception(self):
err = self.sock.sendto.side_effect = RuntimeError()
- transport = _SelectorDatagramTransport(
- self.loop, self.sock, self.protocol)
+ transport = self.datagram_transport()
transport._fatal_error = mock.Mock()
transport._buffer.append((b'data', ()))
transport._sendto_ready()
@@ -1713,8 +1705,7 @@ class SelectorDatagramTransportTests(test_utils.TestCase):
def test_sendto_ready_error_received(self):
self.sock.sendto.side_effect = ConnectionRefusedError
- transport = _SelectorDatagramTransport(
- self.loop, self.sock, self.protocol)
+ transport = self.datagram_transport()
transport._fatal_error = mock.Mock()
transport._buffer.append((b'data', ()))
transport._sendto_ready()
@@ -1724,8 +1715,7 @@ class SelectorDatagramTransportTests(test_utils.TestCase):
def test_sendto_ready_error_received_connection(self):
self.sock.send.side_effect = ConnectionRefusedError
- transport = _SelectorDatagramTransport(
- self.loop, self.sock, self.protocol, ('0.0.0.0', 1))
+ transport = self.datagram_transport(address=('0.0.0.0', 1))
transport._fatal_error = mock.Mock()
transport._buffer.append((b'data', ()))
transport._sendto_ready()
@@ -1735,8 +1725,7 @@ class SelectorDatagramTransportTests(test_utils.TestCase):
@mock.patch('asyncio.base_events.logger.error')
def test_fatal_error_connected(self, m_exc):
- transport = _SelectorDatagramTransport(
- self.loop, self.sock, self.protocol, ('0.0.0.0', 1))
+ transport = self.datagram_transport(address=('0.0.0.0', 1))
err = ConnectionRefusedError()
transport._fatal_error(err)
self.assertFalse(self.protocol.error_received.called)
@@ -1744,7 +1733,6 @@ class SelectorDatagramTransportTests(test_utils.TestCase):
test_utils.MockPattern(
'Fatal error on transport\nprotocol:.*\ntransport:.*'),
exc_info=(ConnectionRefusedError, MOCK_ANY, MOCK_ANY))
- transport.close()
if __name__ == '__main__':
diff --git a/Lib/test/test_asyncio/test_unix_events.py b/Lib/test/test_asyncio/test_unix_events.py
index 5f4b024..126196d 100644
--- a/Lib/test/test_asyncio/test_unix_events.py
+++ b/Lib/test/test_asyncio/test_unix_events.py
@@ -26,6 +26,15 @@ from asyncio import unix_events
MOCK_ANY = mock.ANY
+def close_pipe_transport(transport):
+ # Don't call transport.close() because the event loop and the selector
+ # are mocked
+ if transport._pipe is None:
+ return
+ transport._pipe.close()
+ transport._pipe = None
+
+
@unittest.skipUnless(signal, 'Signals are not supported')
class SelectorEventLoopSignalTests(test_utils.TestCase):
@@ -333,24 +342,28 @@ class UnixReadPipeTransportTests(test_utils.TestCase):
m_fstat.return_value = st
self.addCleanup(fstat_patcher.stop)
+ def read_pipe_transport(self, waiter=None):
+ transport = unix_events._UnixReadPipeTransport(self.loop, self.pipe,
+ self.protocol,
+ waiter=waiter)
+ self.addCleanup(close_pipe_transport, transport)
+ return transport
+
def test_ctor(self):
- tr = unix_events._UnixReadPipeTransport(
- self.loop, self.pipe, self.protocol)
+ tr = self.read_pipe_transport()
self.loop.assert_reader(5, tr._read_ready)
test_utils.run_briefly(self.loop)
self.protocol.connection_made.assert_called_with(tr)
def test_ctor_with_waiter(self):
fut = asyncio.Future(loop=self.loop)
- unix_events._UnixReadPipeTransport(
- self.loop, self.pipe, self.protocol, fut)
+ tr = self.read_pipe_transport(waiter=fut)
test_utils.run_briefly(self.loop)
self.assertIsNone(fut.result())
@mock.patch('os.read')
def test__read_ready(self, m_read):
- tr = unix_events._UnixReadPipeTransport(
- self.loop, self.pipe, self.protocol)
+ tr = self.read_pipe_transport()
m_read.return_value = b'data'
tr._read_ready()
@@ -359,8 +372,7 @@ class UnixReadPipeTransportTests(test_utils.TestCase):
@mock.patch('os.read')
def test__read_ready_eof(self, m_read):
- tr = unix_events._UnixReadPipeTransport(
- self.loop, self.pipe, self.protocol)
+ tr = self.read_pipe_transport()
m_read.return_value = b''
tr._read_ready()
@@ -372,8 +384,7 @@ class UnixReadPipeTransportTests(test_utils.TestCase):
@mock.patch('os.read')
def test__read_ready_blocked(self, m_read):
- tr = unix_events._UnixReadPipeTransport(
- self.loop, self.pipe, self.protocol)
+ tr = self.read_pipe_transport()
m_read.side_effect = BlockingIOError
tr._read_ready()
@@ -384,8 +395,7 @@ class UnixReadPipeTransportTests(test_utils.TestCase):
@mock.patch('asyncio.log.logger.error')
@mock.patch('os.read')
def test__read_ready_error(self, m_read, m_logexc):
- tr = unix_events._UnixReadPipeTransport(
- self.loop, self.pipe, self.protocol)
+ tr = self.read_pipe_transport()
err = OSError()
m_read.side_effect = err
tr._close = mock.Mock()
@@ -401,9 +411,7 @@ class UnixReadPipeTransportTests(test_utils.TestCase):
@mock.patch('os.read')
def test_pause_reading(self, m_read):
- tr = unix_events._UnixReadPipeTransport(
- self.loop, self.pipe, self.protocol)
-
+ tr = self.read_pipe_transport()
m = mock.Mock()
self.loop.add_reader(5, m)
tr.pause_reading()
@@ -411,26 +419,20 @@ class UnixReadPipeTransportTests(test_utils.TestCase):
@mock.patch('os.read')
def test_resume_reading(self, m_read):
- tr = unix_events._UnixReadPipeTransport(
- self.loop, self.pipe, self.protocol)
-
+ tr = self.read_pipe_transport()
tr.resume_reading()
self.loop.assert_reader(5, tr._read_ready)
@mock.patch('os.read')
def test_close(self, m_read):
- tr = unix_events._UnixReadPipeTransport(
- self.loop, self.pipe, self.protocol)
-
+ tr = self.read_pipe_transport()
tr._close = mock.Mock()
tr.close()
tr._close.assert_called_with(None)
@mock.patch('os.read')
def test_close_already_closing(self, m_read):
- tr = unix_events._UnixReadPipeTransport(
- self.loop, self.pipe, self.protocol)
-
+ tr = self.read_pipe_transport()
tr._closing = True
tr._close = mock.Mock()
tr.close()
@@ -438,9 +440,7 @@ class UnixReadPipeTransportTests(test_utils.TestCase):
@mock.patch('os.read')
def test__close(self, m_read):
- tr = unix_events._UnixReadPipeTransport(
- self.loop, self.pipe, self.protocol)
-
+ tr = self.read_pipe_transport()
err = object()
tr._close(err)
self.assertTrue(tr._closing)
@@ -449,8 +449,7 @@ class UnixReadPipeTransportTests(test_utils.TestCase):
self.protocol.connection_lost.assert_called_with(err)
def test__call_connection_lost(self):
- tr = unix_events._UnixReadPipeTransport(
- self.loop, self.pipe, self.protocol)
+ tr = self.read_pipe_transport()
self.assertIsNotNone(tr._protocol)
self.assertIsNotNone(tr._loop)
@@ -463,8 +462,7 @@ class UnixReadPipeTransportTests(test_utils.TestCase):
self.assertIsNone(tr._loop)
def test__call_connection_lost_with_err(self):
- tr = unix_events._UnixReadPipeTransport(
- self.loop, self.pipe, self.protocol)
+ tr = self.read_pipe_transport()
self.assertIsNotNone(tr._protocol)
self.assertIsNotNone(tr._loop)
@@ -496,31 +494,33 @@ class UnixWritePipeTransportTests(test_utils.TestCase):
m_fstat.return_value = st
self.addCleanup(fstat_patcher.stop)
+ def write_pipe_transport(self, waiter=None):
+ transport = unix_events._UnixWritePipeTransport(self.loop, self.pipe,
+ self.protocol,
+ waiter=waiter)
+ self.addCleanup(close_pipe_transport, transport)
+ return transport
+
def test_ctor(self):
- tr = unix_events._UnixWritePipeTransport(
- self.loop, self.pipe, self.protocol)
+ tr = self.write_pipe_transport()
self.loop.assert_reader(5, tr._read_ready)
test_utils.run_briefly(self.loop)
self.protocol.connection_made.assert_called_with(tr)
def test_ctor_with_waiter(self):
fut = asyncio.Future(loop=self.loop)
- tr = unix_events._UnixWritePipeTransport(
- self.loop, self.pipe, self.protocol, fut)
+ tr = self.write_pipe_transport(waiter=fut)
self.loop.assert_reader(5, tr._read_ready)
test_utils.run_briefly(self.loop)
self.assertEqual(None, fut.result())
def test_can_write_eof(self):
- tr = unix_events._UnixWritePipeTransport(
- self.loop, self.pipe, self.protocol)
+ tr = self.write_pipe_transport()
self.assertTrue(tr.can_write_eof())
@mock.patch('os.write')
def test_write(self, m_write):
- tr = unix_events._UnixWritePipeTransport(
- self.loop, self.pipe, self.protocol)
-
+ tr = self.write_pipe_transport()
m_write.return_value = 4
tr.write(b'data')
m_write.assert_called_with(5, b'data')
@@ -529,9 +529,7 @@ class UnixWritePipeTransportTests(test_utils.TestCase):
@mock.patch('os.write')
def test_write_no_data(self, m_write):
- tr = unix_events._UnixWritePipeTransport(
- self.loop, self.pipe, self.protocol)
-
+ tr = self.write_pipe_transport()
tr.write(b'')
self.assertFalse(m_write.called)
self.assertFalse(self.loop.writers)
@@ -539,9 +537,7 @@ class UnixWritePipeTransportTests(test_utils.TestCase):
@mock.patch('os.write')
def test_write_partial(self, m_write):
- tr = unix_events._UnixWritePipeTransport(
- self.loop, self.pipe, self.protocol)
-
+ tr = self.write_pipe_transport()
m_write.return_value = 2
tr.write(b'data')
m_write.assert_called_with(5, b'data')
@@ -550,9 +546,7 @@ class UnixWritePipeTransportTests(test_utils.TestCase):
@mock.patch('os.write')
def test_write_buffer(self, m_write):
- tr = unix_events._UnixWritePipeTransport(
- self.loop, self.pipe, self.protocol)
-
+ tr = self.write_pipe_transport()
self.loop.add_writer(5, tr._write_ready)
tr._buffer = [b'previous']
tr.write(b'data')
@@ -562,9 +556,7 @@ class UnixWritePipeTransportTests(test_utils.TestCase):
@mock.patch('os.write')
def test_write_again(self, m_write):
- tr = unix_events._UnixWritePipeTransport(
- self.loop, self.pipe, self.protocol)
-
+ tr = self.write_pipe_transport()
m_write.side_effect = BlockingIOError()
tr.write(b'data')
m_write.assert_called_with(5, b'data')
@@ -574,9 +566,7 @@ class UnixWritePipeTransportTests(test_utils.TestCase):
@mock.patch('asyncio.unix_events.logger')
@mock.patch('os.write')
def test_write_err(self, m_write, m_log):
- tr = unix_events._UnixWritePipeTransport(
- self.loop, self.pipe, self.protocol)
-
+ tr = self.write_pipe_transport()
err = OSError()
m_write.side_effect = err
tr._fatal_error = mock.Mock()
@@ -602,8 +592,7 @@ class UnixWritePipeTransportTests(test_utils.TestCase):
@mock.patch('os.write')
def test_write_close(self, m_write):
- tr = unix_events._UnixWritePipeTransport(
- self.loop, self.pipe, self.protocol)
+ tr = self.write_pipe_transport()
tr._read_ready() # pipe was closed by peer
tr.write(b'data')
@@ -612,8 +601,7 @@ class UnixWritePipeTransportTests(test_utils.TestCase):
self.assertEqual(tr._conn_lost, 2)
def test__read_ready(self):
- tr = unix_events._UnixWritePipeTransport(self.loop, self.pipe,
- self.protocol)
+ tr = self.write_pipe_transport()
tr._read_ready()
self.assertFalse(self.loop.readers)
self.assertFalse(self.loop.writers)
@@ -623,8 +611,7 @@ class UnixWritePipeTransportTests(test_utils.TestCase):
@mock.patch('os.write')
def test__write_ready(self, m_write):
- tr = unix_events._UnixWritePipeTransport(
- self.loop, self.pipe, self.protocol)
+ tr = self.write_pipe_transport()
self.loop.add_writer(5, tr._write_ready)
tr._buffer = [b'da', b'ta']
m_write.return_value = 4
@@ -635,9 +622,7 @@ class UnixWritePipeTransportTests(test_utils.TestCase):
@mock.patch('os.write')
def test__write_ready_partial(self, m_write):
- tr = unix_events._UnixWritePipeTransport(
- self.loop, self.pipe, self.protocol)
-
+ tr = self.write_pipe_transport()
self.loop.add_writer(5, tr._write_ready)
tr._buffer = [b'da', b'ta']
m_write.return_value = 3
@@ -648,9 +633,7 @@ class UnixWritePipeTransportTests(test_utils.TestCase):
@mock.patch('os.write')
def test__write_ready_again(self, m_write):
- tr = unix_events._UnixWritePipeTransport(
- self.loop, self.pipe, self.protocol)
-
+ tr = self.write_pipe_transport()
self.loop.add_writer(5, tr._write_ready)
tr._buffer = [b'da', b'ta']
m_write.side_effect = BlockingIOError()
@@ -661,9 +644,7 @@ class UnixWritePipeTransportTests(test_utils.TestCase):
@mock.patch('os.write')
def test__write_ready_empty(self, m_write):
- tr = unix_events._UnixWritePipeTransport(
- self.loop, self.pipe, self.protocol)
-
+ tr = self.write_pipe_transport()
self.loop.add_writer(5, tr._write_ready)
tr._buffer = [b'da', b'ta']
m_write.return_value = 0
@@ -675,9 +656,7 @@ class UnixWritePipeTransportTests(test_utils.TestCase):
@mock.patch('asyncio.log.logger.error')
@mock.patch('os.write')
def test__write_ready_err(self, m_write, m_logexc):
- tr = unix_events._UnixWritePipeTransport(
- self.loop, self.pipe, self.protocol)
-
+ tr = self.write_pipe_transport()
self.loop.add_writer(5, tr._write_ready)
tr._buffer = [b'da', b'ta']
m_write.side_effect = err = OSError()
@@ -698,9 +677,7 @@ class UnixWritePipeTransportTests(test_utils.TestCase):
@mock.patch('os.write')
def test__write_ready_closing(self, m_write):
- tr = unix_events._UnixWritePipeTransport(
- self.loop, self.pipe, self.protocol)
-
+ tr = self.write_pipe_transport()
self.loop.add_writer(5, tr._write_ready)
tr._closing = True
tr._buffer = [b'da', b'ta']
@@ -715,9 +692,7 @@ class UnixWritePipeTransportTests(test_utils.TestCase):
@mock.patch('os.write')
def test_abort(self, m_write):
- tr = unix_events._UnixWritePipeTransport(
- self.loop, self.pipe, self.protocol)
-
+ tr = self.write_pipe_transport()
self.loop.add_writer(5, tr._write_ready)
self.loop.add_reader(5, tr._read_ready)
tr._buffer = [b'da', b'ta']
@@ -731,8 +706,7 @@ class UnixWritePipeTransportTests(test_utils.TestCase):
self.protocol.connection_lost.assert_called_with(None)
def test__call_connection_lost(self):
- tr = unix_events._UnixWritePipeTransport(
- self.loop, self.pipe, self.protocol)
+ tr = self.write_pipe_transport()
self.assertIsNotNone(tr._protocol)
self.assertIsNotNone(tr._loop)
@@ -745,8 +719,7 @@ class UnixWritePipeTransportTests(test_utils.TestCase):
self.assertIsNone(tr._loop)
def test__call_connection_lost_with_err(self):
- tr = unix_events._UnixWritePipeTransport(
- self.loop, self.pipe, self.protocol)
+ tr = self.write_pipe_transport()
self.assertIsNotNone(tr._protocol)
self.assertIsNotNone(tr._loop)
@@ -759,26 +732,23 @@ class UnixWritePipeTransportTests(test_utils.TestCase):
self.assertIsNone(tr._loop)
def test_close(self):
- tr = unix_events._UnixWritePipeTransport(
- self.loop, self.pipe, self.protocol)
-
+ tr = self.write_pipe_transport()
tr.write_eof = mock.Mock()
tr.close()
tr.write_eof.assert_called_with()
- def test_close_closing(self):
- tr = unix_events._UnixWritePipeTransport(
- self.loop, self.pipe, self.protocol)
+ # closing the transport twice must not fail
+ tr.close()
+ def test_close_closing(self):
+ tr = self.write_pipe_transport()
tr.write_eof = mock.Mock()
tr._closing = True
tr.close()
self.assertFalse(tr.write_eof.called)
def test_write_eof(self):
- tr = unix_events._UnixWritePipeTransport(
- self.loop, self.pipe, self.protocol)
-
+ tr = self.write_pipe_transport()
tr.write_eof()
self.assertTrue(tr._closing)
self.assertFalse(self.loop.readers)
@@ -786,8 +756,7 @@ class UnixWritePipeTransportTests(test_utils.TestCase):
self.protocol.connection_lost.assert_called_with(None)
def test_write_eof_pending(self):
- tr = unix_events._UnixWritePipeTransport(
- self.loop, self.pipe, self.protocol)
+ tr = self.write_pipe_transport()
tr._buffer = [b'data']
tr.write_eof()
self.assertTrue(tr._closing)