2013-10-17 17:40:50 -03:00
|
|
|
"""Tests for selector_events.py"""
|
|
|
|
|
|
|
|
import errno
|
|
|
|
import gc
|
|
|
|
import pprint
|
|
|
|
import socket
|
|
|
|
import sys
|
|
|
|
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
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
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-02-26 05:25:02 -04:00
|
|
|
selector = mock.Mock()
|
2014-01-25 10:01:33 -04:00
|
|
|
self.loop = TestBaseSelectorEventLoop(selector)
|
2014-06-17 20:36:32 -03:00
|
|
|
self.set_event_loop(self.loop, cleanup=False)
|
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()
|
2014-02-11 06:34:30 -04:00
|
|
|
transport = self.loop._make_socket_transport(m, asyncio.Protocol())
|
|
|
|
self.assertIsInstance(transport, _SelectorSocketTransport)
|
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()
|
|
|
|
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)
|
2014-02-11 06:34:30 -04:00
|
|
|
self.assertIsInstance(transport, _SelectorSslTransport)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
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_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):
|
|
|
|
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(
|
|
|
|
(f, False, sock, ('127.0.0.1', 8080)),
|
|
|
|
self.loop._sock_connect.call_args[0])
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
self.loop._sock_connect(f, False, sock, ('127.0.0.1', 8080))
|
|
|
|
self.assertTrue(f.done())
|
|
|
|
self.assertIsNone(f.result())
|
|
|
|
self.assertTrue(sock.connect.called)
|
|
|
|
|
|
|
|
def test__sock_connect_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_connect(f, False, sock, ('127.0.0.1', 8080))
|
|
|
|
self.assertFalse(sock.connect.called)
|
|
|
|
|
|
|
|
def test__sock_connect_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_connect(f, True, sock, ('127.0.0.1', 8080))
|
|
|
|
self.assertEqual((10,), self.loop.remove_writer.call_args[0])
|
|
|
|
|
|
|
|
def test__sock_connect_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-02-26 05:25:02 -04:00
|
|
|
self.loop.add_writer = mock.Mock()
|
|
|
|
self.loop.remove_writer = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
self.loop._sock_connect(f, True, sock, ('127.0.0.1', 8080))
|
|
|
|
self.assertEqual(
|
|
|
|
(10, self.loop._sock_connect, f,
|
|
|
|
True, sock, ('127.0.0.1', 8080)),
|
|
|
|
self.loop.add_writer.call_args[0])
|
|
|
|
|
|
|
|
def test__sock_connect_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()
|
2013-10-17 17:40:50 -03:00
|
|
|
self.loop._sock_connect(f, True, sock, ('127.0.0.1', 8080))
|
|
|
|
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
|
|
|
|
|
|
|
|
def test_ctor(self):
|
|
|
|
tr = _SelectorTransport(self.loop, self.sock, self.protocol, None)
|
|
|
|
self.assertIs(tr._loop, self.loop)
|
|
|
|
self.assertIs(tr._sock, self.sock)
|
|
|
|
self.assertIs(tr._sock_fd, 7)
|
|
|
|
|
|
|
|
def test_abort(self):
|
|
|
|
tr = _SelectorTransport(self.loop, self.sock, self.protocol, None)
|
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):
|
|
|
|
tr = _SelectorTransport(self.loop, self.sock, self.protocol, None)
|
|
|
|
tr.close()
|
|
|
|
|
|
|
|
self.assertTrue(tr._closing)
|
|
|
|
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):
|
|
|
|
tr = _SelectorTransport(self.loop, self.sock, self.protocol, None)
|
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):
|
|
|
|
tr = _SelectorTransport(self.loop, self.sock, self.protocol, None)
|
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)
|
|
|
|
|
|
|
|
self.assertTrue(tr._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()
|
|
|
|
tr = _SelectorTransport(self.loop, self.sock, self.protocol, None)
|
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()
|
|
|
|
tr = _SelectorTransport(self.loop, self.sock, self.protocol, None)
|
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
|
|
|
|
|
|
|
|
def test_ctor(self):
|
|
|
|
tr = _SelectorSocketTransport(
|
|
|
|
self.loop, self.sock, self.protocol)
|
|
|
|
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):
|
2014-01-25 10:32:06 -04:00
|
|
|
fut = asyncio.Future(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
_SelectorSocketTransport(
|
|
|
|
self.loop, self.sock, self.protocol, fut)
|
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
self.assertIsNone(fut.result())
|
|
|
|
|
2013-10-18 11:58:20 -03:00
|
|
|
def test_pause_resume_reading(self):
|
2013-10-17 17:40:50 -03:00
|
|
|
tr = _SelectorSocketTransport(
|
|
|
|
self.loop, self.sock, self.protocol)
|
|
|
|
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):
|
|
|
|
transport = _SelectorSocketTransport(
|
|
|
|
self.loop, self.sock, self.protocol)
|
|
|
|
|
|
|
|
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):
|
|
|
|
transport = _SelectorSocketTransport(
|
|
|
|
self.loop, self.sock, self.protocol)
|
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):
|
|
|
|
transport = _SelectorSocketTransport(
|
|
|
|
self.loop, self.sock, self.protocol)
|
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
|
|
|
|
|
|
|
|
transport = _SelectorSocketTransport(
|
|
|
|
self.loop, self.sock, self.protocol)
|
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
|
|
|
|
|
|
|
|
transport = _SelectorSocketTransport(
|
|
|
|
self.loop, self.sock, self.protocol)
|
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()
|
|
|
|
|
|
|
|
transport = _SelectorSocketTransport(
|
|
|
|
self.loop, self.sock, self.protocol)
|
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()
|
|
|
|
|
|
|
|
transport = _SelectorSocketTransport(
|
|
|
|
self.loop, self.sock, self.protocol)
|
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)
|
|
|
|
|
|
|
|
transport = _SelectorSocketTransport(
|
|
|
|
self.loop, self.sock, self.protocol)
|
|
|
|
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)
|
|
|
|
|
|
|
|
transport = _SelectorSocketTransport(
|
|
|
|
self.loop, self.sock, self.protocol)
|
|
|
|
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)
|
|
|
|
|
|
|
|
transport = _SelectorSocketTransport(
|
|
|
|
self.loop, self.sock, self.protocol)
|
|
|
|
transport.write(data)
|
|
|
|
self.sock.send.assert_called_with(data)
|
|
|
|
|
2013-10-17 17:40:50 -03:00
|
|
|
def test_write_no_data(self):
|
|
|
|
transport = _SelectorSocketTransport(
|
|
|
|
self.loop, self.sock, self.protocol)
|
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):
|
|
|
|
transport = _SelectorSocketTransport(
|
|
|
|
self.loop, self.sock, self.protocol)
|
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
|
|
|
|
|
|
|
|
transport = _SelectorSocketTransport(
|
|
|
|
self.loop, self.sock, self.protocol)
|
|
|
|
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
|
|
|
|
|
|
|
|
transport = _SelectorSocketTransport(
|
|
|
|
self.loop, self.sock, self.protocol)
|
|
|
|
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
|
|
|
|
|
|
|
|
transport = _SelectorSocketTransport(
|
|
|
|
self.loop, self.sock, self.protocol)
|
|
|
|
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
|
|
|
|
|
|
|
|
transport = _SelectorSocketTransport(
|
|
|
|
self.loop, self.sock, self.protocol)
|
|
|
|
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'
|
|
|
|
transport = _SelectorSocketTransport(
|
|
|
|
self.loop, self.sock, self.protocol)
|
|
|
|
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'
|
|
|
|
transport = _SelectorSocketTransport(
|
|
|
|
self.loop, self.sock, self.protocol)
|
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):
|
|
|
|
transport = _SelectorSocketTransport(
|
|
|
|
self.loop, self.sock, self.protocol)
|
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 = _SelectorSocketTransport(
|
|
|
|
self.loop, self.sock, self.protocol)
|
|
|
|
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)
|
|
|
|
|
|
|
|
transport = _SelectorSocketTransport(
|
|
|
|
self.loop, self.sock, self.protocol)
|
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)
|
|
|
|
|
|
|
|
transport = _SelectorSocketTransport(
|
|
|
|
self.loop, self.sock, self.protocol)
|
|
|
|
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):
|
|
|
|
transport = _SelectorSocketTransport(
|
|
|
|
self.loop, self.sock, self.protocol)
|
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
|
|
|
|
|
|
|
|
transport = _SelectorSocketTransport(
|
|
|
|
self.loop, self.sock, self.protocol)
|
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
|
|
|
|
|
|
|
|
transport = _SelectorSocketTransport(
|
|
|
|
self.loop, self.sock, self.protocol)
|
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
|
|
|
|
|
|
|
|
transport = _SelectorSocketTransport(
|
|
|
|
self.loop, self.sock, self.protocol)
|
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()
|
|
|
|
|
|
|
|
transport = _SelectorSocketTransport(
|
|
|
|
self.loop, self.sock, self.protocol)
|
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
|
|
|
|
|
|
|
transport = _SelectorSocketTransport(
|
|
|
|
self.loop, self.sock, self.protocol)
|
|
|
|
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):
|
|
|
|
tr = _SelectorSocketTransport(
|
|
|
|
self.loop, self.sock, self.protocol)
|
|
|
|
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):
|
|
|
|
tr = _SelectorSocketTransport(
|
|
|
|
self.loop, self.sock, self.protocol)
|
|
|
|
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
|
|
|
|
|
|
|
|
def _make_one(self, create_waiter=None):
|
|
|
|
transport = _SelectorSslTransport(
|
|
|
|
self.loop, self.sock, self.protocol, self.sslcontext)
|
|
|
|
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)
|
2013-10-17 17:40:50 -03:00
|
|
|
tr = _SelectorSslTransport(
|
|
|
|
self.loop, self.sock, self.protocol, self.sslcontext,
|
|
|
|
waiter=waiter)
|
|
|
|
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
|
|
|
|
transport = _SelectorSslTransport(
|
|
|
|
self.loop, self.sock, self.protocol, self.sslcontext)
|
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
|
|
|
|
transport = _SelectorSslTransport(
|
|
|
|
self.loop, self.sock, self.protocol, self.sslcontext)
|
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)
|
|
|
|
transport = _SelectorSslTransport(
|
|
|
|
self.loop, self.sock, self.protocol, self.sslcontext, waiter)
|
|
|
|
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):
|
|
|
|
transport = _SelectorSslTransport(
|
|
|
|
self.loop, self.sock, self.protocol, self.sslcontext)
|
2014-01-25 10:32:06 -04:00
|
|
|
transport._waiter = asyncio.Future(loop=self.loop)
|
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)
|
|
|
|
self.assertTrue(transport._waiter.done())
|
|
|
|
self.assertIs(exc, transport._waiter.exception())
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
def test_close(self):
|
|
|
|
tr = self._make_one()
|
|
|
|
tr.close()
|
|
|
|
|
|
|
|
self.assertTrue(tr._closing)
|
|
|
|
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])
|
|
|
|
|
|
|
|
@unittest.skipIf(ssl is None or not ssl.HAS_SNI, 'No SNI support')
|
|
|
|
def test_server_hostname(self):
|
|
|
|
_SelectorSslTransport(
|
|
|
|
self.loop, self.sock, self.protocol, self.sslcontext,
|
|
|
|
server_hostname='localhost')
|
|
|
|
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
|
|
|
|
|
|
|
|
def test_read_ready(self):
|
|
|
|
transport = _SelectorDatagramTransport(
|
|
|
|
self.loop, self.sock, self.protocol)
|
|
|
|
|
|
|
|
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):
|
|
|
|
transport = _SelectorDatagramTransport(
|
|
|
|
self.loop, self.sock, self.protocol)
|
|
|
|
|
|
|
|
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):
|
|
|
|
transport = _SelectorDatagramTransport(
|
|
|
|
self.loop, self.sock, self.protocol)
|
|
|
|
|
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):
|
|
|
|
transport = _SelectorDatagramTransport(
|
|
|
|
self.loop, self.sock, self.protocol)
|
|
|
|
|
|
|
|
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'
|
|
|
|
transport = _SelectorDatagramTransport(
|
|
|
|
self.loop, self.sock, self.protocol)
|
|
|
|
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')
|
|
|
|
transport = _SelectorDatagramTransport(
|
|
|
|
self.loop, self.sock, self.protocol)
|
|
|
|
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')
|
|
|
|
transport = _SelectorDatagramTransport(
|
|
|
|
self.loop, self.sock, self.protocol)
|
|
|
|
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):
|
|
|
|
transport = _SelectorDatagramTransport(
|
|
|
|
self.loop, self.sock, self.protocol)
|
|
|
|
transport._buffer.append((b'data', ('0.0.0.0', 12345)))
|
|
|
|
transport.sendto(b'', ())
|
|
|
|
self.assertFalse(self.sock.sendto.called)
|
|
|
|
self.assertEqual(
|
|
|
|
[(b'data', ('0.0.0.0', 12345))], list(transport._buffer))
|
|
|
|
|
|
|
|
def test_sendto_buffer(self):
|
|
|
|
transport = _SelectorDatagramTransport(
|
|
|
|
self.loop, self.sock, self.protocol)
|
|
|
|
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')
|
|
|
|
transport = _SelectorDatagramTransport(
|
|
|
|
self.loop, self.sock, self.protocol)
|
|
|
|
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')
|
|
|
|
transport = _SelectorDatagramTransport(
|
|
|
|
self.loop, self.sock, self.protocol)
|
|
|
|
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
|
|
|
|
|
|
|
|
transport = _SelectorDatagramTransport(
|
|
|
|
self.loop, self.sock, self.protocol)
|
|
|
|
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
|
|
|
|
|
|
|
transport = _SelectorDatagramTransport(
|
|
|
|
self.loop, self.sock, self.protocol)
|
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
|
|
|
|
|
|
|
|
transport = _SelectorDatagramTransport(
|
|
|
|
self.loop, self.sock, self.protocol)
|
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
|
|
|
|
|
|
|
|
transport = _SelectorDatagramTransport(
|
|
|
|
self.loop, self.sock, self.protocol, ('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):
|
|
|
|
transport = _SelectorDatagramTransport(
|
|
|
|
self.loop, self.sock, self.protocol)
|
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):
|
|
|
|
transport = _SelectorDatagramTransport(
|
|
|
|
self.loop, self.sock, self.protocol, ('0.0.0.0', 1))
|
|
|
|
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):
|
|
|
|
transport = _SelectorDatagramTransport(
|
|
|
|
self.loop, self.sock, self.protocol, address=(1,))
|
|
|
|
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)
|
|
|
|
|
|
|
|
transport = _SelectorDatagramTransport(
|
|
|
|
self.loop, self.sock, self.protocol)
|
|
|
|
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)
|
|
|
|
|
|
|
|
transport = _SelectorDatagramTransport(
|
|
|
|
self.loop, self.sock, self.protocol)
|
|
|
|
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):
|
|
|
|
transport = _SelectorDatagramTransport(
|
|
|
|
self.loop, self.sock, self.protocol)
|
|
|
|
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
|
|
|
|
|
|
|
|
transport = _SelectorDatagramTransport(
|
|
|
|
self.loop, self.sock, self.protocol)
|
|
|
|
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
|
|
|
|
|
|
|
transport = _SelectorDatagramTransport(
|
|
|
|
self.loop, self.sock, self.protocol)
|
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
|
|
|
|
|
|
|
|
transport = _SelectorDatagramTransport(
|
|
|
|
self.loop, self.sock, self.protocol)
|
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
|
|
|
|
|
|
|
|
transport = _SelectorDatagramTransport(
|
|
|
|
self.loop, self.sock, self.protocol, ('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):
|
|
|
|
transport = _SelectorDatagramTransport(
|
|
|
|
self.loop, self.sock, self.protocol, ('0.0.0.0', 1))
|
|
|
|
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()
|