2013-10-17 17:40:50 -03:00
|
|
|
"""Tests for proactor_events.py"""
|
|
|
|
|
2018-02-25 12:32:14 -04:00
|
|
|
import io
|
2013-10-17 17:40:50 -03:00
|
|
|
import socket
|
|
|
|
import unittest
|
2018-02-25 12:32:14 -04:00
|
|
|
import sys
|
2014-02-26 05:25:02 -04:00
|
|
|
from unittest import mock
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
import asyncio
|
2014-01-25 17:22:18 -04:00
|
|
|
from asyncio.proactor_events import BaseProactorEventLoop
|
2013-10-17 17:40:50 -03:00
|
|
|
from asyncio.proactor_events import _ProactorSocketTransport
|
|
|
|
from asyncio.proactor_events import _ProactorWritePipeTransport
|
|
|
|
from asyncio.proactor_events import _ProactorDuplexPipeTransport
|
2019-05-28 06:52:15 -03:00
|
|
|
from asyncio.proactor_events import _ProactorDatagramTransport
|
2020-08-07 12:18:38 -03:00
|
|
|
from test.support import os_helper
|
2020-04-25 04:06:29 -03:00
|
|
|
from test.support import socket_helper
|
2017-12-11 11:04:40 -04:00
|
|
|
from test.test_asyncio import utils as test_utils
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
|
2018-06-02 00:34:09 -03:00
|
|
|
def tearDownModule():
|
|
|
|
asyncio.set_event_loop_policy(None)
|
|
|
|
|
|
|
|
|
2015-01-15 08:18:32 -04:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2014-06-17 20:36:32 -03:00
|
|
|
class ProactorSocketTransportTests(test_utils.TestCase):
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def setUp(self):
|
2016-11-04 15:29:28 -03:00
|
|
|
super().setUp()
|
2014-06-17 20:36:32 -03:00
|
|
|
self.loop = self.new_test_loop()
|
2015-01-15 08:17:34 -04:00
|
|
|
self.addCleanup(self.loop.close)
|
2014-02-26 05:25:02 -04:00
|
|
|
self.proactor = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
self.loop._proactor = self.proactor
|
|
|
|
self.protocol = test_utils.make_test_protocol(asyncio.Protocol)
|
2014-02-26 05:25:02 -04:00
|
|
|
self.sock = mock.Mock(socket.socket)
|
2020-07-14 16:41:24 -03:00
|
|
|
self.buffer_size = 65536
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2015-01-15 08:18:32 -04:00
|
|
|
def socket_transport(self, waiter=None):
|
|
|
|
transport = _ProactorSocketTransport(self.loop, self.sock,
|
|
|
|
self.protocol, waiter=waiter)
|
|
|
|
self.addCleanup(close_transport, transport)
|
|
|
|
return transport
|
|
|
|
|
2013-10-17 17:40:50 -03:00
|
|
|
def test_ctor(self):
|
2019-09-11 10:07:37 -03:00
|
|
|
fut = self.loop.create_future()
|
2015-01-15 08:18:32 -04:00
|
|
|
tr = self.socket_transport(waiter=fut)
|
2013-10-17 17:40:50 -03:00
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
self.assertIsNone(fut.result())
|
|
|
|
self.protocol.connection_made(tr)
|
2020-07-14 16:41:24 -03:00
|
|
|
self.proactor.recv_into.assert_called_with(self.sock, bytearray(self.buffer_size))
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def test_loop_reading(self):
|
2015-01-15 08:18:32 -04:00
|
|
|
tr = self.socket_transport()
|
2013-10-17 17:40:50 -03:00
|
|
|
tr._loop_reading()
|
2020-07-14 16:41:24 -03:00
|
|
|
self.loop._proactor.recv_into.assert_called_with(self.sock, bytearray(self.buffer_size))
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertFalse(self.protocol.data_received.called)
|
|
|
|
self.assertFalse(self.protocol.eof_received.called)
|
|
|
|
|
|
|
|
def test_loop_reading_data(self):
|
2020-07-14 16:41:24 -03:00
|
|
|
buf = b'data'
|
2019-09-11 10:07:37 -03:00
|
|
|
res = self.loop.create_future()
|
2020-07-14 16:41:24 -03:00
|
|
|
res.set_result(len(buf))
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2015-01-15 08:18:32 -04:00
|
|
|
tr = self.socket_transport()
|
2013-10-17 17:40:50 -03:00
|
|
|
tr._read_fut = res
|
2020-07-14 16:41:24 -03:00
|
|
|
tr._data[:len(buf)] = buf
|
2013-10-17 17:40:50 -03:00
|
|
|
tr._loop_reading(res)
|
2020-07-14 16:41:24 -03:00
|
|
|
called_buf = bytearray(self.buffer_size)
|
|
|
|
called_buf[:len(buf)] = buf
|
|
|
|
self.loop._proactor.recv_into.assert_called_with(self.sock, called_buf)
|
2022-12-10 19:07:02 -04:00
|
|
|
self.protocol.data_received.assert_called_with(buf)
|
|
|
|
# assert_called_with maps bytearray and bytes to the same thing so check manually
|
|
|
|
# regression test for https://github.com/python/cpython/issues/99941
|
|
|
|
self.assertIsInstance(self.protocol.data_received.call_args.args[0], bytes)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2021-12-26 07:13:14 -04:00
|
|
|
@unittest.skipIf(sys.flags.optimize, "Assertions are disabled in optimized mode")
|
2013-10-17 17:40:50 -03:00
|
|
|
def test_loop_reading_no_data(self):
|
2019-09-11 10:07:37 -03:00
|
|
|
res = self.loop.create_future()
|
2020-07-14 16:41:24 -03:00
|
|
|
res.set_result(0)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2015-01-15 08:18:32 -04:00
|
|
|
tr = self.socket_transport()
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertRaises(AssertionError, tr._loop_reading, res)
|
|
|
|
|
2014-02-26 05:25:02 -04:00
|
|
|
tr.close = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
tr._read_fut = res
|
|
|
|
tr._loop_reading(res)
|
2020-07-14 16:41:24 -03:00
|
|
|
self.assertFalse(self.loop._proactor.recv_into.called)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertTrue(self.protocol.eof_received.called)
|
|
|
|
self.assertTrue(tr.close.called)
|
|
|
|
|
|
|
|
def test_loop_reading_aborted(self):
|
2020-07-14 16:41:24 -03:00
|
|
|
err = self.loop._proactor.recv_into.side_effect = ConnectionAbortedError()
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2015-01-15 08:18:32 -04:00
|
|
|
tr = self.socket_transport()
|
2014-02-26 05:25:02 -04:00
|
|
|
tr._fatal_error = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
tr._loop_reading()
|
2014-02-18 20:40:41 -04:00
|
|
|
tr._fatal_error.assert_called_with(
|
|
|
|
err,
|
|
|
|
'Fatal read error on pipe transport')
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def test_loop_reading_aborted_closing(self):
|
2020-07-14 16:41:24 -03:00
|
|
|
self.loop._proactor.recv_into.side_effect = ConnectionAbortedError()
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2015-01-15 08:18:32 -04:00
|
|
|
tr = self.socket_transport()
|
2013-10-17 17:40:50 -03:00
|
|
|
tr._closing = True
|
2014-02-26 05:25:02 -04:00
|
|
|
tr._fatal_error = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
tr._loop_reading()
|
|
|
|
self.assertFalse(tr._fatal_error.called)
|
|
|
|
|
|
|
|
def test_loop_reading_aborted_is_fatal(self):
|
2020-07-14 16:41:24 -03:00
|
|
|
self.loop._proactor.recv_into.side_effect = ConnectionAbortedError()
|
2015-01-15 08:18:32 -04:00
|
|
|
tr = self.socket_transport()
|
2013-10-17 17:40:50 -03:00
|
|
|
tr._closing = False
|
2014-02-26 05:25:02 -04:00
|
|
|
tr._fatal_error = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
tr._loop_reading()
|
|
|
|
self.assertTrue(tr._fatal_error.called)
|
|
|
|
|
|
|
|
def test_loop_reading_conn_reset_lost(self):
|
2020-07-14 16:41:24 -03:00
|
|
|
err = self.loop._proactor.recv_into.side_effect = ConnectionResetError()
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2015-01-15 08:18:32 -04:00
|
|
|
tr = self.socket_transport()
|
2013-10-17 17:40:50 -03:00
|
|
|
tr._closing = False
|
2014-02-26 05:25:02 -04:00
|
|
|
tr._fatal_error = mock.Mock()
|
|
|
|
tr._force_close = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
tr._loop_reading()
|
|
|
|
self.assertFalse(tr._fatal_error.called)
|
|
|
|
tr._force_close.assert_called_with(err)
|
|
|
|
|
|
|
|
def test_loop_reading_exception(self):
|
2020-07-14 16:41:24 -03:00
|
|
|
err = self.loop._proactor.recv_into.side_effect = (OSError())
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2015-01-15 08:18:32 -04:00
|
|
|
tr = self.socket_transport()
|
2014-02-26 05:25:02 -04:00
|
|
|
tr._fatal_error = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
tr._loop_reading()
|
2014-02-18 20:40:41 -04:00
|
|
|
tr._fatal_error.assert_called_with(
|
|
|
|
err,
|
|
|
|
'Fatal read error on pipe transport')
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def test_write(self):
|
2015-01-15 08:18:32 -04:00
|
|
|
tr = self.socket_transport()
|
2014-02-26 05:25:02 -04:00
|
|
|
tr._loop_writing = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
tr.write(b'data')
|
2013-12-04 16:12:07 -04:00
|
|
|
self.assertEqual(tr._buffer, None)
|
|
|
|
tr._loop_writing.assert_called_with(data=b'data')
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def test_write_no_data(self):
|
2015-01-15 08:18:32 -04:00
|
|
|
tr = self.socket_transport()
|
2013-10-17 17:40:50 -03:00
|
|
|
tr.write(b'')
|
|
|
|
self.assertFalse(tr._buffer)
|
|
|
|
|
|
|
|
def test_write_more(self):
|
2015-01-15 08:18:32 -04:00
|
|
|
tr = self.socket_transport()
|
2014-02-26 05:25:02 -04:00
|
|
|
tr._write_fut = mock.Mock()
|
|
|
|
tr._loop_writing = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
tr.write(b'data')
|
2013-12-04 16:12:07 -04:00
|
|
|
self.assertEqual(tr._buffer, b'data')
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertFalse(tr._loop_writing.called)
|
|
|
|
|
|
|
|
def test_loop_writing(self):
|
2015-01-15 08:18:32 -04:00
|
|
|
tr = self.socket_transport()
|
2013-12-04 16:12:07 -04:00
|
|
|
tr._buffer = bytearray(b'data')
|
2013-10-17 17:40:50 -03:00
|
|
|
tr._loop_writing()
|
|
|
|
self.loop._proactor.send.assert_called_with(self.sock, b'data')
|
|
|
|
self.loop._proactor.send.return_value.add_done_callback.\
|
|
|
|
assert_called_with(tr._loop_writing)
|
|
|
|
|
2014-02-26 05:25:02 -04:00
|
|
|
@mock.patch('asyncio.proactor_events.logger')
|
2013-10-17 17:40:50 -03:00
|
|
|
def test_loop_writing_err(self, m_log):
|
|
|
|
err = self.loop._proactor.send.side_effect = OSError()
|
2015-01-15 08:18:32 -04:00
|
|
|
tr = self.socket_transport()
|
2014-02-26 05:25:02 -04:00
|
|
|
tr._fatal_error = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
tr._buffer = [b'da', b'ta']
|
|
|
|
tr._loop_writing()
|
2014-02-18 20:40:41 -04:00
|
|
|
tr._fatal_error.assert_called_with(
|
|
|
|
err,
|
|
|
|
'Fatal write error on pipe transport')
|
2013-10-17 17:40:50 -03:00
|
|
|
tr._conn_lost = 1
|
|
|
|
|
|
|
|
tr.write(b'data')
|
|
|
|
tr.write(b'data')
|
|
|
|
tr.write(b'data')
|
|
|
|
tr.write(b'data')
|
|
|
|
tr.write(b'data')
|
2013-12-04 16:12:07 -04:00
|
|
|
self.assertEqual(tr._buffer, None)
|
2013-10-17 17:40:50 -03:00
|
|
|
m_log.warning.assert_called_with('socket.send() raised exception.')
|
|
|
|
|
|
|
|
def test_loop_writing_stop(self):
|
2019-09-11 10:07:37 -03:00
|
|
|
fut = self.loop.create_future()
|
2013-10-17 17:40:50 -03:00
|
|
|
fut.set_result(b'data')
|
|
|
|
|
2015-01-15 08:18:32 -04:00
|
|
|
tr = self.socket_transport()
|
2013-10-17 17:40:50 -03:00
|
|
|
tr._write_fut = fut
|
|
|
|
tr._loop_writing(fut)
|
|
|
|
self.assertIsNone(tr._write_fut)
|
|
|
|
|
|
|
|
def test_loop_writing_closing(self):
|
2019-09-11 10:07:37 -03:00
|
|
|
fut = self.loop.create_future()
|
2013-10-17 17:40:50 -03:00
|
|
|
fut.set_result(1)
|
|
|
|
|
2015-01-15 08:18:32 -04:00
|
|
|
tr = self.socket_transport()
|
2013-10-17 17:40:50 -03:00
|
|
|
tr._write_fut = fut
|
|
|
|
tr.close()
|
|
|
|
tr._loop_writing(fut)
|
|
|
|
self.assertIsNone(tr._write_fut)
|
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
self.protocol.connection_lost.assert_called_with(None)
|
|
|
|
|
|
|
|
def test_abort(self):
|
2015-01-15 08:18:32 -04:00
|
|
|
tr = self.socket_transport()
|
2014-02-26 05:25:02 -04:00
|
|
|
tr._force_close = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
tr.abort()
|
|
|
|
tr._force_close.assert_called_with(None)
|
|
|
|
|
|
|
|
def test_close(self):
|
2015-01-15 08:18:32 -04:00
|
|
|
tr = self.socket_transport()
|
2013-10-17 17:40:50 -03:00
|
|
|
tr.close()
|
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
self.protocol.connection_lost.assert_called_with(None)
|
2015-11-16 13:43:21 -04:00
|
|
|
self.assertTrue(tr.is_closing())
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertEqual(tr._conn_lost, 1)
|
|
|
|
|
|
|
|
self.protocol.connection_lost.reset_mock()
|
|
|
|
tr.close()
|
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
self.assertFalse(self.protocol.connection_lost.called)
|
|
|
|
|
|
|
|
def test_close_write_fut(self):
|
2015-01-15 08:18:32 -04:00
|
|
|
tr = self.socket_transport()
|
2014-02-26 05:25:02 -04:00
|
|
|
tr._write_fut = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
tr.close()
|
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
self.assertFalse(self.protocol.connection_lost.called)
|
|
|
|
|
|
|
|
def test_close_buffer(self):
|
2015-01-15 08:18:32 -04:00
|
|
|
tr = self.socket_transport()
|
2013-10-17 17:40:50 -03:00
|
|
|
tr._buffer = [b'data']
|
|
|
|
tr.close()
|
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
self.assertFalse(self.protocol.connection_lost.called)
|
|
|
|
|
2022-03-15 11:59:02 -03:00
|
|
|
def test_close_invalid_sockobj(self):
|
|
|
|
tr = self.socket_transport()
|
|
|
|
self.sock.fileno.return_value = -1
|
|
|
|
tr.close()
|
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
self.protocol.connection_lost.assert_called_with(None)
|
|
|
|
self.assertFalse(self.sock.shutdown.called)
|
|
|
|
|
2014-02-26 05:25:02 -04:00
|
|
|
@mock.patch('asyncio.base_events.logger')
|
2013-10-17 17:40:50 -03:00
|
|
|
def test_fatal_error(self, m_logging):
|
2015-01-15 08:18:32 -04:00
|
|
|
tr = self.socket_transport()
|
2014-02-26 05:25:02 -04:00
|
|
|
tr._force_close = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
tr._fatal_error(None)
|
|
|
|
self.assertTrue(tr._force_close.called)
|
2014-02-18 19:02:19 -04:00
|
|
|
self.assertTrue(m_logging.error.called)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def test_force_close(self):
|
2015-01-15 08:18:32 -04:00
|
|
|
tr = self.socket_transport()
|
2013-10-17 17:40:50 -03:00
|
|
|
tr._buffer = [b'data']
|
2014-02-26 05:25:02 -04:00
|
|
|
read_fut = tr._read_fut = mock.Mock()
|
|
|
|
write_fut = tr._write_fut = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
tr._force_close(None)
|
|
|
|
|
|
|
|
read_fut.cancel.assert_called_with()
|
|
|
|
write_fut.cancel.assert_called_with()
|
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
self.protocol.connection_lost.assert_called_with(None)
|
2013-12-04 16:12:07 -04:00
|
|
|
self.assertEqual(None, tr._buffer)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertEqual(tr._conn_lost, 1)
|
|
|
|
|
2018-07-30 15:58:50 -03:00
|
|
|
def test_loop_writing_force_close(self):
|
|
|
|
exc_handler = mock.Mock()
|
|
|
|
self.loop.set_exception_handler(exc_handler)
|
2019-09-11 10:07:37 -03:00
|
|
|
fut = self.loop.create_future()
|
2018-07-30 15:58:50 -03:00
|
|
|
fut.set_result(1)
|
|
|
|
self.proactor.send.return_value = fut
|
|
|
|
|
|
|
|
tr = self.socket_transport()
|
|
|
|
tr.write(b'data')
|
|
|
|
tr._force_close(None)
|
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
exc_handler.assert_not_called()
|
|
|
|
|
2013-10-17 17:40:50 -03:00
|
|
|
def test_force_close_idempotent(self):
|
2015-01-15 08:18:32 -04:00
|
|
|
tr = self.socket_transport()
|
2013-10-17 17:40:50 -03:00
|
|
|
tr._closing = True
|
|
|
|
tr._force_close(None)
|
|
|
|
test_utils.run_briefly(self.loop)
|
2022-10-24 17:21:42 -03:00
|
|
|
# See https://github.com/python/cpython/issues/89237
|
|
|
|
# `protocol.connection_lost` should be called even if
|
|
|
|
# the transport was closed forcefully otherwise
|
|
|
|
# the resources held by protocol will never be freed
|
|
|
|
# and waiters will never be notified leading to hang.
|
|
|
|
self.assertTrue(self.protocol.connection_lost.called)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2022-10-27 00:07:31 -03:00
|
|
|
def test_force_close_protocol_connection_lost_once(self):
|
|
|
|
tr = self.socket_transport()
|
|
|
|
self.assertFalse(self.protocol.connection_lost.called)
|
|
|
|
tr._closing = True
|
|
|
|
# Calling _force_close twice should not call
|
|
|
|
# protocol.connection_lost twice
|
|
|
|
tr._force_close(None)
|
|
|
|
tr._force_close(None)
|
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
self.assertEqual(1, self.protocol.connection_lost.call_count)
|
|
|
|
|
|
|
|
def test_close_protocol_connection_lost_once(self):
|
|
|
|
tr = self.socket_transport()
|
|
|
|
self.assertFalse(self.protocol.connection_lost.called)
|
|
|
|
# Calling close twice should not call
|
|
|
|
# protocol.connection_lost twice
|
|
|
|
tr.close()
|
|
|
|
tr.close()
|
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
self.assertEqual(1, self.protocol.connection_lost.call_count)
|
|
|
|
|
2013-10-17 17:40:50 -03:00
|
|
|
def test_fatal_error_2(self):
|
2015-01-15 08:18:32 -04:00
|
|
|
tr = self.socket_transport()
|
2013-10-17 17:40:50 -03:00
|
|
|
tr._buffer = [b'data']
|
|
|
|
tr._force_close(None)
|
|
|
|
|
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
self.protocol.connection_lost.assert_called_with(None)
|
2013-12-04 16:12:07 -04:00
|
|
|
self.assertEqual(None, tr._buffer)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def test_call_connection_lost(self):
|
2015-01-15 08:18:32 -04:00
|
|
|
tr = self.socket_transport()
|
2013-10-17 17:40:50 -03:00
|
|
|
tr._call_connection_lost(None)
|
|
|
|
self.assertTrue(self.protocol.connection_lost.called)
|
|
|
|
self.assertTrue(self.sock.close.called)
|
|
|
|
|
|
|
|
def test_write_eof(self):
|
2015-01-15 08:18:32 -04:00
|
|
|
tr = self.socket_transport()
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertTrue(tr.can_write_eof())
|
|
|
|
tr.write_eof()
|
|
|
|
self.sock.shutdown.assert_called_with(socket.SHUT_WR)
|
|
|
|
tr.write_eof()
|
|
|
|
self.assertEqual(self.sock.shutdown.call_count, 1)
|
|
|
|
tr.close()
|
|
|
|
|
|
|
|
def test_write_eof_buffer(self):
|
2015-01-15 08:18:32 -04:00
|
|
|
tr = self.socket_transport()
|
2019-09-11 10:07:37 -03:00
|
|
|
f = self.loop.create_future()
|
2013-10-17 17:40:50 -03:00
|
|
|
tr._loop._proactor.send.return_value = f
|
|
|
|
tr.write(b'data')
|
|
|
|
tr.write_eof()
|
|
|
|
self.assertTrue(tr._eof_written)
|
|
|
|
self.assertFalse(self.sock.shutdown.called)
|
|
|
|
tr._loop._proactor.send.assert_called_with(self.sock, b'data')
|
|
|
|
f.set_result(4)
|
|
|
|
self.loop._run_once()
|
|
|
|
self.sock.shutdown.assert_called_with(socket.SHUT_WR)
|
|
|
|
tr.close()
|
|
|
|
|
|
|
|
def test_write_eof_write_pipe(self):
|
|
|
|
tr = _ProactorWritePipeTransport(
|
|
|
|
self.loop, self.sock, self.protocol)
|
|
|
|
self.assertTrue(tr.can_write_eof())
|
|
|
|
tr.write_eof()
|
2015-11-16 13:43:21 -04:00
|
|
|
self.assertTrue(tr.is_closing())
|
2013-10-17 17:40:50 -03:00
|
|
|
self.loop._run_once()
|
|
|
|
self.assertTrue(self.sock.close.called)
|
|
|
|
tr.close()
|
|
|
|
|
|
|
|
def test_write_eof_buffer_write_pipe(self):
|
|
|
|
tr = _ProactorWritePipeTransport(self.loop, self.sock, self.protocol)
|
2019-09-11 10:07:37 -03:00
|
|
|
f = self.loop.create_future()
|
2013-10-17 17:40:50 -03:00
|
|
|
tr._loop._proactor.send.return_value = f
|
|
|
|
tr.write(b'data')
|
|
|
|
tr.write_eof()
|
2015-11-16 13:43:21 -04:00
|
|
|
self.assertTrue(tr.is_closing())
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertFalse(self.sock.shutdown.called)
|
|
|
|
tr._loop._proactor.send.assert_called_with(self.sock, b'data')
|
|
|
|
f.set_result(4)
|
|
|
|
self.loop._run_once()
|
|
|
|
self.loop._run_once()
|
|
|
|
self.assertTrue(self.sock.close.called)
|
|
|
|
tr.close()
|
|
|
|
|
|
|
|
def test_write_eof_duplex_pipe(self):
|
|
|
|
tr = _ProactorDuplexPipeTransport(
|
|
|
|
self.loop, self.sock, self.protocol)
|
|
|
|
self.assertFalse(tr.can_write_eof())
|
|
|
|
with self.assertRaises(NotImplementedError):
|
|
|
|
tr.write_eof()
|
2015-01-15 08:18:32 -04:00
|
|
|
close_transport(tr)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2013-10-18 11:58:20 -03:00
|
|
|
def test_pause_resume_reading(self):
|
2015-01-15 08:18:32 -04:00
|
|
|
tr = self.socket_transport()
|
2020-07-14 16:41:24 -03:00
|
|
|
index = 0
|
|
|
|
msgs = [b'data1', b'data2', b'data3', b'data4', b'data5', b'']
|
|
|
|
reversed_msgs = list(reversed(msgs))
|
|
|
|
|
|
|
|
def recv_into(sock, data):
|
2019-09-11 10:07:37 -03:00
|
|
|
f = self.loop.create_future()
|
2020-07-14 16:41:24 -03:00
|
|
|
msg = reversed_msgs.pop()
|
|
|
|
|
|
|
|
result = f.result
|
|
|
|
def monkey():
|
|
|
|
data[:len(msg)] = msg
|
|
|
|
return result()
|
|
|
|
f.result = monkey
|
|
|
|
|
|
|
|
f.set_result(len(msg))
|
|
|
|
return f
|
2017-12-18 18:03:23 -04:00
|
|
|
|
2020-07-14 16:41:24 -03:00
|
|
|
self.loop._proactor.recv_into.side_effect = recv_into
|
2013-10-17 17:40:50 -03:00
|
|
|
self.loop._run_once()
|
|
|
|
self.assertFalse(tr._paused)
|
2017-12-18 18:03:23 -04:00
|
|
|
self.assertTrue(tr.is_reading())
|
2020-07-14 16:41:24 -03:00
|
|
|
|
|
|
|
for msg in msgs[:2]:
|
|
|
|
self.loop._run_once()
|
|
|
|
self.protocol.data_received.assert_called_with(bytearray(msg))
|
2017-12-18 18:03:23 -04:00
|
|
|
|
|
|
|
tr.pause_reading()
|
2013-10-18 11:58:20 -03:00
|
|
|
tr.pause_reading()
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertTrue(tr._paused)
|
2017-12-18 18:03:23 -04:00
|
|
|
self.assertFalse(tr.is_reading())
|
2013-10-17 17:40:50 -03:00
|
|
|
for i in range(10):
|
|
|
|
self.loop._run_once()
|
2020-07-14 16:41:24 -03:00
|
|
|
self.protocol.data_received.assert_called_with(bytearray(msgs[1]))
|
2017-12-18 18:03:23 -04:00
|
|
|
|
|
|
|
tr.resume_reading()
|
2013-10-18 11:58:20 -03:00
|
|
|
tr.resume_reading()
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertFalse(tr._paused)
|
2017-12-18 18:03:23 -04:00
|
|
|
self.assertTrue(tr.is_reading())
|
2020-07-14 16:41:24 -03:00
|
|
|
|
|
|
|
for msg in msgs[2:4]:
|
|
|
|
self.loop._run_once()
|
|
|
|
self.protocol.data_received.assert_called_with(bytearray(msg))
|
2018-05-20 07:21:10 -03:00
|
|
|
|
|
|
|
tr.pause_reading()
|
|
|
|
tr.resume_reading()
|
|
|
|
self.loop.call_exception_handler = mock.Mock()
|
|
|
|
self.loop._run_once()
|
|
|
|
self.loop.call_exception_handler.assert_not_called()
|
2020-07-14 16:41:24 -03:00
|
|
|
self.protocol.data_received.assert_called_with(bytearray(msgs[4]))
|
2013-10-17 17:40:50 -03:00
|
|
|
tr.close()
|
|
|
|
|
2017-12-18 18:03:23 -04:00
|
|
|
self.assertFalse(tr.is_reading())
|
|
|
|
|
2023-04-27 07:03:29 -03:00
|
|
|
def test_pause_reading_connection_made(self):
|
|
|
|
tr = self.socket_transport()
|
|
|
|
self.protocol.connection_made.side_effect = lambda _: tr.pause_reading()
|
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
self.assertFalse(tr.is_reading())
|
|
|
|
self.loop.assert_no_reader(7)
|
|
|
|
|
|
|
|
tr.resume_reading()
|
|
|
|
self.assertTrue(tr.is_reading())
|
|
|
|
|
|
|
|
tr.close()
|
|
|
|
self.assertFalse(tr.is_reading())
|
|
|
|
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-12-11 17:23:19 -04:00
|
|
|
def pause_writing_transport(self, high):
|
2015-01-15 08:18:32 -04:00
|
|
|
tr = self.socket_transport()
|
2014-12-11 17:23:19 -04:00
|
|
|
tr.set_write_buffer_limits(high=high)
|
|
|
|
|
|
|
|
self.assertEqual(tr.get_write_buffer_size(), 0)
|
|
|
|
self.assertFalse(self.protocol.pause_writing.called)
|
|
|
|
self.assertFalse(self.protocol.resume_writing.called)
|
|
|
|
return tr
|
|
|
|
|
|
|
|
def test_pause_resume_writing(self):
|
|
|
|
tr = self.pause_writing_transport(high=4)
|
|
|
|
|
|
|
|
# write a large chunk, must pause writing
|
2019-09-11 10:07:37 -03:00
|
|
|
fut = self.loop.create_future()
|
2014-12-11 17:23:19 -04:00
|
|
|
self.loop._proactor.send.return_value = fut
|
|
|
|
tr.write(b'large data')
|
|
|
|
self.loop._run_once()
|
|
|
|
self.assertTrue(self.protocol.pause_writing.called)
|
|
|
|
|
|
|
|
# flush the buffer
|
|
|
|
fut.set_result(None)
|
|
|
|
self.loop._run_once()
|
|
|
|
self.assertEqual(tr.get_write_buffer_size(), 0)
|
|
|
|
self.assertTrue(self.protocol.resume_writing.called)
|
|
|
|
|
|
|
|
def test_pause_writing_2write(self):
|
|
|
|
tr = self.pause_writing_transport(high=4)
|
|
|
|
|
|
|
|
# first short write, the buffer is not full (3 <= 4)
|
2019-09-11 10:07:37 -03:00
|
|
|
fut1 = self.loop.create_future()
|
2014-12-11 17:23:19 -04:00
|
|
|
self.loop._proactor.send.return_value = fut1
|
|
|
|
tr.write(b'123')
|
|
|
|
self.loop._run_once()
|
|
|
|
self.assertEqual(tr.get_write_buffer_size(), 3)
|
|
|
|
self.assertFalse(self.protocol.pause_writing.called)
|
|
|
|
|
|
|
|
# fill the buffer, must pause writing (6 > 4)
|
|
|
|
tr.write(b'abc')
|
|
|
|
self.loop._run_once()
|
|
|
|
self.assertEqual(tr.get_write_buffer_size(), 6)
|
|
|
|
self.assertTrue(self.protocol.pause_writing.called)
|
|
|
|
|
|
|
|
def test_pause_writing_3write(self):
|
|
|
|
tr = self.pause_writing_transport(high=4)
|
|
|
|
|
|
|
|
# first short write, the buffer is not full (1 <= 4)
|
2019-09-11 10:07:37 -03:00
|
|
|
fut = self.loop.create_future()
|
2014-12-11 17:23:19 -04:00
|
|
|
self.loop._proactor.send.return_value = fut
|
|
|
|
tr.write(b'1')
|
|
|
|
self.loop._run_once()
|
|
|
|
self.assertEqual(tr.get_write_buffer_size(), 1)
|
|
|
|
self.assertFalse(self.protocol.pause_writing.called)
|
|
|
|
|
|
|
|
# second short write, the buffer is not full (3 <= 4)
|
|
|
|
tr.write(b'23')
|
|
|
|
self.loop._run_once()
|
|
|
|
self.assertEqual(tr.get_write_buffer_size(), 3)
|
|
|
|
self.assertFalse(self.protocol.pause_writing.called)
|
|
|
|
|
|
|
|
# fill the buffer, must pause writing (6 > 4)
|
|
|
|
tr.write(b'abc')
|
|
|
|
self.loop._run_once()
|
|
|
|
self.assertEqual(tr.get_write_buffer_size(), 6)
|
|
|
|
self.assertTrue(self.protocol.pause_writing.called)
|
|
|
|
|
|
|
|
def test_dont_pause_writing(self):
|
|
|
|
tr = self.pause_writing_transport(high=4)
|
|
|
|
|
2017-12-13 11:50:16 -04:00
|
|
|
# write a large chunk which completes immediately,
|
2014-12-11 17:23:19 -04:00
|
|
|
# it should not pause writing
|
2019-09-11 10:07:37 -03:00
|
|
|
fut = self.loop.create_future()
|
2014-12-11 17:23:19 -04:00
|
|
|
fut.set_result(None)
|
|
|
|
self.loop._proactor.send.return_value = fut
|
|
|
|
tr.write(b'very large data')
|
|
|
|
self.loop._run_once()
|
|
|
|
self.assertEqual(tr.get_write_buffer_size(), 0)
|
|
|
|
self.assertFalse(self.protocol.pause_writing.called)
|
|
|
|
|
|
|
|
|
2019-05-28 06:52:15 -03:00
|
|
|
class ProactorDatagramTransportTests(test_utils.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
super().setUp()
|
|
|
|
self.loop = self.new_test_loop()
|
|
|
|
self.proactor = mock.Mock()
|
|
|
|
self.loop._proactor = self.proactor
|
|
|
|
self.protocol = test_utils.make_test_protocol(asyncio.DatagramProtocol)
|
|
|
|
self.sock = mock.Mock(spec_set=socket.socket)
|
|
|
|
self.sock.fileno.return_value = 7
|
|
|
|
|
|
|
|
def datagram_transport(self, address=None):
|
|
|
|
self.sock.getpeername.side_effect = None if address else OSError
|
|
|
|
transport = _ProactorDatagramTransport(self.loop, self.sock,
|
|
|
|
self.protocol,
|
|
|
|
address=address)
|
|
|
|
self.addCleanup(close_transport, transport)
|
|
|
|
return transport
|
|
|
|
|
|
|
|
def test_sendto(self):
|
|
|
|
data = b'data'
|
|
|
|
transport = self.datagram_transport()
|
|
|
|
transport.sendto(data, ('0.0.0.0', 1234))
|
|
|
|
self.assertTrue(self.proactor.sendto.called)
|
|
|
|
self.proactor.sendto.assert_called_with(
|
|
|
|
self.sock, data, addr=('0.0.0.0', 1234))
|
|
|
|
|
|
|
|
def test_sendto_bytearray(self):
|
|
|
|
data = bytearray(b'data')
|
|
|
|
transport = self.datagram_transport()
|
|
|
|
transport.sendto(data, ('0.0.0.0', 1234))
|
|
|
|
self.assertTrue(self.proactor.sendto.called)
|
|
|
|
self.proactor.sendto.assert_called_with(
|
|
|
|
self.sock, b'data', addr=('0.0.0.0', 1234))
|
|
|
|
|
|
|
|
def test_sendto_memoryview(self):
|
|
|
|
data = memoryview(b'data')
|
|
|
|
transport = self.datagram_transport()
|
|
|
|
transport.sendto(data, ('0.0.0.0', 1234))
|
|
|
|
self.assertTrue(self.proactor.sendto.called)
|
|
|
|
self.proactor.sendto.assert_called_with(
|
|
|
|
self.sock, b'data', addr=('0.0.0.0', 1234))
|
|
|
|
|
|
|
|
def test_sendto_no_data(self):
|
|
|
|
transport = self.datagram_transport()
|
|
|
|
transport._buffer.append((b'data', ('0.0.0.0', 12345)))
|
|
|
|
transport.sendto(b'', ())
|
|
|
|
self.assertFalse(self.sock.sendto.called)
|
|
|
|
self.assertEqual(
|
|
|
|
[(b'data', ('0.0.0.0', 12345))], list(transport._buffer))
|
|
|
|
|
|
|
|
def test_sendto_buffer(self):
|
|
|
|
transport = self.datagram_transport()
|
|
|
|
transport._buffer.append((b'data1', ('0.0.0.0', 12345)))
|
|
|
|
transport._write_fut = object()
|
|
|
|
transport.sendto(b'data2', ('0.0.0.0', 12345))
|
|
|
|
self.assertFalse(self.proactor.sendto.called)
|
|
|
|
self.assertEqual(
|
|
|
|
[(b'data1', ('0.0.0.0', 12345)),
|
|
|
|
(b'data2', ('0.0.0.0', 12345))],
|
|
|
|
list(transport._buffer))
|
|
|
|
|
|
|
|
def test_sendto_buffer_bytearray(self):
|
|
|
|
data2 = bytearray(b'data2')
|
|
|
|
transport = self.datagram_transport()
|
|
|
|
transport._buffer.append((b'data1', ('0.0.0.0', 12345)))
|
|
|
|
transport._write_fut = object()
|
|
|
|
transport.sendto(data2, ('0.0.0.0', 12345))
|
|
|
|
self.assertFalse(self.proactor.sendto.called)
|
|
|
|
self.assertEqual(
|
|
|
|
[(b'data1', ('0.0.0.0', 12345)),
|
|
|
|
(b'data2', ('0.0.0.0', 12345))],
|
|
|
|
list(transport._buffer))
|
|
|
|
self.assertIsInstance(transport._buffer[1][0], bytes)
|
|
|
|
|
|
|
|
def test_sendto_buffer_memoryview(self):
|
|
|
|
data2 = memoryview(b'data2')
|
|
|
|
transport = self.datagram_transport()
|
|
|
|
transport._buffer.append((b'data1', ('0.0.0.0', 12345)))
|
|
|
|
transport._write_fut = object()
|
|
|
|
transport.sendto(data2, ('0.0.0.0', 12345))
|
|
|
|
self.assertFalse(self.proactor.sendto.called)
|
|
|
|
self.assertEqual(
|
|
|
|
[(b'data1', ('0.0.0.0', 12345)),
|
|
|
|
(b'data2', ('0.0.0.0', 12345))],
|
|
|
|
list(transport._buffer))
|
|
|
|
self.assertIsInstance(transport._buffer[1][0], bytes)
|
|
|
|
|
|
|
|
@mock.patch('asyncio.proactor_events.logger')
|
|
|
|
def test_sendto_exception(self, m_log):
|
|
|
|
data = b'data'
|
|
|
|
err = self.proactor.sendto.side_effect = RuntimeError()
|
|
|
|
|
|
|
|
transport = self.datagram_transport()
|
|
|
|
transport._fatal_error = mock.Mock()
|
|
|
|
transport.sendto(data, ())
|
|
|
|
|
|
|
|
self.assertTrue(transport._fatal_error.called)
|
|
|
|
transport._fatal_error.assert_called_with(
|
|
|
|
err,
|
|
|
|
'Fatal write error on datagram transport')
|
|
|
|
transport._conn_lost = 1
|
|
|
|
|
|
|
|
transport._address = ('123',)
|
|
|
|
transport.sendto(data)
|
|
|
|
transport.sendto(data)
|
|
|
|
transport.sendto(data)
|
|
|
|
transport.sendto(data)
|
|
|
|
transport.sendto(data)
|
|
|
|
m_log.warning.assert_called_with('socket.sendto() raised exception.')
|
|
|
|
|
|
|
|
def test_sendto_error_received(self):
|
|
|
|
data = b'data'
|
|
|
|
|
|
|
|
self.sock.sendto.side_effect = ConnectionRefusedError
|
|
|
|
|
|
|
|
transport = self.datagram_transport()
|
|
|
|
transport._fatal_error = mock.Mock()
|
|
|
|
transport.sendto(data, ())
|
|
|
|
|
|
|
|
self.assertEqual(transport._conn_lost, 0)
|
|
|
|
self.assertFalse(transport._fatal_error.called)
|
|
|
|
|
|
|
|
def test_sendto_error_received_connected(self):
|
|
|
|
data = b'data'
|
|
|
|
|
|
|
|
self.proactor.send.side_effect = ConnectionRefusedError
|
|
|
|
|
|
|
|
transport = self.datagram_transport(address=('0.0.0.0', 1))
|
|
|
|
transport._fatal_error = mock.Mock()
|
|
|
|
transport.sendto(data)
|
|
|
|
|
|
|
|
self.assertFalse(transport._fatal_error.called)
|
|
|
|
self.assertTrue(self.protocol.error_received.called)
|
|
|
|
|
|
|
|
def test_sendto_str(self):
|
|
|
|
transport = self.datagram_transport()
|
|
|
|
self.assertRaises(TypeError, transport.sendto, 'str', ())
|
|
|
|
|
|
|
|
def test_sendto_connected_addr(self):
|
|
|
|
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 = self.datagram_transport(address=(1,))
|
|
|
|
transport.close()
|
|
|
|
self.assertEqual(transport._conn_lost, 1)
|
|
|
|
transport.sendto(b'data', (1,))
|
|
|
|
self.assertEqual(transport._conn_lost, 2)
|
|
|
|
|
|
|
|
def test__loop_writing_closing(self):
|
|
|
|
transport = self.datagram_transport()
|
|
|
|
transport._closing = True
|
|
|
|
transport._loop_writing()
|
|
|
|
self.assertIsNone(transport._write_fut)
|
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
self.sock.close.assert_called_with()
|
|
|
|
self.protocol.connection_lost.assert_called_with(None)
|
|
|
|
|
|
|
|
def test__loop_writing_exception(self):
|
|
|
|
err = self.proactor.sendto.side_effect = RuntimeError()
|
|
|
|
|
|
|
|
transport = self.datagram_transport()
|
|
|
|
transport._fatal_error = mock.Mock()
|
|
|
|
transport._buffer.append((b'data', ()))
|
|
|
|
transport._loop_writing()
|
|
|
|
|
|
|
|
transport._fatal_error.assert_called_with(
|
|
|
|
err,
|
|
|
|
'Fatal write error on datagram transport')
|
|
|
|
|
|
|
|
def test__loop_writing_error_received(self):
|
|
|
|
self.proactor.sendto.side_effect = ConnectionRefusedError
|
|
|
|
|
|
|
|
transport = self.datagram_transport()
|
|
|
|
transport._fatal_error = mock.Mock()
|
|
|
|
transport._buffer.append((b'data', ()))
|
|
|
|
transport._loop_writing()
|
|
|
|
|
|
|
|
self.assertFalse(transport._fatal_error.called)
|
|
|
|
|
|
|
|
def test__loop_writing_error_received_connection(self):
|
|
|
|
self.proactor.send.side_effect = ConnectionRefusedError
|
|
|
|
|
|
|
|
transport = self.datagram_transport(address=('0.0.0.0', 1))
|
|
|
|
transport._fatal_error = mock.Mock()
|
|
|
|
transport._buffer.append((b'data', ()))
|
|
|
|
transport._loop_writing()
|
|
|
|
|
|
|
|
self.assertFalse(transport._fatal_error.called)
|
|
|
|
self.assertTrue(self.protocol.error_received.called)
|
|
|
|
|
|
|
|
@mock.patch('asyncio.base_events.logger.error')
|
|
|
|
def test_fatal_error_connected(self, m_exc):
|
|
|
|
transport = self.datagram_transport(address=('0.0.0.0', 1))
|
|
|
|
err = ConnectionRefusedError()
|
|
|
|
transport._fatal_error(err)
|
|
|
|
self.assertFalse(self.protocol.error_received.called)
|
|
|
|
m_exc.assert_not_called()
|
|
|
|
|
|
|
|
|
2014-06-17 20:36:32 -03:00
|
|
|
class BaseProactorEventLoopTests(test_utils.TestCase):
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def setUp(self):
|
2016-11-04 15:29:28 -03:00
|
|
|
super().setUp()
|
|
|
|
|
2015-12-16 20:31:17 -04:00
|
|
|
self.sock = test_utils.mock_nonblocking_socket()
|
2014-02-26 05:25:02 -04:00
|
|
|
self.proactor = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-02-26 05:25:02 -04:00
|
|
|
self.ssock, self.csock = mock.Mock(), mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2017-11-28 06:15:26 -04:00
|
|
|
with mock.patch('asyncio.proactor_events.socket.socketpair',
|
|
|
|
return_value=(self.ssock, self.csock)):
|
2018-12-18 17:56:17 -04:00
|
|
|
with mock.patch('signal.set_wakeup_fd'):
|
|
|
|
self.loop = BaseProactorEventLoop(self.proactor)
|
2015-01-15 08:18:32 -04:00
|
|
|
self.set_event_loop(self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2017-11-28 06:15:26 -04:00
|
|
|
@mock.patch('asyncio.proactor_events.socket.socketpair')
|
2018-12-18 17:56:17 -04:00
|
|
|
def test_ctor(self, socketpair):
|
2013-10-17 17:40:50 -03:00
|
|
|
ssock, csock = socketpair.return_value = (
|
2014-02-26 05:25:02 -04:00
|
|
|
mock.Mock(), mock.Mock())
|
2022-09-17 18:12:45 -03:00
|
|
|
with mock.patch('signal.set_wakeup_fd'):
|
|
|
|
loop = BaseProactorEventLoop(self.proactor)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertIs(loop._ssock, ssock)
|
|
|
|
self.assertIs(loop._csock, csock)
|
|
|
|
self.assertEqual(loop._internal_fds, 1)
|
2015-01-15 08:18:32 -04:00
|
|
|
loop.close()
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def test_close_self_pipe(self):
|
|
|
|
self.loop._close_self_pipe()
|
|
|
|
self.assertEqual(self.loop._internal_fds, 0)
|
|
|
|
self.assertTrue(self.ssock.close.called)
|
|
|
|
self.assertTrue(self.csock.close.called)
|
|
|
|
self.assertIsNone(self.loop._ssock)
|
|
|
|
self.assertIsNone(self.loop._csock)
|
|
|
|
|
2015-01-15 08:17:34 -04:00
|
|
|
# Don't call close(): _close_self_pipe() cannot be called twice
|
|
|
|
self.loop._closed = True
|
|
|
|
|
2013-10-17 17:40:50 -03:00
|
|
|
def test_close(self):
|
2014-02-26 05:25:02 -04:00
|
|
|
self.loop._close_self_pipe = mock.Mock()
|
2022-09-17 18:12:45 -03:00
|
|
|
self.loop.close()
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertTrue(self.loop._close_self_pipe.called)
|
|
|
|
self.assertTrue(self.proactor.close.called)
|
|
|
|
self.assertIsNone(self.loop._proactor)
|
|
|
|
|
|
|
|
self.loop._close_self_pipe.reset_mock()
|
|
|
|
self.loop.close()
|
|
|
|
self.assertFalse(self.loop._close_self_pipe.called)
|
|
|
|
|
|
|
|
def test_make_socket_transport(self):
|
2014-02-11 06:34:30 -04:00
|
|
|
tr = self.loop._make_socket_transport(self.sock, asyncio.Protocol())
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertIsInstance(tr, _ProactorSocketTransport)
|
2015-01-15 08:18:32 -04:00
|
|
|
close_transport(tr)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def test_loop_self_reading(self):
|
|
|
|
self.loop._loop_self_reading()
|
|
|
|
self.proactor.recv.assert_called_with(self.ssock, 4096)
|
|
|
|
self.proactor.recv.return_value.add_done_callback.assert_called_with(
|
|
|
|
self.loop._loop_self_reading)
|
|
|
|
|
|
|
|
def test_loop_self_reading_fut(self):
|
2014-02-26 05:25:02 -04:00
|
|
|
fut = mock.Mock()
|
2020-08-31 16:57:52 -03:00
|
|
|
self.loop._self_reading_future = fut
|
2013-10-17 17:40:50 -03:00
|
|
|
self.loop._loop_self_reading(fut)
|
|
|
|
self.assertTrue(fut.result.called)
|
|
|
|
self.proactor.recv.assert_called_with(self.ssock, 4096)
|
|
|
|
self.proactor.recv.return_value.add_done_callback.assert_called_with(
|
|
|
|
self.loop._loop_self_reading)
|
|
|
|
|
|
|
|
def test_loop_self_reading_exception(self):
|
2015-01-21 18:38:37 -04:00
|
|
|
self.loop.call_exception_handler = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
self.proactor.recv.side_effect = OSError()
|
2015-01-21 18:38:37 -04:00
|
|
|
self.loop._loop_self_reading()
|
|
|
|
self.assertTrue(self.loop.call_exception_handler.called)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def test_write_to_self(self):
|
|
|
|
self.loop._write_to_self()
|
2014-07-17 17:43:40 -03:00
|
|
|
self.csock.send.assert_called_with(b'\0')
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def test_process_events(self):
|
|
|
|
self.loop._process_events([])
|
|
|
|
|
2014-02-26 05:25:02 -04:00
|
|
|
@mock.patch('asyncio.base_events.logger')
|
2013-10-17 17:40:50 -03:00
|
|
|
def test_create_server(self, m_log):
|
2014-02-26 05:25:02 -04:00
|
|
|
pf = mock.Mock()
|
|
|
|
call_soon = self.loop.call_soon = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
self.loop._start_serving(pf, self.sock)
|
|
|
|
self.assertTrue(call_soon.called)
|
|
|
|
|
|
|
|
# callback
|
|
|
|
loop = call_soon.call_args[0][0]
|
|
|
|
loop()
|
|
|
|
self.proactor.accept.assert_called_with(self.sock)
|
|
|
|
|
|
|
|
# conn
|
2014-02-26 05:25:02 -04:00
|
|
|
fut = mock.Mock()
|
|
|
|
fut.result.return_value = (mock.Mock(), mock.Mock())
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-02-26 05:25:02 -04:00
|
|
|
make_tr = self.loop._make_socket_transport = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
loop(fut)
|
|
|
|
self.assertTrue(fut.result.called)
|
|
|
|
self.assertTrue(make_tr.called)
|
|
|
|
|
|
|
|
# exception
|
|
|
|
fut.result.side_effect = OSError()
|
|
|
|
loop(fut)
|
|
|
|
self.assertTrue(self.sock.close.called)
|
2014-02-18 19:02:19 -04:00
|
|
|
self.assertTrue(m_log.error.called)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def test_create_server_cancel(self):
|
2014-02-26 05:25:02 -04:00
|
|
|
pf = mock.Mock()
|
|
|
|
call_soon = self.loop.call_soon = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
self.loop._start_serving(pf, self.sock)
|
|
|
|
loop = call_soon.call_args[0][0]
|
|
|
|
|
|
|
|
# cancelled
|
2019-09-11 10:07:37 -03:00
|
|
|
fut = self.loop.create_future()
|
2013-10-17 17:40:50 -03:00
|
|
|
fut.cancel()
|
|
|
|
loop(fut)
|
|
|
|
self.assertTrue(self.sock.close.called)
|
|
|
|
|
|
|
|
def test_stop_serving(self):
|
2017-12-19 08:23:17 -04:00
|
|
|
sock1 = mock.Mock()
|
|
|
|
future1 = mock.Mock()
|
|
|
|
sock2 = mock.Mock()
|
|
|
|
future2 = mock.Mock()
|
|
|
|
self.loop._accept_futures = {
|
|
|
|
sock1.fileno(): future1,
|
|
|
|
sock2.fileno(): future2
|
|
|
|
}
|
|
|
|
|
|
|
|
self.loop._stop_serving(sock1)
|
|
|
|
self.assertTrue(sock1.close.called)
|
|
|
|
self.assertTrue(future1.cancel.called)
|
|
|
|
self.proactor._stop_serving.assert_called_with(sock1)
|
|
|
|
self.assertFalse(sock2.close.called)
|
|
|
|
self.assertFalse(future2.cancel.called)
|
2013-10-20 16:02:53 -03:00
|
|
|
|
2019-05-28 06:52:15 -03:00
|
|
|
def datagram_transport(self):
|
|
|
|
self.protocol = test_utils.make_test_protocol(asyncio.DatagramProtocol)
|
|
|
|
return self.loop._make_datagram_transport(self.sock, self.protocol)
|
|
|
|
|
|
|
|
def test_make_datagram_transport(self):
|
|
|
|
tr = self.datagram_transport()
|
|
|
|
self.assertIsInstance(tr, _ProactorDatagramTransport)
|
2022-02-22 19:39:24 -04:00
|
|
|
self.assertIsInstance(tr, asyncio.DatagramTransport)
|
2019-05-28 06:52:15 -03:00
|
|
|
close_transport(tr)
|
|
|
|
|
|
|
|
def test_datagram_loop_writing(self):
|
|
|
|
tr = self.datagram_transport()
|
|
|
|
tr._buffer.appendleft((b'data', ('127.0.0.1', 12068)))
|
|
|
|
tr._loop_writing()
|
|
|
|
self.loop._proactor.sendto.assert_called_with(self.sock, b'data', addr=('127.0.0.1', 12068))
|
|
|
|
self.loop._proactor.sendto.return_value.add_done_callback.\
|
|
|
|
assert_called_with(tr._loop_writing)
|
|
|
|
|
|
|
|
close_transport(tr)
|
|
|
|
|
|
|
|
def test_datagram_loop_reading(self):
|
|
|
|
tr = self.datagram_transport()
|
|
|
|
tr._loop_reading()
|
|
|
|
self.loop._proactor.recvfrom.assert_called_with(self.sock, 256 * 1024)
|
|
|
|
self.assertFalse(self.protocol.datagram_received.called)
|
|
|
|
self.assertFalse(self.protocol.error_received.called)
|
|
|
|
close_transport(tr)
|
|
|
|
|
|
|
|
def test_datagram_loop_reading_data(self):
|
2019-09-11 10:07:37 -03:00
|
|
|
res = self.loop.create_future()
|
2019-05-28 06:52:15 -03:00
|
|
|
res.set_result((b'data', ('127.0.0.1', 12068)))
|
|
|
|
|
|
|
|
tr = self.datagram_transport()
|
|
|
|
tr._read_fut = res
|
|
|
|
tr._loop_reading(res)
|
|
|
|
self.loop._proactor.recvfrom.assert_called_with(self.sock, 256 * 1024)
|
|
|
|
self.protocol.datagram_received.assert_called_with(b'data', ('127.0.0.1', 12068))
|
|
|
|
close_transport(tr)
|
|
|
|
|
2021-12-26 07:13:14 -04:00
|
|
|
@unittest.skipIf(sys.flags.optimize, "Assertions are disabled in optimized mode")
|
2019-05-28 06:52:15 -03:00
|
|
|
def test_datagram_loop_reading_no_data(self):
|
2019-09-11 10:07:37 -03:00
|
|
|
res = self.loop.create_future()
|
2019-05-28 06:52:15 -03:00
|
|
|
res.set_result((b'', ('127.0.0.1', 12068)))
|
|
|
|
|
|
|
|
tr = self.datagram_transport()
|
|
|
|
self.assertRaises(AssertionError, tr._loop_reading, res)
|
|
|
|
|
|
|
|
tr.close = mock.Mock()
|
|
|
|
tr._read_fut = res
|
|
|
|
tr._loop_reading(res)
|
|
|
|
self.assertTrue(self.loop._proactor.recvfrom.called)
|
|
|
|
self.assertFalse(self.protocol.error_received.called)
|
|
|
|
self.assertFalse(tr.close.called)
|
|
|
|
close_transport(tr)
|
|
|
|
|
|
|
|
def test_datagram_loop_reading_aborted(self):
|
|
|
|
err = self.loop._proactor.recvfrom.side_effect = ConnectionAbortedError()
|
|
|
|
|
|
|
|
tr = self.datagram_transport()
|
|
|
|
tr._fatal_error = mock.Mock()
|
|
|
|
tr._protocol.error_received = mock.Mock()
|
|
|
|
tr._loop_reading()
|
|
|
|
tr._protocol.error_received.assert_called_with(err)
|
|
|
|
close_transport(tr)
|
|
|
|
|
|
|
|
def test_datagram_loop_writing_aborted(self):
|
|
|
|
err = self.loop._proactor.sendto.side_effect = ConnectionAbortedError()
|
|
|
|
|
|
|
|
tr = self.datagram_transport()
|
|
|
|
tr._fatal_error = mock.Mock()
|
|
|
|
tr._protocol.error_received = mock.Mock()
|
|
|
|
tr._buffer.appendleft((b'Hello', ('127.0.0.1', 12068)))
|
|
|
|
tr._loop_writing()
|
|
|
|
tr._protocol.error_received.assert_called_with(err)
|
|
|
|
close_transport(tr)
|
|
|
|
|
2013-10-20 16:02:53 -03:00
|
|
|
|
2018-02-25 12:32:14 -04:00
|
|
|
@unittest.skipIf(sys.platform != 'win32',
|
|
|
|
'Proactor is supported on Windows only')
|
|
|
|
class ProactorEventLoopUnixSockSendfileTests(test_utils.TestCase):
|
|
|
|
DATA = b"12345abcde" * 16 * 1024 # 160 KiB
|
|
|
|
|
|
|
|
class MyProto(asyncio.Protocol):
|
|
|
|
|
|
|
|
def __init__(self, loop):
|
|
|
|
self.started = False
|
|
|
|
self.closed = False
|
|
|
|
self.data = bytearray()
|
|
|
|
self.fut = loop.create_future()
|
|
|
|
self.transport = None
|
|
|
|
|
|
|
|
def connection_made(self, transport):
|
|
|
|
self.started = True
|
|
|
|
self.transport = transport
|
|
|
|
|
|
|
|
def data_received(self, data):
|
|
|
|
self.data.extend(data)
|
|
|
|
|
|
|
|
def connection_lost(self, exc):
|
|
|
|
self.closed = True
|
|
|
|
self.fut.set_result(None)
|
|
|
|
|
|
|
|
async def wait_closed(self):
|
|
|
|
await self.fut
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
2020-08-07 12:18:38 -03:00
|
|
|
with open(os_helper.TESTFN, 'wb') as fp:
|
2018-02-25 12:32:14 -04:00
|
|
|
fp.write(cls.DATA)
|
|
|
|
super().setUpClass()
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
2020-08-07 12:18:38 -03:00
|
|
|
os_helper.unlink(os_helper.TESTFN)
|
2018-02-25 12:32:14 -04:00
|
|
|
super().tearDownClass()
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.loop = asyncio.ProactorEventLoop()
|
|
|
|
self.set_event_loop(self.loop)
|
|
|
|
self.addCleanup(self.loop.close)
|
2020-08-07 12:18:38 -03:00
|
|
|
self.file = open(os_helper.TESTFN, 'rb')
|
2018-02-25 12:32:14 -04:00
|
|
|
self.addCleanup(self.file.close)
|
|
|
|
super().setUp()
|
|
|
|
|
|
|
|
def make_socket(self, cleanup=True):
|
|
|
|
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
sock.setblocking(False)
|
|
|
|
sock.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 1024)
|
|
|
|
sock.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 1024)
|
|
|
|
if cleanup:
|
|
|
|
self.addCleanup(sock.close)
|
|
|
|
return sock
|
|
|
|
|
|
|
|
def run_loop(self, coro):
|
|
|
|
return self.loop.run_until_complete(coro)
|
|
|
|
|
|
|
|
def prepare(self):
|
|
|
|
sock = self.make_socket()
|
|
|
|
proto = self.MyProto(self.loop)
|
2020-04-25 04:06:29 -03:00
|
|
|
port = socket_helper.find_unused_port()
|
2018-02-25 12:32:14 -04:00
|
|
|
srv_sock = self.make_socket(cleanup=False)
|
|
|
|
srv_sock.bind(('127.0.0.1', port))
|
|
|
|
server = self.run_loop(self.loop.create_server(
|
|
|
|
lambda: proto, sock=srv_sock))
|
|
|
|
self.run_loop(self.loop.sock_connect(sock, srv_sock.getsockname()))
|
|
|
|
|
|
|
|
def cleanup():
|
|
|
|
if proto.transport is not None:
|
|
|
|
# can be None if the task was cancelled before
|
|
|
|
# connection_made callback
|
|
|
|
proto.transport.close()
|
|
|
|
self.run_loop(proto.wait_closed())
|
|
|
|
|
|
|
|
server.close()
|
|
|
|
self.run_loop(server.wait_closed())
|
|
|
|
|
|
|
|
self.addCleanup(cleanup)
|
|
|
|
|
|
|
|
return sock, proto
|
|
|
|
|
|
|
|
def test_sock_sendfile_not_a_file(self):
|
|
|
|
sock, proto = self.prepare()
|
|
|
|
f = object()
|
2018-09-11 14:13:04 -03:00
|
|
|
with self.assertRaisesRegex(asyncio.SendfileNotAvailableError,
|
2018-02-25 12:32:14 -04:00
|
|
|
"not a regular file"):
|
|
|
|
self.run_loop(self.loop._sock_sendfile_native(sock, f,
|
|
|
|
0, None))
|
|
|
|
self.assertEqual(self.file.tell(), 0)
|
|
|
|
|
|
|
|
def test_sock_sendfile_iobuffer(self):
|
|
|
|
sock, proto = self.prepare()
|
|
|
|
f = io.BytesIO()
|
2018-09-11 14:13:04 -03:00
|
|
|
with self.assertRaisesRegex(asyncio.SendfileNotAvailableError,
|
2018-02-25 12:32:14 -04:00
|
|
|
"not a regular file"):
|
|
|
|
self.run_loop(self.loop._sock_sendfile_native(sock, f,
|
|
|
|
0, None))
|
|
|
|
self.assertEqual(self.file.tell(), 0)
|
|
|
|
|
|
|
|
def test_sock_sendfile_not_regular_file(self):
|
|
|
|
sock, proto = self.prepare()
|
|
|
|
f = mock.Mock()
|
|
|
|
f.fileno.return_value = -1
|
2018-09-11 14:13:04 -03:00
|
|
|
with self.assertRaisesRegex(asyncio.SendfileNotAvailableError,
|
2018-02-25 12:32:14 -04:00
|
|
|
"not a regular file"):
|
|
|
|
self.run_loop(self.loop._sock_sendfile_native(sock, f,
|
|
|
|
0, None))
|
|
|
|
self.assertEqual(self.file.tell(), 0)
|
|
|
|
|
|
|
|
|
2013-10-20 16:02:53 -03:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|