Commit 02392c92 authored by Victor Stinner's avatar Victor Stinner

Issue #23243: Close explicitly transports in asyncio tests

parent 33cb0396
...@@ -12,6 +12,15 @@ from asyncio.proactor_events import _ProactorDuplexPipeTransport ...@@ -12,6 +12,15 @@ from asyncio.proactor_events import _ProactorDuplexPipeTransport
from asyncio import test_utils 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): class ProactorSocketTransportTests(test_utils.TestCase):
def setUp(self): def setUp(self):
...@@ -22,17 +31,22 @@ class ProactorSocketTransportTests(test_utils.TestCase): ...@@ -22,17 +31,22 @@ class ProactorSocketTransportTests(test_utils.TestCase):
self.protocol = test_utils.make_test_protocol(asyncio.Protocol) self.protocol = test_utils.make_test_protocol(asyncio.Protocol)
self.sock = mock.Mock(socket.socket) 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): def test_ctor(self):
fut = asyncio.Future(loop=self.loop) fut = asyncio.Future(loop=self.loop)
tr = _ProactorSocketTransport( tr = self.socket_transport(waiter=fut)
self.loop, self.sock, self.protocol, fut)
test_utils.run_briefly(self.loop) test_utils.run_briefly(self.loop)
self.assertIsNone(fut.result()) self.assertIsNone(fut.result())
self.protocol.connection_made(tr) self.protocol.connection_made(tr)
self.proactor.recv.assert_called_with(self.sock, 4096) self.proactor.recv.assert_called_with(self.sock, 4096)
def test_loop_reading(self): def test_loop_reading(self):
tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol) tr = self.socket_transport()
tr._loop_reading() tr._loop_reading()
self.loop._proactor.recv.assert_called_with(self.sock, 4096) self.loop._proactor.recv.assert_called_with(self.sock, 4096)
self.assertFalse(self.protocol.data_received.called) self.assertFalse(self.protocol.data_received.called)
...@@ -42,8 +56,7 @@ class ProactorSocketTransportTests(test_utils.TestCase): ...@@ -42,8 +56,7 @@ class ProactorSocketTransportTests(test_utils.TestCase):
res = asyncio.Future(loop=self.loop) res = asyncio.Future(loop=self.loop)
res.set_result(b'data') res.set_result(b'data')
tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol) tr = self.socket_transport()
tr._read_fut = res tr._read_fut = res
tr._loop_reading(res) tr._loop_reading(res)
self.loop._proactor.recv.assert_called_with(self.sock, 4096) self.loop._proactor.recv.assert_called_with(self.sock, 4096)
...@@ -53,8 +66,7 @@ class ProactorSocketTransportTests(test_utils.TestCase): ...@@ -53,8 +66,7 @@ class ProactorSocketTransportTests(test_utils.TestCase):
res = asyncio.Future(loop=self.loop) res = asyncio.Future(loop=self.loop)
res.set_result(b'') res.set_result(b'')
tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol) tr = self.socket_transport()
self.assertRaises(AssertionError, tr._loop_reading, res) self.assertRaises(AssertionError, tr._loop_reading, res)
tr.close = mock.Mock() tr.close = mock.Mock()
...@@ -67,7 +79,7 @@ class ProactorSocketTransportTests(test_utils.TestCase): ...@@ -67,7 +79,7 @@ class ProactorSocketTransportTests(test_utils.TestCase):
def test_loop_reading_aborted(self): def test_loop_reading_aborted(self):
err = self.loop._proactor.recv.side_effect = ConnectionAbortedError() 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._fatal_error = mock.Mock()
tr._loop_reading() tr._loop_reading()
tr._fatal_error.assert_called_with( tr._fatal_error.assert_called_with(
...@@ -77,7 +89,7 @@ class ProactorSocketTransportTests(test_utils.TestCase): ...@@ -77,7 +89,7 @@ class ProactorSocketTransportTests(test_utils.TestCase):
def test_loop_reading_aborted_closing(self): def test_loop_reading_aborted_closing(self):
self.loop._proactor.recv.side_effect = ConnectionAbortedError() self.loop._proactor.recv.side_effect = ConnectionAbortedError()
tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol) tr = self.socket_transport()
tr._closing = True tr._closing = True
tr._fatal_error = mock.Mock() tr._fatal_error = mock.Mock()
tr._loop_reading() tr._loop_reading()
...@@ -85,7 +97,7 @@ class ProactorSocketTransportTests(test_utils.TestCase): ...@@ -85,7 +97,7 @@ class ProactorSocketTransportTests(test_utils.TestCase):
def test_loop_reading_aborted_is_fatal(self): def test_loop_reading_aborted_is_fatal(self):
self.loop._proactor.recv.side_effect = ConnectionAbortedError() self.loop._proactor.recv.side_effect = ConnectionAbortedError()
tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol) tr = self.socket_transport()
tr._closing = False tr._closing = False
tr._fatal_error = mock.Mock() tr._fatal_error = mock.Mock()
tr._loop_reading() tr._loop_reading()
...@@ -94,7 +106,7 @@ class ProactorSocketTransportTests(test_utils.TestCase): ...@@ -94,7 +106,7 @@ class ProactorSocketTransportTests(test_utils.TestCase):
def test_loop_reading_conn_reset_lost(self): def test_loop_reading_conn_reset_lost(self):
err = self.loop._proactor.recv.side_effect = ConnectionResetError() err = self.loop._proactor.recv.side_effect = ConnectionResetError()
tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol) tr = self.socket_transport()
tr._closing = False tr._closing = False
tr._fatal_error = mock.Mock() tr._fatal_error = mock.Mock()
tr._force_close = mock.Mock() tr._force_close = mock.Mock()
...@@ -105,7 +117,7 @@ class ProactorSocketTransportTests(test_utils.TestCase): ...@@ -105,7 +117,7 @@ class ProactorSocketTransportTests(test_utils.TestCase):
def test_loop_reading_exception(self): def test_loop_reading_exception(self):
err = self.loop._proactor.recv.side_effect = (OSError()) 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._fatal_error = mock.Mock()
tr._loop_reading() tr._loop_reading()
tr._fatal_error.assert_called_with( tr._fatal_error.assert_called_with(
...@@ -113,19 +125,19 @@ class ProactorSocketTransportTests(test_utils.TestCase): ...@@ -113,19 +125,19 @@ class ProactorSocketTransportTests(test_utils.TestCase):
'Fatal read error on pipe transport') 'Fatal read error on pipe transport')
def test_write(self): def test_write(self):
tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol) tr = self.socket_transport()
tr._loop_writing = mock.Mock() tr._loop_writing = mock.Mock()
tr.write(b'data') tr.write(b'data')
self.assertEqual(tr._buffer, None) self.assertEqual(tr._buffer, None)
tr._loop_writing.assert_called_with(data=b'data') tr._loop_writing.assert_called_with(data=b'data')
def test_write_no_data(self): def test_write_no_data(self):
tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol) tr = self.socket_transport()
tr.write(b'') tr.write(b'')
self.assertFalse(tr._buffer) self.assertFalse(tr._buffer)
def test_write_more(self): def test_write_more(self):
tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol) tr = self.socket_transport()
tr._write_fut = mock.Mock() tr._write_fut = mock.Mock()
tr._loop_writing = mock.Mock() tr._loop_writing = mock.Mock()
tr.write(b'data') tr.write(b'data')
...@@ -133,7 +145,7 @@ class ProactorSocketTransportTests(test_utils.TestCase): ...@@ -133,7 +145,7 @@ class ProactorSocketTransportTests(test_utils.TestCase):
self.assertFalse(tr._loop_writing.called) self.assertFalse(tr._loop_writing.called)
def test_loop_writing(self): def test_loop_writing(self):
tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol) tr = self.socket_transport()
tr._buffer = bytearray(b'data') tr._buffer = bytearray(b'data')
tr._loop_writing() tr._loop_writing()
self.loop._proactor.send.assert_called_with(self.sock, b'data') self.loop._proactor.send.assert_called_with(self.sock, b'data')
...@@ -143,7 +155,7 @@ class ProactorSocketTransportTests(test_utils.TestCase): ...@@ -143,7 +155,7 @@ class ProactorSocketTransportTests(test_utils.TestCase):
@mock.patch('asyncio.proactor_events.logger') @mock.patch('asyncio.proactor_events.logger')
def test_loop_writing_err(self, m_log): def test_loop_writing_err(self, m_log):
err = self.loop._proactor.send.side_effect = OSError() 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._fatal_error = mock.Mock()
tr._buffer = [b'da', b'ta'] tr._buffer = [b'da', b'ta']
tr._loop_writing() tr._loop_writing()
...@@ -164,7 +176,7 @@ class ProactorSocketTransportTests(test_utils.TestCase): ...@@ -164,7 +176,7 @@ class ProactorSocketTransportTests(test_utils.TestCase):
fut = asyncio.Future(loop=self.loop) fut = asyncio.Future(loop=self.loop)
fut.set_result(b'data') fut.set_result(b'data')
tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol) tr = self.socket_transport()
tr._write_fut = fut tr._write_fut = fut
tr._loop_writing(fut) tr._loop_writing(fut)
self.assertIsNone(tr._write_fut) self.assertIsNone(tr._write_fut)
...@@ -173,7 +185,7 @@ class ProactorSocketTransportTests(test_utils.TestCase): ...@@ -173,7 +185,7 @@ class ProactorSocketTransportTests(test_utils.TestCase):
fut = asyncio.Future(loop=self.loop) fut = asyncio.Future(loop=self.loop)
fut.set_result(1) fut.set_result(1)
tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol) tr = self.socket_transport()
tr._write_fut = fut tr._write_fut = fut
tr.close() tr.close()
tr._loop_writing(fut) tr._loop_writing(fut)
...@@ -182,13 +194,13 @@ class ProactorSocketTransportTests(test_utils.TestCase): ...@@ -182,13 +194,13 @@ class ProactorSocketTransportTests(test_utils.TestCase):
self.protocol.connection_lost.assert_called_with(None) self.protocol.connection_lost.assert_called_with(None)
def test_abort(self): def test_abort(self):
tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol) tr = self.socket_transport()
tr._force_close = mock.Mock() tr._force_close = mock.Mock()
tr.abort() tr.abort()
tr._force_close.assert_called_with(None) tr._force_close.assert_called_with(None)
def test_close(self): def test_close(self):
tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol) tr = self.socket_transport()
tr.close() tr.close()
test_utils.run_briefly(self.loop) test_utils.run_briefly(self.loop)
self.protocol.connection_lost.assert_called_with(None) self.protocol.connection_lost.assert_called_with(None)
...@@ -201,14 +213,14 @@ class ProactorSocketTransportTests(test_utils.TestCase): ...@@ -201,14 +213,14 @@ class ProactorSocketTransportTests(test_utils.TestCase):
self.assertFalse(self.protocol.connection_lost.called) self.assertFalse(self.protocol.connection_lost.called)
def test_close_write_fut(self): def test_close_write_fut(self):
tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol) tr = self.socket_transport()
tr._write_fut = mock.Mock() tr._write_fut = mock.Mock()
tr.close() tr.close()
test_utils.run_briefly(self.loop) test_utils.run_briefly(self.loop)
self.assertFalse(self.protocol.connection_lost.called) self.assertFalse(self.protocol.connection_lost.called)
def test_close_buffer(self): def test_close_buffer(self):
tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol) tr = self.socket_transport()
tr._buffer = [b'data'] tr._buffer = [b'data']
tr.close() tr.close()
test_utils.run_briefly(self.loop) test_utils.run_briefly(self.loop)
...@@ -216,14 +228,14 @@ class ProactorSocketTransportTests(test_utils.TestCase): ...@@ -216,14 +228,14 @@ class ProactorSocketTransportTests(test_utils.TestCase):
@mock.patch('asyncio.base_events.logger') @mock.patch('asyncio.base_events.logger')
def test_fatal_error(self, m_logging): 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._force_close = mock.Mock()
tr._fatal_error(None) tr._fatal_error(None)
self.assertTrue(tr._force_close.called) self.assertTrue(tr._force_close.called)
self.assertTrue(m_logging.error.called) self.assertTrue(m_logging.error.called)
def test_force_close(self): def test_force_close(self):
tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol) tr = self.socket_transport()
tr._buffer = [b'data'] tr._buffer = [b'data']
read_fut = tr._read_fut = mock.Mock() read_fut = tr._read_fut = mock.Mock()
write_fut = tr._write_fut = mock.Mock() write_fut = tr._write_fut = mock.Mock()
...@@ -237,14 +249,14 @@ class ProactorSocketTransportTests(test_utils.TestCase): ...@@ -237,14 +249,14 @@ class ProactorSocketTransportTests(test_utils.TestCase):
self.assertEqual(tr._conn_lost, 1) self.assertEqual(tr._conn_lost, 1)
def test_force_close_idempotent(self): def test_force_close_idempotent(self):
tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol) tr = self.socket_transport()
tr._closing = True tr._closing = True
tr._force_close(None) tr._force_close(None)
test_utils.run_briefly(self.loop) test_utils.run_briefly(self.loop)
self.assertFalse(self.protocol.connection_lost.called) self.assertFalse(self.protocol.connection_lost.called)
def test_fatal_error_2(self): def test_fatal_error_2(self):
tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol) tr = self.socket_transport()
tr._buffer = [b'data'] tr._buffer = [b'data']
tr._force_close(None) tr._force_close(None)
...@@ -253,14 +265,13 @@ class ProactorSocketTransportTests(test_utils.TestCase): ...@@ -253,14 +265,13 @@ class ProactorSocketTransportTests(test_utils.TestCase):
self.assertEqual(None, tr._buffer) self.assertEqual(None, tr._buffer)
def test_call_connection_lost(self): def test_call_connection_lost(self):
tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol) tr = self.socket_transport()
tr._call_connection_lost(None) tr._call_connection_lost(None)
self.assertTrue(self.protocol.connection_lost.called) self.assertTrue(self.protocol.connection_lost.called)
self.assertTrue(self.sock.close.called) self.assertTrue(self.sock.close.called)
def test_write_eof(self): def test_write_eof(self):
tr = _ProactorSocketTransport( tr = self.socket_transport()
self.loop, self.sock, self.protocol)
self.assertTrue(tr.can_write_eof()) self.assertTrue(tr.can_write_eof())
tr.write_eof() tr.write_eof()
self.sock.shutdown.assert_called_with(socket.SHUT_WR) self.sock.shutdown.assert_called_with(socket.SHUT_WR)
...@@ -269,7 +280,7 @@ class ProactorSocketTransportTests(test_utils.TestCase): ...@@ -269,7 +280,7 @@ class ProactorSocketTransportTests(test_utils.TestCase):
tr.close() tr.close()
def test_write_eof_buffer(self): def test_write_eof_buffer(self):
tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol) tr = self.socket_transport()
f = asyncio.Future(loop=self.loop) f = asyncio.Future(loop=self.loop)
tr._loop._proactor.send.return_value = f tr._loop._proactor.send.return_value = f
tr.write(b'data') tr.write(b'data')
...@@ -313,11 +324,10 @@ class ProactorSocketTransportTests(test_utils.TestCase): ...@@ -313,11 +324,10 @@ class ProactorSocketTransportTests(test_utils.TestCase):
self.assertFalse(tr.can_write_eof()) self.assertFalse(tr.can_write_eof())
with self.assertRaises(NotImplementedError): with self.assertRaises(NotImplementedError):
tr.write_eof() tr.write_eof()
tr.close() close_transport(tr)
def test_pause_resume_reading(self): def test_pause_resume_reading(self):
tr = _ProactorSocketTransport( tr = self.socket_transport()
self.loop, self.sock, self.protocol)
futures = [] futures = []
for msg in [b'data1', b'data2', b'data3', b'data4', b'']: for msg in [b'data1', b'data2', b'data3', b'data4', b'']:
f = asyncio.Future(loop=self.loop) f = asyncio.Future(loop=self.loop)
...@@ -345,10 +355,7 @@ class ProactorSocketTransportTests(test_utils.TestCase): ...@@ -345,10 +355,7 @@ class ProactorSocketTransportTests(test_utils.TestCase):
def pause_writing_transport(self, high): def pause_writing_transport(self, high):
tr = _ProactorSocketTransport( tr = self.socket_transport()
self.loop, self.sock, self.protocol)
self.addCleanup(tr.close)
tr.set_write_buffer_limits(high=high) tr.set_write_buffer_limits(high=high)
self.assertEqual(tr.get_write_buffer_size(), 0) self.assertEqual(tr.get_write_buffer_size(), 0)
...@@ -439,7 +446,7 @@ class BaseProactorEventLoopTests(test_utils.TestCase): ...@@ -439,7 +446,7 @@ class BaseProactorEventLoopTests(test_utils.TestCase):
return (self.ssock, self.csock) return (self.ssock, self.csock)
self.loop = EventLoop(self.proactor) 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, 'call_soon')
@mock.patch.object(BaseProactorEventLoop, '_socketpair') @mock.patch.object(BaseProactorEventLoop, '_socketpair')
...@@ -451,6 +458,7 @@ class BaseProactorEventLoopTests(test_utils.TestCase): ...@@ -451,6 +458,7 @@ class BaseProactorEventLoopTests(test_utils.TestCase):
self.assertIs(loop._csock, csock) self.assertIs(loop._csock, csock)
self.assertEqual(loop._internal_fds, 1) self.assertEqual(loop._internal_fds, 1)
call_soon.assert_called_with(loop._loop_self_reading) call_soon.assert_called_with(loop._loop_self_reading)
loop.close()
def test_close_self_pipe(self): def test_close_self_pipe(self):
self.loop._close_self_pipe() self.loop._close_self_pipe()
...@@ -497,6 +505,7 @@ class BaseProactorEventLoopTests(test_utils.TestCase): ...@@ -497,6 +505,7 @@ class BaseProactorEventLoopTests(test_utils.TestCase):
def test_make_socket_transport(self): def test_make_socket_transport(self):
tr = self.loop._make_socket_transport(self.sock, asyncio.Protocol()) tr = self.loop._make_socket_transport(self.sock, asyncio.Protocol())
self.assertIsInstance(tr, _ProactorSocketTransport) self.assertIsInstance(tr, _ProactorSocketTransport)
close_transport(tr)
def test_loop_self_reading(self): def test_loop_self_reading(self):
self.loop._loop_self_reading() self.loop._loop_self_reading()
......
...@@ -39,6 +39,15 @@ def list_to_buffer(l=()): ...@@ -39,6 +39,15 @@ def list_to_buffer(l=()):
return bytearray().join(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): class BaseSelectorEventLoopTests(test_utils.TestCase):
def setUp(self): def setUp(self):
...@@ -52,6 +61,7 @@ class BaseSelectorEventLoopTests(test_utils.TestCase): ...@@ -52,6 +61,7 @@ class BaseSelectorEventLoopTests(test_utils.TestCase):
self.loop.add_reader = mock.Mock() self.loop.add_reader = mock.Mock()
transport = self.loop._make_socket_transport(m, asyncio.Protocol()) transport = self.loop._make_socket_transport(m, asyncio.Protocol())
self.assertIsInstance(transport, _SelectorSocketTransport) self.assertIsInstance(transport, _SelectorSocketTransport)
close_transport(transport)
@unittest.skipIf(ssl is None, 'No ssl module') @unittest.skipIf(ssl is None, 'No ssl module')
def test_make_ssl_transport(self): def test_make_ssl_transport(self):
...@@ -64,11 +74,19 @@ class BaseSelectorEventLoopTests(test_utils.TestCase): ...@@ -64,11 +74,19 @@ class BaseSelectorEventLoopTests(test_utils.TestCase):
with test_utils.disable_logger(): with test_utils.disable_logger():
transport = self.loop._make_ssl_transport( transport = self.loop._make_ssl_transport(
m, asyncio.Protocol(), m, waiter) 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 # Sanity check
class_name = transport.__class__.__name__ class_name = transport.__class__.__name__
self.assertIn("ssl", class_name.lower()) self.assertIn("ssl", class_name.lower())
self.assertIn("transport", 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.selector_events.ssl', None)
@mock.patch('asyncio.sslproto.ssl', None) @mock.patch('asyncio.sslproto.ssl', None)
def test_make_ssl_transport_without_ssl_error(self): def test_make_ssl_transport_without_ssl_error(self):
...@@ -650,21 +668,27 @@ class SelectorTransportTests(test_utils.TestCase): ...@@ -650,21 +668,27 @@ class SelectorTransportTests(test_utils.TestCase):
self.sock = mock.Mock(socket.socket) self.sock = mock.Mock(socket.socket)
self.sock.fileno.return_value = 7 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): 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._loop, self.loop)
self.assertIs(tr._sock, self.sock) self.assertIs(tr._sock, self.sock)
self.assertIs(tr._sock_fd, 7) self.assertIs(tr._sock_fd, 7)
def test_abort(self): def test_abort(self):
tr = _SelectorTransport(self.loop, self.sock, self.protocol, None) tr = self.create_transport()
tr._force_close = mock.Mock() tr._force_close = mock.Mock()
tr.abort() tr.abort()
tr._force_close.assert_called_with(None) tr._force_close.assert_called_with(None)
def test_close(self): def test_close(self):
tr = _SelectorTransport(self.loop, self.sock, self.protocol, None) tr = self.create_transport()
tr.close() tr.close()
self.assertTrue(tr._closing) self.assertTrue(tr._closing)
...@@ -677,7 +701,7 @@ class SelectorTransportTests(test_utils.TestCase): ...@@ -677,7 +701,7 @@ class SelectorTransportTests(test_utils.TestCase):
self.assertEqual(1, self.loop.remove_reader_count[7]) self.assertEqual(1, self.loop.remove_reader_count[7])
def test_close_write_buffer(self): 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._buffer.extend(b'data')
tr.close() tr.close()
...@@ -686,7 +710,7 @@ class SelectorTransportTests(test_utils.TestCase): ...@@ -686,7 +710,7 @@ class SelectorTransportTests(test_utils.TestCase):
self.assertFalse(self.protocol.connection_lost.called) self.assertFalse(self.protocol.connection_lost.called)
def test_force_close(self): def test_force_close(self):
tr = _SelectorTransport(self.loop, self.sock, self.protocol, None) tr = self.create_transport()
tr._buffer.extend(b'1') tr._buffer.extend(b'1')
self.loop.add_reader(7, mock.sentinel) self.loop.add_reader(7, mock.sentinel)
self.loop.add_writer(7, mock.sentinel) self.loop.add_writer(7, mock.sentinel)
...@@ -705,7 +729,7 @@ class SelectorTransportTests(test_utils.TestCase): ...@@ -705,7 +729,7 @@ class SelectorTransportTests(test_utils.TestCase):
@mock.patch('asyncio.log.logger.error') @mock.patch('asyncio.log.logger.error')
def test_fatal_error(self, m_exc): def test_fatal_error(self, m_exc):
exc = OSError() exc = OSError()
tr = _SelectorTransport(self.loop, self.sock, self.protocol, None) tr = self.create_transport()
tr._force_close = mock.Mock() tr._force_close = mock.Mock()
tr._fatal_error(exc) tr._fatal_error(exc)
...@@ -718,7 +742,7 @@ class SelectorTransportTests(test_utils.TestCase): ...@@ -718,7 +742,7 @@ class SelectorTransportTests(test_utils.TestCase):
def test_connection_lost(self): def test_connection_lost(self):
exc = OSError() exc = OSError()
tr = _SelectorTransport(self.loop, self.sock, self.protocol, None) tr = self.create_transport()
self.assertIsNotNone(tr._protocol) self.assertIsNotNone(tr._protocol)
self.assertIsNotNone(tr._loop) self.assertIsNotNone(tr._loop)
tr._call_connection_lost(exc) tr._call_connection_lost(exc)
...@@ -739,9 +763,14 @@ class SelectorSocketTransportTests(test_utils.TestCase): ...@@ -739,9 +763,14 @@ class SelectorSocketTransportTests(test_utils.TestCase):
self.sock = mock.Mock(socket.socket) self.sock = mock.Mock(socket.socket)
self.sock_fd = self.sock.fileno.return_value = 7 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): def test_ctor(self):
tr = _SelectorSocketTransport( tr = self.socket_transport()
self.loop, self.sock, self.protocol)
self.loop.assert_reader(7, tr._read_ready) self.loop.assert_reader(7, tr._read_ready)
test_utils.run_briefly(self.loop) test_utils.run_briefly(self.loop)
self.protocol.connection_made.assert_called_with(tr) self.protocol.connection_made.assert_called_with(tr)
...@@ -749,14 +778,12 @@ class SelectorSocketTransportTests(test_utils.TestCase): ...@@ -749,14 +778,12 @@ class SelectorSocketTransportTests(test_utils.TestCase):
def test_ctor_with_waiter(self): def test_ctor_with_waiter(self):
fut = asyncio.Future(loop=self.loop) fut = asyncio.Future(loop=self.loop)
_SelectorSocketTransport( self.socket_transport(waiter=fut)
self.loop, self.sock, self.protocol, fut)
test_utils.run_briefly(self.loop) test_utils.run_briefly(self.loop)
self.assertIsNone(fut.result()) self.assertIsNone(fut.result())
def test_pause_resume_reading(self): def test_pause_resume_reading(self):
tr = _SelectorSocketTransport( tr = self.socket_transport()
self.loop, self.sock, self.protocol)
self.assertFalse(tr._paused) self.assertFalse(tr._paused)
self.loop.assert_reader(7, tr._read_ready) self.loop.assert_reader(7, tr._read_ready)
tr.pause_reading() tr.pause_reading()
...@@ -769,8 +796,7 @@ class SelectorSocketTransportTests(test_utils.TestCase): ...@@ -769,8 +796,7 @@ class SelectorSocketTransportTests(test_utils.TestCase):
tr.resume_reading() tr.resume_reading()
def test_read_ready(self): def test_read_ready(self):
transport = _SelectorSocketTransport( transport = self.socket_transport()
self.loop, self.sock, self.protocol)
self.sock.recv.return_value = b'data' self.sock.recv.return_value = b'data'
transport._read_ready() transport._read_ready()
...@@ -778,8 +804,7 @@ class SelectorSocketTransportTests(test_utils.TestCase): ...@@ -778,8 +804,7 @@ class SelectorSocketTransportTests(test_utils.TestCase):
self.protocol.data_received.assert_called_with(b'data') self.protocol.data_received.assert_called_with(b'data')
def test_read_ready_eof(self): def test_read_ready_eof(self):
transport = _SelectorSocketTransport( transport = self.socket_transport()
self.loop, self.sock, self.protocol)
transport.close = mock.Mock() transport.close = mock.Mock()
self.sock.recv.return_value = b'' self.sock.recv.return_value = b''
...@@ -789,8 +814,7 @@ class SelectorSocketTransportTests(test_utils.TestCase): ...@@ -789,8 +814,7 @@ class SelectorSocketTransportTests(test_utils.TestCase):
transport.close.assert_called_with() transport.close.assert_called_with()
def test_read_ready_eof_keep_open(self): def test_read_ready_eof_keep_open(self):
transport = _SelectorSocketTransport( transport = self.socket_transport()
self.loop, self.sock, self.protocol)
transport.close = mock.Mock() transport.close = mock.Mock()
self.sock.recv.return_value = b'' self.sock.recv.return_value = b''
...@@ -804,8 +828,7 @@ class SelectorSocketTransportTests(test_utils.TestCase): ...@@ -804,8 +828,7 @@ class SelectorSocketTransportTests(test_utils.TestCase):
def test_read_ready_tryagain(self, m_exc): def test_read_ready_tryagain(self, m_exc):
self.sock.recv.side_effect = BlockingIOError self.sock.recv.side_effect = BlockingIOError
transport = _SelectorSocketTransport( transport = self.socket_transport()
self.loop, self.sock, self.protocol)
transport._fatal_error = mock.Mock() transport._fatal_error = mock.Mock()
transport._read_ready() transport._read_ready()
...@@ -815,8 +838,7 @@ class SelectorSocketTransportTests(test_utils.TestCase): ...@@ -815,8 +838,7 @@ class SelectorSocketTransportTests(test_utils.TestCase):
def test_read_ready_tryagain_interrupted(self, m_exc): def test_read_ready_tryagain_interrupted(self, m_exc):
self.sock.recv.side_effect = InterruptedError self.sock.recv.side_effect = InterruptedError
transport = _SelectorSocketTransport( transport = self.socket_transport()
self.loop, self.sock, self.protocol)
transport._fatal_error = mock.Mock() transport._fatal_error = mock.Mock()
transport._read_ready() transport._read_ready()
...@@ -826,8 +848,7 @@ class SelectorSocketTransportTests(test_utils.TestCase): ...@@ -826,8 +848,7 @@ class SelectorSocketTransportTests(test_utils.TestCase):
def test_read_ready_conn_reset(self, m_exc): def test_read_ready_conn_reset(self, m_exc):
err = self.sock.recv.side_effect = ConnectionResetError() err = self.sock.recv.side_effect = ConnectionResetError()
transport = _SelectorSocketTransport( transport = self.socket_transport()
self.loop, self.sock, self.protocol)
transport._force_close = mock.Mock() transport._force_close = mock.Mock()
with test_utils.disable_logger(): with test_utils.disable_logger():
transport._read_ready() transport._read_ready()
...@@ -837,8 +858,7 @@ class SelectorSocketTransportTests(test_utils.TestCase): ...@@ -837,8 +858,7 @@ class SelectorSocketTransportTests(test_utils.TestCase):
def test_read_ready_err(self, m_exc): def test_read_ready_err(self, m_exc):
err = self.sock.recv.side_effect = OSError() err = self.sock.recv.side_effect = OSError()
transport = _SelectorSocketTransport( transport = self.socket_transport()
self.loop, self.sock, self.protocol)
transport._fatal_error = mock.Mock() transport._fatal_error = mock.Mock()
transport._read_ready() transport._read_ready()
...@@ -850,8 +870,7 @@ class SelectorSocketTransportTests(test_utils.TestCase): ...@@ -850,8 +870,7 @@ class SelectorSocketTransportTests(test_utils.TestCase):
data = b'data' data = b'data'
self.sock.send.return_value = len(data) self.sock.send.return_value = len(data)
transport = _SelectorSocketTransport( transport = self.socket_transport()
self.loop, self.sock, self.protocol)
transport.write(data) transport.write(data)
self.sock.send.assert_called_with(data) self.sock.send.assert_called_with(data)
...@@ -859,8 +878,7 @@ class SelectorSocketTransportTests(test_utils.TestCase): ...@@ -859,8 +878,7 @@ class SelectorSocketTransportTests(test_utils.TestCase):
data = bytearray(b'data') data = bytearray(b'data')
self.sock.send.return_value = len(data) self.sock.send.return_value = len(data)
transport = _SelectorSocketTransport( transport = self.socket_transport()
self.loop, self.sock, self.protocol)
transport.write(data) transport.write(data)
self.sock.send.assert_called_with(data) self.sock.send.assert_called_with(data)
self.assertEqual(data, bytearray(b'data')) # Hasn't been mutated. self.assertEqual(data, bytearray(b'data')) # Hasn't been mutated.
...@@ -869,22 +887,19 @@ class SelectorSocketTransportTests(test_utils.TestCase): ...@@ -869,22 +887,19 @@ class SelectorSocketTransportTests(test_utils.TestCase):
data = memoryview(b'data') data = memoryview(b'data')
self.sock.send.return_value = len(data) self.sock.send.return_value = len(data)
transport = _SelectorSocketTransport( transport = self.socket_transport()
self.loop, self.sock, self.protocol)
transport.write(data) transport.write(data)
self.sock.send.assert_called_with(data) self.sock.send.assert_called_with(data)
def test_write_no_data(self): def test_write_no_data(self):
transport = _SelectorSocketTransport( transport = self.socket_transport()
self.loop, self.sock, self.protocol)
transport._buffer.extend(b'data') transport._buffer.extend(b'data')
transport.write(b'') transport.write(b'')
self.assertFalse(self.sock.send.called) self.assertFalse(self.sock.send.called)
self.assertEqual(list_to_buffer([b'data']), transport._buffer) self.assertEqual(list_to_buffer([b'data']), transport._buffer)
def test_write_buffer(self): def test_write_buffer(self):
transport = _SelectorSocketTransport( transport = self.socket_transport()
self.loop, self.sock, self.protocol)
transport._buffer.extend(b'data1') transport._buffer.extend(b'data1')
transport.write(b'data2') transport.write(b'data2')
self.assertFalse(self.sock.send.called) self.assertFalse(self.sock.send.called)
...@@ -895,8 +910,7 @@ class SelectorSocketTransportTests(test_utils.TestCase): ...@@ -895,8 +910,7 @@ class SelectorSocketTransportTests(test_utils.TestCase):
data = b'data' data = b'data'
self.sock.send.return_value = 2 self.sock.send.return_value = 2
transport = _SelectorSocketTransport( transport = self.socket_transport()
self.loop, self.sock, self.protocol)
transport.write(data) transport.write(data)
self.loop.assert_writer(7, transport._write_ready) self.loop.assert_writer(7, transport._write_ready)
...@@ -906,8 +920,7 @@ class SelectorSocketTransportTests(test_utils.TestCase): ...@@ -906,8 +920,7 @@ class SelectorSocketTransportTests(test_utils.TestCase):
data = bytearray(b'data') data = bytearray(b'data')
self.sock.send.return_value = 2 self.sock.send.return_value = 2
transport = _SelectorSocketTransport( transport = self.socket_transport()
self.loop, self.sock, self.protocol)
transport.write(data) transport.write(data)
self.loop.assert_writer(7, transport._write_ready) self.loop.assert_writer(7, transport._write_ready)
...@@ -918,8 +931,7 @@ class SelectorSocketTransportTests(test_utils.TestCase): ...@@ -918,8 +931,7 @@ class SelectorSocketTransportTests(test_utils.TestCase):
data = memoryview(b'data') data = memoryview(b'data')
self.sock.send.return_value = 2 self.sock.send.return_value = 2
transport = _SelectorSocketTransport( transport = self.socket_transport()
self.loop, self.sock, self.protocol)
transport.write(data) transport.write(data)
self.loop.assert_writer(7, transport._write_ready) self.loop.assert_writer(7, transport._write_ready)
...@@ -930,8 +942,7 @@ class SelectorSocketTransportTests(test_utils.TestCase): ...@@ -930,8 +942,7 @@ class SelectorSocketTransportTests(test_utils.TestCase):
self.sock.send.return_value = 0 self.sock.send.return_value = 0
self.sock.fileno.return_value = 7 self.sock.fileno.return_value = 7
transport = _SelectorSocketTransport( transport = self.socket_transport()
self.loop, self.sock, self.protocol)
transport.write(data) transport.write(data)
self.loop.assert_writer(7, transport._write_ready) self.loop.assert_writer(7, transport._write_ready)
...@@ -941,8 +952,7 @@ class SelectorSocketTransportTests(test_utils.TestCase): ...@@ -941,8 +952,7 @@ class SelectorSocketTransportTests(test_utils.TestCase):
self.sock.send.side_effect = BlockingIOError self.sock.send.side_effect = BlockingIOError
data = b'data' data = b'data'
transport = _SelectorSocketTransport( transport = self.socket_transport()
self.loop, self.sock, self.protocol)
transport.write(data) transport.write(data)
self.loop.assert_writer(7, transport._write_ready) self.loop.assert_writer(7, transport._write_ready)
...@@ -953,8 +963,7 @@ class SelectorSocketTransportTests(test_utils.TestCase): ...@@ -953,8 +963,7 @@ class SelectorSocketTransportTests(test_utils.TestCase):
err = self.sock.send.side_effect = OSError() err = self.sock.send.side_effect = OSError()
data = b'data' data = b'data'
transport = _SelectorSocketTransport( transport = self.socket_transport()
self.loop, self.sock, self.protocol)
transport._fatal_error = mock.Mock() transport._fatal_error = mock.Mock()
transport.write(data) transport.write(data)
transport._fatal_error.assert_called_with( transport._fatal_error.assert_called_with(
...@@ -973,13 +982,11 @@ class SelectorSocketTransportTests(test_utils.TestCase): ...@@ -973,13 +982,11 @@ class SelectorSocketTransportTests(test_utils.TestCase):
m_log.warning.assert_called_with('socket.send() raised exception.') m_log.warning.assert_called_with('socket.send() raised exception.')
def test_write_str(self): def test_write_str(self):
transport = _SelectorSocketTransport( transport = self.socket_transport()
self.loop, self.sock, self.protocol)
self.assertRaises(TypeError, transport.write, 'str') self.assertRaises(TypeError, transport.write, 'str')
def test_write_closing(self): def test_write_closing(self):
transport = _SelectorSocketTransport( transport = self.socket_transport()
self.loop, self.sock, self.protocol)
transport.close() transport.close()
self.assertEqual(transport._conn_lost, 1) self.assertEqual(transport._conn_lost, 1)
transport.write(b'data') transport.write(b'data')
...@@ -989,8 +996,7 @@ class SelectorSocketTransportTests(test_utils.TestCase): ...@@ -989,8 +996,7 @@ class SelectorSocketTransportTests(test_utils.TestCase):
data = b'data' data = b'data'
self.sock.send.return_value = len(data) self.sock.send.return_value = len(data)
transport = _SelectorSocketTransport( transport = self.socket_transport()
self.loop, self.sock, self.protocol)
transport._buffer.extend(data) transport._buffer.extend(data)
self.loop.add_writer(7, transport._write_ready) self.loop.add_writer(7, transport._write_ready)
transport._write_ready() transport._write_ready()
...@@ -1001,8 +1007,7 @@ class SelectorSocketTransportTests(test_utils.TestCase): ...@@ -1001,8 +1007,7 @@ class SelectorSocketTransportTests(test_utils.TestCase):
data = b'data' data = b'data'
self.sock.send.return_value = len(data) self.sock.send.return_value = len(data)
transport = _SelectorSocketTransport( transport = self.socket_transport()
self.loop, self.sock, self.protocol)
transport._closing = True transport._closing = True
transport._buffer.extend(data) transport._buffer.extend(data)
self.loop.add_writer(7, transport._write_ready) self.loop.add_writer(7, transport._write_ready)
...@@ -1013,8 +1018,7 @@ class SelectorSocketTransportTests(test_utils.TestCase): ...@@ -1013,8 +1018,7 @@ class SelectorSocketTransportTests(test_utils.TestCase):
self.protocol.connection_lost.assert_called_with(None) self.protocol.connection_lost.assert_called_with(None)
def test_write_ready_no_data(self): def test_write_ready_no_data(self):
transport = _SelectorSocketTransport( transport = self.socket_transport()
self.loop, self.sock, self.protocol)
# This is an internal error. # This is an internal error.
self.assertRaises(AssertionError, transport._write_ready) self.assertRaises(AssertionError, transport._write_ready)
...@@ -1022,8 +1026,7 @@ class SelectorSocketTransportTests(test_utils.TestCase): ...@@ -1022,8 +1026,7 @@ class SelectorSocketTransportTests(test_utils.TestCase):
data = b'data' data = b'data'
self.sock.send.return_value = 2 self.sock.send.return_value = 2
transport = _SelectorSocketTransport( transport = self.socket_transport()
self.loop, self.sock, self.protocol)
transport._buffer.extend(data) transport._buffer.extend(data)
self.loop.add_writer(7, transport._write_ready) self.loop.add_writer(7, transport._write_ready)
transport._write_ready() transport._write_ready()
...@@ -1034,8 +1037,7 @@ class SelectorSocketTransportTests(test_utils.TestCase): ...@@ -1034,8 +1037,7 @@ class SelectorSocketTransportTests(test_utils.TestCase):
data = b'data' data = b'data'
self.sock.send.return_value = 0 self.sock.send.return_value = 0
transport = _SelectorSocketTransport( transport = self.socket_transport()
self.loop, self.sock, self.protocol)
transport._buffer.extend(data) transport._buffer.extend(data)
self.loop.add_writer(7, transport._write_ready) self.loop.add_writer(7, transport._write_ready)
transport._write_ready() transport._write_ready()
...@@ -1045,8 +1047,7 @@ class SelectorSocketTransportTests(test_utils.TestCase): ...@@ -1045,8 +1047,7 @@ class SelectorSocketTransportTests(test_utils.TestCase):
def test_write_ready_tryagain(self): def test_write_ready_tryagain(self):
self.sock.send.side_effect = BlockingIOError self.sock.send.side_effect = BlockingIOError
transport = _SelectorSocketTransport( transport = self.socket_transport()
self.loop, self.sock, self.protocol)
transport._buffer = list_to_buffer([b'data1', b'data2']) transport._buffer = list_to_buffer([b'data1', b'data2'])
self.loop.add_writer(7, transport._write_ready) self.loop.add_writer(7, transport._write_ready)
transport._write_ready() transport._write_ready()
...@@ -1057,8 +1058,7 @@ class SelectorSocketTransportTests(test_utils.TestCase): ...@@ -1057,8 +1058,7 @@ class SelectorSocketTransportTests(test_utils.TestCase):
def test_write_ready_exception(self): def test_write_ready_exception(self):
err = self.sock.send.side_effect = OSError() err = self.sock.send.side_effect = OSError()
transport = _SelectorSocketTransport( transport = self.socket_transport()
self.loop, self.sock, self.protocol)
transport._fatal_error = mock.Mock() transport._fatal_error = mock.Mock()
transport._buffer.extend(b'data') transport._buffer.extend(b'data')
transport._write_ready() transport._write_ready()
...@@ -1071,16 +1071,14 @@ class SelectorSocketTransportTests(test_utils.TestCase): ...@@ -1071,16 +1071,14 @@ class SelectorSocketTransportTests(test_utils.TestCase):
self.sock.send.side_effect = OSError() self.sock.send.side_effect = OSError()
remove_writer = self.loop.remove_writer = mock.Mock() remove_writer = self.loop.remove_writer = mock.Mock()
transport = _SelectorSocketTransport( transport = self.socket_transport()
self.loop, self.sock, self.protocol)
transport.close() transport.close()
transport._buffer.extend(b'data') transport._buffer.extend(b'data')
transport._write_ready() transport._write_ready()
remove_writer.assert_called_with(self.sock_fd) remove_writer.assert_called_with(self.sock_fd)
def test_write_eof(self): def test_write_eof(self):
tr = _SelectorSocketTransport( tr = self.socket_transport()
self.loop, self.sock, self.protocol)
self.assertTrue(tr.can_write_eof()) self.assertTrue(tr.can_write_eof())
tr.write_eof() tr.write_eof()
self.sock.shutdown.assert_called_with(socket.SHUT_WR) self.sock.shutdown.assert_called_with(socket.SHUT_WR)
...@@ -1089,8 +1087,7 @@ class SelectorSocketTransportTests(test_utils.TestCase): ...@@ -1089,8 +1087,7 @@ class SelectorSocketTransportTests(test_utils.TestCase):
tr.close() tr.close()
def test_write_eof_buffer(self): def test_write_eof_buffer(self):
tr = _SelectorSocketTransport( tr = self.socket_transport()
self.loop, self.sock, self.protocol)
self.sock.send.side_effect = BlockingIOError self.sock.send.side_effect = BlockingIOError
tr.write(b'data') tr.write(b'data')
tr.write_eof() tr.write_eof()
...@@ -1117,9 +1114,15 @@ class SelectorSslTransportTests(test_utils.TestCase): ...@@ -1117,9 +1114,15 @@ class SelectorSslTransportTests(test_utils.TestCase):
self.sslcontext = mock.Mock() self.sslcontext = mock.Mock()
self.sslcontext.wrap_socket.return_value = self.sslsock 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): def _make_one(self, create_waiter=None):
transport = _SelectorSslTransport( transport = self.ssl_transport()
self.loop, self.sock, self.protocol, self.sslcontext)
self.sock.reset_mock() self.sock.reset_mock()
self.sslsock.reset_mock() self.sslsock.reset_mock()
self.sslcontext.reset_mock() self.sslcontext.reset_mock()
...@@ -1128,9 +1131,7 @@ class SelectorSslTransportTests(test_utils.TestCase): ...@@ -1128,9 +1131,7 @@ class SelectorSslTransportTests(test_utils.TestCase):
def test_on_handshake(self): def test_on_handshake(self):
waiter = asyncio.Future(loop=self.loop) waiter = asyncio.Future(loop=self.loop)
tr = _SelectorSslTransport( tr = self.ssl_transport(waiter=waiter)
self.loop, self.sock, self.protocol, self.sslcontext,
waiter=waiter)
self.assertTrue(self.sslsock.do_handshake.called) self.assertTrue(self.sslsock.do_handshake.called)
self.loop.assert_reader(1, tr._read_ready) self.loop.assert_reader(1, tr._read_ready)
test_utils.run_briefly(self.loop) test_utils.run_briefly(self.loop)
...@@ -1139,15 +1140,13 @@ class SelectorSslTransportTests(test_utils.TestCase): ...@@ -1139,15 +1140,13 @@ class SelectorSslTransportTests(test_utils.TestCase):
def test_on_handshake_reader_retry(self): def test_on_handshake_reader_retry(self):
self.loop.set_debug(False) self.loop.set_debug(False)
self.sslsock.do_handshake.side_effect = ssl.SSLWantReadError self.sslsock.do_handshake.side_effect = ssl.SSLWantReadError
transport = _SelectorSslTransport( transport = self.ssl_transport()
self.loop, self.sock, self.protocol, self.sslcontext)
self.loop.assert_reader(1, transport._on_handshake, None) self.loop.assert_reader(1, transport._on_handshake, None)
def test_on_handshake_writer_retry(self): def test_on_handshake_writer_retry(self):
self.loop.set_debug(False) self.loop.set_debug(False)
self.sslsock.do_handshake.side_effect = ssl.SSLWantWriteError self.sslsock.do_handshake.side_effect = ssl.SSLWantWriteError
transport = _SelectorSslTransport( transport = self.ssl_transport()
self.loop, self.sock, self.protocol, self.sslcontext)
self.loop.assert_writer(1, transport._on_handshake, None) self.loop.assert_writer(1, transport._on_handshake, None)
def test_on_handshake_exc(self): def test_on_handshake_exc(self):
...@@ -1155,16 +1154,14 @@ class SelectorSslTransportTests(test_utils.TestCase): ...@@ -1155,16 +1154,14 @@ class SelectorSslTransportTests(test_utils.TestCase):
self.sslsock.do_handshake.side_effect = exc self.sslsock.do_handshake.side_effect = exc
with test_utils.disable_logger(): with test_utils.disable_logger():
waiter = asyncio.Future(loop=self.loop) waiter = asyncio.Future(loop=self.loop)
transport = _SelectorSslTransport( transport = self.ssl_transport(waiter=waiter)
self.loop, self.sock, self.protocol, self.sslcontext, waiter)
self.assertTrue(waiter.done()) self.assertTrue(waiter.done())
self.assertIs(exc, waiter.exception()) self.assertIs(exc, waiter.exception())
self.assertTrue(self.sslsock.close.called) self.assertTrue(self.sslsock.close.called)
def test_on_handshake_base_exc(self): def test_on_handshake_base_exc(self):
waiter = asyncio.Future(loop=self.loop) waiter = asyncio.Future(loop=self.loop)
transport = _SelectorSslTransport( transport = self.ssl_transport(waiter=waiter)
self.loop, self.sock, self.protocol, self.sslcontext, waiter)
exc = BaseException() exc = BaseException()
self.sslsock.do_handshake.side_effect = exc self.sslsock.do_handshake.side_effect = exc
with test_utils.disable_logger(): with test_utils.disable_logger():
...@@ -1177,8 +1174,7 @@ class SelectorSslTransportTests(test_utils.TestCase): ...@@ -1177,8 +1174,7 @@ class SelectorSslTransportTests(test_utils.TestCase):
# Python issue #23197: cancelling an handshake must not raise an # Python issue #23197: cancelling an handshake must not raise an
# exception or log an error, even if the handshake failed # exception or log an error, even if the handshake failed
waiter = asyncio.Future(loop=self.loop) waiter = asyncio.Future(loop=self.loop)
transport = _SelectorSslTransport( transport = self.ssl_transport(waiter=waiter)
self.loop, self.sock, self.protocol, self.sslcontext, waiter)
waiter.cancel() waiter.cancel()
exc = ValueError() exc = ValueError()
self.sslsock.do_handshake.side_effect = exc self.sslsock.do_handshake.side_effect = exc
...@@ -1437,9 +1433,7 @@ class SelectorSslTransportTests(test_utils.TestCase): ...@@ -1437,9 +1433,7 @@ class SelectorSslTransportTests(test_utils.TestCase):
@unittest.skipIf(ssl is None, 'No SSL support') @unittest.skipIf(ssl is None, 'No SSL support')
def test_server_hostname(self): def test_server_hostname(self):
_SelectorSslTransport( self.ssl_transport(server_hostname='localhost')
self.loop, self.sock, self.protocol, self.sslcontext,
server_hostname='localhost')
self.sslcontext.wrap_socket.assert_called_with( self.sslcontext.wrap_socket.assert_called_with(
self.sock, do_handshake_on_connect=False, server_side=False, self.sock, do_handshake_on_connect=False, server_side=False,
server_hostname='localhost') server_hostname='localhost')
...@@ -1462,9 +1456,15 @@ class SelectorDatagramTransportTests(test_utils.TestCase): ...@@ -1462,9 +1456,15 @@ class SelectorDatagramTransportTests(test_utils.TestCase):
self.sock = mock.Mock(spec_set=socket.socket) self.sock = mock.Mock(spec_set=socket.socket)
self.sock.fileno.return_value = 7 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): def test_read_ready(self):
transport = _SelectorDatagramTransport( transport = self.datagram_transport()
self.loop, self.sock, self.protocol)
self.sock.recvfrom.return_value = (b'data', ('0.0.0.0', 1234)) self.sock.recvfrom.return_value = (b'data', ('0.0.0.0', 1234))
transport._read_ready() transport._read_ready()
...@@ -1473,8 +1473,7 @@ class SelectorDatagramTransportTests(test_utils.TestCase): ...@@ -1473,8 +1473,7 @@ class SelectorDatagramTransportTests(test_utils.TestCase):
b'data', ('0.0.0.0', 1234)) b'data', ('0.0.0.0', 1234))
def test_read_ready_tryagain(self): def test_read_ready_tryagain(self):
transport = _SelectorDatagramTransport( transport = self.datagram_transport()
self.loop, self.sock, self.protocol)
self.sock.recvfrom.side_effect = BlockingIOError self.sock.recvfrom.side_effect = BlockingIOError
transport._fatal_error = mock.Mock() transport._fatal_error = mock.Mock()
...@@ -1483,8 +1482,7 @@ class SelectorDatagramTransportTests(test_utils.TestCase): ...@@ -1483,8 +1482,7 @@ class SelectorDatagramTransportTests(test_utils.TestCase):
self.assertFalse(transport._fatal_error.called) self.assertFalse(transport._fatal_error.called)
def test_read_ready_err(self): def test_read_ready_err(self):
transport = _SelectorDatagramTransport( transport = self.datagram_transport()
self.loop, self.sock, self.protocol)
err = self.sock.recvfrom.side_effect = RuntimeError() err = self.sock.recvfrom.side_effect = RuntimeError()
transport._fatal_error = mock.Mock() transport._fatal_error = mock.Mock()
...@@ -1495,8 +1493,7 @@ class SelectorDatagramTransportTests(test_utils.TestCase): ...@@ -1495,8 +1493,7 @@ class SelectorDatagramTransportTests(test_utils.TestCase):
'Fatal read error on datagram transport') 'Fatal read error on datagram transport')
def test_read_ready_oserr(self): def test_read_ready_oserr(self):
transport = _SelectorDatagramTransport( transport = self.datagram_transport()
self.loop, self.sock, self.protocol)
err = self.sock.recvfrom.side_effect = OSError() err = self.sock.recvfrom.side_effect = OSError()
transport._fatal_error = mock.Mock() transport._fatal_error = mock.Mock()
...@@ -1507,8 +1504,7 @@ class SelectorDatagramTransportTests(test_utils.TestCase): ...@@ -1507,8 +1504,7 @@ class SelectorDatagramTransportTests(test_utils.TestCase):
def test_sendto(self): def test_sendto(self):
data = b'data' data = b'data'
transport = _SelectorDatagramTransport( transport = self.datagram_transport()
self.loop, self.sock, self.protocol)
transport.sendto(data, ('0.0.0.0', 1234)) transport.sendto(data, ('0.0.0.0', 1234))
self.assertTrue(self.sock.sendto.called) self.assertTrue(self.sock.sendto.called)
self.assertEqual( self.assertEqual(
...@@ -1516,8 +1512,7 @@ class SelectorDatagramTransportTests(test_utils.TestCase): ...@@ -1516,8 +1512,7 @@ class SelectorDatagramTransportTests(test_utils.TestCase):
def test_sendto_bytearray(self): def test_sendto_bytearray(self):
data = bytearray(b'data') data = bytearray(b'data')
transport = _SelectorDatagramTransport( transport = self.datagram_transport()
self.loop, self.sock, self.protocol)
transport.sendto(data, ('0.0.0.0', 1234)) transport.sendto(data, ('0.0.0.0', 1234))
self.assertTrue(self.sock.sendto.called) self.assertTrue(self.sock.sendto.called)
self.assertEqual( self.assertEqual(
...@@ -1525,16 +1520,14 @@ class SelectorDatagramTransportTests(test_utils.TestCase): ...@@ -1525,16 +1520,14 @@ class SelectorDatagramTransportTests(test_utils.TestCase):
def test_sendto_memoryview(self): def test_sendto_memoryview(self):
data = memoryview(b'data') data = memoryview(b'data')
transport = _SelectorDatagramTransport( transport = self.datagram_transport()
self.loop, self.sock, self.protocol)
transport.sendto(data, ('0.0.0.0', 1234)) transport.sendto(data, ('0.0.0.0', 1234))
self.assertTrue(self.sock.sendto.called) self.assertTrue(self.sock.sendto.called)
self.assertEqual( self.assertEqual(
self.sock.sendto.call_args[0], (data, ('0.0.0.0', 1234))) self.sock.sendto.call_args[0], (data, ('0.0.0.0', 1234)))
def test_sendto_no_data(self): def test_sendto_no_data(self):
transport = _SelectorDatagramTransport( transport = self.datagram_transport()
self.loop, self.sock, self.protocol)
transport._buffer.append((b'data', ('0.0.0.0', 12345))) transport._buffer.append((b'data', ('0.0.0.0', 12345)))
transport.sendto(b'', ()) transport.sendto(b'', ())
self.assertFalse(self.sock.sendto.called) self.assertFalse(self.sock.sendto.called)
...@@ -1542,8 +1535,7 @@ class SelectorDatagramTransportTests(test_utils.TestCase): ...@@ -1542,8 +1535,7 @@ class SelectorDatagramTransportTests(test_utils.TestCase):
[(b'data', ('0.0.0.0', 12345))], list(transport._buffer)) [(b'data', ('0.0.0.0', 12345))], list(transport._buffer))
def test_sendto_buffer(self): def test_sendto_buffer(self):
transport = _SelectorDatagramTransport( transport = self.datagram_transport()
self.loop, self.sock, self.protocol)
transport._buffer.append((b'data1', ('0.0.0.0', 12345))) transport._buffer.append((b'data1', ('0.0.0.0', 12345)))
transport.sendto(b'data2', ('0.0.0.0', 12345)) transport.sendto(b'data2', ('0.0.0.0', 12345))
self.assertFalse(self.sock.sendto.called) self.assertFalse(self.sock.sendto.called)
...@@ -1554,8 +1546,7 @@ class SelectorDatagramTransportTests(test_utils.TestCase): ...@@ -1554,8 +1546,7 @@ class SelectorDatagramTransportTests(test_utils.TestCase):
def test_sendto_buffer_bytearray(self): def test_sendto_buffer_bytearray(self):
data2 = bytearray(b'data2') data2 = bytearray(b'data2')
transport = _SelectorDatagramTransport( transport = self.datagram_transport()
self.loop, self.sock, self.protocol)
transport._buffer.append((b'data1', ('0.0.0.0', 12345))) transport._buffer.append((b'data1', ('0.0.0.0', 12345)))
transport.sendto(data2, ('0.0.0.0', 12345)) transport.sendto(data2, ('0.0.0.0', 12345))
self.assertFalse(self.sock.sendto.called) self.assertFalse(self.sock.sendto.called)
...@@ -1567,8 +1558,7 @@ class SelectorDatagramTransportTests(test_utils.TestCase): ...@@ -1567,8 +1558,7 @@ class SelectorDatagramTransportTests(test_utils.TestCase):
def test_sendto_buffer_memoryview(self): def test_sendto_buffer_memoryview(self):
data2 = memoryview(b'data2') data2 = memoryview(b'data2')
transport = _SelectorDatagramTransport( transport = self.datagram_transport()
self.loop, self.sock, self.protocol)
transport._buffer.append((b'data1', ('0.0.0.0', 12345))) transport._buffer.append((b'data1', ('0.0.0.0', 12345)))
transport.sendto(data2, ('0.0.0.0', 12345)) transport.sendto(data2, ('0.0.0.0', 12345))
self.assertFalse(self.sock.sendto.called) self.assertFalse(self.sock.sendto.called)
...@@ -1583,8 +1573,7 @@ class SelectorDatagramTransportTests(test_utils.TestCase): ...@@ -1583,8 +1573,7 @@ class SelectorDatagramTransportTests(test_utils.TestCase):
self.sock.sendto.side_effect = BlockingIOError self.sock.sendto.side_effect = BlockingIOError
transport = _SelectorDatagramTransport( transport = self.datagram_transport()
self.loop, self.sock, self.protocol)
transport.sendto(data, ('0.0.0.0', 12345)) transport.sendto(data, ('0.0.0.0', 12345))
self.loop.assert_writer(7, transport._sendto_ready) self.loop.assert_writer(7, transport._sendto_ready)
...@@ -1596,8 +1585,7 @@ class SelectorDatagramTransportTests(test_utils.TestCase): ...@@ -1596,8 +1585,7 @@ class SelectorDatagramTransportTests(test_utils.TestCase):
data = b'data' data = b'data'
err = self.sock.sendto.side_effect = RuntimeError() err = self.sock.sendto.side_effect = RuntimeError()
transport = _SelectorDatagramTransport( transport = self.datagram_transport()
self.loop, self.sock, self.protocol)
transport._fatal_error = mock.Mock() transport._fatal_error = mock.Mock()
transport.sendto(data, ()) transport.sendto(data, ())
...@@ -1620,8 +1608,7 @@ class SelectorDatagramTransportTests(test_utils.TestCase): ...@@ -1620,8 +1608,7 @@ class SelectorDatagramTransportTests(test_utils.TestCase):
self.sock.sendto.side_effect = ConnectionRefusedError self.sock.sendto.side_effect = ConnectionRefusedError
transport = _SelectorDatagramTransport( transport = self.datagram_transport()
self.loop, self.sock, self.protocol)
transport._fatal_error = mock.Mock() transport._fatal_error = mock.Mock()
transport.sendto(data, ()) transport.sendto(data, ())
...@@ -1633,8 +1620,7 @@ class SelectorDatagramTransportTests(test_utils.TestCase): ...@@ -1633,8 +1620,7 @@ class SelectorDatagramTransportTests(test_utils.TestCase):
self.sock.send.side_effect = ConnectionRefusedError self.sock.send.side_effect = ConnectionRefusedError
transport = _SelectorDatagramTransport( transport = self.datagram_transport(address=('0.0.0.0', 1))
self.loop, self.sock, self.protocol, ('0.0.0.0', 1))
transport._fatal_error = mock.Mock() transport._fatal_error = mock.Mock()
transport.sendto(data) transport.sendto(data)
...@@ -1642,19 +1628,16 @@ class SelectorDatagramTransportTests(test_utils.TestCase): ...@@ -1642,19 +1628,16 @@ class SelectorDatagramTransportTests(test_utils.TestCase):
self.assertTrue(self.protocol.error_received.called) self.assertTrue(self.protocol.error_received.called)
def test_sendto_str(self): def test_sendto_str(self):
transport = _SelectorDatagramTransport( transport = self.datagram_transport()
self.loop, self.sock, self.protocol)
self.assertRaises(TypeError, transport.sendto, 'str', ()) self.assertRaises(TypeError, transport.sendto, 'str', ())
def test_sendto_connected_addr(self): def test_sendto_connected_addr(self):
transport = _SelectorDatagramTransport( transport = self.datagram_transport(address=('0.0.0.0', 1))
self.loop, self.sock, self.protocol, ('0.0.0.0', 1))
self.assertRaises( self.assertRaises(
ValueError, transport.sendto, b'str', ('0.0.0.0', 2)) ValueError, transport.sendto, b'str', ('0.0.0.0', 2))
def test_sendto_closing(self): def test_sendto_closing(self):
transport = _SelectorDatagramTransport( transport = self.datagram_transport(address=(1,))
self.loop, self.sock, self.protocol, address=(1,))
transport.close() transport.close()
self.assertEqual(transport._conn_lost, 1) self.assertEqual(transport._conn_lost, 1)
transport.sendto(b'data', (1,)) transport.sendto(b'data', (1,))
...@@ -1664,8 +1647,7 @@ class SelectorDatagramTransportTests(test_utils.TestCase): ...@@ -1664,8 +1647,7 @@ class SelectorDatagramTransportTests(test_utils.TestCase):
data = b'data' data = b'data'
self.sock.sendto.return_value = len(data) self.sock.sendto.return_value = len(data)
transport = _SelectorDatagramTransport( transport = self.datagram_transport()
self.loop, self.sock, self.protocol)
transport._buffer.append((data, ('0.0.0.0', 12345))) transport._buffer.append((data, ('0.0.0.0', 12345)))
self.loop.add_writer(7, transport._sendto_ready) self.loop.add_writer(7, transport._sendto_ready)
transport._sendto_ready() transport._sendto_ready()
...@@ -1678,8 +1660,7 @@ class SelectorDatagramTransportTests(test_utils.TestCase): ...@@ -1678,8 +1660,7 @@ class SelectorDatagramTransportTests(test_utils.TestCase):
data = b'data' data = b'data'
self.sock.send.return_value = len(data) self.sock.send.return_value = len(data)
transport = _SelectorDatagramTransport( transport = self.datagram_transport()
self.loop, self.sock, self.protocol)
transport._closing = True transport._closing = True
transport._buffer.append((data, ())) transport._buffer.append((data, ()))
self.loop.add_writer(7, transport._sendto_ready) self.loop.add_writer(7, transport._sendto_ready)
...@@ -1690,8 +1671,7 @@ class SelectorDatagramTransportTests(test_utils.TestCase): ...@@ -1690,8 +1671,7 @@ class SelectorDatagramTransportTests(test_utils.TestCase):
self.protocol.connection_lost.assert_called_with(None) self.protocol.connection_lost.assert_called_with(None)
def test_sendto_ready_no_data(self): def test_sendto_ready_no_data(self):
transport = _SelectorDatagramTransport( transport = self.datagram_transport()
self.loop, self.sock, self.protocol)
self.loop.add_writer(7, transport._sendto_ready) self.loop.add_writer(7, transport._sendto_ready)
transport._sendto_ready() transport._sendto_ready()
self.assertFalse(self.sock.sendto.called) self.assertFalse(self.sock.sendto.called)
...@@ -1700,8 +1680,7 @@ class SelectorDatagramTransportTests(test_utils.TestCase): ...@@ -1700,8 +1680,7 @@ class SelectorDatagramTransportTests(test_utils.TestCase):
def test_sendto_ready_tryagain(self): def test_sendto_ready_tryagain(self):
self.sock.sendto.side_effect = BlockingIOError self.sock.sendto.side_effect = BlockingIOError
transport = _SelectorDatagramTransport( transport = self.datagram_transport()
self.loop, self.sock, self.protocol)
transport._buffer.extend([(b'data1', ()), (b'data2', ())]) transport._buffer.extend([(b'data1', ()), (b'data2', ())])
self.loop.add_writer(7, transport._sendto_ready) self.loop.add_writer(7, transport._sendto_ready)
transport._sendto_ready() transport._sendto_ready()
...@@ -1714,8 +1693,7 @@ class SelectorDatagramTransportTests(test_utils.TestCase): ...@@ -1714,8 +1693,7 @@ class SelectorDatagramTransportTests(test_utils.TestCase):
def test_sendto_ready_exception(self): def test_sendto_ready_exception(self):
err = self.sock.sendto.side_effect = RuntimeError() err = self.sock.sendto.side_effect = RuntimeError()
transport = _SelectorDatagramTransport( transport = self.datagram_transport()
self.loop, self.sock, self.protocol)
transport._fatal_error = mock.Mock() transport._fatal_error = mock.Mock()
transport._buffer.append((b'data', ())) transport._buffer.append((b'data', ()))
transport._sendto_ready() transport._sendto_ready()
...@@ -1727,8 +1705,7 @@ class SelectorDatagramTransportTests(test_utils.TestCase): ...@@ -1727,8 +1705,7 @@ class SelectorDatagramTransportTests(test_utils.TestCase):
def test_sendto_ready_error_received(self): def test_sendto_ready_error_received(self):
self.sock.sendto.side_effect = ConnectionRefusedError self.sock.sendto.side_effect = ConnectionRefusedError
transport = _SelectorDatagramTransport( transport = self.datagram_transport()
self.loop, self.sock, self.protocol)
transport._fatal_error = mock.Mock() transport._fatal_error = mock.Mock()
transport._buffer.append((b'data', ())) transport._buffer.append((b'data', ()))
transport._sendto_ready() transport._sendto_ready()
...@@ -1738,8 +1715,7 @@ class SelectorDatagramTransportTests(test_utils.TestCase): ...@@ -1738,8 +1715,7 @@ class SelectorDatagramTransportTests(test_utils.TestCase):
def test_sendto_ready_error_received_connection(self): def test_sendto_ready_error_received_connection(self):
self.sock.send.side_effect = ConnectionRefusedError self.sock.send.side_effect = ConnectionRefusedError
transport = _SelectorDatagramTransport( transport = self.datagram_transport(address=('0.0.0.0', 1))
self.loop, self.sock, self.protocol, ('0.0.0.0', 1))
transport._fatal_error = mock.Mock() transport._fatal_error = mock.Mock()
transport._buffer.append((b'data', ())) transport._buffer.append((b'data', ()))
transport._sendto_ready() transport._sendto_ready()
...@@ -1749,8 +1725,7 @@ class SelectorDatagramTransportTests(test_utils.TestCase): ...@@ -1749,8 +1725,7 @@ class SelectorDatagramTransportTests(test_utils.TestCase):
@mock.patch('asyncio.base_events.logger.error') @mock.patch('asyncio.base_events.logger.error')
def test_fatal_error_connected(self, m_exc): def test_fatal_error_connected(self, m_exc):
transport = _SelectorDatagramTransport( transport = self.datagram_transport(address=('0.0.0.0', 1))
self.loop, self.sock, self.protocol, ('0.0.0.0', 1))
err = ConnectionRefusedError() err = ConnectionRefusedError()
transport._fatal_error(err) transport._fatal_error(err)
self.assertFalse(self.protocol.error_received.called) self.assertFalse(self.protocol.error_received.called)
...@@ -1758,7 +1733,6 @@ class SelectorDatagramTransportTests(test_utils.TestCase): ...@@ -1758,7 +1733,6 @@ class SelectorDatagramTransportTests(test_utils.TestCase):
test_utils.MockPattern( test_utils.MockPattern(
'Fatal error on transport\nprotocol:.*\ntransport:.*'), 'Fatal error on transport\nprotocol:.*\ntransport:.*'),
exc_info=(ConnectionRefusedError, MOCK_ANY, MOCK_ANY)) exc_info=(ConnectionRefusedError, MOCK_ANY, MOCK_ANY))
transport.close()
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -26,6 +26,15 @@ from asyncio import unix_events ...@@ -26,6 +26,15 @@ from asyncio import unix_events
MOCK_ANY = mock.ANY 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') @unittest.skipUnless(signal, 'Signals are not supported')
class SelectorEventLoopSignalTests(test_utils.TestCase): class SelectorEventLoopSignalTests(test_utils.TestCase):
...@@ -333,24 +342,28 @@ class UnixReadPipeTransportTests(test_utils.TestCase): ...@@ -333,24 +342,28 @@ class UnixReadPipeTransportTests(test_utils.TestCase):
m_fstat.return_value = st m_fstat.return_value = st
self.addCleanup(fstat_patcher.stop) 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): def test_ctor(self):
tr = unix_events._UnixReadPipeTransport( tr = self.read_pipe_transport()
self.loop, self.pipe, self.protocol)
self.loop.assert_reader(5, tr._read_ready) self.loop.assert_reader(5, tr._read_ready)
test_utils.run_briefly(self.loop) test_utils.run_briefly(self.loop)
self.protocol.connection_made.assert_called_with(tr) self.protocol.connection_made.assert_called_with(tr)
def test_ctor_with_waiter(self): def test_ctor_with_waiter(self):
fut = asyncio.Future(loop=self.loop) fut = asyncio.Future(loop=self.loop)
unix_events._UnixReadPipeTransport( tr = self.read_pipe_transport(waiter=fut)
self.loop, self.pipe, self.protocol, fut)
test_utils.run_briefly(self.loop) test_utils.run_briefly(self.loop)
self.assertIsNone(fut.result()) self.assertIsNone(fut.result())
@mock.patch('os.read') @mock.patch('os.read')
def test__read_ready(self, m_read): def test__read_ready(self, m_read):
tr = unix_events._UnixReadPipeTransport( tr = self.read_pipe_transport()
self.loop, self.pipe, self.protocol)
m_read.return_value = b'data' m_read.return_value = b'data'
tr._read_ready() tr._read_ready()
...@@ -359,8 +372,7 @@ class UnixReadPipeTransportTests(test_utils.TestCase): ...@@ -359,8 +372,7 @@ class UnixReadPipeTransportTests(test_utils.TestCase):
@mock.patch('os.read') @mock.patch('os.read')
def test__read_ready_eof(self, m_read): def test__read_ready_eof(self, m_read):
tr = unix_events._UnixReadPipeTransport( tr = self.read_pipe_transport()
self.loop, self.pipe, self.protocol)
m_read.return_value = b'' m_read.return_value = b''
tr._read_ready() tr._read_ready()
...@@ -372,8 +384,7 @@ class UnixReadPipeTransportTests(test_utils.TestCase): ...@@ -372,8 +384,7 @@ class UnixReadPipeTransportTests(test_utils.TestCase):
@mock.patch('os.read') @mock.patch('os.read')
def test__read_ready_blocked(self, m_read): def test__read_ready_blocked(self, m_read):
tr = unix_events._UnixReadPipeTransport( tr = self.read_pipe_transport()
self.loop, self.pipe, self.protocol)
m_read.side_effect = BlockingIOError m_read.side_effect = BlockingIOError
tr._read_ready() tr._read_ready()
...@@ -384,8 +395,7 @@ class UnixReadPipeTransportTests(test_utils.TestCase): ...@@ -384,8 +395,7 @@ class UnixReadPipeTransportTests(test_utils.TestCase):
@mock.patch('asyncio.log.logger.error') @mock.patch('asyncio.log.logger.error')
@mock.patch('os.read') @mock.patch('os.read')
def test__read_ready_error(self, m_read, m_logexc): def test__read_ready_error(self, m_read, m_logexc):
tr = unix_events._UnixReadPipeTransport( tr = self.read_pipe_transport()
self.loop, self.pipe, self.protocol)
err = OSError() err = OSError()
m_read.side_effect = err m_read.side_effect = err
tr._close = mock.Mock() tr._close = mock.Mock()
...@@ -401,9 +411,7 @@ class UnixReadPipeTransportTests(test_utils.TestCase): ...@@ -401,9 +411,7 @@ class UnixReadPipeTransportTests(test_utils.TestCase):
@mock.patch('os.read') @mock.patch('os.read')
def test_pause_reading(self, m_read): def test_pause_reading(self, m_read):
tr = unix_events._UnixReadPipeTransport( tr = self.read_pipe_transport()
self.loop, self.pipe, self.protocol)
m = mock.Mock() m = mock.Mock()
self.loop.add_reader(5, m) self.loop.add_reader(5, m)
tr.pause_reading() tr.pause_reading()
...@@ -411,26 +419,20 @@ class UnixReadPipeTransportTests(test_utils.TestCase): ...@@ -411,26 +419,20 @@ class UnixReadPipeTransportTests(test_utils.TestCase):
@mock.patch('os.read') @mock.patch('os.read')
def test_resume_reading(self, m_read): def test_resume_reading(self, m_read):
tr = unix_events._UnixReadPipeTransport( tr = self.read_pipe_transport()
self.loop, self.pipe, self.protocol)
tr.resume_reading() tr.resume_reading()
self.loop.assert_reader(5, tr._read_ready) self.loop.assert_reader(5, tr._read_ready)
@mock.patch('os.read') @mock.patch('os.read')
def test_close(self, m_read): def test_close(self, m_read):
tr = unix_events._UnixReadPipeTransport( tr = self.read_pipe_transport()
self.loop, self.pipe, self.protocol)
tr._close = mock.Mock() tr._close = mock.Mock()
tr.close() tr.close()
tr._close.assert_called_with(None) tr._close.assert_called_with(None)
@mock.patch('os.read') @mock.patch('os.read')
def test_close_already_closing(self, m_read): def test_close_already_closing(self, m_read):
tr = unix_events._UnixReadPipeTransport( tr = self.read_pipe_transport()
self.loop, self.pipe, self.protocol)
tr._closing = True tr._closing = True
tr._close = mock.Mock() tr._close = mock.Mock()
tr.close() tr.close()
...@@ -438,9 +440,7 @@ class UnixReadPipeTransportTests(test_utils.TestCase): ...@@ -438,9 +440,7 @@ class UnixReadPipeTransportTests(test_utils.TestCase):
@mock.patch('os.read') @mock.patch('os.read')
def test__close(self, m_read): def test__close(self, m_read):
tr = unix_events._UnixReadPipeTransport( tr = self.read_pipe_transport()
self.loop, self.pipe, self.protocol)
err = object() err = object()
tr._close(err) tr._close(err)
self.assertTrue(tr._closing) self.assertTrue(tr._closing)
...@@ -449,8 +449,7 @@ class UnixReadPipeTransportTests(test_utils.TestCase): ...@@ -449,8 +449,7 @@ class UnixReadPipeTransportTests(test_utils.TestCase):
self.protocol.connection_lost.assert_called_with(err) self.protocol.connection_lost.assert_called_with(err)
def test__call_connection_lost(self): def test__call_connection_lost(self):
tr = unix_events._UnixReadPipeTransport( tr = self.read_pipe_transport()
self.loop, self.pipe, self.protocol)
self.assertIsNotNone(tr._protocol) self.assertIsNotNone(tr._protocol)
self.assertIsNotNone(tr._loop) self.assertIsNotNone(tr._loop)
...@@ -463,8 +462,7 @@ class UnixReadPipeTransportTests(test_utils.TestCase): ...@@ -463,8 +462,7 @@ class UnixReadPipeTransportTests(test_utils.TestCase):
self.assertIsNone(tr._loop) self.assertIsNone(tr._loop)
def test__call_connection_lost_with_err(self): def test__call_connection_lost_with_err(self):
tr = unix_events._UnixReadPipeTransport( tr = self.read_pipe_transport()
self.loop, self.pipe, self.protocol)
self.assertIsNotNone(tr._protocol) self.assertIsNotNone(tr._protocol)
self.assertIsNotNone(tr._loop) self.assertIsNotNone(tr._loop)
...@@ -496,31 +494,33 @@ class UnixWritePipeTransportTests(test_utils.TestCase): ...@@ -496,31 +494,33 @@ class UnixWritePipeTransportTests(test_utils.TestCase):
m_fstat.return_value = st m_fstat.return_value = st
self.addCleanup(fstat_patcher.stop) 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): def test_ctor(self):
tr = unix_events._UnixWritePipeTransport( tr = self.write_pipe_transport()
self.loop, self.pipe, self.protocol)
self.loop.assert_reader(5, tr._read_ready) self.loop.assert_reader(5, tr._read_ready)
test_utils.run_briefly(self.loop) test_utils.run_briefly(self.loop)
self.protocol.connection_made.assert_called_with(tr) self.protocol.connection_made.assert_called_with(tr)
def test_ctor_with_waiter(self): def test_ctor_with_waiter(self):
fut = asyncio.Future(loop=self.loop) fut = asyncio.Future(loop=self.loop)
tr = unix_events._UnixWritePipeTransport( tr = self.write_pipe_transport(waiter=fut)
self.loop, self.pipe, self.protocol, fut)
self.loop.assert_reader(5, tr._read_ready) self.loop.assert_reader(5, tr._read_ready)
test_utils.run_briefly(self.loop) test_utils.run_briefly(self.loop)
self.assertEqual(None, fut.result()) self.assertEqual(None, fut.result())
def test_can_write_eof(self): def test_can_write_eof(self):
tr = unix_events._UnixWritePipeTransport( tr = self.write_pipe_transport()
self.loop, self.pipe, self.protocol)
self.assertTrue(tr.can_write_eof()) self.assertTrue(tr.can_write_eof())
@mock.patch('os.write') @mock.patch('os.write')
def test_write(self, m_write): def test_write(self, m_write):
tr = unix_events._UnixWritePipeTransport( tr = self.write_pipe_transport()
self.loop, self.pipe, self.protocol)
m_write.return_value = 4 m_write.return_value = 4
tr.write(b'data') tr.write(b'data')
m_write.assert_called_with(5, b'data') m_write.assert_called_with(5, b'data')
...@@ -529,9 +529,7 @@ class UnixWritePipeTransportTests(test_utils.TestCase): ...@@ -529,9 +529,7 @@ class UnixWritePipeTransportTests(test_utils.TestCase):
@mock.patch('os.write') @mock.patch('os.write')
def test_write_no_data(self, m_write): def test_write_no_data(self, m_write):
tr = unix_events._UnixWritePipeTransport( tr = self.write_pipe_transport()
self.loop, self.pipe, self.protocol)
tr.write(b'') tr.write(b'')
self.assertFalse(m_write.called) self.assertFalse(m_write.called)
self.assertFalse(self.loop.writers) self.assertFalse(self.loop.writers)
...@@ -539,9 +537,7 @@ class UnixWritePipeTransportTests(test_utils.TestCase): ...@@ -539,9 +537,7 @@ class UnixWritePipeTransportTests(test_utils.TestCase):
@mock.patch('os.write') @mock.patch('os.write')
def test_write_partial(self, m_write): def test_write_partial(self, m_write):
tr = unix_events._UnixWritePipeTransport( tr = self.write_pipe_transport()
self.loop, self.pipe, self.protocol)
m_write.return_value = 2 m_write.return_value = 2
tr.write(b'data') tr.write(b'data')
m_write.assert_called_with(5, b'data') m_write.assert_called_with(5, b'data')
...@@ -550,9 +546,7 @@ class UnixWritePipeTransportTests(test_utils.TestCase): ...@@ -550,9 +546,7 @@ class UnixWritePipeTransportTests(test_utils.TestCase):
@mock.patch('os.write') @mock.patch('os.write')
def test_write_buffer(self, m_write): def test_write_buffer(self, m_write):
tr = unix_events._UnixWritePipeTransport( tr = self.write_pipe_transport()
self.loop, self.pipe, self.protocol)
self.loop.add_writer(5, tr._write_ready) self.loop.add_writer(5, tr._write_ready)
tr._buffer = [b'previous'] tr._buffer = [b'previous']
tr.write(b'data') tr.write(b'data')
...@@ -562,9 +556,7 @@ class UnixWritePipeTransportTests(test_utils.TestCase): ...@@ -562,9 +556,7 @@ class UnixWritePipeTransportTests(test_utils.TestCase):
@mock.patch('os.write') @mock.patch('os.write')
def test_write_again(self, m_write): def test_write_again(self, m_write):
tr = unix_events._UnixWritePipeTransport( tr = self.write_pipe_transport()
self.loop, self.pipe, self.protocol)
m_write.side_effect = BlockingIOError() m_write.side_effect = BlockingIOError()
tr.write(b'data') tr.write(b'data')
m_write.assert_called_with(5, b'data') m_write.assert_called_with(5, b'data')
...@@ -574,9 +566,7 @@ class UnixWritePipeTransportTests(test_utils.TestCase): ...@@ -574,9 +566,7 @@ class UnixWritePipeTransportTests(test_utils.TestCase):
@mock.patch('asyncio.unix_events.logger') @mock.patch('asyncio.unix_events.logger')
@mock.patch('os.write') @mock.patch('os.write')
def test_write_err(self, m_write, m_log): def test_write_err(self, m_write, m_log):
tr = unix_events._UnixWritePipeTransport( tr = self.write_pipe_transport()
self.loop, self.pipe, self.protocol)
err = OSError() err = OSError()
m_write.side_effect = err m_write.side_effect = err
tr._fatal_error = mock.Mock() tr._fatal_error = mock.Mock()
...@@ -602,8 +592,7 @@ class UnixWritePipeTransportTests(test_utils.TestCase): ...@@ -602,8 +592,7 @@ class UnixWritePipeTransportTests(test_utils.TestCase):
@mock.patch('os.write') @mock.patch('os.write')
def test_write_close(self, m_write): def test_write_close(self, m_write):
tr = unix_events._UnixWritePipeTransport( tr = self.write_pipe_transport()
self.loop, self.pipe, self.protocol)
tr._read_ready() # pipe was closed by peer tr._read_ready() # pipe was closed by peer
tr.write(b'data') tr.write(b'data')
...@@ -612,8 +601,7 @@ class UnixWritePipeTransportTests(test_utils.TestCase): ...@@ -612,8 +601,7 @@ class UnixWritePipeTransportTests(test_utils.TestCase):
self.assertEqual(tr._conn_lost, 2) self.assertEqual(tr._conn_lost, 2)
def test__read_ready(self): def test__read_ready(self):
tr = unix_events._UnixWritePipeTransport(self.loop, self.pipe, tr = self.write_pipe_transport()
self.protocol)
tr._read_ready() tr._read_ready()
self.assertFalse(self.loop.readers) self.assertFalse(self.loop.readers)
self.assertFalse(self.loop.writers) self.assertFalse(self.loop.writers)
...@@ -623,8 +611,7 @@ class UnixWritePipeTransportTests(test_utils.TestCase): ...@@ -623,8 +611,7 @@ class UnixWritePipeTransportTests(test_utils.TestCase):
@mock.patch('os.write') @mock.patch('os.write')
def test__write_ready(self, m_write): def test__write_ready(self, m_write):
tr = unix_events._UnixWritePipeTransport( tr = self.write_pipe_transport()
self.loop, self.pipe, self.protocol)
self.loop.add_writer(5, tr._write_ready) self.loop.add_writer(5, tr._write_ready)
tr._buffer = [b'da', b'ta'] tr._buffer = [b'da', b'ta']
m_write.return_value = 4 m_write.return_value = 4
...@@ -635,9 +622,7 @@ class UnixWritePipeTransportTests(test_utils.TestCase): ...@@ -635,9 +622,7 @@ class UnixWritePipeTransportTests(test_utils.TestCase):
@mock.patch('os.write') @mock.patch('os.write')
def test__write_ready_partial(self, m_write): def test__write_ready_partial(self, m_write):
tr = unix_events._UnixWritePipeTransport( tr = self.write_pipe_transport()
self.loop, self.pipe, self.protocol)
self.loop.add_writer(5, tr._write_ready) self.loop.add_writer(5, tr._write_ready)
tr._buffer = [b'da', b'ta'] tr._buffer = [b'da', b'ta']
m_write.return_value = 3 m_write.return_value = 3
...@@ -648,9 +633,7 @@ class UnixWritePipeTransportTests(test_utils.TestCase): ...@@ -648,9 +633,7 @@ class UnixWritePipeTransportTests(test_utils.TestCase):
@mock.patch('os.write') @mock.patch('os.write')
def test__write_ready_again(self, m_write): def test__write_ready_again(self, m_write):
tr = unix_events._UnixWritePipeTransport( tr = self.write_pipe_transport()
self.loop, self.pipe, self.protocol)
self.loop.add_writer(5, tr._write_ready) self.loop.add_writer(5, tr._write_ready)
tr._buffer = [b'da', b'ta'] tr._buffer = [b'da', b'ta']
m_write.side_effect = BlockingIOError() m_write.side_effect = BlockingIOError()
...@@ -661,9 +644,7 @@ class UnixWritePipeTransportTests(test_utils.TestCase): ...@@ -661,9 +644,7 @@ class UnixWritePipeTransportTests(test_utils.TestCase):
@mock.patch('os.write') @mock.patch('os.write')
def test__write_ready_empty(self, m_write): def test__write_ready_empty(self, m_write):
tr = unix_events._UnixWritePipeTransport( tr = self.write_pipe_transport()
self.loop, self.pipe, self.protocol)
self.loop.add_writer(5, tr._write_ready) self.loop.add_writer(5, tr._write_ready)
tr._buffer = [b'da', b'ta'] tr._buffer = [b'da', b'ta']
m_write.return_value = 0 m_write.return_value = 0
...@@ -675,9 +656,7 @@ class UnixWritePipeTransportTests(test_utils.TestCase): ...@@ -675,9 +656,7 @@ class UnixWritePipeTransportTests(test_utils.TestCase):
@mock.patch('asyncio.log.logger.error') @mock.patch('asyncio.log.logger.error')
@mock.patch('os.write') @mock.patch('os.write')
def test__write_ready_err(self, m_write, m_logexc): def test__write_ready_err(self, m_write, m_logexc):
tr = unix_events._UnixWritePipeTransport( tr = self.write_pipe_transport()
self.loop, self.pipe, self.protocol)
self.loop.add_writer(5, tr._write_ready) self.loop.add_writer(5, tr._write_ready)
tr._buffer = [b'da', b'ta'] tr._buffer = [b'da', b'ta']
m_write.side_effect = err = OSError() m_write.side_effect = err = OSError()
...@@ -698,9 +677,7 @@ class UnixWritePipeTransportTests(test_utils.TestCase): ...@@ -698,9 +677,7 @@ class UnixWritePipeTransportTests(test_utils.TestCase):
@mock.patch('os.write') @mock.patch('os.write')
def test__write_ready_closing(self, m_write): def test__write_ready_closing(self, m_write):
tr = unix_events._UnixWritePipeTransport( tr = self.write_pipe_transport()
self.loop, self.pipe, self.protocol)
self.loop.add_writer(5, tr._write_ready) self.loop.add_writer(5, tr._write_ready)
tr._closing = True tr._closing = True
tr._buffer = [b'da', b'ta'] tr._buffer = [b'da', b'ta']
...@@ -715,9 +692,7 @@ class UnixWritePipeTransportTests(test_utils.TestCase): ...@@ -715,9 +692,7 @@ class UnixWritePipeTransportTests(test_utils.TestCase):
@mock.patch('os.write') @mock.patch('os.write')
def test_abort(self, m_write): def test_abort(self, m_write):
tr = unix_events._UnixWritePipeTransport( tr = self.write_pipe_transport()
self.loop, self.pipe, self.protocol)
self.loop.add_writer(5, tr._write_ready) self.loop.add_writer(5, tr._write_ready)
self.loop.add_reader(5, tr._read_ready) self.loop.add_reader(5, tr._read_ready)
tr._buffer = [b'da', b'ta'] tr._buffer = [b'da', b'ta']
...@@ -731,8 +706,7 @@ class UnixWritePipeTransportTests(test_utils.TestCase): ...@@ -731,8 +706,7 @@ class UnixWritePipeTransportTests(test_utils.TestCase):
self.protocol.connection_lost.assert_called_with(None) self.protocol.connection_lost.assert_called_with(None)
def test__call_connection_lost(self): def test__call_connection_lost(self):
tr = unix_events._UnixWritePipeTransport( tr = self.write_pipe_transport()
self.loop, self.pipe, self.protocol)
self.assertIsNotNone(tr._protocol) self.assertIsNotNone(tr._protocol)
self.assertIsNotNone(tr._loop) self.assertIsNotNone(tr._loop)
...@@ -745,8 +719,7 @@ class UnixWritePipeTransportTests(test_utils.TestCase): ...@@ -745,8 +719,7 @@ class UnixWritePipeTransportTests(test_utils.TestCase):
self.assertIsNone(tr._loop) self.assertIsNone(tr._loop)
def test__call_connection_lost_with_err(self): def test__call_connection_lost_with_err(self):
tr = unix_events._UnixWritePipeTransport( tr = self.write_pipe_transport()
self.loop, self.pipe, self.protocol)
self.assertIsNotNone(tr._protocol) self.assertIsNotNone(tr._protocol)
self.assertIsNotNone(tr._loop) self.assertIsNotNone(tr._loop)
...@@ -759,9 +732,7 @@ class UnixWritePipeTransportTests(test_utils.TestCase): ...@@ -759,9 +732,7 @@ class UnixWritePipeTransportTests(test_utils.TestCase):
self.assertIsNone(tr._loop) self.assertIsNone(tr._loop)
def test_close(self): def test_close(self):
tr = unix_events._UnixWritePipeTransport( tr = self.write_pipe_transport()
self.loop, self.pipe, self.protocol)
tr.write_eof = mock.Mock() tr.write_eof = mock.Mock()
tr.close() tr.close()
tr.write_eof.assert_called_with() tr.write_eof.assert_called_with()
...@@ -770,18 +741,14 @@ class UnixWritePipeTransportTests(test_utils.TestCase): ...@@ -770,18 +741,14 @@ class UnixWritePipeTransportTests(test_utils.TestCase):
tr.close() tr.close()
def test_close_closing(self): def test_close_closing(self):
tr = unix_events._UnixWritePipeTransport( tr = self.write_pipe_transport()
self.loop, self.pipe, self.protocol)
tr.write_eof = mock.Mock() tr.write_eof = mock.Mock()
tr._closing = True tr._closing = True
tr.close() tr.close()
self.assertFalse(tr.write_eof.called) self.assertFalse(tr.write_eof.called)
def test_write_eof(self): def test_write_eof(self):
tr = unix_events._UnixWritePipeTransport( tr = self.write_pipe_transport()
self.loop, self.pipe, self.protocol)
tr.write_eof() tr.write_eof()
self.assertTrue(tr._closing) self.assertTrue(tr._closing)
self.assertFalse(self.loop.readers) self.assertFalse(self.loop.readers)
...@@ -789,8 +756,7 @@ class UnixWritePipeTransportTests(test_utils.TestCase): ...@@ -789,8 +756,7 @@ class UnixWritePipeTransportTests(test_utils.TestCase):
self.protocol.connection_lost.assert_called_with(None) self.protocol.connection_lost.assert_called_with(None)
def test_write_eof_pending(self): def test_write_eof_pending(self):
tr = unix_events._UnixWritePipeTransport( tr = self.write_pipe_transport()
self.loop, self.pipe, self.protocol)
tr._buffer = [b'data'] tr._buffer = [b'data']
tr.write_eof() tr.write_eof()
self.assertTrue(tr._closing) self.assertTrue(tr._closing)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment