2013-10-17 17:40:50 -03:00
|
|
|
"""Tests for selector_events.py"""
|
|
|
|
|
2021-12-26 07:13:14 -04:00
|
|
|
import sys
|
2017-11-28 10:19:56 -04:00
|
|
|
import selectors
|
2013-10-17 17:40:50 -03:00
|
|
|
import socket
|
|
|
|
import unittest
|
2014-02-26 05:25:02 -04:00
|
|
|
from unittest import mock
|
2013-10-17 17:40:50 -03:00
|
|
|
try:
|
|
|
|
import ssl
|
|
|
|
except ImportError:
|
|
|
|
ssl = None
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
import asyncio
|
2014-01-25 17:22:18 -04:00
|
|
|
from asyncio.selector_events import BaseSelectorEventLoop
|
2013-10-17 17:40:50 -03:00
|
|
|
from asyncio.selector_events import _SelectorTransport
|
|
|
|
from asyncio.selector_events import _SelectorSocketTransport
|
|
|
|
from asyncio.selector_events import _SelectorDatagramTransport
|
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
|
|
|
|
|
|
|
|
2014-02-26 05:25:02 -04:00
|
|
|
MOCK_ANY = mock.ANY
|
2014-02-18 19:02:19 -04:00
|
|
|
|
|
|
|
|
2018-06-02 00:34:09 -03:00
|
|
|
def tearDownModule():
|
|
|
|
asyncio.set_event_loop_policy(None)
|
|
|
|
|
|
|
|
|
2014-01-25 17:22:18 -04:00
|
|
|
class TestBaseSelectorEventLoop(BaseSelectorEventLoop):
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def _make_self_pipe(self):
|
2014-02-26 05:25:02 -04:00
|
|
|
self._ssock = mock.Mock()
|
|
|
|
self._csock = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
self._internal_fds += 1
|
|
|
|
|
2017-06-30 06:12:33 -03:00
|
|
|
def _close_self_pipe(self):
|
|
|
|
pass
|
|
|
|
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2013-11-27 18:12:48 -04:00
|
|
|
def list_to_buffer(l=()):
|
|
|
|
return bytearray().join(l)
|
|
|
|
|
|
|
|
|
2015-01-15 08:18:32 -04:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2014-06-17 20:36:32 -03:00
|
|
|
class BaseSelectorEventLoopTests(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-08-31 10:07:57 -03:00
|
|
|
self.selector = mock.Mock()
|
|
|
|
self.selector.select.return_value = []
|
|
|
|
self.loop = TestBaseSelectorEventLoop(self.selector)
|
2015-01-15 08:17:34 -04:00
|
|
|
self.set_event_loop(self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def test_make_socket_transport(self):
|
2014-02-26 05:25:02 -04:00
|
|
|
m = mock.Mock()
|
|
|
|
self.loop.add_reader = mock.Mock()
|
2022-10-07 16:56:50 -03:00
|
|
|
self.loop._ensure_fd_no_transport = mock.Mock()
|
2014-02-11 06:34:30 -04:00
|
|
|
transport = self.loop._make_socket_transport(m, asyncio.Protocol())
|
|
|
|
self.assertIsInstance(transport, _SelectorSocketTransport)
|
2022-10-07 16:56:50 -03:00
|
|
|
self.assertEqual(self.loop._ensure_fd_no_transport.call_count, 1)
|
2015-03-27 11:20:08 -03:00
|
|
|
|
|
|
|
# Calling repr() must not fail when the event loop is closed
|
|
|
|
self.loop.close()
|
|
|
|
repr(transport)
|
|
|
|
|
2015-01-15 08:18:32 -04:00
|
|
|
close_transport(transport)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-02-26 05:25:02 -04:00
|
|
|
@mock.patch('asyncio.selector_events.ssl', None)
|
2015-01-13 19:19:09 -04:00
|
|
|
@mock.patch('asyncio.sslproto.ssl', None)
|
2013-11-01 18:22:30 -03:00
|
|
|
def test_make_ssl_transport_without_ssl_error(self):
|
2014-02-26 05:25:02 -04:00
|
|
|
m = mock.Mock()
|
|
|
|
self.loop.add_reader = mock.Mock()
|
|
|
|
self.loop.add_writer = mock.Mock()
|
|
|
|
self.loop.remove_reader = mock.Mock()
|
|
|
|
self.loop.remove_writer = mock.Mock()
|
2022-10-07 16:56:50 -03:00
|
|
|
self.loop._ensure_fd_no_transport = mock.Mock()
|
2013-11-01 18:22:30 -03:00
|
|
|
with self.assertRaises(RuntimeError):
|
|
|
|
self.loop._make_ssl_transport(m, m, m, m)
|
2022-10-07 16:56:50 -03:00
|
|
|
self.assertEqual(self.loop._ensure_fd_no_transport.call_count, 1)
|
2013-11-01 18:22:30 -03:00
|
|
|
|
2013-10-17 17:40:50 -03:00
|
|
|
def test_close(self):
|
2015-01-15 08:17:34 -04:00
|
|
|
class EventLoop(BaseSelectorEventLoop):
|
|
|
|
def _make_self_pipe(self):
|
|
|
|
self._ssock = mock.Mock()
|
|
|
|
self._csock = mock.Mock()
|
|
|
|
self._internal_fds += 1
|
|
|
|
|
|
|
|
self.loop = EventLoop(self.selector)
|
|
|
|
self.set_event_loop(self.loop)
|
|
|
|
|
2013-10-17 17:40:50 -03:00
|
|
|
ssock = self.loop._ssock
|
|
|
|
ssock.fileno.return_value = 7
|
|
|
|
csock = self.loop._csock
|
|
|
|
csock.fileno.return_value = 1
|
2016-10-05 18:48:59 -03:00
|
|
|
remove_reader = self.loop._remove_reader = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
self.loop._selector.close()
|
2014-02-26 05:25:02 -04:00
|
|
|
self.loop._selector = selector = mock.Mock()
|
2014-06-10 05:23:10 -03:00
|
|
|
self.assertFalse(self.loop.is_closed())
|
|
|
|
|
2013-10-17 17:40:50 -03:00
|
|
|
self.loop.close()
|
2014-06-10 05:23:10 -03:00
|
|
|
self.assertTrue(self.loop.is_closed())
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertIsNone(self.loop._selector)
|
|
|
|
self.assertIsNone(self.loop._csock)
|
|
|
|
self.assertIsNone(self.loop._ssock)
|
|
|
|
selector.close.assert_called_with()
|
|
|
|
ssock.close.assert_called_with()
|
|
|
|
csock.close.assert_called_with()
|
|
|
|
remove_reader.assert_called_with(7)
|
|
|
|
|
2014-06-10 05:23:10 -03:00
|
|
|
# it should be possible to call close() more than once
|
2013-10-17 17:40:50 -03:00
|
|
|
self.loop.close()
|
|
|
|
self.loop.close()
|
|
|
|
|
2014-06-10 05:23:10 -03:00
|
|
|
# operation blocked when the loop is closed
|
2019-09-11 10:07:37 -03:00
|
|
|
f = self.loop.create_future()
|
2014-06-10 05:23:10 -03:00
|
|
|
self.assertRaises(RuntimeError, self.loop.run_forever)
|
|
|
|
self.assertRaises(RuntimeError, self.loop.run_until_complete, f)
|
|
|
|
fd = 0
|
|
|
|
def callback():
|
|
|
|
pass
|
|
|
|
self.assertRaises(RuntimeError, self.loop.add_reader, fd, callback)
|
|
|
|
self.assertRaises(RuntimeError, self.loop.add_writer, fd, callback)
|
|
|
|
|
2013-10-17 17:40:50 -03:00
|
|
|
def test_close_no_selector(self):
|
2014-06-17 22:25:23 -03:00
|
|
|
self.loop.remove_reader = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
self.loop._selector.close()
|
|
|
|
self.loop._selector = None
|
|
|
|
self.loop.close()
|
|
|
|
self.assertIsNone(self.loop._selector)
|
|
|
|
|
|
|
|
def test_read_from_self_tryagain(self):
|
|
|
|
self.loop._ssock.recv.side_effect = BlockingIOError
|
|
|
|
self.assertIsNone(self.loop._read_from_self())
|
|
|
|
|
|
|
|
def test_read_from_self_exception(self):
|
|
|
|
self.loop._ssock.recv.side_effect = OSError
|
|
|
|
self.assertRaises(OSError, self.loop._read_from_self)
|
|
|
|
|
|
|
|
def test_write_to_self_tryagain(self):
|
|
|
|
self.loop._csock.send.side_effect = BlockingIOError
|
2014-08-25 18:20:52 -03:00
|
|
|
with test_utils.disable_logger():
|
|
|
|
self.assertIsNone(self.loop._write_to_self())
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def test_write_to_self_exception(self):
|
2014-05-06 18:42:40 -03:00
|
|
|
# _write_to_self() swallows OSError
|
|
|
|
self.loop._csock.send.side_effect = RuntimeError()
|
|
|
|
self.assertRaises(RuntimeError, self.loop._write_to_self)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2022-02-22 16:35:57 -04:00
|
|
|
@mock.patch('socket.getaddrinfo')
|
|
|
|
def test_sock_connect_resolve_using_socket_params(self, m_gai):
|
|
|
|
addr = ('need-resolution.com', 8080)
|
|
|
|
for sock_type in [socket.SOCK_STREAM, socket.SOCK_DGRAM]:
|
|
|
|
with self.subTest(sock_type):
|
|
|
|
sock = test_utils.mock_nonblocking_socket(type=sock_type)
|
|
|
|
|
|
|
|
m_gai.side_effect = \
|
|
|
|
lambda *args: [(None, None, None, None, ('127.0.0.1', 0))]
|
|
|
|
|
|
|
|
con = self.loop.create_task(self.loop.sock_connect(sock, addr))
|
|
|
|
self.loop.run_until_complete(con)
|
|
|
|
m_gai.assert_called_with(
|
|
|
|
addr[0], addr[1], sock.family, sock.type, sock.proto, 0)
|
|
|
|
|
|
|
|
self.loop.run_until_complete(con)
|
|
|
|
sock.connect.assert_called_with(('127.0.0.1', 0))
|
|
|
|
|
2013-10-17 17:40:50 -03:00
|
|
|
def test_add_reader(self):
|
|
|
|
self.loop._selector.get_key.side_effect = KeyError
|
|
|
|
cb = lambda: True
|
|
|
|
self.loop.add_reader(1, cb)
|
|
|
|
|
|
|
|
self.assertTrue(self.loop._selector.register.called)
|
|
|
|
fd, mask, (r, w) = self.loop._selector.register.call_args[0]
|
|
|
|
self.assertEqual(1, fd)
|
|
|
|
self.assertEqual(selectors.EVENT_READ, mask)
|
|
|
|
self.assertEqual(cb, r._callback)
|
|
|
|
self.assertIsNone(w)
|
|
|
|
|
|
|
|
def test_add_reader_existing(self):
|
2014-02-26 05:25:02 -04:00
|
|
|
reader = mock.Mock()
|
|
|
|
writer = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
self.loop._selector.get_key.return_value = selectors.SelectorKey(
|
|
|
|
1, 1, selectors.EVENT_WRITE, (reader, writer))
|
|
|
|
cb = lambda: True
|
|
|
|
self.loop.add_reader(1, cb)
|
|
|
|
|
|
|
|
self.assertTrue(reader.cancel.called)
|
|
|
|
self.assertFalse(self.loop._selector.register.called)
|
|
|
|
self.assertTrue(self.loop._selector.modify.called)
|
|
|
|
fd, mask, (r, w) = self.loop._selector.modify.call_args[0]
|
|
|
|
self.assertEqual(1, fd)
|
|
|
|
self.assertEqual(selectors.EVENT_WRITE | selectors.EVENT_READ, mask)
|
|
|
|
self.assertEqual(cb, r._callback)
|
|
|
|
self.assertEqual(writer, w)
|
|
|
|
|
|
|
|
def test_add_reader_existing_writer(self):
|
2014-02-26 05:25:02 -04:00
|
|
|
writer = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
self.loop._selector.get_key.return_value = selectors.SelectorKey(
|
|
|
|
1, 1, selectors.EVENT_WRITE, (None, writer))
|
|
|
|
cb = lambda: True
|
|
|
|
self.loop.add_reader(1, cb)
|
|
|
|
|
|
|
|
self.assertFalse(self.loop._selector.register.called)
|
|
|
|
self.assertTrue(self.loop._selector.modify.called)
|
|
|
|
fd, mask, (r, w) = self.loop._selector.modify.call_args[0]
|
|
|
|
self.assertEqual(1, fd)
|
|
|
|
self.assertEqual(selectors.EVENT_WRITE | selectors.EVENT_READ, mask)
|
|
|
|
self.assertEqual(cb, r._callback)
|
|
|
|
self.assertEqual(writer, w)
|
|
|
|
|
|
|
|
def test_remove_reader(self):
|
|
|
|
self.loop._selector.get_key.return_value = selectors.SelectorKey(
|
|
|
|
1, 1, selectors.EVENT_READ, (None, None))
|
|
|
|
self.assertFalse(self.loop.remove_reader(1))
|
|
|
|
|
|
|
|
self.assertTrue(self.loop._selector.unregister.called)
|
|
|
|
|
|
|
|
def test_remove_reader_read_write(self):
|
2014-02-26 05:25:02 -04:00
|
|
|
reader = mock.Mock()
|
|
|
|
writer = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
self.loop._selector.get_key.return_value = selectors.SelectorKey(
|
|
|
|
1, 1, selectors.EVENT_READ | selectors.EVENT_WRITE,
|
|
|
|
(reader, writer))
|
|
|
|
self.assertTrue(
|
|
|
|
self.loop.remove_reader(1))
|
|
|
|
|
|
|
|
self.assertFalse(self.loop._selector.unregister.called)
|
|
|
|
self.assertEqual(
|
|
|
|
(1, selectors.EVENT_WRITE, (None, writer)),
|
|
|
|
self.loop._selector.modify.call_args[0])
|
|
|
|
|
|
|
|
def test_remove_reader_unknown(self):
|
|
|
|
self.loop._selector.get_key.side_effect = KeyError
|
|
|
|
self.assertFalse(
|
|
|
|
self.loop.remove_reader(1))
|
|
|
|
|
|
|
|
def test_add_writer(self):
|
|
|
|
self.loop._selector.get_key.side_effect = KeyError
|
|
|
|
cb = lambda: True
|
|
|
|
self.loop.add_writer(1, cb)
|
|
|
|
|
|
|
|
self.assertTrue(self.loop._selector.register.called)
|
|
|
|
fd, mask, (r, w) = self.loop._selector.register.call_args[0]
|
|
|
|
self.assertEqual(1, fd)
|
|
|
|
self.assertEqual(selectors.EVENT_WRITE, mask)
|
|
|
|
self.assertIsNone(r)
|
|
|
|
self.assertEqual(cb, w._callback)
|
|
|
|
|
|
|
|
def test_add_writer_existing(self):
|
2014-02-26 05:25:02 -04:00
|
|
|
reader = mock.Mock()
|
|
|
|
writer = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
self.loop._selector.get_key.return_value = selectors.SelectorKey(
|
|
|
|
1, 1, selectors.EVENT_READ, (reader, writer))
|
|
|
|
cb = lambda: True
|
|
|
|
self.loop.add_writer(1, cb)
|
|
|
|
|
|
|
|
self.assertTrue(writer.cancel.called)
|
|
|
|
self.assertFalse(self.loop._selector.register.called)
|
|
|
|
self.assertTrue(self.loop._selector.modify.called)
|
|
|
|
fd, mask, (r, w) = self.loop._selector.modify.call_args[0]
|
|
|
|
self.assertEqual(1, fd)
|
|
|
|
self.assertEqual(selectors.EVENT_WRITE | selectors.EVENT_READ, mask)
|
|
|
|
self.assertEqual(reader, r)
|
|
|
|
self.assertEqual(cb, w._callback)
|
|
|
|
|
|
|
|
def test_remove_writer(self):
|
|
|
|
self.loop._selector.get_key.return_value = selectors.SelectorKey(
|
|
|
|
1, 1, selectors.EVENT_WRITE, (None, None))
|
|
|
|
self.assertFalse(self.loop.remove_writer(1))
|
|
|
|
|
|
|
|
self.assertTrue(self.loop._selector.unregister.called)
|
|
|
|
|
|
|
|
def test_remove_writer_read_write(self):
|
2014-02-26 05:25:02 -04:00
|
|
|
reader = mock.Mock()
|
|
|
|
writer = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
self.loop._selector.get_key.return_value = selectors.SelectorKey(
|
|
|
|
1, 1, selectors.EVENT_READ | selectors.EVENT_WRITE,
|
|
|
|
(reader, writer))
|
|
|
|
self.assertTrue(
|
|
|
|
self.loop.remove_writer(1))
|
|
|
|
|
|
|
|
self.assertFalse(self.loop._selector.unregister.called)
|
|
|
|
self.assertEqual(
|
|
|
|
(1, selectors.EVENT_READ, (reader, None)),
|
|
|
|
self.loop._selector.modify.call_args[0])
|
|
|
|
|
|
|
|
def test_remove_writer_unknown(self):
|
|
|
|
self.loop._selector.get_key.side_effect = KeyError
|
|
|
|
self.assertFalse(
|
|
|
|
self.loop.remove_writer(1))
|
|
|
|
|
|
|
|
def test_process_events_read(self):
|
2014-02-26 05:25:02 -04:00
|
|
|
reader = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
reader._cancelled = False
|
|
|
|
|
2014-02-26 05:25:02 -04:00
|
|
|
self.loop._add_callback = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
self.loop._process_events(
|
|
|
|
[(selectors.SelectorKey(
|
|
|
|
1, 1, selectors.EVENT_READ, (reader, None)),
|
|
|
|
selectors.EVENT_READ)])
|
|
|
|
self.assertTrue(self.loop._add_callback.called)
|
|
|
|
self.loop._add_callback.assert_called_with(reader)
|
|
|
|
|
|
|
|
def test_process_events_read_cancelled(self):
|
2014-02-26 05:25:02 -04:00
|
|
|
reader = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
reader.cancelled = True
|
|
|
|
|
2016-10-05 18:48:59 -03:00
|
|
|
self.loop._remove_reader = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
self.loop._process_events(
|
|
|
|
[(selectors.SelectorKey(
|
|
|
|
1, 1, selectors.EVENT_READ, (reader, None)),
|
|
|
|
selectors.EVENT_READ)])
|
2016-10-05 18:48:59 -03:00
|
|
|
self.loop._remove_reader.assert_called_with(1)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def test_process_events_write(self):
|
2014-02-26 05:25:02 -04:00
|
|
|
writer = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
writer._cancelled = False
|
|
|
|
|
2014-02-26 05:25:02 -04:00
|
|
|
self.loop._add_callback = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
self.loop._process_events(
|
|
|
|
[(selectors.SelectorKey(1, 1, selectors.EVENT_WRITE,
|
|
|
|
(None, writer)),
|
|
|
|
selectors.EVENT_WRITE)])
|
|
|
|
self.loop._add_callback.assert_called_with(writer)
|
|
|
|
|
|
|
|
def test_process_events_write_cancelled(self):
|
2014-02-26 05:25:02 -04:00
|
|
|
writer = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
writer.cancelled = True
|
2016-10-05 18:48:59 -03:00
|
|
|
self.loop._remove_writer = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
self.loop._process_events(
|
|
|
|
[(selectors.SelectorKey(1, 1, selectors.EVENT_WRITE,
|
|
|
|
(None, writer)),
|
|
|
|
selectors.EVENT_WRITE)])
|
2016-10-05 18:48:59 -03:00
|
|
|
self.loop._remove_writer.assert_called_with(1)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2016-09-15 15:13:15 -03:00
|
|
|
def test_accept_connection_multiple(self):
|
|
|
|
sock = mock.Mock()
|
|
|
|
sock.accept.return_value = (mock.Mock(), mock.Mock())
|
|
|
|
backlog = 100
|
|
|
|
# Mock the coroutine generation for a connection to prevent
|
2019-05-30 07:00:29 -03:00
|
|
|
# warnings related to un-awaited coroutines. _accept_connection2
|
|
|
|
# is an async function that is patched with AsyncMock. create_task
|
|
|
|
# creates a task out of coroutine returned by AsyncMock, so use
|
|
|
|
# asyncio.sleep(0) to ensure created tasks are complete to avoid
|
|
|
|
# task pending warnings.
|
2016-09-15 15:13:15 -03:00
|
|
|
mock_obj = mock.patch.object
|
|
|
|
with mock_obj(self.loop, '_accept_connection2') as accept2_mock:
|
2019-05-30 07:00:29 -03:00
|
|
|
self.loop._accept_connection(
|
|
|
|
mock.Mock(), sock, backlog=backlog)
|
|
|
|
self.loop.run_until_complete(asyncio.sleep(0))
|
2016-09-15 15:13:15 -03:00
|
|
|
self.assertEqual(sock.accept.call_count, backlog)
|
|
|
|
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-06-17 20:36:32 -03:00
|
|
|
class SelectorTransportTests(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()
|
2014-01-25 10:32:06 -04:00
|
|
|
self.protocol = test_utils.make_test_protocol(asyncio.Protocol)
|
2014-02-26 05:25:02 -04:00
|
|
|
self.sock = mock.Mock(socket.socket)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.sock.fileno.return_value = 7
|
|
|
|
|
2015-01-15 08:18:32 -04:00
|
|
|
def create_transport(self):
|
|
|
|
transport = _SelectorTransport(self.loop, self.sock, self.protocol,
|
|
|
|
None)
|
|
|
|
self.addCleanup(close_transport, transport)
|
|
|
|
return transport
|
|
|
|
|
2013-10-17 17:40:50 -03:00
|
|
|
def test_ctor(self):
|
2015-01-15 08:18:32 -04:00
|
|
|
tr = self.create_transport()
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertIs(tr._loop, self.loop)
|
|
|
|
self.assertIs(tr._sock, self.sock)
|
|
|
|
self.assertIs(tr._sock_fd, 7)
|
|
|
|
|
|
|
|
def test_abort(self):
|
2015-01-15 08:18:32 -04:00
|
|
|
tr = self.create_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.create_transport()
|
2013-10-17 17:40:50 -03:00
|
|
|
tr.close()
|
|
|
|
|
2015-11-16 13:43:21 -04:00
|
|
|
self.assertTrue(tr.is_closing())
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertEqual(1, self.loop.remove_reader_count[7])
|
|
|
|
self.protocol.connection_lost(None)
|
|
|
|
self.assertEqual(tr._conn_lost, 1)
|
|
|
|
|
|
|
|
tr.close()
|
|
|
|
self.assertEqual(tr._conn_lost, 1)
|
|
|
|
self.assertEqual(1, self.loop.remove_reader_count[7])
|
|
|
|
|
|
|
|
def test_close_write_buffer(self):
|
2015-01-15 08:18:32 -04:00
|
|
|
tr = self.create_transport()
|
2013-11-27 18:12:48 -04:00
|
|
|
tr._buffer.extend(b'data')
|
2013-10-17 17:40:50 -03:00
|
|
|
tr.close()
|
|
|
|
|
|
|
|
self.assertFalse(self.loop.readers)
|
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
self.assertFalse(self.protocol.connection_lost.called)
|
|
|
|
|
|
|
|
def test_force_close(self):
|
2015-01-15 08:18:32 -04:00
|
|
|
tr = self.create_transport()
|
2013-11-27 18:12:48 -04:00
|
|
|
tr._buffer.extend(b'1')
|
2016-10-05 18:48:59 -03:00
|
|
|
self.loop._add_reader(7, mock.sentinel)
|
|
|
|
self.loop._add_writer(7, mock.sentinel)
|
2013-10-17 17:40:50 -03:00
|
|
|
tr._force_close(None)
|
|
|
|
|
2015-11-16 13:43:21 -04:00
|
|
|
self.assertTrue(tr.is_closing())
|
2013-11-27 18:12:48 -04:00
|
|
|
self.assertEqual(tr._buffer, list_to_buffer())
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertFalse(self.loop.readers)
|
|
|
|
self.assertFalse(self.loop.writers)
|
|
|
|
|
|
|
|
# second close should not remove reader
|
|
|
|
tr._force_close(None)
|
|
|
|
self.assertFalse(self.loop.readers)
|
|
|
|
self.assertEqual(1, self.loop.remove_reader_count[7])
|
|
|
|
|
2014-02-26 05:25:02 -04:00
|
|
|
@mock.patch('asyncio.log.logger.error')
|
2013-10-17 17:40:50 -03:00
|
|
|
def test_fatal_error(self, m_exc):
|
|
|
|
exc = OSError()
|
2015-01-15 08:18:32 -04:00
|
|
|
tr = self.create_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(exc)
|
|
|
|
|
2019-05-27 10:28:34 -03:00
|
|
|
m_exc.assert_not_called()
|
|
|
|
|
|
|
|
tr._force_close.assert_called_with(exc)
|
|
|
|
|
|
|
|
@mock.patch('asyncio.log.logger.error')
|
|
|
|
def test_fatal_error_custom_exception(self, m_exc):
|
|
|
|
class MyError(Exception):
|
|
|
|
pass
|
|
|
|
exc = MyError()
|
|
|
|
tr = self.create_transport()
|
|
|
|
tr._force_close = mock.Mock()
|
|
|
|
tr._fatal_error(exc)
|
|
|
|
|
2014-02-18 19:02:19 -04:00
|
|
|
m_exc.assert_called_with(
|
|
|
|
test_utils.MockPattern(
|
2014-02-18 20:40:41 -04:00
|
|
|
'Fatal error on transport\nprotocol:.*\ntransport:.*'),
|
2019-05-27 10:28:34 -03:00
|
|
|
exc_info=(MyError, MOCK_ANY, MOCK_ANY))
|
2014-02-18 19:02:19 -04:00
|
|
|
|
2013-10-17 17:40:50 -03:00
|
|
|
tr._force_close.assert_called_with(exc)
|
|
|
|
|
|
|
|
def test_connection_lost(self):
|
|
|
|
exc = OSError()
|
2015-01-15 08:18:32 -04:00
|
|
|
tr = self.create_transport()
|
2014-07-23 13:21:45 -03:00
|
|
|
self.assertIsNotNone(tr._protocol)
|
|
|
|
self.assertIsNotNone(tr._loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
tr._call_connection_lost(exc)
|
|
|
|
|
|
|
|
self.protocol.connection_lost.assert_called_with(exc)
|
|
|
|
self.sock.close.assert_called_with()
|
|
|
|
self.assertIsNone(tr._sock)
|
|
|
|
|
|
|
|
self.assertIsNone(tr._protocol)
|
|
|
|
self.assertIsNone(tr._loop)
|
|
|
|
|
2018-05-21 05:13:45 -03:00
|
|
|
def test__add_reader(self):
|
|
|
|
tr = self.create_transport()
|
|
|
|
tr._buffer.extend(b'1')
|
|
|
|
tr._add_reader(7, mock.sentinel)
|
|
|
|
self.assertTrue(self.loop.readers)
|
|
|
|
|
|
|
|
tr._force_close(None)
|
|
|
|
|
|
|
|
self.assertTrue(tr.is_closing())
|
|
|
|
self.assertFalse(self.loop.readers)
|
|
|
|
|
|
|
|
# can not add readers after closing
|
|
|
|
tr._add_reader(7, mock.sentinel)
|
|
|
|
self.assertFalse(self.loop.readers)
|
|
|
|
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-06-17 20:36:32 -03:00
|
|
|
class SelectorSocketTransportTests(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()
|
2014-01-25 10:32:06 -04:00
|
|
|
self.protocol = test_utils.make_test_protocol(asyncio.Protocol)
|
2014-02-26 05:25:02 -04:00
|
|
|
self.sock = mock.Mock(socket.socket)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.sock_fd = self.sock.fileno.return_value = 7
|
|
|
|
|
2015-01-15 08:18:32 -04:00
|
|
|
def socket_transport(self, waiter=None):
|
|
|
|
transport = _SelectorSocketTransport(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
|
|
|
waiter = self.loop.create_future()
|
2015-01-28 19:36:51 -04:00
|
|
|
tr = self.socket_transport(waiter=waiter)
|
|
|
|
self.loop.run_until_complete(waiter)
|
|
|
|
|
2013-10-17 17:40:50 -03:00
|
|
|
self.loop.assert_reader(7, tr._read_ready)
|
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
self.protocol.connection_made.assert_called_with(tr)
|
|
|
|
|
|
|
|
def test_ctor_with_waiter(self):
|
2019-09-11 10:07:37 -03:00
|
|
|
waiter = self.loop.create_future()
|
2015-01-28 19:36:51 -04:00
|
|
|
self.socket_transport(waiter=waiter)
|
|
|
|
self.loop.run_until_complete(waiter)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2015-01-28 19:36:51 -04:00
|
|
|
self.assertIsNone(waiter.result())
|
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()
|
2015-01-28 19:36:51 -04:00
|
|
|
test_utils.run_briefly(self.loop)
|
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())
|
2013-10-17 17:40:50 -03:00
|
|
|
self.loop.assert_reader(7, tr._read_ready)
|
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())
|
|
|
|
self.loop.assert_no_reader(7)
|
|
|
|
|
|
|
|
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())
|
2013-10-17 17:40:50 -03:00
|
|
|
self.loop.assert_reader(7, tr._read_ready)
|
2017-12-18 18:03:23 -04:00
|
|
|
|
|
|
|
tr.close()
|
|
|
|
self.assertFalse(tr.is_reading())
|
|
|
|
self.loop.assert_no_reader(7)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2018-01-28 17:30:26 -04:00
|
|
|
def test_read_eof_received_error(self):
|
|
|
|
transport = self.socket_transport()
|
|
|
|
transport.close = mock.Mock()
|
|
|
|
transport._fatal_error = mock.Mock()
|
|
|
|
|
|
|
|
self.loop.call_exception_handler = mock.Mock()
|
|
|
|
|
|
|
|
self.protocol.eof_received.side_effect = LookupError()
|
|
|
|
|
|
|
|
self.sock.recv.return_value = b''
|
|
|
|
transport._read_ready()
|
|
|
|
|
|
|
|
self.protocol.eof_received.assert_called_with()
|
|
|
|
self.assertTrue(transport._fatal_error.called)
|
|
|
|
|
|
|
|
def test_data_received_error(self):
|
|
|
|
transport = self.socket_transport()
|
|
|
|
transport._fatal_error = mock.Mock()
|
|
|
|
|
|
|
|
self.loop.call_exception_handler = mock.Mock()
|
|
|
|
self.protocol.data_received.side_effect = LookupError()
|
|
|
|
|
|
|
|
self.sock.recv.return_value = b'data'
|
|
|
|
transport._read_ready()
|
|
|
|
|
|
|
|
self.assertTrue(transport._fatal_error.called)
|
|
|
|
self.assertTrue(self.protocol.data_received.called)
|
|
|
|
|
2013-10-17 17:40:50 -03:00
|
|
|
def test_read_ready(self):
|
2015-01-15 08:18:32 -04:00
|
|
|
transport = self.socket_transport()
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
self.sock.recv.return_value = b'data'
|
|
|
|
transport._read_ready()
|
|
|
|
|
|
|
|
self.protocol.data_received.assert_called_with(b'data')
|
|
|
|
|
|
|
|
def test_read_ready_eof(self):
|
2015-01-15 08:18:32 -04:00
|
|
|
transport = self.socket_transport()
|
2014-02-26 05:25:02 -04:00
|
|
|
transport.close = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
self.sock.recv.return_value = b''
|
|
|
|
transport._read_ready()
|
|
|
|
|
|
|
|
self.protocol.eof_received.assert_called_with()
|
|
|
|
transport.close.assert_called_with()
|
|
|
|
|
|
|
|
def test_read_ready_eof_keep_open(self):
|
2015-01-15 08:18:32 -04:00
|
|
|
transport = self.socket_transport()
|
2014-02-26 05:25:02 -04:00
|
|
|
transport.close = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
self.sock.recv.return_value = b''
|
|
|
|
self.protocol.eof_received.return_value = True
|
|
|
|
transport._read_ready()
|
|
|
|
|
|
|
|
self.protocol.eof_received.assert_called_with()
|
|
|
|
self.assertFalse(transport.close.called)
|
|
|
|
|
2014-02-26 05:25:02 -04:00
|
|
|
@mock.patch('logging.exception')
|
2013-10-17 17:40:50 -03:00
|
|
|
def test_read_ready_tryagain(self, m_exc):
|
|
|
|
self.sock.recv.side_effect = BlockingIOError
|
|
|
|
|
2015-01-15 08:18:32 -04:00
|
|
|
transport = self.socket_transport()
|
2014-02-26 05:25:02 -04:00
|
|
|
transport._fatal_error = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
transport._read_ready()
|
|
|
|
|
|
|
|
self.assertFalse(transport._fatal_error.called)
|
|
|
|
|
2014-02-26 05:25:02 -04:00
|
|
|
@mock.patch('logging.exception')
|
2013-10-17 17:40:50 -03:00
|
|
|
def test_read_ready_tryagain_interrupted(self, m_exc):
|
|
|
|
self.sock.recv.side_effect = InterruptedError
|
|
|
|
|
2015-01-15 08:18:32 -04:00
|
|
|
transport = self.socket_transport()
|
2014-02-26 05:25:02 -04:00
|
|
|
transport._fatal_error = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
transport._read_ready()
|
|
|
|
|
|
|
|
self.assertFalse(transport._fatal_error.called)
|
|
|
|
|
2014-02-26 05:25:02 -04:00
|
|
|
@mock.patch('logging.exception')
|
2013-10-17 17:40:50 -03:00
|
|
|
def test_read_ready_conn_reset(self, m_exc):
|
|
|
|
err = self.sock.recv.side_effect = ConnectionResetError()
|
|
|
|
|
2015-01-15 08:18:32 -04:00
|
|
|
transport = self.socket_transport()
|
2014-02-26 05:25:02 -04:00
|
|
|
transport._force_close = mock.Mock()
|
2014-08-25 18:20:52 -03:00
|
|
|
with test_utils.disable_logger():
|
|
|
|
transport._read_ready()
|
2013-10-17 17:40:50 -03:00
|
|
|
transport._force_close.assert_called_with(err)
|
|
|
|
|
2014-02-26 05:25:02 -04:00
|
|
|
@mock.patch('logging.exception')
|
2013-10-17 17:40:50 -03:00
|
|
|
def test_read_ready_err(self, m_exc):
|
|
|
|
err = self.sock.recv.side_effect = OSError()
|
|
|
|
|
2015-01-15 08:18:32 -04:00
|
|
|
transport = self.socket_transport()
|
2014-02-26 05:25:02 -04:00
|
|
|
transport._fatal_error = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
transport._read_ready()
|
|
|
|
|
2014-02-18 20:40:41 -04:00
|
|
|
transport._fatal_error.assert_called_with(
|
|
|
|
err,
|
|
|
|
'Fatal read error on socket transport')
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def test_write(self):
|
|
|
|
data = b'data'
|
|
|
|
self.sock.send.return_value = len(data)
|
|
|
|
|
2015-01-15 08:18:32 -04:00
|
|
|
transport = self.socket_transport()
|
2013-10-17 17:40:50 -03:00
|
|
|
transport.write(data)
|
|
|
|
self.sock.send.assert_called_with(data)
|
|
|
|
|
2013-11-27 18:12:48 -04:00
|
|
|
def test_write_bytearray(self):
|
|
|
|
data = bytearray(b'data')
|
|
|
|
self.sock.send.return_value = len(data)
|
|
|
|
|
2015-01-15 08:18:32 -04:00
|
|
|
transport = self.socket_transport()
|
2013-11-27 18:12:48 -04:00
|
|
|
transport.write(data)
|
|
|
|
self.sock.send.assert_called_with(data)
|
|
|
|
self.assertEqual(data, bytearray(b'data')) # Hasn't been mutated.
|
|
|
|
|
|
|
|
def test_write_memoryview(self):
|
|
|
|
data = memoryview(b'data')
|
|
|
|
self.sock.send.return_value = len(data)
|
|
|
|
|
2015-01-15 08:18:32 -04:00
|
|
|
transport = self.socket_transport()
|
2013-11-27 18:12:48 -04:00
|
|
|
transport.write(data)
|
|
|
|
self.sock.send.assert_called_with(data)
|
|
|
|
|
2013-10-17 17:40:50 -03:00
|
|
|
def test_write_no_data(self):
|
2015-01-15 08:18:32 -04:00
|
|
|
transport = self.socket_transport()
|
2013-11-27 18:12:48 -04:00
|
|
|
transport._buffer.extend(b'data')
|
2013-10-17 17:40:50 -03:00
|
|
|
transport.write(b'')
|
|
|
|
self.assertFalse(self.sock.send.called)
|
2013-11-27 18:12:48 -04:00
|
|
|
self.assertEqual(list_to_buffer([b'data']), transport._buffer)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def test_write_buffer(self):
|
2015-01-15 08:18:32 -04:00
|
|
|
transport = self.socket_transport()
|
2013-11-27 18:12:48 -04:00
|
|
|
transport._buffer.extend(b'data1')
|
2013-10-17 17:40:50 -03:00
|
|
|
transport.write(b'data2')
|
|
|
|
self.assertFalse(self.sock.send.called)
|
2013-11-27 18:12:48 -04:00
|
|
|
self.assertEqual(list_to_buffer([b'data1', b'data2']),
|
2013-10-17 17:40:50 -03:00
|
|
|
transport._buffer)
|
|
|
|
|
|
|
|
def test_write_partial(self):
|
|
|
|
data = b'data'
|
|
|
|
self.sock.send.return_value = 2
|
|
|
|
|
2015-01-15 08:18:32 -04:00
|
|
|
transport = self.socket_transport()
|
2013-10-17 17:40:50 -03:00
|
|
|
transport.write(data)
|
|
|
|
|
|
|
|
self.loop.assert_writer(7, transport._write_ready)
|
2013-11-27 18:12:48 -04:00
|
|
|
self.assertEqual(list_to_buffer([b'ta']), transport._buffer)
|
|
|
|
|
|
|
|
def test_write_partial_bytearray(self):
|
|
|
|
data = bytearray(b'data')
|
|
|
|
self.sock.send.return_value = 2
|
|
|
|
|
2015-01-15 08:18:32 -04:00
|
|
|
transport = self.socket_transport()
|
2013-11-27 18:12:48 -04:00
|
|
|
transport.write(data)
|
|
|
|
|
|
|
|
self.loop.assert_writer(7, transport._write_ready)
|
|
|
|
self.assertEqual(list_to_buffer([b'ta']), transport._buffer)
|
|
|
|
self.assertEqual(data, bytearray(b'data')) # Hasn't been mutated.
|
|
|
|
|
|
|
|
def test_write_partial_memoryview(self):
|
|
|
|
data = memoryview(b'data')
|
|
|
|
self.sock.send.return_value = 2
|
|
|
|
|
2015-01-15 08:18:32 -04:00
|
|
|
transport = self.socket_transport()
|
2013-11-27 18:12:48 -04:00
|
|
|
transport.write(data)
|
|
|
|
|
|
|
|
self.loop.assert_writer(7, transport._write_ready)
|
|
|
|
self.assertEqual(list_to_buffer([b'ta']), transport._buffer)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def test_write_partial_none(self):
|
|
|
|
data = b'data'
|
|
|
|
self.sock.send.return_value = 0
|
|
|
|
self.sock.fileno.return_value = 7
|
|
|
|
|
2015-01-15 08:18:32 -04:00
|
|
|
transport = self.socket_transport()
|
2013-10-17 17:40:50 -03:00
|
|
|
transport.write(data)
|
|
|
|
|
|
|
|
self.loop.assert_writer(7, transport._write_ready)
|
2013-11-27 18:12:48 -04:00
|
|
|
self.assertEqual(list_to_buffer([b'data']), transport._buffer)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def test_write_tryagain(self):
|
|
|
|
self.sock.send.side_effect = BlockingIOError
|
|
|
|
|
|
|
|
data = b'data'
|
2015-01-15 08:18:32 -04:00
|
|
|
transport = self.socket_transport()
|
2013-10-17 17:40:50 -03:00
|
|
|
transport.write(data)
|
|
|
|
|
|
|
|
self.loop.assert_writer(7, transport._write_ready)
|
2013-11-27 18:12:48 -04:00
|
|
|
self.assertEqual(list_to_buffer([b'data']), transport._buffer)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-02-26 05:25:02 -04:00
|
|
|
@mock.patch('asyncio.selector_events.logger')
|
2013-10-17 17:40:50 -03:00
|
|
|
def test_write_exception(self, m_log):
|
|
|
|
err = self.sock.send.side_effect = OSError()
|
|
|
|
|
|
|
|
data = b'data'
|
2015-01-15 08:18:32 -04:00
|
|
|
transport = self.socket_transport()
|
2014-02-26 05:25:02 -04:00
|
|
|
transport._fatal_error = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
transport.write(data)
|
2014-02-18 20:40:41 -04:00
|
|
|
transport._fatal_error.assert_called_with(
|
|
|
|
err,
|
|
|
|
'Fatal write error on socket transport')
|
2013-10-17 17:40:50 -03:00
|
|
|
transport._conn_lost = 1
|
|
|
|
|
|
|
|
self.sock.reset_mock()
|
|
|
|
transport.write(data)
|
|
|
|
self.assertFalse(self.sock.send.called)
|
|
|
|
self.assertEqual(transport._conn_lost, 2)
|
|
|
|
transport.write(data)
|
|
|
|
transport.write(data)
|
|
|
|
transport.write(data)
|
|
|
|
transport.write(data)
|
|
|
|
m_log.warning.assert_called_with('socket.send() raised exception.')
|
|
|
|
|
|
|
|
def test_write_str(self):
|
2015-01-15 08:18:32 -04:00
|
|
|
transport = self.socket_transport()
|
2013-11-27 18:12:48 -04:00
|
|
|
self.assertRaises(TypeError, transport.write, 'str')
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def test_write_closing(self):
|
2015-01-15 08:18:32 -04:00
|
|
|
transport = self.socket_transport()
|
2013-10-17 17:40:50 -03:00
|
|
|
transport.close()
|
|
|
|
self.assertEqual(transport._conn_lost, 1)
|
|
|
|
transport.write(b'data')
|
|
|
|
self.assertEqual(transport._conn_lost, 2)
|
|
|
|
|
|
|
|
def test_write_ready(self):
|
|
|
|
data = b'data'
|
|
|
|
self.sock.send.return_value = len(data)
|
|
|
|
|
2015-01-15 08:18:32 -04:00
|
|
|
transport = self.socket_transport()
|
2013-11-27 18:12:48 -04:00
|
|
|
transport._buffer.extend(data)
|
2016-10-05 18:48:59 -03:00
|
|
|
self.loop._add_writer(7, transport._write_ready)
|
2013-10-17 17:40:50 -03:00
|
|
|
transport._write_ready()
|
|
|
|
self.assertTrue(self.sock.send.called)
|
|
|
|
self.assertFalse(self.loop.writers)
|
|
|
|
|
|
|
|
def test_write_ready_closing(self):
|
|
|
|
data = b'data'
|
|
|
|
self.sock.send.return_value = len(data)
|
|
|
|
|
2015-01-15 08:18:32 -04:00
|
|
|
transport = self.socket_transport()
|
2013-10-17 17:40:50 -03:00
|
|
|
transport._closing = True
|
2013-11-27 18:12:48 -04:00
|
|
|
transport._buffer.extend(data)
|
2016-10-05 18:48:59 -03:00
|
|
|
self.loop._add_writer(7, transport._write_ready)
|
2013-10-17 17:40:50 -03:00
|
|
|
transport._write_ready()
|
2013-11-27 18:12:48 -04:00
|
|
|
self.assertTrue(self.sock.send.called)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertFalse(self.loop.writers)
|
|
|
|
self.sock.close.assert_called_with()
|
|
|
|
self.protocol.connection_lost.assert_called_with(None)
|
|
|
|
|
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_write_ready_no_data(self):
|
2015-01-15 08:18:32 -04:00
|
|
|
transport = self.socket_transport()
|
2013-11-27 18:12:48 -04:00
|
|
|
# This is an internal error.
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertRaises(AssertionError, transport._write_ready)
|
|
|
|
|
|
|
|
def test_write_ready_partial(self):
|
|
|
|
data = b'data'
|
|
|
|
self.sock.send.return_value = 2
|
|
|
|
|
2015-01-15 08:18:32 -04:00
|
|
|
transport = self.socket_transport()
|
2013-11-27 18:12:48 -04:00
|
|
|
transport._buffer.extend(data)
|
2016-10-05 18:48:59 -03:00
|
|
|
self.loop._add_writer(7, transport._write_ready)
|
2013-10-17 17:40:50 -03:00
|
|
|
transport._write_ready()
|
|
|
|
self.loop.assert_writer(7, transport._write_ready)
|
2013-11-27 18:12:48 -04:00
|
|
|
self.assertEqual(list_to_buffer([b'ta']), transport._buffer)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def test_write_ready_partial_none(self):
|
|
|
|
data = b'data'
|
|
|
|
self.sock.send.return_value = 0
|
|
|
|
|
2015-01-15 08:18:32 -04:00
|
|
|
transport = self.socket_transport()
|
2013-11-27 18:12:48 -04:00
|
|
|
transport._buffer.extend(data)
|
2016-10-05 18:48:59 -03:00
|
|
|
self.loop._add_writer(7, transport._write_ready)
|
2013-10-17 17:40:50 -03:00
|
|
|
transport._write_ready()
|
|
|
|
self.loop.assert_writer(7, transport._write_ready)
|
2013-11-27 18:12:48 -04:00
|
|
|
self.assertEqual(list_to_buffer([b'data']), transport._buffer)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def test_write_ready_tryagain(self):
|
|
|
|
self.sock.send.side_effect = BlockingIOError
|
|
|
|
|
2015-01-15 08:18:32 -04:00
|
|
|
transport = self.socket_transport()
|
2013-11-27 18:12:48 -04:00
|
|
|
transport._buffer = list_to_buffer([b'data1', b'data2'])
|
2016-10-05 18:48:59 -03:00
|
|
|
self.loop._add_writer(7, transport._write_ready)
|
2013-10-17 17:40:50 -03:00
|
|
|
transport._write_ready()
|
|
|
|
|
|
|
|
self.loop.assert_writer(7, transport._write_ready)
|
2013-11-27 18:12:48 -04:00
|
|
|
self.assertEqual(list_to_buffer([b'data1data2']), transport._buffer)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def test_write_ready_exception(self):
|
|
|
|
err = self.sock.send.side_effect = OSError()
|
|
|
|
|
2015-01-15 08:18:32 -04:00
|
|
|
transport = self.socket_transport()
|
2014-02-26 05:25:02 -04:00
|
|
|
transport._fatal_error = mock.Mock()
|
2013-11-27 18:12:48 -04:00
|
|
|
transport._buffer.extend(b'data')
|
2013-10-17 17:40:50 -03:00
|
|
|
transport._write_ready()
|
2014-02-18 20:40:41 -04:00
|
|
|
transport._fatal_error.assert_called_with(
|
|
|
|
err,
|
|
|
|
'Fatal write error on socket transport')
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
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()
|
2013-10-17 17:40:50 -03:00
|
|
|
self.sock.send.side_effect = BlockingIOError
|
|
|
|
tr.write(b'data')
|
|
|
|
tr.write_eof()
|
2013-11-27 18:12:48 -04:00
|
|
|
self.assertEqual(tr._buffer, list_to_buffer([b'data']))
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertTrue(tr._eof)
|
|
|
|
self.assertFalse(self.sock.shutdown.called)
|
|
|
|
self.sock.send.side_effect = lambda _: 4
|
|
|
|
tr._write_ready()
|
2013-11-27 18:12:48 -04:00
|
|
|
self.assertTrue(self.sock.send.called)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.sock.shutdown.assert_called_with(socket.SHUT_WR)
|
|
|
|
tr.close()
|
|
|
|
|
2018-05-28 12:16:45 -03:00
|
|
|
def test_write_eof_after_close(self):
|
|
|
|
tr = self.socket_transport()
|
|
|
|
tr.close()
|
|
|
|
self.loop.run_until_complete(asyncio.sleep(0))
|
|
|
|
tr.write_eof()
|
|
|
|
|
2016-06-11 12:19:47 -03:00
|
|
|
@mock.patch('asyncio.base_events.logger')
|
|
|
|
def test_transport_close_remove_writer(self, m_log):
|
2016-10-05 18:48:59 -03:00
|
|
|
remove_writer = self.loop._remove_writer = mock.Mock()
|
2016-06-11 12:19:47 -03:00
|
|
|
|
|
|
|
transport = self.socket_transport()
|
|
|
|
transport.close()
|
|
|
|
remove_writer.assert_called_with(self.sock_fd)
|
|
|
|
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2018-01-28 17:30:26 -04:00
|
|
|
class SelectorSocketTransportBufferedProtocolTests(test_utils.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
super().setUp()
|
|
|
|
self.loop = self.new_test_loop()
|
|
|
|
|
|
|
|
self.protocol = test_utils.make_test_protocol(asyncio.BufferedProtocol)
|
2018-05-28 15:31:28 -03:00
|
|
|
self.buf = bytearray(1)
|
|
|
|
self.protocol.get_buffer.side_effect = lambda hint: self.buf
|
2018-01-28 17:30:26 -04:00
|
|
|
|
|
|
|
self.sock = mock.Mock(socket.socket)
|
|
|
|
self.sock_fd = self.sock.fileno.return_value = 7
|
|
|
|
|
|
|
|
def socket_transport(self, waiter=None):
|
|
|
|
transport = _SelectorSocketTransport(self.loop, self.sock,
|
|
|
|
self.protocol, waiter=waiter)
|
|
|
|
self.addCleanup(close_transport, transport)
|
|
|
|
return transport
|
|
|
|
|
|
|
|
def test_ctor(self):
|
2019-09-11 10:07:37 -03:00
|
|
|
waiter = self.loop.create_future()
|
2018-01-28 17:30:26 -04:00
|
|
|
tr = self.socket_transport(waiter=waiter)
|
|
|
|
self.loop.run_until_complete(waiter)
|
|
|
|
|
|
|
|
self.loop.assert_reader(7, tr._read_ready)
|
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
self.protocol.connection_made.assert_called_with(tr)
|
|
|
|
|
|
|
|
def test_get_buffer_error(self):
|
|
|
|
transport = self.socket_transport()
|
|
|
|
transport._fatal_error = mock.Mock()
|
|
|
|
|
|
|
|
self.loop.call_exception_handler = mock.Mock()
|
|
|
|
self.protocol.get_buffer.side_effect = LookupError()
|
|
|
|
|
|
|
|
transport._read_ready()
|
|
|
|
|
|
|
|
self.assertTrue(transport._fatal_error.called)
|
|
|
|
self.assertTrue(self.protocol.get_buffer.called)
|
|
|
|
self.assertFalse(self.protocol.buffer_updated.called)
|
|
|
|
|
2018-05-28 15:31:28 -03:00
|
|
|
def test_get_buffer_zerosized(self):
|
|
|
|
transport = self.socket_transport()
|
|
|
|
transport._fatal_error = mock.Mock()
|
|
|
|
|
|
|
|
self.loop.call_exception_handler = mock.Mock()
|
|
|
|
self.protocol.get_buffer.side_effect = lambda hint: bytearray(0)
|
|
|
|
|
|
|
|
transport._read_ready()
|
|
|
|
|
|
|
|
self.assertTrue(transport._fatal_error.called)
|
|
|
|
self.assertTrue(self.protocol.get_buffer.called)
|
|
|
|
self.assertFalse(self.protocol.buffer_updated.called)
|
|
|
|
|
|
|
|
def test_proto_type_switch(self):
|
|
|
|
self.protocol = test_utils.make_test_protocol(asyncio.Protocol)
|
|
|
|
transport = self.socket_transport()
|
|
|
|
|
|
|
|
self.sock.recv.return_value = b'data'
|
|
|
|
transport._read_ready()
|
|
|
|
|
|
|
|
self.protocol.data_received.assert_called_with(b'data')
|
|
|
|
|
|
|
|
# switch protocol to a BufferedProtocol
|
|
|
|
|
|
|
|
buf_proto = test_utils.make_test_protocol(asyncio.BufferedProtocol)
|
|
|
|
buf = bytearray(4)
|
|
|
|
buf_proto.get_buffer.side_effect = lambda hint: buf
|
|
|
|
|
|
|
|
transport.set_protocol(buf_proto)
|
|
|
|
|
|
|
|
self.sock.recv_into.return_value = 10
|
|
|
|
transport._read_ready()
|
|
|
|
|
|
|
|
buf_proto.get_buffer.assert_called_with(-1)
|
|
|
|
buf_proto.buffer_updated.assert_called_with(10)
|
|
|
|
|
2018-01-28 17:30:26 -04:00
|
|
|
def test_buffer_updated_error(self):
|
|
|
|
transport = self.socket_transport()
|
|
|
|
transport._fatal_error = mock.Mock()
|
|
|
|
|
|
|
|
self.loop.call_exception_handler = mock.Mock()
|
|
|
|
self.protocol.buffer_updated.side_effect = LookupError()
|
|
|
|
|
|
|
|
self.sock.recv_into.return_value = 10
|
|
|
|
transport._read_ready()
|
|
|
|
|
|
|
|
self.assertTrue(transport._fatal_error.called)
|
|
|
|
self.assertTrue(self.protocol.get_buffer.called)
|
|
|
|
self.assertTrue(self.protocol.buffer_updated.called)
|
|
|
|
|
|
|
|
def test_read_eof_received_error(self):
|
|
|
|
transport = self.socket_transport()
|
|
|
|
transport.close = mock.Mock()
|
|
|
|
transport._fatal_error = mock.Mock()
|
|
|
|
|
|
|
|
self.loop.call_exception_handler = mock.Mock()
|
|
|
|
|
|
|
|
self.protocol.eof_received.side_effect = LookupError()
|
|
|
|
|
|
|
|
self.sock.recv_into.return_value = 0
|
|
|
|
transport._read_ready()
|
|
|
|
|
|
|
|
self.protocol.eof_received.assert_called_with()
|
|
|
|
self.assertTrue(transport._fatal_error.called)
|
|
|
|
|
|
|
|
def test_read_ready(self):
|
|
|
|
transport = self.socket_transport()
|
|
|
|
|
|
|
|
self.sock.recv_into.return_value = 10
|
|
|
|
transport._read_ready()
|
|
|
|
|
2018-05-28 15:31:28 -03:00
|
|
|
self.protocol.get_buffer.assert_called_with(-1)
|
2018-01-28 17:30:26 -04:00
|
|
|
self.protocol.buffer_updated.assert_called_with(10)
|
|
|
|
|
|
|
|
def test_read_ready_eof(self):
|
|
|
|
transport = self.socket_transport()
|
|
|
|
transport.close = mock.Mock()
|
|
|
|
|
|
|
|
self.sock.recv_into.return_value = 0
|
|
|
|
transport._read_ready()
|
|
|
|
|
|
|
|
self.protocol.eof_received.assert_called_with()
|
|
|
|
transport.close.assert_called_with()
|
|
|
|
|
|
|
|
def test_read_ready_eof_keep_open(self):
|
|
|
|
transport = self.socket_transport()
|
|
|
|
transport.close = mock.Mock()
|
|
|
|
|
|
|
|
self.sock.recv_into.return_value = 0
|
|
|
|
self.protocol.eof_received.return_value = True
|
|
|
|
transport._read_ready()
|
|
|
|
|
|
|
|
self.protocol.eof_received.assert_called_with()
|
|
|
|
self.assertFalse(transport.close.called)
|
|
|
|
|
|
|
|
@mock.patch('logging.exception')
|
|
|
|
def test_read_ready_tryagain(self, m_exc):
|
|
|
|
self.sock.recv_into.side_effect = BlockingIOError
|
|
|
|
|
|
|
|
transport = self.socket_transport()
|
|
|
|
transport._fatal_error = mock.Mock()
|
|
|
|
transport._read_ready()
|
|
|
|
|
|
|
|
self.assertFalse(transport._fatal_error.called)
|
|
|
|
|
|
|
|
@mock.patch('logging.exception')
|
|
|
|
def test_read_ready_tryagain_interrupted(self, m_exc):
|
|
|
|
self.sock.recv_into.side_effect = InterruptedError
|
|
|
|
|
|
|
|
transport = self.socket_transport()
|
|
|
|
transport._fatal_error = mock.Mock()
|
|
|
|
transport._read_ready()
|
|
|
|
|
|
|
|
self.assertFalse(transport._fatal_error.called)
|
|
|
|
|
|
|
|
@mock.patch('logging.exception')
|
|
|
|
def test_read_ready_conn_reset(self, m_exc):
|
|
|
|
err = self.sock.recv_into.side_effect = ConnectionResetError()
|
|
|
|
|
|
|
|
transport = self.socket_transport()
|
|
|
|
transport._force_close = mock.Mock()
|
|
|
|
with test_utils.disable_logger():
|
|
|
|
transport._read_ready()
|
|
|
|
transport._force_close.assert_called_with(err)
|
|
|
|
|
|
|
|
@mock.patch('logging.exception')
|
|
|
|
def test_read_ready_err(self, m_exc):
|
|
|
|
err = self.sock.recv_into.side_effect = OSError()
|
|
|
|
|
|
|
|
transport = self.socket_transport()
|
|
|
|
transport._fatal_error = mock.Mock()
|
|
|
|
transport._read_ready()
|
|
|
|
|
|
|
|
transport._fatal_error.assert_called_with(
|
|
|
|
err,
|
|
|
|
'Fatal read error on socket transport')
|
|
|
|
|
|
|
|
|
2014-06-17 20:36:32 -03:00
|
|
|
class SelectorDatagramTransportTests(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()
|
2014-01-25 10:32:06 -04:00
|
|
|
self.protocol = test_utils.make_test_protocol(asyncio.DatagramProtocol)
|
2014-02-26 05:25:02 -04:00
|
|
|
self.sock = mock.Mock(spec_set=socket.socket)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.sock.fileno.return_value = 7
|
|
|
|
|
2015-01-15 08:18:32 -04:00
|
|
|
def datagram_transport(self, address=None):
|
2019-05-07 14:18:49 -03:00
|
|
|
self.sock.getpeername.side_effect = None if address else OSError
|
2015-01-15 08:18:32 -04:00
|
|
|
transport = _SelectorDatagramTransport(self.loop, self.sock,
|
|
|
|
self.protocol,
|
|
|
|
address=address)
|
|
|
|
self.addCleanup(close_transport, transport)
|
|
|
|
return transport
|
|
|
|
|
2013-10-17 17:40:50 -03:00
|
|
|
def test_read_ready(self):
|
2015-01-15 08:18:32 -04:00
|
|
|
transport = self.datagram_transport()
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
self.sock.recvfrom.return_value = (b'data', ('0.0.0.0', 1234))
|
|
|
|
transport._read_ready()
|
|
|
|
|
|
|
|
self.protocol.datagram_received.assert_called_with(
|
|
|
|
b'data', ('0.0.0.0', 1234))
|
|
|
|
|
2022-10-29 13:43:42 -03:00
|
|
|
def test_transport_inheritance(self):
|
|
|
|
transport = self.datagram_transport()
|
|
|
|
self.assertIsInstance(transport, asyncio.DatagramTransport)
|
|
|
|
|
2013-10-17 17:40:50 -03:00
|
|
|
def test_read_ready_tryagain(self):
|
2015-01-15 08:18:32 -04:00
|
|
|
transport = self.datagram_transport()
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
self.sock.recvfrom.side_effect = BlockingIOError
|
2014-02-26 05:25:02 -04:00
|
|
|
transport._fatal_error = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
transport._read_ready()
|
|
|
|
|
|
|
|
self.assertFalse(transport._fatal_error.called)
|
|
|
|
|
|
|
|
def test_read_ready_err(self):
|
2015-01-15 08:18:32 -04:00
|
|
|
transport = self.datagram_transport()
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2013-11-15 20:51:48 -04:00
|
|
|
err = self.sock.recvfrom.side_effect = RuntimeError()
|
2014-02-26 05:25:02 -04:00
|
|
|
transport._fatal_error = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
transport._read_ready()
|
|
|
|
|
2014-02-18 20:40:41 -04:00
|
|
|
transport._fatal_error.assert_called_with(
|
|
|
|
err,
|
|
|
|
'Fatal read error on datagram transport')
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2013-11-15 20:51:48 -04:00
|
|
|
def test_read_ready_oserr(self):
|
2015-01-15 08:18:32 -04:00
|
|
|
transport = self.datagram_transport()
|
2013-11-15 20:51:48 -04:00
|
|
|
|
|
|
|
err = self.sock.recvfrom.side_effect = OSError()
|
2014-02-26 05:25:02 -04:00
|
|
|
transport._fatal_error = mock.Mock()
|
2013-11-15 20:51:48 -04:00
|
|
|
transport._read_ready()
|
|
|
|
|
|
|
|
self.assertFalse(transport._fatal_error.called)
|
|
|
|
self.protocol.error_received.assert_called_with(err)
|
|
|
|
|
2013-10-17 17:40:50 -03:00
|
|
|
def test_sendto(self):
|
|
|
|
data = b'data'
|
2015-01-15 08:18:32 -04:00
|
|
|
transport = self.datagram_transport()
|
2013-10-17 17:40:50 -03:00
|
|
|
transport.sendto(data, ('0.0.0.0', 1234))
|
|
|
|
self.assertTrue(self.sock.sendto.called)
|
|
|
|
self.assertEqual(
|
|
|
|
self.sock.sendto.call_args[0], (data, ('0.0.0.0', 1234)))
|
|
|
|
|
2013-11-27 18:12:48 -04:00
|
|
|
def test_sendto_bytearray(self):
|
|
|
|
data = bytearray(b'data')
|
2015-01-15 08:18:32 -04:00
|
|
|
transport = self.datagram_transport()
|
2013-11-27 18:12:48 -04:00
|
|
|
transport.sendto(data, ('0.0.0.0', 1234))
|
|
|
|
self.assertTrue(self.sock.sendto.called)
|
|
|
|
self.assertEqual(
|
|
|
|
self.sock.sendto.call_args[0], (data, ('0.0.0.0', 1234)))
|
|
|
|
|
|
|
|
def test_sendto_memoryview(self):
|
|
|
|
data = memoryview(b'data')
|
2015-01-15 08:18:32 -04:00
|
|
|
transport = self.datagram_transport()
|
2013-11-27 18:12:48 -04:00
|
|
|
transport.sendto(data, ('0.0.0.0', 1234))
|
|
|
|
self.assertTrue(self.sock.sendto.called)
|
|
|
|
self.assertEqual(
|
|
|
|
self.sock.sendto.call_args[0], (data, ('0.0.0.0', 1234)))
|
|
|
|
|
2013-10-17 17:40:50 -03:00
|
|
|
def test_sendto_no_data(self):
|
2015-01-15 08:18:32 -04:00
|
|
|
transport = self.datagram_transport()
|
2013-10-17 17:40:50 -03:00
|
|
|
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):
|
2015-01-15 08:18:32 -04:00
|
|
|
transport = self.datagram_transport()
|
2013-10-17 17:40:50 -03:00
|
|
|
transport._buffer.append((b'data1', ('0.0.0.0', 12345)))
|
|
|
|
transport.sendto(b'data2', ('0.0.0.0', 12345))
|
|
|
|
self.assertFalse(self.sock.sendto.called)
|
|
|
|
self.assertEqual(
|
|
|
|
[(b'data1', ('0.0.0.0', 12345)),
|
|
|
|
(b'data2', ('0.0.0.0', 12345))],
|
|
|
|
list(transport._buffer))
|
|
|
|
|
2013-11-27 18:12:48 -04:00
|
|
|
def test_sendto_buffer_bytearray(self):
|
|
|
|
data2 = bytearray(b'data2')
|
2015-01-15 08:18:32 -04:00
|
|
|
transport = self.datagram_transport()
|
2013-11-27 18:12:48 -04:00
|
|
|
transport._buffer.append((b'data1', ('0.0.0.0', 12345)))
|
|
|
|
transport.sendto(data2, ('0.0.0.0', 12345))
|
|
|
|
self.assertFalse(self.sock.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')
|
2015-01-15 08:18:32 -04:00
|
|
|
transport = self.datagram_transport()
|
2013-11-27 18:12:48 -04:00
|
|
|
transport._buffer.append((b'data1', ('0.0.0.0', 12345)))
|
|
|
|
transport.sendto(data2, ('0.0.0.0', 12345))
|
|
|
|
self.assertFalse(self.sock.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)
|
|
|
|
|
2013-10-17 17:40:50 -03:00
|
|
|
def test_sendto_tryagain(self):
|
|
|
|
data = b'data'
|
|
|
|
|
|
|
|
self.sock.sendto.side_effect = BlockingIOError
|
|
|
|
|
2015-01-15 08:18:32 -04:00
|
|
|
transport = self.datagram_transport()
|
2013-10-17 17:40:50 -03:00
|
|
|
transport.sendto(data, ('0.0.0.0', 12345))
|
|
|
|
|
|
|
|
self.loop.assert_writer(7, transport._sendto_ready)
|
|
|
|
self.assertEqual(
|
|
|
|
[(b'data', ('0.0.0.0', 12345))], list(transport._buffer))
|
|
|
|
|
2014-02-26 05:25:02 -04:00
|
|
|
@mock.patch('asyncio.selector_events.logger')
|
2013-10-17 17:40:50 -03:00
|
|
|
def test_sendto_exception(self, m_log):
|
|
|
|
data = b'data'
|
2013-11-15 20:51:48 -04:00
|
|
|
err = self.sock.sendto.side_effect = RuntimeError()
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2015-01-15 08:18:32 -04:00
|
|
|
transport = self.datagram_transport()
|
2014-02-26 05:25:02 -04:00
|
|
|
transport._fatal_error = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
transport.sendto(data, ())
|
|
|
|
|
|
|
|
self.assertTrue(transport._fatal_error.called)
|
2014-02-18 20:40:41 -04:00
|
|
|
transport._fatal_error.assert_called_with(
|
|
|
|
err,
|
|
|
|
'Fatal write error on datagram transport')
|
2013-10-17 17:40:50 -03:00
|
|
|
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.send() raised exception.')
|
|
|
|
|
2013-11-15 20:51:48 -04:00
|
|
|
def test_sendto_error_received(self):
|
2013-10-17 17:40:50 -03:00
|
|
|
data = b'data'
|
|
|
|
|
|
|
|
self.sock.sendto.side_effect = ConnectionRefusedError
|
|
|
|
|
2015-01-15 08:18:32 -04:00
|
|
|
transport = self.datagram_transport()
|
2014-02-26 05:25:02 -04:00
|
|
|
transport._fatal_error = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
transport.sendto(data, ())
|
|
|
|
|
|
|
|
self.assertEqual(transport._conn_lost, 0)
|
|
|
|
self.assertFalse(transport._fatal_error.called)
|
|
|
|
|
2013-11-15 20:51:48 -04:00
|
|
|
def test_sendto_error_received_connected(self):
|
2013-10-17 17:40:50 -03:00
|
|
|
data = b'data'
|
|
|
|
|
|
|
|
self.sock.send.side_effect = ConnectionRefusedError
|
|
|
|
|
2015-01-15 08:18:32 -04:00
|
|
|
transport = self.datagram_transport(address=('0.0.0.0', 1))
|
2014-02-26 05:25:02 -04:00
|
|
|
transport._fatal_error = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
transport.sendto(data)
|
|
|
|
|
2013-11-15 20:51:48 -04:00
|
|
|
self.assertFalse(transport._fatal_error.called)
|
|
|
|
self.assertTrue(self.protocol.error_received.called)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def test_sendto_str(self):
|
2015-01-15 08:18:32 -04:00
|
|
|
transport = self.datagram_transport()
|
2013-11-27 18:12:48 -04:00
|
|
|
self.assertRaises(TypeError, transport.sendto, 'str', ())
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def test_sendto_connected_addr(self):
|
2015-01-15 08:18:32 -04:00
|
|
|
transport = self.datagram_transport(address=('0.0.0.0', 1))
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertRaises(
|
2013-11-27 18:12:48 -04:00
|
|
|
ValueError, transport.sendto, b'str', ('0.0.0.0', 2))
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def test_sendto_closing(self):
|
2015-01-15 08:18:32 -04:00
|
|
|
transport = self.datagram_transport(address=(1,))
|
2013-10-17 17:40:50 -03:00
|
|
|
transport.close()
|
|
|
|
self.assertEqual(transport._conn_lost, 1)
|
|
|
|
transport.sendto(b'data', (1,))
|
|
|
|
self.assertEqual(transport._conn_lost, 2)
|
|
|
|
|
|
|
|
def test_sendto_ready(self):
|
|
|
|
data = b'data'
|
|
|
|
self.sock.sendto.return_value = len(data)
|
|
|
|
|
2015-01-15 08:18:32 -04:00
|
|
|
transport = self.datagram_transport()
|
2013-10-17 17:40:50 -03:00
|
|
|
transport._buffer.append((data, ('0.0.0.0', 12345)))
|
2016-10-05 18:48:59 -03:00
|
|
|
self.loop._add_writer(7, transport._sendto_ready)
|
2013-10-17 17:40:50 -03:00
|
|
|
transport._sendto_ready()
|
|
|
|
self.assertTrue(self.sock.sendto.called)
|
|
|
|
self.assertEqual(
|
|
|
|
self.sock.sendto.call_args[0], (data, ('0.0.0.0', 12345)))
|
|
|
|
self.assertFalse(self.loop.writers)
|
|
|
|
|
|
|
|
def test_sendto_ready_closing(self):
|
|
|
|
data = b'data'
|
|
|
|
self.sock.send.return_value = len(data)
|
|
|
|
|
2015-01-15 08:18:32 -04:00
|
|
|
transport = self.datagram_transport()
|
2013-10-17 17:40:50 -03:00
|
|
|
transport._closing = True
|
|
|
|
transport._buffer.append((data, ()))
|
2016-10-05 18:48:59 -03:00
|
|
|
self.loop._add_writer(7, transport._sendto_ready)
|
2013-10-17 17:40:50 -03:00
|
|
|
transport._sendto_ready()
|
|
|
|
self.sock.sendto.assert_called_with(data, ())
|
|
|
|
self.assertFalse(self.loop.writers)
|
|
|
|
self.sock.close.assert_called_with()
|
|
|
|
self.protocol.connection_lost.assert_called_with(None)
|
|
|
|
|
|
|
|
def test_sendto_ready_no_data(self):
|
2015-01-15 08:18:32 -04:00
|
|
|
transport = self.datagram_transport()
|
2016-10-05 18:48:59 -03:00
|
|
|
self.loop._add_writer(7, transport._sendto_ready)
|
2013-10-17 17:40:50 -03:00
|
|
|
transport._sendto_ready()
|
|
|
|
self.assertFalse(self.sock.sendto.called)
|
|
|
|
self.assertFalse(self.loop.writers)
|
|
|
|
|
|
|
|
def test_sendto_ready_tryagain(self):
|
|
|
|
self.sock.sendto.side_effect = BlockingIOError
|
|
|
|
|
2015-01-15 08:18:32 -04:00
|
|
|
transport = self.datagram_transport()
|
2013-10-17 17:40:50 -03:00
|
|
|
transport._buffer.extend([(b'data1', ()), (b'data2', ())])
|
2016-10-05 18:48:59 -03:00
|
|
|
self.loop._add_writer(7, transport._sendto_ready)
|
2013-10-17 17:40:50 -03:00
|
|
|
transport._sendto_ready()
|
|
|
|
|
|
|
|
self.loop.assert_writer(7, transport._sendto_ready)
|
|
|
|
self.assertEqual(
|
|
|
|
[(b'data1', ()), (b'data2', ())],
|
|
|
|
list(transport._buffer))
|
|
|
|
|
|
|
|
def test_sendto_ready_exception(self):
|
2013-11-15 20:51:48 -04:00
|
|
|
err = self.sock.sendto.side_effect = RuntimeError()
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2015-01-15 08:18:32 -04:00
|
|
|
transport = self.datagram_transport()
|
2014-02-26 05:25:02 -04:00
|
|
|
transport._fatal_error = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
transport._buffer.append((b'data', ()))
|
|
|
|
transport._sendto_ready()
|
|
|
|
|
2014-02-18 20:40:41 -04:00
|
|
|
transport._fatal_error.assert_called_with(
|
|
|
|
err,
|
|
|
|
'Fatal write error on datagram transport')
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2013-11-15 20:51:48 -04:00
|
|
|
def test_sendto_ready_error_received(self):
|
2013-10-17 17:40:50 -03:00
|
|
|
self.sock.sendto.side_effect = ConnectionRefusedError
|
|
|
|
|
2015-01-15 08:18:32 -04:00
|
|
|
transport = self.datagram_transport()
|
2014-02-26 05:25:02 -04:00
|
|
|
transport._fatal_error = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
transport._buffer.append((b'data', ()))
|
|
|
|
transport._sendto_ready()
|
|
|
|
|
|
|
|
self.assertFalse(transport._fatal_error.called)
|
|
|
|
|
2013-11-15 20:51:48 -04:00
|
|
|
def test_sendto_ready_error_received_connection(self):
|
2013-10-17 17:40:50 -03:00
|
|
|
self.sock.send.side_effect = ConnectionRefusedError
|
|
|
|
|
2015-01-15 08:18:32 -04:00
|
|
|
transport = self.datagram_transport(address=('0.0.0.0', 1))
|
2014-02-26 05:25:02 -04:00
|
|
|
transport._fatal_error = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
transport._buffer.append((b'data', ()))
|
|
|
|
transport._sendto_ready()
|
|
|
|
|
2013-11-15 20:51:48 -04:00
|
|
|
self.assertFalse(transport._fatal_error.called)
|
|
|
|
self.assertTrue(self.protocol.error_received.called)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-02-26 05:25:02 -04:00
|
|
|
@mock.patch('asyncio.base_events.logger.error')
|
2013-10-17 17:40:50 -03:00
|
|
|
def test_fatal_error_connected(self, m_exc):
|
2015-01-15 08:18:32 -04:00
|
|
|
transport = self.datagram_transport(address=('0.0.0.0', 1))
|
2013-10-17 17:40:50 -03:00
|
|
|
err = ConnectionRefusedError()
|
|
|
|
transport._fatal_error(err)
|
2013-11-15 20:51:48 -04:00
|
|
|
self.assertFalse(self.protocol.error_received.called)
|
2019-05-27 10:28:34 -03:00
|
|
|
m_exc.assert_not_called()
|
|
|
|
|
|
|
|
@mock.patch('asyncio.base_events.logger.error')
|
|
|
|
def test_fatal_error_connected_custom_error(self, m_exc):
|
|
|
|
class MyException(Exception):
|
|
|
|
pass
|
|
|
|
transport = self.datagram_transport(address=('0.0.0.0', 1))
|
|
|
|
err = MyException()
|
|
|
|
transport._fatal_error(err)
|
|
|
|
self.assertFalse(self.protocol.error_received.called)
|
2014-02-18 19:02:19 -04:00
|
|
|
m_exc.assert_called_with(
|
|
|
|
test_utils.MockPattern(
|
2014-02-18 20:40:41 -04:00
|
|
|
'Fatal error on transport\nprotocol:.*\ntransport:.*'),
|
2019-05-27 10:28:34 -03:00
|
|
|
exc_info=(MyException, MOCK_ANY, MOCK_ANY))
|
2013-10-20 16:02:53 -03:00
|
|
|
|
2017-12-15 20:32:25 -04:00
|
|
|
|
2013-10-20 16:02:53 -03:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|