2013-10-17 17:40:50 -03:00
|
|
|
"""Tests for selector_events.py"""
|
|
|
|
|
|
|
|
import errno
|
|
|
|
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
|
2013-10-17 17:40:50 -03:00
|
|
|
from asyncio import selectors
|
|
|
|
from asyncio import test_utils
|
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 _SelectorSslTransport
|
|
|
|
from asyncio.selector_events import _SelectorSocketTransport
|
|
|
|
from asyncio.selector_events import _SelectorDatagramTransport
|
|
|
|
|
|
|
|
|
2014-02-26 05:25:02 -04:00
|
|
|
MOCK_ANY = mock.ANY
|
2014-02-18 19:02:19 -04:00
|
|
|
|
|
|
|
|
2014-01-25 17:22:18 -04:00
|
|
|
class TestBaseSelectorEventLoop(BaseSelectorEventLoop):
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2015-01-15 08:17:34 -04:00
|
|
|
def close(self):
|
|
|
|
# Don't call the close() method of the parent class, because the
|
|
|
|
# selector is mocked
|
|
|
|
self._closed = True
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
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):
|
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()
|
2015-01-28 19:36:51 -04:00
|
|
|
self.loop.add_reader._is_coroutine = False
|
2014-02-11 06:34:30 -04:00
|
|
|
transport = self.loop._make_socket_transport(m, asyncio.Protocol())
|
|
|
|
self.assertIsInstance(transport, _SelectorSocketTransport)
|
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
|
|
|
|
2013-11-01 18:22:30 -03:00
|
|
|
@unittest.skipIf(ssl is None, 'No ssl module')
|
2013-10-17 17:40:50 -03:00
|
|
|
def test_make_ssl_transport(self):
|
2014-02-26 05:25:02 -04:00
|
|
|
m = mock.Mock()
|
|
|
|
self.loop.add_reader = mock.Mock()
|
2015-01-28 19:36:51 -04:00
|
|
|
self.loop.add_reader._is_coroutine = False
|
2014-02-26 05:25:02 -04:00
|
|
|
self.loop.add_writer = mock.Mock()
|
|
|
|
self.loop.remove_reader = mock.Mock()
|
|
|
|
self.loop.remove_writer = mock.Mock()
|
2014-02-11 06:34:30 -04:00
|
|
|
waiter = asyncio.Future(loop=self.loop)
|
2014-08-25 18:20:52 -03:00
|
|
|
with test_utils.disable_logger():
|
|
|
|
transport = self.loop._make_ssl_transport(
|
|
|
|
m, asyncio.Protocol(), m, waiter)
|
2015-01-15 08:18:32 -04:00
|
|
|
# execute the handshake while the logger is disabled
|
|
|
|
# to ignore SSL handshake failure
|
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
|
2015-01-13 19:19:09 -04:00
|
|
|
# Sanity check
|
|
|
|
class_name = transport.__class__.__name__
|
|
|
|
self.assertIn("ssl", class_name.lower())
|
|
|
|
self.assertIn("transport", class_name.lower())
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2015-01-15 08:18:32 -04:00
|
|
|
transport.close()
|
|
|
|
# execute pending callbacks to close the socket transport
|
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
|
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()
|
2013-11-01 18:22:30 -03:00
|
|
|
with self.assertRaises(RuntimeError):
|
|
|
|
self.loop._make_ssl_transport(m, m, m, m)
|
|
|
|
|
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
|
2014-02-26 05:25:02 -04: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
|
|
|
|
f = asyncio.Future(loop=self.loop)
|
|
|
|
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_socketpair(self):
|
|
|
|
self.assertRaises(NotImplementedError, self.loop._socketpair)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
def test_sock_recv(self):
|
2014-08-25 18:20:52 -03:00
|
|
|
sock = test_utils.mock_nonblocking_socket()
|
2014-02-26 05:25:02 -04:00
|
|
|
self.loop._sock_recv = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
f = self.loop.sock_recv(sock, 1024)
|
2014-01-25 10:32:06 -04:00
|
|
|
self.assertIsInstance(f, asyncio.Future)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.loop._sock_recv.assert_called_with(f, False, sock, 1024)
|
|
|
|
|
|
|
|
def test__sock_recv_canceled_fut(self):
|
2014-02-26 05:25:02 -04:00
|
|
|
sock = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
f = asyncio.Future(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
f.cancel()
|
|
|
|
|
|
|
|
self.loop._sock_recv(f, False, sock, 1024)
|
|
|
|
self.assertFalse(sock.recv.called)
|
|
|
|
|
|
|
|
def test__sock_recv_unregister(self):
|
2014-02-26 05:25:02 -04:00
|
|
|
sock = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
sock.fileno.return_value = 10
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
f = asyncio.Future(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
f.cancel()
|
|
|
|
|
2014-02-26 05:25:02 -04:00
|
|
|
self.loop.remove_reader = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
self.loop._sock_recv(f, True, sock, 1024)
|
|
|
|
self.assertEqual((10,), self.loop.remove_reader.call_args[0])
|
|
|
|
|
|
|
|
def test__sock_recv_tryagain(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
f = asyncio.Future(loop=self.loop)
|
2014-02-26 05:25:02 -04:00
|
|
|
sock = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
sock.fileno.return_value = 10
|
|
|
|
sock.recv.side_effect = BlockingIOError
|
|
|
|
|
2014-02-26 05:25:02 -04:00
|
|
|
self.loop.add_reader = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
self.loop._sock_recv(f, False, sock, 1024)
|
|
|
|
self.assertEqual((10, self.loop._sock_recv, f, True, sock, 1024),
|
|
|
|
self.loop.add_reader.call_args[0])
|
|
|
|
|
|
|
|
def test__sock_recv_exception(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
f = asyncio.Future(loop=self.loop)
|
2014-02-26 05:25:02 -04:00
|
|
|
sock = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
sock.fileno.return_value = 10
|
|
|
|
err = sock.recv.side_effect = OSError()
|
|
|
|
|
|
|
|
self.loop._sock_recv(f, False, sock, 1024)
|
|
|
|
self.assertIs(err, f.exception())
|
|
|
|
|
|
|
|
def test_sock_sendall(self):
|
2014-08-25 18:20:52 -03:00
|
|
|
sock = test_utils.mock_nonblocking_socket()
|
2014-02-26 05:25:02 -04:00
|
|
|
self.loop._sock_sendall = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
f = self.loop.sock_sendall(sock, b'data')
|
2014-01-25 10:32:06 -04:00
|
|
|
self.assertIsInstance(f, asyncio.Future)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertEqual(
|
|
|
|
(f, False, sock, b'data'),
|
|
|
|
self.loop._sock_sendall.call_args[0])
|
|
|
|
|
|
|
|
def test_sock_sendall_nodata(self):
|
2014-08-25 18:20:52 -03:00
|
|
|
sock = test_utils.mock_nonblocking_socket()
|
2014-02-26 05:25:02 -04:00
|
|
|
self.loop._sock_sendall = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
f = self.loop.sock_sendall(sock, b'')
|
2014-01-25 10:32:06 -04:00
|
|
|
self.assertIsInstance(f, asyncio.Future)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertTrue(f.done())
|
|
|
|
self.assertIsNone(f.result())
|
|
|
|
self.assertFalse(self.loop._sock_sendall.called)
|
|
|
|
|
|
|
|
def test__sock_sendall_canceled_fut(self):
|
2014-02-26 05:25:02 -04:00
|
|
|
sock = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
f = asyncio.Future(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
f.cancel()
|
|
|
|
|
|
|
|
self.loop._sock_sendall(f, False, sock, b'data')
|
|
|
|
self.assertFalse(sock.send.called)
|
|
|
|
|
|
|
|
def test__sock_sendall_unregister(self):
|
2014-02-26 05:25:02 -04:00
|
|
|
sock = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
sock.fileno.return_value = 10
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
f = asyncio.Future(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
f.cancel()
|
|
|
|
|
2014-02-26 05:25:02 -04:00
|
|
|
self.loop.remove_writer = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
self.loop._sock_sendall(f, True, sock, b'data')
|
|
|
|
self.assertEqual((10,), self.loop.remove_writer.call_args[0])
|
|
|
|
|
|
|
|
def test__sock_sendall_tryagain(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
f = asyncio.Future(loop=self.loop)
|
2014-02-26 05:25:02 -04:00
|
|
|
sock = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
sock.fileno.return_value = 10
|
|
|
|
sock.send.side_effect = BlockingIOError
|
|
|
|
|
2014-02-26 05:25:02 -04:00
|
|
|
self.loop.add_writer = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
self.loop._sock_sendall(f, False, sock, b'data')
|
|
|
|
self.assertEqual(
|
|
|
|
(10, self.loop._sock_sendall, f, True, sock, b'data'),
|
|
|
|
self.loop.add_writer.call_args[0])
|
|
|
|
|
|
|
|
def test__sock_sendall_interrupted(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
f = asyncio.Future(loop=self.loop)
|
2014-02-26 05:25:02 -04:00
|
|
|
sock = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
sock.fileno.return_value = 10
|
|
|
|
sock.send.side_effect = InterruptedError
|
|
|
|
|
2014-02-26 05:25:02 -04:00
|
|
|
self.loop.add_writer = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
self.loop._sock_sendall(f, False, sock, b'data')
|
|
|
|
self.assertEqual(
|
|
|
|
(10, self.loop._sock_sendall, f, True, sock, b'data'),
|
|
|
|
self.loop.add_writer.call_args[0])
|
|
|
|
|
|
|
|
def test__sock_sendall_exception(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
f = asyncio.Future(loop=self.loop)
|
2014-02-26 05:25:02 -04:00
|
|
|
sock = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
sock.fileno.return_value = 10
|
|
|
|
err = sock.send.side_effect = OSError()
|
|
|
|
|
|
|
|
self.loop._sock_sendall(f, False, sock, b'data')
|
|
|
|
self.assertIs(f.exception(), err)
|
|
|
|
|
|
|
|
def test__sock_sendall(self):
|
2014-02-26 05:25:02 -04:00
|
|
|
sock = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
f = asyncio.Future(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
sock.fileno.return_value = 10
|
|
|
|
sock.send.return_value = 4
|
|
|
|
|
|
|
|
self.loop._sock_sendall(f, False, sock, b'data')
|
|
|
|
self.assertTrue(f.done())
|
|
|
|
self.assertIsNone(f.result())
|
|
|
|
|
|
|
|
def test__sock_sendall_partial(self):
|
2014-02-26 05:25:02 -04:00
|
|
|
sock = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
f = asyncio.Future(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
sock.fileno.return_value = 10
|
|
|
|
sock.send.return_value = 2
|
|
|
|
|
2014-02-26 05:25:02 -04:00
|
|
|
self.loop.add_writer = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
self.loop._sock_sendall(f, False, sock, b'data')
|
|
|
|
self.assertFalse(f.done())
|
|
|
|
self.assertEqual(
|
|
|
|
(10, self.loop._sock_sendall, f, True, sock, b'ta'),
|
|
|
|
self.loop.add_writer.call_args[0])
|
|
|
|
|
|
|
|
def test__sock_sendall_none(self):
|
2014-02-26 05:25:02 -04:00
|
|
|
sock = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
f = asyncio.Future(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
sock.fileno.return_value = 10
|
|
|
|
sock.send.return_value = 0
|
|
|
|
|
2014-02-26 05:25:02 -04:00
|
|
|
self.loop.add_writer = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
self.loop._sock_sendall(f, False, sock, b'data')
|
|
|
|
self.assertFalse(f.done())
|
|
|
|
self.assertEqual(
|
|
|
|
(10, self.loop._sock_sendall, f, True, sock, b'data'),
|
|
|
|
self.loop.add_writer.call_args[0])
|
|
|
|
|
|
|
|
def test_sock_connect(self):
|
2014-08-25 18:20:52 -03:00
|
|
|
sock = test_utils.mock_nonblocking_socket()
|
2014-02-26 05:25:02 -04:00
|
|
|
self.loop._sock_connect = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
f = self.loop.sock_connect(sock, ('127.0.0.1', 8080))
|
2014-01-25 10:32:06 -04:00
|
|
|
self.assertIsInstance(f, asyncio.Future)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertEqual(
|
2014-08-31 10:07:57 -03:00
|
|
|
(f, sock, ('127.0.0.1', 8080)),
|
2013-10-17 17:40:50 -03:00
|
|
|
self.loop._sock_connect.call_args[0])
|
|
|
|
|
2014-08-31 10:07:57 -03:00
|
|
|
def test_sock_connect_timeout(self):
|
2015-07-09 18:13:50 -03:00
|
|
|
# asyncio issue #205: sock_connect() must unregister the socket on
|
2014-08-31 10:07:57 -03:00
|
|
|
# timeout error
|
|
|
|
|
|
|
|
# prepare mocks
|
|
|
|
self.loop.add_writer = mock.Mock()
|
|
|
|
self.loop.remove_writer = mock.Mock()
|
|
|
|
sock = test_utils.mock_nonblocking_socket()
|
|
|
|
sock.connect.side_effect = BlockingIOError
|
|
|
|
|
|
|
|
# first call to sock_connect() registers the socket
|
|
|
|
fut = self.loop.sock_connect(sock, ('127.0.0.1', 80))
|
|
|
|
self.assertTrue(sock.connect.called)
|
|
|
|
self.assertTrue(self.loop.add_writer.called)
|
|
|
|
self.assertEqual(len(fut._callbacks), 1)
|
|
|
|
|
|
|
|
# on timeout, the socket must be unregistered
|
|
|
|
sock.connect.reset_mock()
|
|
|
|
fut.set_exception(asyncio.TimeoutError)
|
|
|
|
with self.assertRaises(asyncio.TimeoutError):
|
|
|
|
self.loop.run_until_complete(fut)
|
|
|
|
self.assertTrue(self.loop.remove_writer.called)
|
|
|
|
|
2013-10-17 17:40:50 -03:00
|
|
|
def test__sock_connect(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
f = asyncio.Future(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-02-26 05:25:02 -04:00
|
|
|
sock = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
sock.fileno.return_value = 10
|
|
|
|
|
2014-08-31 10:07:57 -03:00
|
|
|
self.loop._sock_connect(f, sock, ('127.0.0.1', 8080))
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertTrue(f.done())
|
|
|
|
self.assertIsNone(f.result())
|
|
|
|
self.assertTrue(sock.connect.called)
|
|
|
|
|
2014-08-31 10:07:57 -03:00
|
|
|
def test__sock_connect_cb_cancelled_fut(self):
|
2014-02-26 05:25:02 -04:00
|
|
|
sock = mock.Mock()
|
2014-08-31 10:07:57 -03:00
|
|
|
self.loop.remove_writer = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
f = asyncio.Future(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
f.cancel()
|
|
|
|
|
2014-08-31 10:07:57 -03:00
|
|
|
self.loop._sock_connect_cb(f, sock, ('127.0.0.1', 8080))
|
|
|
|
self.assertFalse(sock.getsockopt.called)
|
|
|
|
|
|
|
|
def test__sock_connect_writer(self):
|
|
|
|
# check that the fd is registered and then unregistered
|
|
|
|
self.loop._process_events = mock.Mock()
|
|
|
|
self.loop.add_writer = mock.Mock()
|
|
|
|
self.loop.remove_writer = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-02-26 05:25:02 -04:00
|
|
|
sock = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
sock.fileno.return_value = 10
|
2014-08-31 10:07:57 -03:00
|
|
|
sock.connect.side_effect = BlockingIOError
|
|
|
|
sock.getsockopt.return_value = 0
|
|
|
|
address = ('127.0.0.1', 8080)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
f = asyncio.Future(loop=self.loop)
|
2014-08-31 10:07:57 -03:00
|
|
|
self.loop._sock_connect(f, sock, address)
|
|
|
|
self.assertTrue(self.loop.add_writer.called)
|
|
|
|
self.assertEqual(10, self.loop.add_writer.call_args[0][0])
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-08-31 10:07:57 -03:00
|
|
|
self.loop._sock_connect_cb(f, sock, address)
|
|
|
|
# need to run the event loop to execute _sock_connect_done() callback
|
|
|
|
self.loop.run_until_complete(f)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertEqual((10,), self.loop.remove_writer.call_args[0])
|
|
|
|
|
2014-08-31 10:07:57 -03:00
|
|
|
def test__sock_connect_cb_tryagain(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
f = asyncio.Future(loop=self.loop)
|
2014-02-26 05:25:02 -04:00
|
|
|
sock = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
sock.fileno.return_value = 10
|
|
|
|
sock.getsockopt.return_value = errno.EAGAIN
|
|
|
|
|
2014-08-31 10:07:57 -03:00
|
|
|
# check that the exception is handled
|
|
|
|
self.loop._sock_connect_cb(f, sock, ('127.0.0.1', 8080))
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-08-31 10:07:57 -03:00
|
|
|
def test__sock_connect_cb_exception(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
f = asyncio.Future(loop=self.loop)
|
2014-02-26 05:25:02 -04:00
|
|
|
sock = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
sock.fileno.return_value = 10
|
|
|
|
sock.getsockopt.return_value = errno.ENOTCONN
|
|
|
|
|
2014-02-26 05:25:02 -04:00
|
|
|
self.loop.remove_writer = mock.Mock()
|
2014-08-31 10:07:57 -03:00
|
|
|
self.loop._sock_connect_cb(f, sock, ('127.0.0.1', 8080))
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertIsInstance(f.exception(), OSError)
|
|
|
|
|
|
|
|
def test_sock_accept(self):
|
2014-08-25 18:20:52 -03:00
|
|
|
sock = test_utils.mock_nonblocking_socket()
|
2014-02-26 05:25:02 -04:00
|
|
|
self.loop._sock_accept = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
f = self.loop.sock_accept(sock)
|
2014-01-25 10:32:06 -04:00
|
|
|
self.assertIsInstance(f, asyncio.Future)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertEqual(
|
|
|
|
(f, False, sock), self.loop._sock_accept.call_args[0])
|
|
|
|
|
|
|
|
def test__sock_accept(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
f = asyncio.Future(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-02-26 05:25:02 -04:00
|
|
|
conn = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-02-26 05:25:02 -04:00
|
|
|
sock = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
sock.fileno.return_value = 10
|
|
|
|
sock.accept.return_value = conn, ('127.0.0.1', 1000)
|
|
|
|
|
|
|
|
self.loop._sock_accept(f, False, sock)
|
|
|
|
self.assertTrue(f.done())
|
|
|
|
self.assertEqual((conn, ('127.0.0.1', 1000)), f.result())
|
|
|
|
self.assertEqual((False,), conn.setblocking.call_args[0])
|
|
|
|
|
|
|
|
def test__sock_accept_canceled_fut(self):
|
2014-02-26 05:25:02 -04:00
|
|
|
sock = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
f = asyncio.Future(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
f.cancel()
|
|
|
|
|
|
|
|
self.loop._sock_accept(f, False, sock)
|
|
|
|
self.assertFalse(sock.accept.called)
|
|
|
|
|
|
|
|
def test__sock_accept_unregister(self):
|
2014-02-26 05:25:02 -04:00
|
|
|
sock = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
sock.fileno.return_value = 10
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
f = asyncio.Future(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
f.cancel()
|
|
|
|
|
2014-02-26 05:25:02 -04:00
|
|
|
self.loop.remove_reader = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
self.loop._sock_accept(f, True, sock)
|
|
|
|
self.assertEqual((10,), self.loop.remove_reader.call_args[0])
|
|
|
|
|
|
|
|
def test__sock_accept_tryagain(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
f = asyncio.Future(loop=self.loop)
|
2014-02-26 05:25:02 -04:00
|
|
|
sock = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
sock.fileno.return_value = 10
|
|
|
|
sock.accept.side_effect = BlockingIOError
|
|
|
|
|
2014-02-26 05:25:02 -04:00
|
|
|
self.loop.add_reader = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
self.loop._sock_accept(f, False, sock)
|
|
|
|
self.assertEqual(
|
|
|
|
(10, self.loop._sock_accept, f, True, sock),
|
|
|
|
self.loop.add_reader.call_args[0])
|
|
|
|
|
|
|
|
def test__sock_accept_exception(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
f = asyncio.Future(loop=self.loop)
|
2014-02-26 05:25:02 -04:00
|
|
|
sock = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
sock.fileno.return_value = 10
|
|
|
|
err = sock.accept.side_effect = OSError()
|
|
|
|
|
|
|
|
self.loop._sock_accept(f, False, sock)
|
|
|
|
self.assertIs(err, f.exception())
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2014-02-26 05:25:02 -04: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)])
|
|
|
|
self.loop.remove_reader.assert_called_with(1)
|
|
|
|
|
|
|
|
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
|
2014-02-26 05:25:02 -04: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)])
|
|
|
|
self.loop.remove_writer.assert_called_with(1)
|
|
|
|
|
|
|
|
|
2014-06-17 20:36:32 -03:00
|
|
|
class SelectorTransportTests(test_utils.TestCase):
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def setUp(self):
|
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')
|
2014-02-26 05:25:02 -04: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)
|
|
|
|
|
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:.*'),
|
2014-02-18 19:02:19 -04:00
|
|
|
exc_info=(OSError, MOCK_ANY, MOCK_ANY))
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
2014-06-17 20:36:32 -03:00
|
|
|
class SelectorSocketTransportTests(test_utils.TestCase):
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def setUp(self):
|
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):
|
2015-01-28 19:36:51 -04:00
|
|
|
waiter = asyncio.Future(loop=self.loop)
|
|
|
|
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):
|
2015-01-28 19:36:51 -04:00
|
|
|
waiter = asyncio.Future(loop=self.loop)
|
|
|
|
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)
|
|
|
|
self.loop.assert_reader(7, tr._read_ready)
|
2013-10-18 11:58:20 -03:00
|
|
|
tr.pause_reading()
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertTrue(tr._paused)
|
|
|
|
self.assertFalse(7 in self.loop.readers)
|
2013-10-18 11:58:20 -03:00
|
|
|
tr.resume_reading()
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertFalse(tr._paused)
|
|
|
|
self.loop.assert_reader(7, tr._read_ready)
|
2014-05-27 15:24:43 -03:00
|
|
|
with self.assertRaises(RuntimeError):
|
|
|
|
tr.resume_reading()
|
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)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.loop.add_writer(7, transport._write_ready)
|
|
|
|
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)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.loop.add_writer(7, transport._write_ready)
|
|
|
|
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)
|
|
|
|
|
|
|
|
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)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.loop.add_writer(7, transport._write_ready)
|
|
|
|
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)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.loop.add_writer(7, transport._write_ready)
|
|
|
|
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'])
|
2013-10-17 17:40:50 -03:00
|
|
|
self.loop.add_writer(7, transport._write_ready)
|
|
|
|
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
|
|
|
|
2014-02-26 05:25:02 -04:00
|
|
|
@mock.patch('asyncio.base_events.logger')
|
2013-10-17 17:40:50 -03:00
|
|
|
def test_write_ready_exception_and_close(self, m_log):
|
|
|
|
self.sock.send.side_effect = OSError()
|
2014-02-26 05:25:02 -04:00
|
|
|
remove_writer = self.loop.remove_writer = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2015-01-15 08:18:32 -04:00
|
|
|
transport = self.socket_transport()
|
2013-10-17 17:40:50 -03:00
|
|
|
transport.close()
|
2013-11-27 18:12:48 -04:00
|
|
|
transport._buffer.extend(b'data')
|
2013-10-17 17:40:50 -03:00
|
|
|
transport._write_ready()
|
|
|
|
remove_writer.assert_called_with(self.sock_fd)
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
|
|
|
|
@unittest.skipIf(ssl is None, 'No ssl module')
|
2014-06-17 20:36:32 -03:00
|
|
|
class SelectorSslTransportTests(test_utils.TestCase):
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def setUp(self):
|
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
|
2014-02-26 05:25:02 -04:00
|
|
|
self.sslsock = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
self.sslsock.fileno.return_value = 1
|
2014-02-26 05:25:02 -04:00
|
|
|
self.sslcontext = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
self.sslcontext.wrap_socket.return_value = self.sslsock
|
|
|
|
|
2015-01-15 08:18:32 -04:00
|
|
|
def ssl_transport(self, waiter=None, server_hostname=None):
|
|
|
|
transport = _SelectorSslTransport(self.loop, self.sock, self.protocol,
|
|
|
|
self.sslcontext, waiter=waiter,
|
|
|
|
server_hostname=server_hostname)
|
|
|
|
self.addCleanup(close_transport, transport)
|
|
|
|
return transport
|
|
|
|
|
2013-10-17 17:40:50 -03:00
|
|
|
def _make_one(self, create_waiter=None):
|
2015-01-15 08:18:32 -04:00
|
|
|
transport = self.ssl_transport()
|
2013-10-17 17:40:50 -03:00
|
|
|
self.sock.reset_mock()
|
|
|
|
self.sslsock.reset_mock()
|
|
|
|
self.sslcontext.reset_mock()
|
|
|
|
self.loop.reset_counters()
|
|
|
|
return transport
|
|
|
|
|
|
|
|
def test_on_handshake(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
waiter = asyncio.Future(loop=self.loop)
|
2015-01-15 08:18:32 -04:00
|
|
|
tr = self.ssl_transport(waiter=waiter)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertTrue(self.sslsock.do_handshake.called)
|
2013-11-01 18:18:02 -03:00
|
|
|
self.loop.assert_reader(1, tr._read_ready)
|
2013-10-17 17:40:50 -03:00
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
self.assertIsNone(waiter.result())
|
|
|
|
|
|
|
|
def test_on_handshake_reader_retry(self):
|
2014-07-16 13:02:10 -03:00
|
|
|
self.loop.set_debug(False)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.sslsock.do_handshake.side_effect = ssl.SSLWantReadError
|
2015-01-15 08:18:32 -04:00
|
|
|
transport = self.ssl_transport()
|
2014-07-11 22:11:53 -03:00
|
|
|
self.loop.assert_reader(1, transport._on_handshake, None)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def test_on_handshake_writer_retry(self):
|
2014-07-16 13:02:10 -03:00
|
|
|
self.loop.set_debug(False)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.sslsock.do_handshake.side_effect = ssl.SSLWantWriteError
|
2015-01-15 08:18:32 -04:00
|
|
|
transport = self.ssl_transport()
|
2014-07-11 22:11:53 -03:00
|
|
|
self.loop.assert_writer(1, transport._on_handshake, None)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def test_on_handshake_exc(self):
|
|
|
|
exc = ValueError()
|
|
|
|
self.sslsock.do_handshake.side_effect = exc
|
2014-07-14 17:26:34 -03:00
|
|
|
with test_utils.disable_logger():
|
|
|
|
waiter = asyncio.Future(loop=self.loop)
|
2015-01-15 08:18:32 -04:00
|
|
|
transport = self.ssl_transport(waiter=waiter)
|
2014-07-14 17:26:34 -03:00
|
|
|
self.assertTrue(waiter.done())
|
|
|
|
self.assertIs(exc, waiter.exception())
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertTrue(self.sslsock.close.called)
|
|
|
|
|
|
|
|
def test_on_handshake_base_exc(self):
|
2015-01-14 11:56:20 -04:00
|
|
|
waiter = asyncio.Future(loop=self.loop)
|
2015-01-15 08:18:32 -04:00
|
|
|
transport = self.ssl_transport(waiter=waiter)
|
2013-10-17 17:40:50 -03:00
|
|
|
exc = BaseException()
|
|
|
|
self.sslsock.do_handshake.side_effect = exc
|
2014-07-14 17:26:34 -03:00
|
|
|
with test_utils.disable_logger():
|
2014-07-16 13:02:10 -03:00
|
|
|
self.assertRaises(BaseException, transport._on_handshake, 0)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertTrue(self.sslsock.close.called)
|
2015-01-14 11:56:20 -04:00
|
|
|
self.assertTrue(waiter.done())
|
|
|
|
self.assertIs(exc, waiter.exception())
|
|
|
|
|
|
|
|
def test_cancel_handshake(self):
|
|
|
|
# Python issue #23197: cancelling an handshake must not raise an
|
|
|
|
# exception or log an error, even if the handshake failed
|
|
|
|
waiter = asyncio.Future(loop=self.loop)
|
2015-01-15 08:18:32 -04:00
|
|
|
transport = self.ssl_transport(waiter=waiter)
|
2015-01-14 11:56:20 -04:00
|
|
|
waiter.cancel()
|
|
|
|
exc = ValueError()
|
|
|
|
self.sslsock.do_handshake.side_effect = exc
|
|
|
|
with test_utils.disable_logger():
|
|
|
|
transport._on_handshake(0)
|
2015-01-15 09:24:55 -04:00
|
|
|
transport.close()
|
|
|
|
test_utils.run_briefly(self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2013-10-18 11:58:20 -03:00
|
|
|
def test_pause_resume_reading(self):
|
2013-10-17 17:40:50 -03:00
|
|
|
tr = self._make_one()
|
|
|
|
self.assertFalse(tr._paused)
|
2013-11-01 18:18:02 -03:00
|
|
|
self.loop.assert_reader(1, tr._read_ready)
|
2013-10-18 11:58:20 -03:00
|
|
|
tr.pause_reading()
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertTrue(tr._paused)
|
|
|
|
self.assertFalse(1 in self.loop.readers)
|
2013-10-18 11:58:20 -03:00
|
|
|
tr.resume_reading()
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertFalse(tr._paused)
|
2013-11-01 18:18:02 -03:00
|
|
|
self.loop.assert_reader(1, tr._read_ready)
|
2014-05-27 15:24:43 -03:00
|
|
|
with self.assertRaises(RuntimeError):
|
|
|
|
tr.resume_reading()
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2013-11-27 18:12:48 -04:00
|
|
|
def test_write(self):
|
|
|
|
transport = self._make_one()
|
|
|
|
transport.write(b'data')
|
|
|
|
self.assertEqual(list_to_buffer([b'data']), transport._buffer)
|
|
|
|
|
|
|
|
def test_write_bytearray(self):
|
|
|
|
transport = self._make_one()
|
|
|
|
data = bytearray(b'data')
|
|
|
|
transport.write(data)
|
|
|
|
self.assertEqual(list_to_buffer([b'data']), transport._buffer)
|
|
|
|
self.assertEqual(data, bytearray(b'data')) # Hasn't been mutated.
|
|
|
|
self.assertIsNot(data, transport._buffer) # Hasn't been incorporated.
|
|
|
|
|
|
|
|
def test_write_memoryview(self):
|
|
|
|
transport = self._make_one()
|
|
|
|
data = memoryview(b'data')
|
|
|
|
transport.write(data)
|
|
|
|
self.assertEqual(list_to_buffer([b'data']), transport._buffer)
|
|
|
|
|
2013-10-17 17:40:50 -03:00
|
|
|
def test_write_no_data(self):
|
|
|
|
transport = self._make_one()
|
2013-11-27 18:12:48 -04:00
|
|
|
transport._buffer.extend(b'data')
|
2013-10-17 17:40:50 -03:00
|
|
|
transport.write(b'')
|
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_str(self):
|
|
|
|
transport = self._make_one()
|
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):
|
|
|
|
transport = self._make_one()
|
|
|
|
transport.close()
|
|
|
|
self.assertEqual(transport._conn_lost, 1)
|
|
|
|
transport.write(b'data')
|
|
|
|
self.assertEqual(transport._conn_lost, 2)
|
|
|
|
|
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):
|
|
|
|
transport = self._make_one()
|
|
|
|
transport._conn_lost = 1
|
|
|
|
transport.write(b'data')
|
2013-11-27 18:12:48 -04:00
|
|
|
self.assertEqual(transport._buffer, list_to_buffer())
|
2013-10-17 17:40:50 -03:00
|
|
|
transport.write(b'data')
|
|
|
|
transport.write(b'data')
|
|
|
|
transport.write(b'data')
|
|
|
|
transport.write(b'data')
|
|
|
|
m_log.warning.assert_called_with('socket.send() raised exception.')
|
|
|
|
|
2013-11-01 18:18:02 -03:00
|
|
|
def test_read_ready_recv(self):
|
2013-10-17 17:40:50 -03:00
|
|
|
self.sslsock.recv.return_value = b'data'
|
|
|
|
transport = self._make_one()
|
2013-11-01 18:18:02 -03:00
|
|
|
transport._read_ready()
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertTrue(self.sslsock.recv.called)
|
|
|
|
self.assertEqual((b'data',), self.protocol.data_received.call_args[0])
|
|
|
|
|
2013-11-01 18:18:02 -03:00
|
|
|
def test_read_ready_write_wants_read(self):
|
2014-02-26 05:25:02 -04:00
|
|
|
self.loop.add_writer = mock.Mock()
|
2013-11-01 18:18:02 -03:00
|
|
|
self.sslsock.recv.side_effect = BlockingIOError
|
|
|
|
transport = self._make_one()
|
|
|
|
transport._write_wants_read = True
|
2014-02-26 05:25:02 -04:00
|
|
|
transport._write_ready = mock.Mock()
|
2013-11-27 18:12:48 -04:00
|
|
|
transport._buffer.extend(b'data')
|
2013-11-01 18:18:02 -03:00
|
|
|
transport._read_ready()
|
|
|
|
|
|
|
|
self.assertFalse(transport._write_wants_read)
|
|
|
|
transport._write_ready.assert_called_with()
|
|
|
|
self.loop.add_writer.assert_called_with(
|
|
|
|
transport._sock_fd, transport._write_ready)
|
|
|
|
|
|
|
|
def test_read_ready_recv_eof(self):
|
2013-10-17 17:40:50 -03:00
|
|
|
self.sslsock.recv.return_value = b''
|
|
|
|
transport = self._make_one()
|
2014-02-26 05:25:02 -04:00
|
|
|
transport.close = mock.Mock()
|
2013-11-01 18:18:02 -03:00
|
|
|
transport._read_ready()
|
2013-10-17 17:40:50 -03:00
|
|
|
transport.close.assert_called_with()
|
|
|
|
self.protocol.eof_received.assert_called_with()
|
|
|
|
|
2013-11-01 18:18:02 -03:00
|
|
|
def test_read_ready_recv_conn_reset(self):
|
2013-10-17 17:40:50 -03:00
|
|
|
err = self.sslsock.recv.side_effect = ConnectionResetError()
|
|
|
|
transport = self._make_one()
|
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)
|
|
|
|
|
2013-11-01 18:18:02 -03:00
|
|
|
def test_read_ready_recv_retry(self):
|
2013-10-17 17:40:50 -03:00
|
|
|
self.sslsock.recv.side_effect = ssl.SSLWantReadError
|
|
|
|
transport = self._make_one()
|
2013-11-01 18:18:02 -03:00
|
|
|
transport._read_ready()
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertTrue(self.sslsock.recv.called)
|
|
|
|
self.assertFalse(self.protocol.data_received.called)
|
|
|
|
|
|
|
|
self.sslsock.recv.side_effect = BlockingIOError
|
2013-11-01 18:18:02 -03:00
|
|
|
transport._read_ready()
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertFalse(self.protocol.data_received.called)
|
|
|
|
|
|
|
|
self.sslsock.recv.side_effect = InterruptedError
|
2013-11-01 18:18:02 -03:00
|
|
|
transport._read_ready()
|
|
|
|
self.assertFalse(self.protocol.data_received.called)
|
|
|
|
|
|
|
|
def test_read_ready_recv_write(self):
|
2014-02-26 05:25:02 -04:00
|
|
|
self.loop.remove_reader = mock.Mock()
|
|
|
|
self.loop.add_writer = mock.Mock()
|
2013-11-01 18:18:02 -03:00
|
|
|
self.sslsock.recv.side_effect = ssl.SSLWantWriteError
|
|
|
|
transport = self._make_one()
|
|
|
|
transport._read_ready()
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertFalse(self.protocol.data_received.called)
|
2013-11-01 18:18:02 -03:00
|
|
|
self.assertTrue(transport._read_wants_write)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2013-11-01 18:18:02 -03:00
|
|
|
self.loop.remove_reader.assert_called_with(transport._sock_fd)
|
|
|
|
self.loop.add_writer.assert_called_with(
|
|
|
|
transport._sock_fd, transport._write_ready)
|
|
|
|
|
|
|
|
def test_read_ready_recv_exc(self):
|
2013-10-17 17:40:50 -03:00
|
|
|
err = self.sslsock.recv.side_effect = OSError()
|
|
|
|
transport = self._make_one()
|
2014-02-26 05:25:02 -04:00
|
|
|
transport._fatal_error = mock.Mock()
|
2013-11-01 18:18:02 -03:00
|
|
|
transport._read_ready()
|
2014-02-18 20:40:41 -04:00
|
|
|
transport._fatal_error.assert_called_with(
|
|
|
|
err,
|
|
|
|
'Fatal read error on SSL transport')
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2013-11-01 18:18:02 -03:00
|
|
|
def test_write_ready_send(self):
|
2013-10-17 17:40:50 -03:00
|
|
|
self.sslsock.send.return_value = 4
|
|
|
|
transport = self._make_one()
|
2013-11-27 18:12:48 -04:00
|
|
|
transport._buffer = list_to_buffer([b'data'])
|
2013-11-01 18:18:02 -03:00
|
|
|
transport._write_ready()
|
2013-11-27 18:12:48 -04:00
|
|
|
self.assertEqual(list_to_buffer(), transport._buffer)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertTrue(self.sslsock.send.called)
|
|
|
|
|
2013-11-01 18:18:02 -03:00
|
|
|
def test_write_ready_send_none(self):
|
2013-10-17 17:40:50 -03:00
|
|
|
self.sslsock.send.return_value = 0
|
|
|
|
transport = self._make_one()
|
2013-11-27 18:12:48 -04:00
|
|
|
transport._buffer = list_to_buffer([b'data1', b'data2'])
|
2013-11-01 18:18:02 -03:00
|
|
|
transport._write_ready()
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertTrue(self.sslsock.send.called)
|
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
|
|
|
|
2013-11-01 18:18:02 -03:00
|
|
|
def test_write_ready_send_partial(self):
|
2013-10-17 17:40:50 -03:00
|
|
|
self.sslsock.send.return_value = 2
|
|
|
|
transport = self._make_one()
|
2013-11-27 18:12:48 -04:00
|
|
|
transport._buffer = list_to_buffer([b'data1', b'data2'])
|
2013-11-01 18:18:02 -03:00
|
|
|
transport._write_ready()
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertTrue(self.sslsock.send.called)
|
2013-11-27 18:12:48 -04:00
|
|
|
self.assertEqual(list_to_buffer([b'ta1data2']), transport._buffer)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2013-11-01 18:18:02 -03:00
|
|
|
def test_write_ready_send_closing_partial(self):
|
2013-10-17 17:40:50 -03:00
|
|
|
self.sslsock.send.return_value = 2
|
|
|
|
transport = self._make_one()
|
2013-11-27 18:12:48 -04:00
|
|
|
transport._buffer = list_to_buffer([b'data1', b'data2'])
|
2013-11-01 18:18:02 -03:00
|
|
|
transport._write_ready()
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertTrue(self.sslsock.send.called)
|
|
|
|
self.assertFalse(self.sslsock.close.called)
|
|
|
|
|
2013-11-01 18:18:02 -03:00
|
|
|
def test_write_ready_send_closing(self):
|
2013-10-17 17:40:50 -03:00
|
|
|
self.sslsock.send.return_value = 4
|
|
|
|
transport = self._make_one()
|
|
|
|
transport.close()
|
2013-11-27 18:12:48 -04:00
|
|
|
transport._buffer = list_to_buffer([b'data'])
|
2013-11-01 18:18:02 -03:00
|
|
|
transport._write_ready()
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertFalse(self.loop.writers)
|
|
|
|
self.protocol.connection_lost.assert_called_with(None)
|
|
|
|
|
2013-11-01 18:18:02 -03:00
|
|
|
def test_write_ready_send_closing_empty_buffer(self):
|
2013-10-17 17:40:50 -03:00
|
|
|
self.sslsock.send.return_value = 4
|
|
|
|
transport = self._make_one()
|
|
|
|
transport.close()
|
2013-11-27 18:12:48 -04:00
|
|
|
transport._buffer = list_to_buffer()
|
2013-11-01 18:18:02 -03:00
|
|
|
transport._write_ready()
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertFalse(self.loop.writers)
|
|
|
|
self.protocol.connection_lost.assert_called_with(None)
|
|
|
|
|
2013-11-01 18:18:02 -03:00
|
|
|
def test_write_ready_send_retry(self):
|
2013-10-17 17:40:50 -03:00
|
|
|
transport = self._make_one()
|
2013-11-27 18:12:48 -04:00
|
|
|
transport._buffer = list_to_buffer([b'data'])
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
self.sslsock.send.side_effect = ssl.SSLWantWriteError
|
2013-11-01 18:18:02 -03:00
|
|
|
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
|
|
|
|
|
|
|
self.sslsock.send.side_effect = BlockingIOError()
|
2013-11-01 18:18:02 -03:00
|
|
|
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
|
|
|
|
2013-11-01 18:18:02 -03:00
|
|
|
def test_write_ready_send_read(self):
|
|
|
|
transport = self._make_one()
|
2013-11-27 18:12:48 -04:00
|
|
|
transport._buffer = list_to_buffer([b'data'])
|
2013-11-01 18:18:02 -03:00
|
|
|
|
2014-02-26 05:25:02 -04:00
|
|
|
self.loop.remove_writer = mock.Mock()
|
2013-11-01 18:18:02 -03:00
|
|
|
self.sslsock.send.side_effect = ssl.SSLWantReadError
|
|
|
|
transport._write_ready()
|
|
|
|
self.assertFalse(self.protocol.data_received.called)
|
|
|
|
self.assertTrue(transport._write_wants_read)
|
|
|
|
self.loop.remove_writer.assert_called_with(transport._sock_fd)
|
|
|
|
|
|
|
|
def test_write_ready_send_exc(self):
|
2013-10-17 17:40:50 -03:00
|
|
|
err = self.sslsock.send.side_effect = OSError()
|
|
|
|
|
|
|
|
transport = self._make_one()
|
2013-11-27 18:12:48 -04:00
|
|
|
transport._buffer = list_to_buffer([b'data'])
|
2014-02-26 05:25:02 -04:00
|
|
|
transport._fatal_error = mock.Mock()
|
2013-11-01 18:18:02 -03:00
|
|
|
transport._write_ready()
|
2014-02-18 20:40:41 -04:00
|
|
|
transport._fatal_error.assert_called_with(
|
|
|
|
err,
|
|
|
|
'Fatal write error on SSL transport')
|
2013-11-27 18:12:48 -04:00
|
|
|
self.assertEqual(list_to_buffer(), transport._buffer)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2013-11-01 18:18:02 -03:00
|
|
|
def test_write_ready_read_wants_write(self):
|
2014-02-26 05:25:02 -04:00
|
|
|
self.loop.add_reader = mock.Mock()
|
2013-11-01 18:18:02 -03:00
|
|
|
self.sslsock.send.side_effect = BlockingIOError
|
|
|
|
transport = self._make_one()
|
|
|
|
transport._read_wants_write = True
|
2014-02-26 05:25:02 -04:00
|
|
|
transport._read_ready = mock.Mock()
|
2013-11-01 18:18:02 -03:00
|
|
|
transport._write_ready()
|
|
|
|
|
|
|
|
self.assertFalse(transport._read_wants_write)
|
|
|
|
transport._read_ready.assert_called_with()
|
|
|
|
self.loop.add_reader.assert_called_with(
|
|
|
|
transport._sock_fd, transport._read_ready)
|
|
|
|
|
2013-10-17 17:40:50 -03:00
|
|
|
def test_write_eof(self):
|
|
|
|
tr = self._make_one()
|
|
|
|
self.assertFalse(tr.can_write_eof())
|
|
|
|
self.assertRaises(NotImplementedError, tr.write_eof)
|
|
|
|
|
2015-01-28 21:56:05 -04:00
|
|
|
def check_close(self):
|
2013-10-17 17:40:50 -03:00
|
|
|
tr = self._make_one()
|
|
|
|
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[1])
|
|
|
|
self.assertEqual(tr._conn_lost, 1)
|
|
|
|
|
|
|
|
tr.close()
|
|
|
|
self.assertEqual(tr._conn_lost, 1)
|
|
|
|
self.assertEqual(1, self.loop.remove_reader_count[1])
|
|
|
|
|
2015-01-28 21:56:05 -04:00
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
|
|
|
|
def test_close(self):
|
|
|
|
self.check_close()
|
|
|
|
self.assertTrue(self.protocol.connection_made.called)
|
|
|
|
self.assertTrue(self.protocol.connection_lost.called)
|
|
|
|
|
|
|
|
def test_close_not_connected(self):
|
|
|
|
self.sslsock.do_handshake.side_effect = ssl.SSLWantReadError
|
|
|
|
self.check_close()
|
|
|
|
self.assertFalse(self.protocol.connection_made.called)
|
|
|
|
self.assertFalse(self.protocol.connection_lost.called)
|
|
|
|
|
2014-11-23 19:04:34 -04:00
|
|
|
@unittest.skipIf(ssl is None, 'No SSL support')
|
2013-10-17 17:40:50 -03:00
|
|
|
def test_server_hostname(self):
|
2015-01-15 08:18:32 -04:00
|
|
|
self.ssl_transport(server_hostname='localhost')
|
2013-10-17 17:40:50 -03:00
|
|
|
self.sslcontext.wrap_socket.assert_called_with(
|
|
|
|
self.sock, do_handshake_on_connect=False, server_side=False,
|
|
|
|
server_hostname='localhost')
|
|
|
|
|
|
|
|
|
2013-11-01 18:22:30 -03:00
|
|
|
class SelectorSslWithoutSslTransportTests(unittest.TestCase):
|
|
|
|
|
2014-02-26 05:25:02 -04:00
|
|
|
@mock.patch('asyncio.selector_events.ssl', None)
|
2013-11-01 18:22:30 -03:00
|
|
|
def test_ssl_transport_requires_ssl_module(self):
|
2014-02-26 05:25:02 -04:00
|
|
|
Mock = mock.Mock
|
2013-11-01 18:22:30 -03:00
|
|
|
with self.assertRaises(RuntimeError):
|
2014-02-26 06:07:42 -04:00
|
|
|
_SelectorSslTransport(Mock(), Mock(), Mock(), Mock())
|
2013-11-01 18:22:30 -03:00
|
|
|
|
|
|
|
|
2014-06-17 20:36:32 -03:00
|
|
|
class SelectorDatagramTransportTests(test_utils.TestCase):
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def setUp(self):
|
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):
|
|
|
|
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))
|
|
|
|
|
|
|
|
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)))
|
|
|
|
self.loop.add_writer(7, transport._sendto_ready)
|
|
|
|
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, ()))
|
|
|
|
self.loop.add_writer(7, transport._sendto_ready)
|
|
|
|
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()
|
2013-10-17 17:40:50 -03:00
|
|
|
self.loop.add_writer(7, transport._sendto_ready)
|
|
|
|
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', ())])
|
|
|
|
self.loop.add_writer(7, transport._sendto_ready)
|
|
|
|
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)
|
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:.*'),
|
2014-02-18 19:02:19 -04:00
|
|
|
exc_info=(ConnectionRefusedError, MOCK_ANY, MOCK_ANY))
|
2013-10-20 16:02:53 -03:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|