summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_asyncio
diff options
context:
space:
mode:
authorYury Selivanov <yury@magic.io>2017-12-15 01:53:26 (GMT)
committerGitHub <noreply@github.com>2017-12-15 01:53:26 (GMT)
commit19a44f63c738388ef3c8515348b4ffc061dfd627 (patch)
tree6de5ddd62a1bdee7a90e5fe8fc59348fe7c4f4f8 /Lib/test/test_asyncio
parent41264f1cd4d6066b2797ff07cae465c1e06ff3b2 (diff)
downloadcpython-19a44f63c738388ef3c8515348b4ffc061dfd627.zip
cpython-19a44f63c738388ef3c8515348b4ffc061dfd627.tar.gz
cpython-19a44f63c738388ef3c8515348b4ffc061dfd627.tar.bz2
bpo-32327: Convert asyncio functions documented as coroutines to coroutines. (#4872)
Diffstat (limited to 'Lib/test/test_asyncio')
-rw-r--r--Lib/test/test_asyncio/test_base_events.py50
-rw-r--r--Lib/test/test_asyncio/test_events.py5
-rw-r--r--Lib/test/test_asyncio/test_proactor_events.py21
-rw-r--r--Lib/test/test_asyncio/test_selector_events.py74
-rw-r--r--Lib/test/test_asyncio/test_tasks.py21
-rw-r--r--Lib/test/test_asyncio/test_windows_events.py2
6 files changed, 84 insertions, 89 deletions
diff --git a/Lib/test/test_asyncio/test_base_events.py b/Lib/test/test_asyncio/test_base_events.py
index e43fe69..f8427cd 100644
--- a/Lib/test/test_asyncio/test_base_events.py
+++ b/Lib/test/test_asyncio/test_base_events.py
@@ -217,14 +217,6 @@ class BaseEventLoopTests(test_utils.TestCase):
self.loop.set_default_executor(executor)
self.assertIs(executor, self.loop._default_executor)
- def test_getnameinfo(self):
- sockaddr = mock.Mock()
- self.loop.run_in_executor = mock.Mock()
- self.loop.getnameinfo(sockaddr)
- self.assertEqual(
- (None, socket.getnameinfo, sockaddr, 0),
- self.loop.run_in_executor.call_args[0])
-
def test_call_soon(self):
def cb():
pass
@@ -345,26 +337,6 @@ class BaseEventLoopTests(test_utils.TestCase):
# check disabled if debug mode is disabled
test_thread(self.loop, False, create_loop=True)
- def test_run_once_in_executor_plain(self):
- def cb():
- pass
- f = asyncio.Future(loop=self.loop)
- executor = mock.Mock()
- executor.submit.return_value = f
-
- self.loop.set_default_executor(executor)
-
- res = self.loop.run_in_executor(None, cb)
- self.assertIs(f, res)
-
- executor = mock.Mock()
- executor.submit.return_value = f
- res = self.loop.run_in_executor(executor, cb)
- self.assertIs(f, res)
- self.assertTrue(executor.submit.called)
-
- f.cancel() # Don't complain about abandoned Future.
-
def test__run_once(self):
h1 = asyncio.TimerHandle(time.monotonic() + 5.0, lambda: True, (),
self.loop)
@@ -1007,6 +979,12 @@ class BaseEventLoopWithSelectorTests(test_utils.TestCase):
self.loop = asyncio.new_event_loop()
self.set_event_loop(self.loop)
+ @mock.patch('socket.getnameinfo')
+ def test_getnameinfo(self, m_gai):
+ m_gai.side_effect = lambda *args: 42
+ r = self.loop.run_until_complete(self.loop.getnameinfo(('abc', 123)))
+ self.assertEqual(r, 42)
+
@patch_socket
def test_create_connection_multiple_errors(self, m_socket):
@@ -1119,9 +1097,7 @@ class BaseEventLoopWithSelectorTests(test_utils.TestCase):
OSError, self.loop.run_until_complete, coro)
def test_create_connection_connect_err(self):
- @asyncio.coroutine
- def getaddrinfo(*args, **kw):
- yield from []
+ async def getaddrinfo(*args, **kw):
return [(2, 1, 6, '', ('107.6.106.82', 80))]
def getaddrinfo_task(*args, **kwds):
@@ -1714,10 +1690,11 @@ class BaseEventLoopWithSelectorTests(test_utils.TestCase):
self.assertTrue(m_log.error.called)
self.assertFalse(sock.close.called)
self.loop._remove_reader.assert_called_with(10)
- self.loop.call_later.assert_called_with(constants.ACCEPT_RETRY_DELAY,
- # self.loop._start_serving
- mock.ANY,
- MyProto, sock, None, None, mock.ANY)
+ self.loop.call_later.assert_called_with(
+ constants.ACCEPT_RETRY_DELAY,
+ # self.loop._start_serving
+ mock.ANY,
+ MyProto, sock, None, None, mock.ANY)
def test_call_coroutine(self):
@asyncio.coroutine
@@ -1738,7 +1715,8 @@ class BaseEventLoopWithSelectorTests(test_utils.TestCase):
with self.assertRaises(TypeError):
self.loop.call_at(self.loop.time() + 60, func)
with self.assertRaises(TypeError):
- self.loop.run_in_executor(None, func)
+ self.loop.run_until_complete(
+ self.loop.run_in_executor(None, func))
@mock.patch('asyncio.base_events.logger')
def test_log_slow_callbacks(self, m_logger):
diff --git a/Lib/test/test_asyncio/test_events.py b/Lib/test/test_asyncio/test_events.py
index 45a8bb8..58e94d4 100644
--- a/Lib/test/test_asyncio/test_events.py
+++ b/Lib/test/test_asyncio/test_events.py
@@ -1702,7 +1702,7 @@ class EventLoopTestsMixin:
def test_prompt_cancellation(self):
r, w = socket.socketpair()
r.setblocking(False)
- f = self.loop.sock_recv(r, 1)
+ f = self.loop.create_task(self.loop.sock_recv(r, 1))
ov = getattr(f, 'ov', None)
if ov is not None:
self.assertTrue(ov.pending)
@@ -1819,7 +1819,8 @@ class EventLoopTestsMixin:
with self.assertRaises(RuntimeError):
self.loop.call_at(self.loop.time() + .0, func)
with self.assertRaises(RuntimeError):
- self.loop.run_in_executor(None, func)
+ self.loop.run_until_complete(
+ self.loop.run_in_executor(None, func))
with self.assertRaises(RuntimeError):
self.loop.create_task(coro)
with self.assertRaises(RuntimeError):
diff --git a/Lib/test/test_asyncio/test_proactor_events.py b/Lib/test/test_asyncio/test_proactor_events.py
index c3bac95..910f259 100644
--- a/Lib/test/test_asyncio/test_proactor_events.py
+++ b/Lib/test/test_asyncio/test_proactor_events.py
@@ -483,27 +483,6 @@ class BaseProactorEventLoopTests(test_utils.TestCase):
self.loop.close()
self.assertFalse(self.loop._close_self_pipe.called)
- def test_sock_recv(self):
- self.loop.sock_recv(self.sock, 1024)
- self.proactor.recv.assert_called_with(self.sock, 1024)
-
- def test_sock_recv_into(self):
- buf = bytearray(10)
- self.loop.sock_recv_into(self.sock, buf)
- self.proactor.recv_into.assert_called_with(self.sock, buf)
-
- def test_sock_sendall(self):
- self.loop.sock_sendall(self.sock, b'data')
- self.proactor.send.assert_called_with(self.sock, b'data')
-
- def test_sock_connect(self):
- self.loop.sock_connect(self.sock, ('1.2.3.4', 123))
- self.proactor.connect.assert_called_with(self.sock, ('1.2.3.4', 123))
-
- def test_sock_accept(self):
- self.loop.sock_accept(self.sock)
- self.proactor.accept.assert_called_with(self.sock)
-
def test_make_socket_transport(self):
tr = self.loop._make_socket_transport(self.sock, asyncio.Protocol())
self.assertIsInstance(tr, _ProactorSocketTransport)
diff --git a/Lib/test/test_asyncio/test_selector_events.py b/Lib/test/test_asyncio/test_selector_events.py
index 24feb30..04b0f97 100644
--- a/Lib/test/test_asyncio/test_selector_events.py
+++ b/Lib/test/test_asyncio/test_selector_events.py
@@ -176,9 +176,15 @@ class BaseSelectorEventLoopTests(test_utils.TestCase):
sock = test_utils.mock_nonblocking_socket()
self.loop._sock_recv = mock.Mock()
- f = self.loop.sock_recv(sock, 1024)
- self.assertIsInstance(f, asyncio.Future)
- self.loop._sock_recv.assert_called_with(f, None, sock, 1024)
+ f = self.loop.create_task(self.loop.sock_recv(sock, 1024))
+ self.loop.run_until_complete(asyncio.sleep(0.01, loop=self.loop))
+
+ self.assertEqual(self.loop._sock_recv.call_args[0][1:],
+ (None, sock, 1024))
+
+ f.cancel()
+ with self.assertRaises(asyncio.CancelledError):
+ self.loop.run_until_complete(f)
def test_sock_recv_reconnection(self):
sock = mock.Mock()
@@ -188,7 +194,11 @@ class BaseSelectorEventLoopTests(test_utils.TestCase):
self.loop.add_reader = mock.Mock()
self.loop.remove_reader = mock.Mock()
- fut = self.loop.sock_recv(sock, 1024)
+ fut = self.loop.create_task(
+ self.loop.sock_recv(sock, 1024))
+
+ self.loop.run_until_complete(asyncio.sleep(0.01, loop=self.loop))
+
callback = self.loop.add_reader.call_args[0][1]
params = self.loop.add_reader.call_args[0][2:]
@@ -198,6 +208,8 @@ class BaseSelectorEventLoopTests(test_utils.TestCase):
sock.recv.side_effect = OSError(9)
callback(*params)
+ self.loop.run_until_complete(asyncio.sleep(0.01, loop=self.loop))
+
self.assertIsInstance(fut.exception(), OSError)
self.assertEqual((10,), self.loop.remove_reader.call_args[0])
@@ -245,18 +257,26 @@ class BaseSelectorEventLoopTests(test_utils.TestCase):
sock = test_utils.mock_nonblocking_socket()
self.loop._sock_sendall = mock.Mock()
- f = self.loop.sock_sendall(sock, b'data')
- self.assertIsInstance(f, asyncio.Future)
+ f = self.loop.create_task(
+ self.loop.sock_sendall(sock, b'data'))
+
+ self.loop.run_until_complete(asyncio.sleep(0.01, loop=self.loop))
+
self.assertEqual(
- (f, None, sock, b'data'),
- self.loop._sock_sendall.call_args[0])
+ (None, sock, b'data'),
+ self.loop._sock_sendall.call_args[0][1:])
+
+ f.cancel()
+ with self.assertRaises(asyncio.CancelledError):
+ self.loop.run_until_complete(f)
def test_sock_sendall_nodata(self):
sock = test_utils.mock_nonblocking_socket()
self.loop._sock_sendall = mock.Mock()
- f = self.loop.sock_sendall(sock, b'')
- self.assertIsInstance(f, asyncio.Future)
+ f = self.loop.create_task(self.loop.sock_sendall(sock, b''))
+ self.loop.run_until_complete(asyncio.sleep(0, loop=self.loop))
+
self.assertTrue(f.done())
self.assertIsNone(f.result())
self.assertFalse(self.loop._sock_sendall.called)
@@ -269,7 +289,10 @@ class BaseSelectorEventLoopTests(test_utils.TestCase):
self.loop.add_writer = mock.Mock()
self.loop.remove_writer = mock.Mock()
- fut = self.loop.sock_sendall(sock, b'data')
+ fut = self.loop.create_task(self.loop.sock_sendall(sock, b'data'))
+
+ self.loop.run_until_complete(asyncio.sleep(0.01, loop=self.loop))
+
callback = self.loop.add_writer.call_args[0][1]
params = self.loop.add_writer.call_args[0][2:]
@@ -279,6 +302,8 @@ class BaseSelectorEventLoopTests(test_utils.TestCase):
sock.send.side_effect = OSError(9)
callback(*params)
+ self.loop.run_until_complete(asyncio.sleep(0.01, loop=self.loop))
+
self.assertIsInstance(fut.exception(), OSError)
self.assertEqual((10,), self.loop.remove_writer.call_args[0])
@@ -402,17 +427,17 @@ class BaseSelectorEventLoopTests(test_utils.TestCase):
def test_sock_connect_resolve_using_socket_params(self, m_gai):
addr = ('need-resolution.com', 8080)
sock = test_utils.mock_nonblocking_socket()
- m_gai.side_effect = (None, None, None, None, ('127.0.0.1', 0))
- m_gai._is_coroutine = False
+
+ m_gai.side_effect = \
+ lambda *args: [(None, None, None, None, ('127.0.0.1', 0))]
+
con = self.loop.create_task(self.loop.sock_connect(sock, addr))
- while not m_gai.called:
- self.loop._run_once()
+ self.loop.run_until_complete(con)
m_gai.assert_called_with(
addr[0], addr[1], sock.family, sock.type, sock.proto, 0)
- con.cancel()
- with self.assertRaises(asyncio.CancelledError):
- self.loop.run_until_complete(con)
+ self.loop.run_until_complete(con)
+ sock.connect.assert_called_with(('127.0.0.1', 0))
def test__sock_connect(self):
f = asyncio.Future(loop=self.loop)
@@ -487,10 +512,15 @@ class BaseSelectorEventLoopTests(test_utils.TestCase):
sock = test_utils.mock_nonblocking_socket()
self.loop._sock_accept = mock.Mock()
- f = self.loop.sock_accept(sock)
- self.assertIsInstance(f, asyncio.Future)
- self.assertEqual(
- (f, False, sock), self.loop._sock_accept.call_args[0])
+ f = self.loop.create_task(self.loop.sock_accept(sock))
+ self.loop.run_until_complete(asyncio.sleep(0.01, loop=self.loop))
+
+ self.assertFalse(self.loop._sock_accept.call_args[0][1])
+ self.assertIs(self.loop._sock_accept.call_args[0][2], sock)
+
+ f.cancel()
+ with self.assertRaises(asyncio.CancelledError):
+ self.loop.run_until_complete(f)
def test__sock_accept(self):
f = asyncio.Future(loop=self.loop)
diff --git a/Lib/test/test_asyncio/test_tasks.py b/Lib/test/test_asyncio/test_tasks.py
index 0838ebf..a5563ba 100644
--- a/Lib/test/test_asyncio/test_tasks.py
+++ b/Lib/test/test_asyncio/test_tasks.py
@@ -2545,19 +2545,26 @@ class RunCoroutineThreadsafeTests(test_utils.TestCase):
def test_run_coroutine_threadsafe_task_factory_exception(self):
"""Test coroutine submission from a tread to an event loop
when the task factory raise an exception."""
- # Schedule the target
- future = self.loop.run_in_executor(
- None, lambda: self.target(advance_coro=True))
- # Set corrupted task factory
- self.loop.set_task_factory(lambda loop, coro: wrong_name)
+
+ def task_factory(loop, coro):
+ raise NameError
+
+ run = self.loop.create_task(
+ self.loop.run_in_executor(
+ None, lambda: self.target(advance_coro=True)))
+
# Set exception handler
callback = test_utils.MockCallback()
self.loop.set_exception_handler(callback)
+
+ # Set corrupted task factory
+ self.loop.set_task_factory(task_factory)
+
# Run event loop
with self.assertRaises(NameError) as exc_context:
- self.loop.run_until_complete(future)
+ self.loop.run_until_complete(run)
+
# Check exceptions
- self.assertIn('wrong_name', exc_context.exception.args[0])
self.assertEqual(len(callback.call_args_list), 1)
(loop, context), kwargs = callback.call_args
self.assertEqual(context['exception'], exc_context.exception)
diff --git a/Lib/test/test_asyncio/test_windows_events.py b/Lib/test/test_asyncio/test_windows_events.py
index fdba636..e4ff7fc 100644
--- a/Lib/test/test_asyncio/test_windows_events.py
+++ b/Lib/test/test_asyncio/test_windows_events.py
@@ -39,7 +39,7 @@ class ProactorTests(test_utils.TestCase):
def test_close(self):
a, b = socket.socketpair()
trans = self.loop._make_socket_transport(a, asyncio.Protocol())
- f = asyncio.ensure_future(self.loop.sock_recv(b, 100))
+ f = asyncio.ensure_future(self.loop.sock_recv(b, 100), loop=self.loop)
trans.close()
self.loop.run_until_complete(f)
self.assertEqual(f.result(), b'')