2013-10-17 17:40:50 -03:00
|
|
|
"""Tests for tasks.py."""
|
|
|
|
|
2015-05-02 22:38:24 -03:00
|
|
|
import contextlib
|
|
|
|
import functools
|
2015-08-14 16:30:59 -03:00
|
|
|
import io
|
2014-09-25 20:12:10 -03:00
|
|
|
import os
|
2014-06-27 07:28:41 -03:00
|
|
|
import re
|
2014-06-16 12:11:05 -03:00
|
|
|
import sys
|
2014-04-15 16:06:34 -03:00
|
|
|
import types
|
2013-10-17 17:40:50 -03:00
|
|
|
import unittest
|
2014-04-27 14:44:22 -03:00
|
|
|
import weakref
|
2014-06-24 17:37:53 -03:00
|
|
|
from unittest import mock
|
2014-12-26 16:16:42 -04:00
|
|
|
|
|
|
|
import asyncio
|
|
|
|
from asyncio import coroutines
|
|
|
|
from asyncio import test_utils
|
2014-12-18 07:29:53 -04:00
|
|
|
try:
|
2014-12-26 16:16:42 -04:00
|
|
|
from test import support
|
2014-12-18 07:29:53 -04:00
|
|
|
except ImportError:
|
|
|
|
from asyncio import test_support as support
|
2015-05-11 15:23:38 -03:00
|
|
|
try:
|
|
|
|
from test.support.script_helper import assert_python_ok
|
|
|
|
except ImportError:
|
|
|
|
try:
|
|
|
|
from test.script_helper import assert_python_ok
|
|
|
|
except ImportError:
|
|
|
|
from asyncio.test_support import assert_python_ok
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
|
2014-06-24 17:37:53 -03:00
|
|
|
PY34 = (sys.version_info >= (3, 4))
|
2014-06-17 20:14:59 -03:00
|
|
|
PY35 = (sys.version_info >= (3, 5))
|
|
|
|
|
|
|
|
|
2014-02-11 06:54:08 -04:00
|
|
|
@asyncio.coroutine
|
|
|
|
def coroutine_function():
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2015-05-02 22:38:24 -03:00
|
|
|
@contextlib.contextmanager
|
|
|
|
def set_coroutine_debug(enabled):
|
|
|
|
coroutines = asyncio.coroutines
|
|
|
|
|
|
|
|
old_debug = coroutines._DEBUG
|
|
|
|
try:
|
|
|
|
coroutines._DEBUG = enabled
|
|
|
|
yield
|
|
|
|
finally:
|
|
|
|
coroutines._DEBUG = old_debug
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-07-10 19:21:27 -03:00
|
|
|
def format_coroutine(qualname, state, src, source_traceback, generator=False):
|
|
|
|
if generator:
|
|
|
|
state = '%s' % state
|
|
|
|
else:
|
|
|
|
state = '%s, defined' % state
|
|
|
|
if source_traceback is not None:
|
|
|
|
frame = source_traceback[-1]
|
|
|
|
return ('coro=<%s() %s at %s> created at %s:%s'
|
|
|
|
% (qualname, state, src, frame[0], frame[1]))
|
|
|
|
else:
|
|
|
|
return 'coro=<%s() %s at %s>' % (qualname, state, src)
|
|
|
|
|
|
|
|
|
2013-10-17 17:40:50 -03:00
|
|
|
class Dummy:
|
|
|
|
|
|
|
|
def __repr__(self):
|
2014-06-25 16:41:58 -03:00
|
|
|
return '<Dummy>'
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def __call__(self, *args):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2014-06-17 20:36:32 -03:00
|
|
|
class TaskTests(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()
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2015-12-11 12:33:59 -04:00
|
|
|
def test_other_loop_future(self):
|
|
|
|
other_loop = asyncio.new_event_loop()
|
|
|
|
fut = asyncio.Future(loop=other_loop)
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def run(fut):
|
|
|
|
yield from fut
|
|
|
|
|
|
|
|
try:
|
|
|
|
with self.assertRaisesRegex(RuntimeError,
|
|
|
|
r'Task .* got Future .* attached'):
|
|
|
|
self.loop.run_until_complete(run(fut))
|
|
|
|
finally:
|
|
|
|
other_loop.close()
|
|
|
|
|
2013-10-17 17:40:50 -03:00
|
|
|
def test_task_class(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def notmuch():
|
|
|
|
return 'ok'
|
2014-01-25 10:32:06 -04:00
|
|
|
t = asyncio.Task(notmuch(), loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.loop.run_until_complete(t)
|
|
|
|
self.assertTrue(t.done())
|
|
|
|
self.assertEqual(t.result(), 'ok')
|
|
|
|
self.assertIs(t._loop, self.loop)
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
loop = asyncio.new_event_loop()
|
2014-06-17 20:36:32 -03:00
|
|
|
self.set_event_loop(loop)
|
2014-01-25 10:32:06 -04:00
|
|
|
t = asyncio.Task(notmuch(), loop=loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertIs(t._loop, loop)
|
2014-06-25 18:32:25 -03:00
|
|
|
loop.run_until_complete(t)
|
2013-10-17 17:40:50 -03:00
|
|
|
loop.close()
|
|
|
|
|
2015-05-11 15:48:38 -03:00
|
|
|
def test_ensure_future_coroutine(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def notmuch():
|
|
|
|
return 'ok'
|
2015-05-11 15:48:38 -03:00
|
|
|
t = asyncio.ensure_future(notmuch(), loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.loop.run_until_complete(t)
|
|
|
|
self.assertTrue(t.done())
|
|
|
|
self.assertEqual(t.result(), 'ok')
|
|
|
|
self.assertIs(t._loop, self.loop)
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
loop = asyncio.new_event_loop()
|
2014-06-17 20:36:32 -03:00
|
|
|
self.set_event_loop(loop)
|
2015-05-11 15:48:38 -03:00
|
|
|
t = asyncio.ensure_future(notmuch(), loop=loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertIs(t._loop, loop)
|
2014-06-25 18:32:25 -03:00
|
|
|
loop.run_until_complete(t)
|
2013-10-17 17:40:50 -03:00
|
|
|
loop.close()
|
|
|
|
|
2015-05-11 15:48:38 -03:00
|
|
|
def test_ensure_future_future(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
f_orig = asyncio.Future(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
f_orig.set_result('ko')
|
|
|
|
|
2015-05-11 15:48:38 -03:00
|
|
|
f = asyncio.ensure_future(f_orig)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.loop.run_until_complete(f)
|
|
|
|
self.assertTrue(f.done())
|
|
|
|
self.assertEqual(f.result(), 'ko')
|
|
|
|
self.assertIs(f, f_orig)
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
loop = asyncio.new_event_loop()
|
2014-06-17 20:36:32 -03:00
|
|
|
self.set_event_loop(loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
with self.assertRaises(ValueError):
|
2015-05-11 15:48:38 -03:00
|
|
|
f = asyncio.ensure_future(f_orig, loop=loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
loop.close()
|
|
|
|
|
2015-05-11 15:48:38 -03:00
|
|
|
f = asyncio.ensure_future(f_orig, loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertIs(f, f_orig)
|
|
|
|
|
2015-05-11 15:48:38 -03:00
|
|
|
def test_ensure_future_task(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def notmuch():
|
|
|
|
return 'ok'
|
2014-01-25 10:32:06 -04:00
|
|
|
t_orig = asyncio.Task(notmuch(), loop=self.loop)
|
2015-05-11 15:48:38 -03:00
|
|
|
t = asyncio.ensure_future(t_orig)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.loop.run_until_complete(t)
|
|
|
|
self.assertTrue(t.done())
|
|
|
|
self.assertEqual(t.result(), 'ok')
|
|
|
|
self.assertIs(t, t_orig)
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
loop = asyncio.new_event_loop()
|
2014-06-17 20:36:32 -03:00
|
|
|
self.set_event_loop(loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
with self.assertRaises(ValueError):
|
2015-05-11 15:48:38 -03:00
|
|
|
t = asyncio.ensure_future(t_orig, loop=loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
loop.close()
|
|
|
|
|
2015-05-11 15:48:38 -03:00
|
|
|
t = asyncio.ensure_future(t_orig, loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertIs(t, t_orig)
|
|
|
|
|
2015-10-02 16:00:19 -03:00
|
|
|
@unittest.skipUnless(PY35, 'need python 3.5 or later')
|
|
|
|
def test_ensure_future_awaitable(self):
|
|
|
|
class Aw:
|
|
|
|
def __init__(self, coro):
|
|
|
|
self.coro = coro
|
|
|
|
def __await__(self):
|
|
|
|
return (yield from self.coro)
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def coro():
|
|
|
|
return 'ok'
|
|
|
|
|
|
|
|
loop = asyncio.new_event_loop()
|
|
|
|
self.set_event_loop(loop)
|
|
|
|
fut = asyncio.ensure_future(Aw(coro()), loop=loop)
|
|
|
|
loop.run_until_complete(fut)
|
|
|
|
assert fut.result() == 'ok'
|
|
|
|
|
2015-05-11 15:48:38 -03:00
|
|
|
def test_ensure_future_neither(self):
|
2013-10-17 17:40:50 -03:00
|
|
|
with self.assertRaises(TypeError):
|
2015-05-11 15:48:38 -03:00
|
|
|
asyncio.ensure_future('ok')
|
|
|
|
|
|
|
|
def test_async_warning(self):
|
|
|
|
f = asyncio.Future(loop=self.loop)
|
|
|
|
with self.assertWarnsRegex(DeprecationWarning,
|
|
|
|
'function is deprecated, use ensure_'):
|
|
|
|
self.assertIs(f, asyncio.async(f))
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2015-08-14 16:30:59 -03:00
|
|
|
def test_get_stack(self):
|
|
|
|
T = None
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def foo():
|
|
|
|
yield from bar()
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def bar():
|
|
|
|
# test get_stack()
|
|
|
|
f = T.get_stack(limit=1)
|
|
|
|
try:
|
|
|
|
self.assertEqual(f[0].f_code.co_name, 'foo')
|
|
|
|
finally:
|
|
|
|
f = None
|
|
|
|
|
|
|
|
# test print_stack()
|
|
|
|
file = io.StringIO()
|
|
|
|
T.print_stack(limit=1, file=file)
|
|
|
|
file.seek(0)
|
|
|
|
tb = file.read()
|
|
|
|
self.assertRegex(tb, r'foo\(\) running')
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def runner():
|
|
|
|
nonlocal T
|
|
|
|
T = asyncio.ensure_future(foo(), loop=self.loop)
|
|
|
|
yield from T
|
|
|
|
|
|
|
|
self.loop.run_until_complete(runner())
|
|
|
|
|
2013-10-17 17:40:50 -03:00
|
|
|
def test_task_repr(self):
|
2014-07-29 07:58:23 -03:00
|
|
|
self.loop.set_debug(False)
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def notmuch():
|
|
|
|
yield from []
|
|
|
|
return 'abc'
|
|
|
|
|
2014-06-25 16:41:58 -03:00
|
|
|
# test coroutine function
|
2014-06-17 20:14:59 -03:00
|
|
|
self.assertEqual(notmuch.__name__, 'notmuch')
|
|
|
|
if PY35:
|
|
|
|
self.assertEqual(notmuch.__qualname__,
|
|
|
|
'TaskTests.test_task_repr.<locals>.notmuch')
|
|
|
|
self.assertEqual(notmuch.__module__, __name__)
|
|
|
|
|
2014-06-12 13:39:26 -03:00
|
|
|
filename, lineno = test_utils.get_function_source(notmuch)
|
|
|
|
src = "%s:%s" % (filename, lineno)
|
|
|
|
|
2014-06-25 16:41:58 -03:00
|
|
|
# test coroutine object
|
2014-06-17 20:14:59 -03:00
|
|
|
gen = notmuch()
|
2014-07-10 20:04:16 -03:00
|
|
|
if coroutines._DEBUG or PY35:
|
2014-06-25 16:41:58 -03:00
|
|
|
coro_qualname = 'TaskTests.test_task_repr.<locals>.notmuch'
|
|
|
|
else:
|
|
|
|
coro_qualname = 'notmuch'
|
2014-06-17 20:14:59 -03:00
|
|
|
self.assertEqual(gen.__name__, 'notmuch')
|
|
|
|
if PY35:
|
|
|
|
self.assertEqual(gen.__qualname__,
|
2014-06-25 16:41:58 -03:00
|
|
|
coro_qualname)
|
2014-06-17 20:14:59 -03:00
|
|
|
|
2014-06-25 16:41:58 -03:00
|
|
|
# test pending Task
|
2014-06-17 20:14:59 -03:00
|
|
|
t = asyncio.Task(gen, loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
t.add_done_callback(Dummy())
|
2014-07-10 19:21:27 -03:00
|
|
|
|
|
|
|
coro = format_coroutine(coro_qualname, 'running', src,
|
|
|
|
t._source_traceback, generator=True)
|
2014-06-12 13:39:26 -03:00
|
|
|
self.assertEqual(repr(t),
|
2014-06-25 16:41:58 -03:00
|
|
|
'<Task pending %s cb=[<Dummy>()]>' % coro)
|
2014-06-12 13:39:26 -03:00
|
|
|
|
2014-06-25 16:41:58 -03:00
|
|
|
# test cancelling Task
|
2013-10-17 17:40:50 -03:00
|
|
|
t.cancel() # Does not take immediate effect!
|
2014-06-12 13:39:26 -03:00
|
|
|
self.assertEqual(repr(t),
|
2014-06-25 16:41:58 -03:00
|
|
|
'<Task cancelling %s cb=[<Dummy>()]>' % coro)
|
|
|
|
|
|
|
|
# test cancelled Task
|
2014-01-25 10:32:06 -04:00
|
|
|
self.assertRaises(asyncio.CancelledError,
|
2013-10-17 17:40:50 -03:00
|
|
|
self.loop.run_until_complete, t)
|
2014-07-10 19:21:27 -03:00
|
|
|
coro = format_coroutine(coro_qualname, 'done', src,
|
|
|
|
t._source_traceback)
|
2014-06-12 13:39:26 -03:00
|
|
|
self.assertEqual(repr(t),
|
2014-06-25 16:41:58 -03:00
|
|
|
'<Task cancelled %s>' % coro)
|
2014-06-12 13:39:26 -03:00
|
|
|
|
2014-06-25 16:41:58 -03:00
|
|
|
# test finished Task
|
2014-01-25 10:32:06 -04:00
|
|
|
t = asyncio.Task(notmuch(), loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.loop.run_until_complete(t)
|
2014-07-10 19:21:27 -03:00
|
|
|
coro = format_coroutine(coro_qualname, 'done', src,
|
|
|
|
t._source_traceback)
|
2014-06-12 13:39:26 -03:00
|
|
|
self.assertEqual(repr(t),
|
2014-06-25 16:41:58 -03:00
|
|
|
"<Task finished %s result='abc'>" % coro)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-06-25 16:41:58 -03:00
|
|
|
def test_task_repr_coro_decorator(self):
|
2014-07-29 07:58:23 -03:00
|
|
|
self.loop.set_debug(False)
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2014-06-12 13:39:26 -03:00
|
|
|
def notmuch():
|
2014-06-25 16:41:58 -03:00
|
|
|
# notmuch() function doesn't use yield from: it will be wrapped by
|
|
|
|
# @coroutine decorator
|
|
|
|
return 123
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-06-25 16:41:58 -03:00
|
|
|
# test coroutine function
|
2014-06-17 20:14:59 -03:00
|
|
|
self.assertEqual(notmuch.__name__, 'notmuch')
|
|
|
|
if PY35:
|
|
|
|
self.assertEqual(notmuch.__qualname__,
|
2015-01-08 19:09:10 -04:00
|
|
|
'TaskTests.test_task_repr_coro_decorator'
|
|
|
|
'.<locals>.notmuch')
|
2014-06-25 16:41:58 -03:00
|
|
|
self.assertEqual(notmuch.__module__, __name__)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-06-25 16:41:58 -03:00
|
|
|
# test coroutine object
|
2014-06-12 13:39:26 -03:00
|
|
|
gen = notmuch()
|
2014-07-10 20:04:16 -03:00
|
|
|
if coroutines._DEBUG or PY35:
|
2014-06-17 20:14:59 -03:00
|
|
|
# On Python >= 3.5, generators now inherit the name of the
|
|
|
|
# function, as expected, and have a qualified name (__qualname__
|
2014-06-25 16:41:58 -03:00
|
|
|
# attribute).
|
2014-06-17 20:14:59 -03:00
|
|
|
coro_name = 'notmuch'
|
2015-01-08 19:09:10 -04:00
|
|
|
coro_qualname = ('TaskTests.test_task_repr_coro_decorator'
|
|
|
|
'.<locals>.notmuch')
|
2014-06-16 12:11:05 -03:00
|
|
|
else:
|
|
|
|
# On Python < 3.5, generators inherit the name of the code, not of
|
|
|
|
# the function. See: http://bugs.python.org/issue21205
|
2014-06-25 16:41:58 -03:00
|
|
|
coro_name = coro_qualname = 'coro'
|
2014-06-17 20:14:59 -03:00
|
|
|
self.assertEqual(gen.__name__, coro_name)
|
|
|
|
if PY35:
|
2014-06-25 16:41:58 -03:00
|
|
|
self.assertEqual(gen.__qualname__, coro_qualname)
|
|
|
|
|
2014-07-05 10:29:41 -03:00
|
|
|
# test repr(CoroWrapper)
|
|
|
|
if coroutines._DEBUG:
|
2014-07-10 19:21:27 -03:00
|
|
|
# format the coroutine object
|
|
|
|
if coroutines._DEBUG:
|
|
|
|
filename, lineno = test_utils.get_function_source(notmuch)
|
|
|
|
frame = gen._source_traceback[-1]
|
|
|
|
coro = ('%s() running, defined at %s:%s, created at %s:%s'
|
|
|
|
% (coro_qualname, filename, lineno,
|
|
|
|
frame[0], frame[1]))
|
|
|
|
else:
|
|
|
|
code = gen.gi_code
|
|
|
|
coro = ('%s() running at %s:%s'
|
2015-01-08 19:09:10 -04:00
|
|
|
% (coro_qualname, code.co_filename,
|
|
|
|
code.co_firstlineno))
|
2014-07-10 19:21:27 -03:00
|
|
|
|
2014-07-05 10:29:41 -03:00
|
|
|
self.assertEqual(repr(gen), '<CoroWrapper %s>' % coro)
|
|
|
|
|
2014-06-25 16:41:58 -03:00
|
|
|
# test pending Task
|
|
|
|
t = asyncio.Task(gen, loop=self.loop)
|
|
|
|
t.add_done_callback(Dummy())
|
2014-07-10 19:21:27 -03:00
|
|
|
|
|
|
|
# format the coroutine object
|
|
|
|
if coroutines._DEBUG:
|
|
|
|
src = '%s:%s' % test_utils.get_function_source(notmuch)
|
|
|
|
else:
|
|
|
|
code = gen.gi_code
|
|
|
|
src = '%s:%s' % (code.co_filename, code.co_firstlineno)
|
|
|
|
coro = format_coroutine(coro_qualname, 'running', src,
|
|
|
|
t._source_traceback,
|
|
|
|
generator=not coroutines._DEBUG)
|
2014-06-25 16:41:58 -03:00
|
|
|
self.assertEqual(repr(t),
|
|
|
|
'<Task pending %s cb=[<Dummy>()]>' % coro)
|
2014-06-25 18:32:25 -03:00
|
|
|
self.loop.run_until_complete(t)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-07-02 19:59:00 -03:00
|
|
|
def test_task_repr_wait_for(self):
|
2014-07-29 07:58:23 -03:00
|
|
|
self.loop.set_debug(False)
|
|
|
|
|
2014-07-02 19:59:00 -03:00
|
|
|
@asyncio.coroutine
|
|
|
|
def wait_for(fut):
|
|
|
|
return (yield from fut)
|
|
|
|
|
|
|
|
fut = asyncio.Future(loop=self.loop)
|
|
|
|
task = asyncio.Task(wait_for(fut), loop=self.loop)
|
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
self.assertRegex(repr(task),
|
|
|
|
'<Task .* wait_for=%s>' % re.escape(repr(fut)))
|
|
|
|
|
2014-07-08 06:29:25 -03:00
|
|
|
fut.set_result(None)
|
|
|
|
self.loop.run_until_complete(task)
|
|
|
|
|
2015-05-02 22:38:24 -03:00
|
|
|
def test_task_repr_partial_corowrapper(self):
|
|
|
|
# Issue #222: repr(CoroWrapper) must not fail in debug mode if the
|
|
|
|
# coroutine is a partial function
|
|
|
|
with set_coroutine_debug(True):
|
|
|
|
self.loop.set_debug(True)
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def func(x, y):
|
|
|
|
yield from asyncio.sleep(0)
|
|
|
|
|
|
|
|
partial_func = asyncio.coroutine(functools.partial(func, 1))
|
|
|
|
task = self.loop.create_task(partial_func(2))
|
|
|
|
|
|
|
|
# make warnings quiet
|
|
|
|
task._log_destroy_pending = False
|
|
|
|
self.addCleanup(task._coro.close)
|
|
|
|
|
|
|
|
coro_repr = repr(task._coro)
|
|
|
|
expected = ('<CoroWrapper TaskTests.test_task_repr_partial_corowrapper'
|
|
|
|
'.<locals>.func(1)() running, ')
|
|
|
|
self.assertTrue(coro_repr.startswith(expected),
|
|
|
|
coro_repr)
|
|
|
|
|
2013-10-17 17:40:50 -03:00
|
|
|
def test_task_basics(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def outer():
|
|
|
|
a = yield from inner1()
|
|
|
|
b = yield from inner2()
|
|
|
|
return a+b
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def inner1():
|
|
|
|
return 42
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def inner2():
|
|
|
|
return 1000
|
|
|
|
|
|
|
|
t = outer()
|
|
|
|
self.assertEqual(self.loop.run_until_complete(t), 1042)
|
|
|
|
|
|
|
|
def test_cancel(self):
|
|
|
|
|
|
|
|
def gen():
|
|
|
|
when = yield
|
|
|
|
self.assertAlmostEqual(10.0, when)
|
|
|
|
yield 0
|
|
|
|
|
2014-06-17 20:36:32 -03:00
|
|
|
loop = self.new_test_loop(gen)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def task():
|
2014-01-25 10:32:06 -04:00
|
|
|
yield from asyncio.sleep(10.0, loop=loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
return 12
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
t = asyncio.Task(task(), loop=loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
loop.call_soon(t.cancel)
|
2014-01-25 10:32:06 -04:00
|
|
|
with self.assertRaises(asyncio.CancelledError):
|
2013-10-17 17:40:50 -03:00
|
|
|
loop.run_until_complete(t)
|
|
|
|
self.assertTrue(t.done())
|
|
|
|
self.assertTrue(t.cancelled())
|
|
|
|
self.assertFalse(t.cancel())
|
|
|
|
|
|
|
|
def test_cancel_yield(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def task():
|
|
|
|
yield
|
|
|
|
yield
|
|
|
|
return 12
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
t = asyncio.Task(task(), loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
test_utils.run_briefly(self.loop) # start coro
|
|
|
|
t.cancel()
|
|
|
|
self.assertRaises(
|
2014-01-25 10:32:06 -04:00
|
|
|
asyncio.CancelledError, self.loop.run_until_complete, t)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertTrue(t.done())
|
|
|
|
self.assertTrue(t.cancelled())
|
|
|
|
self.assertFalse(t.cancel())
|
|
|
|
|
|
|
|
def test_cancel_inner_future(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
f = asyncio.Future(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def task():
|
|
|
|
yield from f
|
|
|
|
return 12
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
t = asyncio.Task(task(), loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
test_utils.run_briefly(self.loop) # start task
|
|
|
|
f.cancel()
|
2014-01-25 10:32:06 -04:00
|
|
|
with self.assertRaises(asyncio.CancelledError):
|
2013-10-17 17:40:50 -03:00
|
|
|
self.loop.run_until_complete(t)
|
|
|
|
self.assertTrue(f.cancelled())
|
|
|
|
self.assertTrue(t.cancelled())
|
|
|
|
|
|
|
|
def test_cancel_both_task_and_inner_future(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
f = asyncio.Future(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def task():
|
|
|
|
yield from f
|
|
|
|
return 12
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
t = asyncio.Task(task(), loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
|
|
|
|
f.cancel()
|
|
|
|
t.cancel()
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
with self.assertRaises(asyncio.CancelledError):
|
2013-10-17 17:40:50 -03:00
|
|
|
self.loop.run_until_complete(t)
|
|
|
|
|
|
|
|
self.assertTrue(t.done())
|
|
|
|
self.assertTrue(f.cancelled())
|
|
|
|
self.assertTrue(t.cancelled())
|
|
|
|
|
|
|
|
def test_cancel_task_catching(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
fut1 = asyncio.Future(loop=self.loop)
|
|
|
|
fut2 = asyncio.Future(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def task():
|
|
|
|
yield from fut1
|
|
|
|
try:
|
|
|
|
yield from fut2
|
2014-01-25 10:32:06 -04:00
|
|
|
except asyncio.CancelledError:
|
2013-10-17 17:40:50 -03:00
|
|
|
return 42
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
t = asyncio.Task(task(), loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
self.assertIs(t._fut_waiter, fut1) # White-box test.
|
|
|
|
fut1.set_result(None)
|
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
self.assertIs(t._fut_waiter, fut2) # White-box test.
|
|
|
|
t.cancel()
|
|
|
|
self.assertTrue(fut2.cancelled())
|
|
|
|
res = self.loop.run_until_complete(t)
|
|
|
|
self.assertEqual(res, 42)
|
|
|
|
self.assertFalse(t.cancelled())
|
|
|
|
|
|
|
|
def test_cancel_task_ignoring(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
fut1 = asyncio.Future(loop=self.loop)
|
|
|
|
fut2 = asyncio.Future(loop=self.loop)
|
|
|
|
fut3 = asyncio.Future(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def task():
|
|
|
|
yield from fut1
|
|
|
|
try:
|
|
|
|
yield from fut2
|
2014-01-25 10:32:06 -04:00
|
|
|
except asyncio.CancelledError:
|
2013-10-17 17:40:50 -03:00
|
|
|
pass
|
|
|
|
res = yield from fut3
|
|
|
|
return res
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
t = asyncio.Task(task(), loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
self.assertIs(t._fut_waiter, fut1) # White-box test.
|
|
|
|
fut1.set_result(None)
|
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
self.assertIs(t._fut_waiter, fut2) # White-box test.
|
|
|
|
t.cancel()
|
|
|
|
self.assertTrue(fut2.cancelled())
|
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
self.assertIs(t._fut_waiter, fut3) # White-box test.
|
|
|
|
fut3.set_result(42)
|
|
|
|
res = self.loop.run_until_complete(t)
|
|
|
|
self.assertEqual(res, 42)
|
|
|
|
self.assertFalse(fut3.cancelled())
|
|
|
|
self.assertFalse(t.cancelled())
|
|
|
|
|
|
|
|
def test_cancel_current_task(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
loop = asyncio.new_event_loop()
|
2014-06-17 20:36:32 -03:00
|
|
|
self.set_event_loop(loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def task():
|
|
|
|
t.cancel()
|
|
|
|
self.assertTrue(t._must_cancel) # White-box test.
|
|
|
|
# The sleep should be cancelled immediately.
|
2014-01-25 10:32:06 -04:00
|
|
|
yield from asyncio.sleep(100, loop=loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
return 12
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
t = asyncio.Task(task(), loop=loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertRaises(
|
2014-01-25 10:32:06 -04:00
|
|
|
asyncio.CancelledError, loop.run_until_complete, t)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertTrue(t.done())
|
|
|
|
self.assertFalse(t._must_cancel) # White-box test.
|
|
|
|
self.assertFalse(t.cancel())
|
|
|
|
|
|
|
|
def test_stop_while_run_in_complete(self):
|
|
|
|
|
|
|
|
def gen():
|
|
|
|
when = yield
|
|
|
|
self.assertAlmostEqual(0.1, when)
|
|
|
|
when = yield 0.1
|
|
|
|
self.assertAlmostEqual(0.2, when)
|
|
|
|
when = yield 0.1
|
|
|
|
self.assertAlmostEqual(0.3, when)
|
|
|
|
yield 0.1
|
|
|
|
|
2014-06-17 20:36:32 -03:00
|
|
|
loop = self.new_test_loop(gen)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
x = 0
|
|
|
|
waiters = []
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def task():
|
|
|
|
nonlocal x
|
|
|
|
while x < 10:
|
2014-01-25 10:32:06 -04:00
|
|
|
waiters.append(asyncio.sleep(0.1, loop=loop))
|
2013-10-17 17:40:50 -03:00
|
|
|
yield from waiters[-1]
|
|
|
|
x += 1
|
|
|
|
if x == 2:
|
|
|
|
loop.stop()
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
t = asyncio.Task(task(), loop=loop)
|
2014-06-25 18:57:50 -03:00
|
|
|
with self.assertRaises(RuntimeError) as cm:
|
|
|
|
loop.run_until_complete(t)
|
|
|
|
self.assertEqual(str(cm.exception),
|
|
|
|
'Event loop stopped before Future completed.')
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertFalse(t.done())
|
|
|
|
self.assertEqual(x, 2)
|
|
|
|
self.assertAlmostEqual(0.3, loop.time())
|
|
|
|
|
|
|
|
# close generators
|
|
|
|
for w in waiters:
|
|
|
|
w.close()
|
2014-06-25 18:57:50 -03:00
|
|
|
t.cancel()
|
|
|
|
self.assertRaises(asyncio.CancelledError, loop.run_until_complete, t)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def test_wait_for(self):
|
|
|
|
|
|
|
|
def gen():
|
|
|
|
when = yield
|
|
|
|
self.assertAlmostEqual(0.2, when)
|
|
|
|
when = yield 0
|
|
|
|
self.assertAlmostEqual(0.1, when)
|
|
|
|
when = yield 0.1
|
|
|
|
|
2014-06-17 20:36:32 -03:00
|
|
|
loop = self.new_test_loop(gen)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-01-23 12:40:59 -04:00
|
|
|
foo_running = None
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def foo():
|
2014-01-23 12:40:59 -04:00
|
|
|
nonlocal foo_running
|
|
|
|
foo_running = True
|
|
|
|
try:
|
2014-01-25 10:32:06 -04:00
|
|
|
yield from asyncio.sleep(0.2, loop=loop)
|
2014-01-23 12:40:59 -04:00
|
|
|
finally:
|
|
|
|
foo_running = False
|
2013-10-17 17:40:50 -03:00
|
|
|
return 'done'
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
fut = asyncio.Task(foo(), loop=loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
with self.assertRaises(asyncio.TimeoutError):
|
|
|
|
loop.run_until_complete(asyncio.wait_for(fut, 0.1, loop=loop))
|
2014-01-23 12:40:59 -04:00
|
|
|
self.assertTrue(fut.done())
|
|
|
|
# it should have been cancelled due to the timeout
|
|
|
|
self.assertTrue(fut.cancelled())
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertAlmostEqual(0.1, loop.time())
|
2014-01-23 12:40:59 -04:00
|
|
|
self.assertEqual(foo_running, False)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-01-29 18:30:38 -04:00
|
|
|
def test_wait_for_blocking(self):
|
2014-06-17 20:36:32 -03:00
|
|
|
loop = self.new_test_loop()
|
2014-01-29 18:30:38 -04:00
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def coro():
|
|
|
|
return 'done'
|
|
|
|
|
2014-01-30 20:05:28 -04:00
|
|
|
res = loop.run_until_complete(asyncio.wait_for(coro(),
|
|
|
|
timeout=None,
|
|
|
|
loop=loop))
|
2014-01-29 18:30:38 -04:00
|
|
|
self.assertEqual(res, 'done')
|
|
|
|
|
2013-10-17 17:40:50 -03:00
|
|
|
def test_wait_for_with_global_loop(self):
|
|
|
|
|
|
|
|
def gen():
|
|
|
|
when = yield
|
|
|
|
self.assertAlmostEqual(0.2, when)
|
|
|
|
when = yield 0
|
|
|
|
self.assertAlmostEqual(0.01, when)
|
|
|
|
yield 0.01
|
|
|
|
|
2014-06-17 20:36:32 -03:00
|
|
|
loop = self.new_test_loop(gen)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def foo():
|
2014-01-25 10:32:06 -04:00
|
|
|
yield from asyncio.sleep(0.2, loop=loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
return 'done'
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
asyncio.set_event_loop(loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
try:
|
2014-01-25 10:32:06 -04:00
|
|
|
fut = asyncio.Task(foo(), loop=loop)
|
|
|
|
with self.assertRaises(asyncio.TimeoutError):
|
|
|
|
loop.run_until_complete(asyncio.wait_for(fut, 0.01))
|
2013-10-17 17:40:50 -03:00
|
|
|
finally:
|
2014-01-25 10:32:06 -04:00
|
|
|
asyncio.set_event_loop(None)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
self.assertAlmostEqual(0.01, loop.time())
|
2014-01-23 12:40:59 -04:00
|
|
|
self.assertTrue(fut.done())
|
|
|
|
self.assertTrue(fut.cancelled())
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-08-28 06:19:25 -03:00
|
|
|
def test_wait_for_race_condition(self):
|
|
|
|
|
|
|
|
def gen():
|
|
|
|
yield 0.1
|
|
|
|
yield 0.1
|
|
|
|
yield 0.1
|
|
|
|
|
|
|
|
loop = self.new_test_loop(gen)
|
|
|
|
|
|
|
|
fut = asyncio.Future(loop=loop)
|
|
|
|
task = asyncio.wait_for(fut, timeout=0.2, loop=loop)
|
|
|
|
loop.call_later(0.1, fut.set_result, "ok")
|
|
|
|
res = loop.run_until_complete(task)
|
|
|
|
self.assertEqual(res, "ok")
|
|
|
|
|
2013-10-17 17:40:50 -03:00
|
|
|
def test_wait(self):
|
|
|
|
|
|
|
|
def gen():
|
|
|
|
when = yield
|
|
|
|
self.assertAlmostEqual(0.1, when)
|
|
|
|
when = yield 0
|
|
|
|
self.assertAlmostEqual(0.15, when)
|
|
|
|
yield 0.15
|
|
|
|
|
2014-06-17 20:36:32 -03:00
|
|
|
loop = self.new_test_loop(gen)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
a = asyncio.Task(asyncio.sleep(0.1, loop=loop), loop=loop)
|
|
|
|
b = asyncio.Task(asyncio.sleep(0.15, loop=loop), loop=loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def foo():
|
2014-01-25 10:32:06 -04:00
|
|
|
done, pending = yield from asyncio.wait([b, a], loop=loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertEqual(done, set([a, b]))
|
|
|
|
self.assertEqual(pending, set())
|
|
|
|
return 42
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
res = loop.run_until_complete(asyncio.Task(foo(), loop=loop))
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertEqual(res, 42)
|
|
|
|
self.assertAlmostEqual(0.15, loop.time())
|
|
|
|
|
|
|
|
# Doing it again should take no time and exercise a different path.
|
2014-01-25 10:32:06 -04:00
|
|
|
res = loop.run_until_complete(asyncio.Task(foo(), loop=loop))
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertAlmostEqual(0.15, loop.time())
|
|
|
|
self.assertEqual(res, 42)
|
|
|
|
|
|
|
|
def test_wait_with_global_loop(self):
|
|
|
|
|
|
|
|
def gen():
|
|
|
|
when = yield
|
|
|
|
self.assertAlmostEqual(0.01, when)
|
|
|
|
when = yield 0
|
|
|
|
self.assertAlmostEqual(0.015, when)
|
|
|
|
yield 0.015
|
|
|
|
|
2014-06-17 20:36:32 -03:00
|
|
|
loop = self.new_test_loop(gen)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
a = asyncio.Task(asyncio.sleep(0.01, loop=loop), loop=loop)
|
|
|
|
b = asyncio.Task(asyncio.sleep(0.015, loop=loop), loop=loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def foo():
|
2014-01-25 10:32:06 -04:00
|
|
|
done, pending = yield from asyncio.wait([b, a])
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertEqual(done, set([a, b]))
|
|
|
|
self.assertEqual(pending, set())
|
|
|
|
return 42
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
asyncio.set_event_loop(loop)
|
2014-06-17 20:36:32 -03:00
|
|
|
res = loop.run_until_complete(
|
|
|
|
asyncio.Task(foo(), loop=loop))
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
self.assertEqual(res, 42)
|
|
|
|
|
2014-02-06 23:06:16 -04:00
|
|
|
def test_wait_duplicate_coroutines(self):
|
|
|
|
@asyncio.coroutine
|
|
|
|
def coro(s):
|
|
|
|
return s
|
|
|
|
c = coro('test')
|
|
|
|
|
|
|
|
task = asyncio.Task(
|
|
|
|
asyncio.wait([c, c, coro('spam')], loop=self.loop),
|
|
|
|
loop=self.loop)
|
|
|
|
|
|
|
|
done, pending = self.loop.run_until_complete(task)
|
|
|
|
|
|
|
|
self.assertFalse(pending)
|
|
|
|
self.assertEqual(set(f.result() for f in done), {'test', 'spam'})
|
|
|
|
|
2013-10-17 17:40:50 -03:00
|
|
|
def test_wait_errors(self):
|
|
|
|
self.assertRaises(
|
|
|
|
ValueError, self.loop.run_until_complete,
|
2014-01-25 10:32:06 -04:00
|
|
|
asyncio.wait(set(), loop=self.loop))
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-07-16 13:50:39 -03:00
|
|
|
# -1 is an invalid return_when value
|
|
|
|
sleep_coro = asyncio.sleep(10.0, loop=self.loop)
|
|
|
|
wait_coro = asyncio.wait([sleep_coro], return_when=-1, loop=self.loop)
|
|
|
|
self.assertRaises(ValueError,
|
|
|
|
self.loop.run_until_complete, wait_coro)
|
|
|
|
|
|
|
|
sleep_coro.close()
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def test_wait_first_completed(self):
|
|
|
|
|
|
|
|
def gen():
|
|
|
|
when = yield
|
|
|
|
self.assertAlmostEqual(10.0, when)
|
|
|
|
when = yield 0
|
|
|
|
self.assertAlmostEqual(0.1, when)
|
|
|
|
yield 0.1
|
|
|
|
|
2014-06-17 20:36:32 -03:00
|
|
|
loop = self.new_test_loop(gen)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
a = asyncio.Task(asyncio.sleep(10.0, loop=loop), loop=loop)
|
|
|
|
b = asyncio.Task(asyncio.sleep(0.1, loop=loop), loop=loop)
|
|
|
|
task = asyncio.Task(
|
|
|
|
asyncio.wait([b, a], return_when=asyncio.FIRST_COMPLETED,
|
2014-01-30 20:05:28 -04:00
|
|
|
loop=loop),
|
2013-10-17 17:40:50 -03:00
|
|
|
loop=loop)
|
|
|
|
|
|
|
|
done, pending = loop.run_until_complete(task)
|
|
|
|
self.assertEqual({b}, done)
|
|
|
|
self.assertEqual({a}, pending)
|
|
|
|
self.assertFalse(a.done())
|
|
|
|
self.assertTrue(b.done())
|
|
|
|
self.assertIsNone(b.result())
|
|
|
|
self.assertAlmostEqual(0.1, loop.time())
|
|
|
|
|
|
|
|
# move forward to close generator
|
|
|
|
loop.advance_time(10)
|
2014-01-25 10:32:06 -04:00
|
|
|
loop.run_until_complete(asyncio.wait([a, b], loop=loop))
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def test_wait_really_done(self):
|
|
|
|
# there is possibility that some tasks in the pending list
|
|
|
|
# became done but their callbacks haven't all been called yet
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def coro1():
|
|
|
|
yield
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def coro2():
|
|
|
|
yield
|
|
|
|
yield
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
a = asyncio.Task(coro1(), loop=self.loop)
|
|
|
|
b = asyncio.Task(coro2(), loop=self.loop)
|
|
|
|
task = asyncio.Task(
|
|
|
|
asyncio.wait([b, a], return_when=asyncio.FIRST_COMPLETED,
|
2014-01-30 20:05:28 -04:00
|
|
|
loop=self.loop),
|
2013-10-17 17:40:50 -03:00
|
|
|
loop=self.loop)
|
|
|
|
|
|
|
|
done, pending = self.loop.run_until_complete(task)
|
|
|
|
self.assertEqual({a, b}, done)
|
|
|
|
self.assertTrue(a.done())
|
|
|
|
self.assertIsNone(a.result())
|
|
|
|
self.assertTrue(b.done())
|
|
|
|
self.assertIsNone(b.result())
|
|
|
|
|
|
|
|
def test_wait_first_exception(self):
|
|
|
|
|
|
|
|
def gen():
|
|
|
|
when = yield
|
|
|
|
self.assertAlmostEqual(10.0, when)
|
|
|
|
yield 0
|
|
|
|
|
2014-06-17 20:36:32 -03:00
|
|
|
loop = self.new_test_loop(gen)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
# first_exception, task already has exception
|
2014-01-25 10:32:06 -04:00
|
|
|
a = asyncio.Task(asyncio.sleep(10.0, loop=loop), loop=loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def exc():
|
|
|
|
raise ZeroDivisionError('err')
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
b = asyncio.Task(exc(), loop=loop)
|
|
|
|
task = asyncio.Task(
|
|
|
|
asyncio.wait([b, a], return_when=asyncio.FIRST_EXCEPTION,
|
2014-01-30 20:05:28 -04:00
|
|
|
loop=loop),
|
2013-10-17 17:40:50 -03:00
|
|
|
loop=loop)
|
|
|
|
|
|
|
|
done, pending = loop.run_until_complete(task)
|
|
|
|
self.assertEqual({b}, done)
|
|
|
|
self.assertEqual({a}, pending)
|
|
|
|
self.assertAlmostEqual(0, loop.time())
|
|
|
|
|
|
|
|
# move forward to close generator
|
|
|
|
loop.advance_time(10)
|
2014-01-25 10:32:06 -04:00
|
|
|
loop.run_until_complete(asyncio.wait([a, b], loop=loop))
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def test_wait_first_exception_in_wait(self):
|
|
|
|
|
|
|
|
def gen():
|
|
|
|
when = yield
|
|
|
|
self.assertAlmostEqual(10.0, when)
|
|
|
|
when = yield 0
|
|
|
|
self.assertAlmostEqual(0.01, when)
|
|
|
|
yield 0.01
|
|
|
|
|
2014-06-17 20:36:32 -03:00
|
|
|
loop = self.new_test_loop(gen)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
# first_exception, exception during waiting
|
2014-01-25 10:32:06 -04:00
|
|
|
a = asyncio.Task(asyncio.sleep(10.0, loop=loop), loop=loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def exc():
|
2014-01-25 10:32:06 -04:00
|
|
|
yield from asyncio.sleep(0.01, loop=loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
raise ZeroDivisionError('err')
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
b = asyncio.Task(exc(), loop=loop)
|
|
|
|
task = asyncio.wait([b, a], return_when=asyncio.FIRST_EXCEPTION,
|
2014-01-30 20:05:28 -04:00
|
|
|
loop=loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
done, pending = loop.run_until_complete(task)
|
|
|
|
self.assertEqual({b}, done)
|
|
|
|
self.assertEqual({a}, pending)
|
|
|
|
self.assertAlmostEqual(0.01, loop.time())
|
|
|
|
|
|
|
|
# move forward to close generator
|
|
|
|
loop.advance_time(10)
|
2014-01-25 10:32:06 -04:00
|
|
|
loop.run_until_complete(asyncio.wait([a, b], loop=loop))
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def test_wait_with_exception(self):
|
|
|
|
|
|
|
|
def gen():
|
|
|
|
when = yield
|
|
|
|
self.assertAlmostEqual(0.1, when)
|
|
|
|
when = yield 0
|
|
|
|
self.assertAlmostEqual(0.15, when)
|
|
|
|
yield 0.15
|
|
|
|
|
2014-06-17 20:36:32 -03:00
|
|
|
loop = self.new_test_loop(gen)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
a = asyncio.Task(asyncio.sleep(0.1, loop=loop), loop=loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def sleeper():
|
2014-01-25 10:32:06 -04:00
|
|
|
yield from asyncio.sleep(0.15, loop=loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
raise ZeroDivisionError('really')
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
b = asyncio.Task(sleeper(), loop=loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def foo():
|
2014-01-25 10:32:06 -04:00
|
|
|
done, pending = yield from asyncio.wait([b, a], loop=loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertEqual(len(done), 2)
|
|
|
|
self.assertEqual(pending, set())
|
|
|
|
errors = set(f for f in done if f.exception() is not None)
|
|
|
|
self.assertEqual(len(errors), 1)
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
loop.run_until_complete(asyncio.Task(foo(), loop=loop))
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertAlmostEqual(0.15, loop.time())
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
loop.run_until_complete(asyncio.Task(foo(), loop=loop))
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertAlmostEqual(0.15, loop.time())
|
|
|
|
|
|
|
|
def test_wait_with_timeout(self):
|
|
|
|
|
|
|
|
def gen():
|
|
|
|
when = yield
|
|
|
|
self.assertAlmostEqual(0.1, when)
|
|
|
|
when = yield 0
|
|
|
|
self.assertAlmostEqual(0.15, when)
|
|
|
|
when = yield 0
|
|
|
|
self.assertAlmostEqual(0.11, when)
|
|
|
|
yield 0.11
|
|
|
|
|
2014-06-17 20:36:32 -03:00
|
|
|
loop = self.new_test_loop(gen)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
a = asyncio.Task(asyncio.sleep(0.1, loop=loop), loop=loop)
|
|
|
|
b = asyncio.Task(asyncio.sleep(0.15, loop=loop), loop=loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def foo():
|
2014-01-25 10:32:06 -04:00
|
|
|
done, pending = yield from asyncio.wait([b, a], timeout=0.11,
|
2014-01-30 20:05:28 -04:00
|
|
|
loop=loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertEqual(done, set([a]))
|
|
|
|
self.assertEqual(pending, set([b]))
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
loop.run_until_complete(asyncio.Task(foo(), loop=loop))
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertAlmostEqual(0.11, loop.time())
|
|
|
|
|
|
|
|
# move forward to close generator
|
|
|
|
loop.advance_time(10)
|
2014-01-25 10:32:06 -04:00
|
|
|
loop.run_until_complete(asyncio.wait([a, b], loop=loop))
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def test_wait_concurrent_complete(self):
|
|
|
|
|
|
|
|
def gen():
|
|
|
|
when = yield
|
|
|
|
self.assertAlmostEqual(0.1, when)
|
|
|
|
when = yield 0
|
|
|
|
self.assertAlmostEqual(0.15, when)
|
|
|
|
when = yield 0
|
|
|
|
self.assertAlmostEqual(0.1, when)
|
|
|
|
yield 0.1
|
|
|
|
|
2014-06-17 20:36:32 -03:00
|
|
|
loop = self.new_test_loop(gen)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
a = asyncio.Task(asyncio.sleep(0.1, loop=loop), loop=loop)
|
|
|
|
b = asyncio.Task(asyncio.sleep(0.15, loop=loop), loop=loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
done, pending = loop.run_until_complete(
|
2014-01-25 10:32:06 -04:00
|
|
|
asyncio.wait([b, a], timeout=0.1, loop=loop))
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
self.assertEqual(done, set([a]))
|
|
|
|
self.assertEqual(pending, set([b]))
|
|
|
|
self.assertAlmostEqual(0.1, loop.time())
|
|
|
|
|
|
|
|
# move forward to close generator
|
|
|
|
loop.advance_time(10)
|
2014-01-25 10:32:06 -04:00
|
|
|
loop.run_until_complete(asyncio.wait([a, b], loop=loop))
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def test_as_completed(self):
|
|
|
|
|
|
|
|
def gen():
|
|
|
|
yield 0
|
|
|
|
yield 0
|
|
|
|
yield 0.01
|
|
|
|
yield 0
|
|
|
|
|
2014-06-17 20:36:32 -03:00
|
|
|
loop = self.new_test_loop(gen)
|
2014-07-16 13:54:13 -03:00
|
|
|
# disable "slow callback" warning
|
|
|
|
loop.slow_callback_duration = 1.0
|
2013-10-17 17:40:50 -03:00
|
|
|
completed = set()
|
|
|
|
time_shifted = False
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def sleeper(dt, x):
|
|
|
|
nonlocal time_shifted
|
2014-01-25 10:32:06 -04:00
|
|
|
yield from asyncio.sleep(dt, loop=loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
completed.add(x)
|
|
|
|
if not time_shifted and 'a' in completed and 'b' in completed:
|
|
|
|
time_shifted = True
|
|
|
|
loop.advance_time(0.14)
|
|
|
|
return x
|
|
|
|
|
|
|
|
a = sleeper(0.01, 'a')
|
|
|
|
b = sleeper(0.01, 'b')
|
|
|
|
c = sleeper(0.15, 'c')
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def foo():
|
|
|
|
values = []
|
2014-01-25 10:32:06 -04:00
|
|
|
for f in asyncio.as_completed([b, c, a], loop=loop):
|
2013-10-17 17:40:50 -03:00
|
|
|
values.append((yield from f))
|
|
|
|
return values
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
res = loop.run_until_complete(asyncio.Task(foo(), loop=loop))
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertAlmostEqual(0.15, loop.time())
|
|
|
|
self.assertTrue('a' in res[:2])
|
|
|
|
self.assertTrue('b' in res[:2])
|
|
|
|
self.assertEqual(res[2], 'c')
|
|
|
|
|
|
|
|
# Doing it again should take no time and exercise a different path.
|
2014-01-25 10:32:06 -04:00
|
|
|
res = loop.run_until_complete(asyncio.Task(foo(), loop=loop))
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertAlmostEqual(0.15, loop.time())
|
|
|
|
|
|
|
|
def test_as_completed_with_timeout(self):
|
|
|
|
|
|
|
|
def gen():
|
2014-02-06 23:06:16 -04:00
|
|
|
yield
|
|
|
|
yield 0
|
|
|
|
yield 0
|
|
|
|
yield 0.1
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-06-17 20:36:32 -03:00
|
|
|
loop = self.new_test_loop(gen)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
a = asyncio.sleep(0.1, 'a', loop=loop)
|
|
|
|
b = asyncio.sleep(0.15, 'b', loop=loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def foo():
|
|
|
|
values = []
|
2014-01-25 10:32:06 -04:00
|
|
|
for f in asyncio.as_completed([a, b], timeout=0.12, loop=loop):
|
2014-02-12 21:58:19 -04:00
|
|
|
if values:
|
|
|
|
loop.advance_time(0.02)
|
2013-10-17 17:40:50 -03:00
|
|
|
try:
|
|
|
|
v = yield from f
|
|
|
|
values.append((1, v))
|
2014-01-25 10:32:06 -04:00
|
|
|
except asyncio.TimeoutError as exc:
|
2013-10-17 17:40:50 -03:00
|
|
|
values.append((2, exc))
|
|
|
|
return values
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
res = loop.run_until_complete(asyncio.Task(foo(), loop=loop))
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertEqual(len(res), 2, res)
|
|
|
|
self.assertEqual(res[0], (1, 'a'))
|
|
|
|
self.assertEqual(res[1][0], 2)
|
2014-01-25 10:32:06 -04:00
|
|
|
self.assertIsInstance(res[1][1], asyncio.TimeoutError)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertAlmostEqual(0.12, loop.time())
|
|
|
|
|
|
|
|
# move forward to close generator
|
|
|
|
loop.advance_time(10)
|
2014-01-25 10:32:06 -04:00
|
|
|
loop.run_until_complete(asyncio.wait([a, b], loop=loop))
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-02-12 21:58:19 -04:00
|
|
|
def test_as_completed_with_unused_timeout(self):
|
|
|
|
|
|
|
|
def gen():
|
|
|
|
yield
|
|
|
|
yield 0
|
|
|
|
yield 0.01
|
|
|
|
|
2014-06-17 20:36:32 -03:00
|
|
|
loop = self.new_test_loop(gen)
|
2014-02-12 21:58:19 -04:00
|
|
|
|
|
|
|
a = asyncio.sleep(0.01, 'a', loop=loop)
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def foo():
|
|
|
|
for f in asyncio.as_completed([a], timeout=1, loop=loop):
|
|
|
|
v = yield from f
|
|
|
|
self.assertEqual(v, 'a')
|
|
|
|
|
2014-02-26 06:07:42 -04:00
|
|
|
loop.run_until_complete(asyncio.Task(foo(), loop=loop))
|
2014-02-12 21:58:19 -04:00
|
|
|
|
2013-10-17 17:40:50 -03:00
|
|
|
def test_as_completed_reverse_wait(self):
|
|
|
|
|
|
|
|
def gen():
|
|
|
|
yield 0
|
|
|
|
yield 0.05
|
|
|
|
yield 0
|
|
|
|
|
2014-06-17 20:36:32 -03:00
|
|
|
loop = self.new_test_loop(gen)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
a = asyncio.sleep(0.05, 'a', loop=loop)
|
|
|
|
b = asyncio.sleep(0.10, 'b', loop=loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
fs = {a, b}
|
2014-01-25 10:32:06 -04:00
|
|
|
futs = list(asyncio.as_completed(fs, loop=loop))
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertEqual(len(futs), 2)
|
|
|
|
|
|
|
|
x = loop.run_until_complete(futs[1])
|
|
|
|
self.assertEqual(x, 'a')
|
|
|
|
self.assertAlmostEqual(0.05, loop.time())
|
|
|
|
loop.advance_time(0.05)
|
|
|
|
y = loop.run_until_complete(futs[0])
|
|
|
|
self.assertEqual(y, 'b')
|
|
|
|
self.assertAlmostEqual(0.10, loop.time())
|
|
|
|
|
|
|
|
def test_as_completed_concurrent(self):
|
|
|
|
|
|
|
|
def gen():
|
|
|
|
when = yield
|
|
|
|
self.assertAlmostEqual(0.05, when)
|
|
|
|
when = yield 0
|
|
|
|
self.assertAlmostEqual(0.05, when)
|
|
|
|
yield 0.05
|
|
|
|
|
2014-06-17 20:36:32 -03:00
|
|
|
loop = self.new_test_loop(gen)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
a = asyncio.sleep(0.05, 'a', loop=loop)
|
|
|
|
b = asyncio.sleep(0.05, 'b', loop=loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
fs = {a, b}
|
2014-01-25 10:32:06 -04:00
|
|
|
futs = list(asyncio.as_completed(fs, loop=loop))
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertEqual(len(futs), 2)
|
2014-01-25 10:32:06 -04:00
|
|
|
waiter = asyncio.wait(futs, loop=loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
done, pending = loop.run_until_complete(waiter)
|
|
|
|
self.assertEqual(set(f.result() for f in done), {'a', 'b'})
|
|
|
|
|
2014-02-06 23:06:16 -04:00
|
|
|
def test_as_completed_duplicate_coroutines(self):
|
2014-02-18 23:56:15 -04:00
|
|
|
|
2014-02-06 23:06:16 -04:00
|
|
|
@asyncio.coroutine
|
|
|
|
def coro(s):
|
|
|
|
return s
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def runner():
|
|
|
|
result = []
|
|
|
|
c = coro('ham')
|
2014-02-18 23:56:15 -04:00
|
|
|
for f in asyncio.as_completed([c, c, coro('spam')],
|
|
|
|
loop=self.loop):
|
2014-02-06 23:06:16 -04:00
|
|
|
result.append((yield from f))
|
|
|
|
return result
|
|
|
|
|
|
|
|
fut = asyncio.Task(runner(), loop=self.loop)
|
|
|
|
self.loop.run_until_complete(fut)
|
|
|
|
result = fut.result()
|
|
|
|
self.assertEqual(set(result), {'ham', 'spam'})
|
|
|
|
self.assertEqual(len(result), 2)
|
|
|
|
|
2013-10-17 17:40:50 -03:00
|
|
|
def test_sleep(self):
|
|
|
|
|
|
|
|
def gen():
|
|
|
|
when = yield
|
|
|
|
self.assertAlmostEqual(0.05, when)
|
|
|
|
when = yield 0.05
|
|
|
|
self.assertAlmostEqual(0.1, when)
|
|
|
|
yield 0.05
|
|
|
|
|
2014-06-17 20:36:32 -03:00
|
|
|
loop = self.new_test_loop(gen)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def sleeper(dt, arg):
|
2014-01-25 10:32:06 -04:00
|
|
|
yield from asyncio.sleep(dt/2, loop=loop)
|
|
|
|
res = yield from asyncio.sleep(dt/2, arg, loop=loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
return res
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
t = asyncio.Task(sleeper(0.1, 'yeah'), loop=loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
loop.run_until_complete(t)
|
|
|
|
self.assertTrue(t.done())
|
|
|
|
self.assertEqual(t.result(), 'yeah')
|
|
|
|
self.assertAlmostEqual(0.1, loop.time())
|
|
|
|
|
|
|
|
def test_sleep_cancel(self):
|
|
|
|
|
|
|
|
def gen():
|
|
|
|
when = yield
|
|
|
|
self.assertAlmostEqual(10.0, when)
|
|
|
|
yield 0
|
|
|
|
|
2014-06-17 20:36:32 -03:00
|
|
|
loop = self.new_test_loop(gen)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
t = asyncio.Task(asyncio.sleep(10.0, 'yeah', loop=loop),
|
2014-01-30 20:05:28 -04:00
|
|
|
loop=loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
handle = None
|
|
|
|
orig_call_later = loop.call_later
|
|
|
|
|
2014-07-01 07:38:51 -03:00
|
|
|
def call_later(delay, callback, *args):
|
2013-10-17 17:40:50 -03:00
|
|
|
nonlocal handle
|
2014-07-01 07:38:51 -03:00
|
|
|
handle = orig_call_later(delay, callback, *args)
|
2013-10-17 17:40:50 -03:00
|
|
|
return handle
|
|
|
|
|
|
|
|
loop.call_later = call_later
|
|
|
|
test_utils.run_briefly(loop)
|
|
|
|
|
|
|
|
self.assertFalse(handle._cancelled)
|
|
|
|
|
|
|
|
t.cancel()
|
|
|
|
test_utils.run_briefly(loop)
|
|
|
|
self.assertTrue(handle._cancelled)
|
|
|
|
|
|
|
|
def test_task_cancel_sleeping_task(self):
|
|
|
|
|
|
|
|
def gen():
|
|
|
|
when = yield
|
|
|
|
self.assertAlmostEqual(0.1, when)
|
|
|
|
when = yield 0
|
|
|
|
self.assertAlmostEqual(5000, when)
|
|
|
|
yield 0.1
|
|
|
|
|
2014-06-17 20:36:32 -03:00
|
|
|
loop = self.new_test_loop(gen)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def sleep(dt):
|
2014-02-26 06:07:42 -04:00
|
|
|
yield from asyncio.sleep(dt, loop=loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def doit():
|
2014-01-25 10:32:06 -04:00
|
|
|
sleeper = asyncio.Task(sleep(5000), loop=loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
loop.call_later(0.1, sleeper.cancel)
|
|
|
|
try:
|
|
|
|
yield from sleeper
|
2014-01-25 10:32:06 -04:00
|
|
|
except asyncio.CancelledError:
|
2013-10-17 17:40:50 -03:00
|
|
|
return 'cancelled'
|
|
|
|
else:
|
|
|
|
return 'slept in'
|
|
|
|
|
|
|
|
doer = doit()
|
|
|
|
self.assertEqual(loop.run_until_complete(doer), 'cancelled')
|
|
|
|
self.assertAlmostEqual(0.1, loop.time())
|
|
|
|
|
|
|
|
def test_task_cancel_waiter_future(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
fut = asyncio.Future(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def coro():
|
|
|
|
yield from fut
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
task = asyncio.Task(coro(), loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
self.assertIs(task._fut_waiter, fut)
|
|
|
|
|
|
|
|
task.cancel()
|
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
self.assertRaises(
|
2014-01-25 10:32:06 -04:00
|
|
|
asyncio.CancelledError, self.loop.run_until_complete, task)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertIsNone(task._fut_waiter)
|
|
|
|
self.assertTrue(fut.cancelled())
|
|
|
|
|
|
|
|
def test_step_in_completed_task(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def notmuch():
|
|
|
|
return 'ko'
|
|
|
|
|
|
|
|
gen = notmuch()
|
2014-01-25 10:32:06 -04:00
|
|
|
task = asyncio.Task(gen, loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
task.set_result('ok')
|
|
|
|
|
|
|
|
self.assertRaises(AssertionError, task._step)
|
|
|
|
gen.close()
|
|
|
|
|
|
|
|
def test_step_result(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def notmuch():
|
|
|
|
yield None
|
|
|
|
yield 1
|
|
|
|
return 'ko'
|
|
|
|
|
|
|
|
self.assertRaises(
|
|
|
|
RuntimeError, self.loop.run_until_complete, notmuch())
|
|
|
|
|
|
|
|
def test_step_result_future(self):
|
|
|
|
# If coroutine returns future, task waits on this future.
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
class Fut(asyncio.Future):
|
2013-10-17 17:40:50 -03:00
|
|
|
def __init__(self, *args, **kwds):
|
|
|
|
self.cb_added = False
|
|
|
|
super().__init__(*args, **kwds)
|
|
|
|
|
|
|
|
def add_done_callback(self, fn):
|
|
|
|
self.cb_added = True
|
|
|
|
super().add_done_callback(fn)
|
|
|
|
|
|
|
|
fut = Fut(loop=self.loop)
|
|
|
|
result = None
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def wait_for_future():
|
|
|
|
nonlocal result
|
|
|
|
result = yield from fut
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
t = asyncio.Task(wait_for_future(), loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
self.assertTrue(fut.cb_added)
|
|
|
|
|
|
|
|
res = object()
|
|
|
|
fut.set_result(res)
|
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
self.assertIs(res, result)
|
|
|
|
self.assertTrue(t.done())
|
|
|
|
self.assertIsNone(t.result())
|
|
|
|
|
|
|
|
def test_step_with_baseexception(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def notmutch():
|
|
|
|
raise BaseException()
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
task = asyncio.Task(notmutch(), loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertRaises(BaseException, task._step)
|
|
|
|
|
|
|
|
self.assertTrue(task.done())
|
|
|
|
self.assertIsInstance(task.exception(), BaseException)
|
|
|
|
|
|
|
|
def test_baseexception_during_cancel(self):
|
|
|
|
|
|
|
|
def gen():
|
|
|
|
when = yield
|
|
|
|
self.assertAlmostEqual(10.0, when)
|
|
|
|
yield 0
|
|
|
|
|
2014-06-17 20:36:32 -03:00
|
|
|
loop = self.new_test_loop(gen)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def sleeper():
|
2014-01-25 10:32:06 -04:00
|
|
|
yield from asyncio.sleep(10, loop=loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
base_exc = BaseException()
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def notmutch():
|
|
|
|
try:
|
|
|
|
yield from sleeper()
|
2014-01-25 10:32:06 -04:00
|
|
|
except asyncio.CancelledError:
|
2013-10-17 17:40:50 -03:00
|
|
|
raise base_exc
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
task = asyncio.Task(notmutch(), loop=loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
test_utils.run_briefly(loop)
|
|
|
|
|
|
|
|
task.cancel()
|
|
|
|
self.assertFalse(task.done())
|
|
|
|
|
|
|
|
self.assertRaises(BaseException, test_utils.run_briefly, loop)
|
|
|
|
|
|
|
|
self.assertTrue(task.done())
|
|
|
|
self.assertFalse(task.cancelled())
|
|
|
|
self.assertIs(task.exception(), base_exc)
|
|
|
|
|
|
|
|
def test_iscoroutinefunction(self):
|
|
|
|
def fn():
|
|
|
|
pass
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
self.assertFalse(asyncio.iscoroutinefunction(fn))
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def fn1():
|
|
|
|
yield
|
2014-01-25 10:32:06 -04:00
|
|
|
self.assertFalse(asyncio.iscoroutinefunction(fn1))
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def fn2():
|
|
|
|
yield
|
2014-01-25 10:32:06 -04:00
|
|
|
self.assertTrue(asyncio.iscoroutinefunction(fn2))
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def test_yield_vs_yield_from(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
fut = asyncio.Future(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def wait_for_future():
|
|
|
|
yield fut
|
|
|
|
|
|
|
|
task = wait_for_future()
|
|
|
|
with self.assertRaises(RuntimeError):
|
|
|
|
self.loop.run_until_complete(task)
|
|
|
|
|
|
|
|
self.assertFalse(fut.done())
|
|
|
|
|
|
|
|
def test_yield_vs_yield_from_generator(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def coro():
|
|
|
|
yield
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def wait_for_future():
|
|
|
|
gen = coro()
|
|
|
|
try:
|
|
|
|
yield gen
|
|
|
|
finally:
|
|
|
|
gen.close()
|
|
|
|
|
|
|
|
task = wait_for_future()
|
|
|
|
self.assertRaises(
|
|
|
|
RuntimeError,
|
|
|
|
self.loop.run_until_complete, task)
|
|
|
|
|
|
|
|
def test_coroutine_non_gen_function(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def func():
|
|
|
|
return 'test'
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
self.assertTrue(asyncio.iscoroutinefunction(func))
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
coro = func()
|
2014-01-25 10:32:06 -04:00
|
|
|
self.assertTrue(asyncio.iscoroutine(coro))
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
res = self.loop.run_until_complete(coro)
|
|
|
|
self.assertEqual(res, 'test')
|
|
|
|
|
|
|
|
def test_coroutine_non_gen_function_return_future(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
fut = asyncio.Future(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def func():
|
|
|
|
return fut
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def coro():
|
|
|
|
fut.set_result('test')
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
t1 = asyncio.Task(func(), loop=self.loop)
|
|
|
|
t2 = asyncio.Task(coro(), loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
res = self.loop.run_until_complete(t1)
|
|
|
|
self.assertEqual(res, 'test')
|
|
|
|
self.assertIsNone(t2.result())
|
|
|
|
|
2013-12-06 16:57:40 -04:00
|
|
|
def test_current_task(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
self.assertIsNone(asyncio.Task.current_task(loop=self.loop))
|
2013-12-19 17:49:32 -04:00
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-12-06 16:57:40 -04:00
|
|
|
def coro(loop):
|
2014-01-25 10:32:06 -04:00
|
|
|
self.assertTrue(asyncio.Task.current_task(loop=loop) is task)
|
2013-12-06 16:57:40 -04:00
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
task = asyncio.Task(coro(self.loop), loop=self.loop)
|
2013-12-06 16:57:40 -04:00
|
|
|
self.loop.run_until_complete(task)
|
2014-01-25 10:32:06 -04:00
|
|
|
self.assertIsNone(asyncio.Task.current_task(loop=self.loop))
|
2013-12-06 16:57:40 -04:00
|
|
|
|
|
|
|
def test_current_task_with_interleaving_tasks(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
self.assertIsNone(asyncio.Task.current_task(loop=self.loop))
|
2013-12-06 16:57:40 -04:00
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
fut1 = asyncio.Future(loop=self.loop)
|
|
|
|
fut2 = asyncio.Future(loop=self.loop)
|
2013-12-06 16:57:40 -04:00
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-12-06 16:57:40 -04:00
|
|
|
def coro1(loop):
|
2014-01-25 10:32:06 -04:00
|
|
|
self.assertTrue(asyncio.Task.current_task(loop=loop) is task1)
|
2013-12-06 16:57:40 -04:00
|
|
|
yield from fut1
|
2014-01-25 10:32:06 -04:00
|
|
|
self.assertTrue(asyncio.Task.current_task(loop=loop) is task1)
|
2013-12-06 16:57:40 -04:00
|
|
|
fut2.set_result(True)
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-12-06 16:57:40 -04:00
|
|
|
def coro2(loop):
|
2014-01-25 10:32:06 -04:00
|
|
|
self.assertTrue(asyncio.Task.current_task(loop=loop) is task2)
|
2013-12-06 16:57:40 -04:00
|
|
|
fut1.set_result(True)
|
|
|
|
yield from fut2
|
2014-01-25 10:32:06 -04:00
|
|
|
self.assertTrue(asyncio.Task.current_task(loop=loop) is task2)
|
2013-12-06 16:57:40 -04:00
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
task1 = asyncio.Task(coro1(self.loop), loop=self.loop)
|
|
|
|
task2 = asyncio.Task(coro2(self.loop), loop=self.loop)
|
2013-12-06 16:57:40 -04:00
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
self.loop.run_until_complete(asyncio.wait((task1, task2),
|
2014-01-30 20:05:28 -04:00
|
|
|
loop=self.loop))
|
2014-01-25 10:32:06 -04:00
|
|
|
self.assertIsNone(asyncio.Task.current_task(loop=self.loop))
|
2013-12-06 16:57:40 -04:00
|
|
|
|
2013-10-17 17:40:50 -03:00
|
|
|
# Some thorough tests for cancellation propagation through
|
|
|
|
# coroutines, tasks and wait().
|
|
|
|
|
|
|
|
def test_yield_future_passes_cancel(self):
|
|
|
|
# Cancelling outer() cancels inner() cancels waiter.
|
|
|
|
proof = 0
|
2014-01-25 10:32:06 -04:00
|
|
|
waiter = asyncio.Future(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def inner():
|
|
|
|
nonlocal proof
|
|
|
|
try:
|
|
|
|
yield from waiter
|
2014-01-25 10:32:06 -04:00
|
|
|
except asyncio.CancelledError:
|
2013-10-17 17:40:50 -03:00
|
|
|
proof += 1
|
|
|
|
raise
|
|
|
|
else:
|
|
|
|
self.fail('got past sleep() in inner()')
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def outer():
|
|
|
|
nonlocal proof
|
|
|
|
try:
|
|
|
|
yield from inner()
|
2014-01-25 10:32:06 -04:00
|
|
|
except asyncio.CancelledError:
|
2013-10-17 17:40:50 -03:00
|
|
|
proof += 100 # Expect this path.
|
|
|
|
else:
|
|
|
|
proof += 10
|
|
|
|
|
2015-05-11 15:48:38 -03:00
|
|
|
f = asyncio.ensure_future(outer(), loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
f.cancel()
|
|
|
|
self.loop.run_until_complete(f)
|
|
|
|
self.assertEqual(proof, 101)
|
|
|
|
self.assertTrue(waiter.cancelled())
|
|
|
|
|
|
|
|
def test_yield_wait_does_not_shield_cancel(self):
|
|
|
|
# Cancelling outer() makes wait() return early, leaves inner()
|
|
|
|
# running.
|
|
|
|
proof = 0
|
2014-01-25 10:32:06 -04:00
|
|
|
waiter = asyncio.Future(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def inner():
|
|
|
|
nonlocal proof
|
|
|
|
yield from waiter
|
|
|
|
proof += 1
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def outer():
|
|
|
|
nonlocal proof
|
2014-01-25 10:32:06 -04:00
|
|
|
d, p = yield from asyncio.wait([inner()], loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
proof += 100
|
|
|
|
|
2015-05-11 15:48:38 -03:00
|
|
|
f = asyncio.ensure_future(outer(), loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
f.cancel()
|
|
|
|
self.assertRaises(
|
2014-01-25 10:32:06 -04:00
|
|
|
asyncio.CancelledError, self.loop.run_until_complete, f)
|
2013-10-17 17:40:50 -03:00
|
|
|
waiter.set_result(None)
|
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
self.assertEqual(proof, 1)
|
|
|
|
|
|
|
|
def test_shield_result(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
inner = asyncio.Future(loop=self.loop)
|
|
|
|
outer = asyncio.shield(inner)
|
2013-10-17 17:40:50 -03:00
|
|
|
inner.set_result(42)
|
|
|
|
res = self.loop.run_until_complete(outer)
|
|
|
|
self.assertEqual(res, 42)
|
|
|
|
|
|
|
|
def test_shield_exception(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
inner = asyncio.Future(loop=self.loop)
|
|
|
|
outer = asyncio.shield(inner)
|
2013-10-17 17:40:50 -03:00
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
exc = RuntimeError('expected')
|
|
|
|
inner.set_exception(exc)
|
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
self.assertIs(outer.exception(), exc)
|
|
|
|
|
|
|
|
def test_shield_cancel(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
inner = asyncio.Future(loop=self.loop)
|
|
|
|
outer = asyncio.shield(inner)
|
2013-10-17 17:40:50 -03:00
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
inner.cancel()
|
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
self.assertTrue(outer.cancelled())
|
|
|
|
|
|
|
|
def test_shield_shortcut(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
fut = asyncio.Future(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
fut.set_result(42)
|
2014-01-25 10:32:06 -04:00
|
|
|
res = self.loop.run_until_complete(asyncio.shield(fut))
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertEqual(res, 42)
|
|
|
|
|
|
|
|
def test_shield_effect(self):
|
|
|
|
# Cancelling outer() does not affect inner().
|
|
|
|
proof = 0
|
2014-01-25 10:32:06 -04:00
|
|
|
waiter = asyncio.Future(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def inner():
|
|
|
|
nonlocal proof
|
|
|
|
yield from waiter
|
|
|
|
proof += 1
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def outer():
|
|
|
|
nonlocal proof
|
2014-01-25 10:32:06 -04:00
|
|
|
yield from asyncio.shield(inner(), loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
proof += 100
|
|
|
|
|
2015-05-11 15:48:38 -03:00
|
|
|
f = asyncio.ensure_future(outer(), loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
f.cancel()
|
2014-01-25 10:32:06 -04:00
|
|
|
with self.assertRaises(asyncio.CancelledError):
|
2013-10-17 17:40:50 -03:00
|
|
|
self.loop.run_until_complete(f)
|
|
|
|
waiter.set_result(None)
|
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
self.assertEqual(proof, 1)
|
|
|
|
|
|
|
|
def test_shield_gather(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
child1 = asyncio.Future(loop=self.loop)
|
|
|
|
child2 = asyncio.Future(loop=self.loop)
|
|
|
|
parent = asyncio.gather(child1, child2, loop=self.loop)
|
|
|
|
outer = asyncio.shield(parent, loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
outer.cancel()
|
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
self.assertTrue(outer.cancelled())
|
|
|
|
child1.set_result(1)
|
|
|
|
child2.set_result(2)
|
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
self.assertEqual(parent.result(), [1, 2])
|
|
|
|
|
|
|
|
def test_gather_shield(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
child1 = asyncio.Future(loop=self.loop)
|
|
|
|
child2 = asyncio.Future(loop=self.loop)
|
|
|
|
inner1 = asyncio.shield(child1, loop=self.loop)
|
|
|
|
inner2 = asyncio.shield(child2, loop=self.loop)
|
|
|
|
parent = asyncio.gather(inner1, inner2, loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
parent.cancel()
|
|
|
|
# This should cancel inner1 and inner2 but bot child1 and child2.
|
|
|
|
test_utils.run_briefly(self.loop)
|
2014-01-25 10:32:06 -04:00
|
|
|
self.assertIsInstance(parent.exception(), asyncio.CancelledError)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertTrue(inner1.cancelled())
|
|
|
|
self.assertTrue(inner2.cancelled())
|
|
|
|
child1.set_result(1)
|
|
|
|
child2.set_result(2)
|
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
|
2014-02-11 06:54:08 -04:00
|
|
|
def test_as_completed_invalid_args(self):
|
|
|
|
fut = asyncio.Future(loop=self.loop)
|
|
|
|
|
|
|
|
# as_completed() expects a list of futures, not a future instance
|
|
|
|
self.assertRaises(TypeError, self.loop.run_until_complete,
|
|
|
|
asyncio.as_completed(fut, loop=self.loop))
|
2014-06-27 20:19:11 -03:00
|
|
|
coro = coroutine_function()
|
2014-02-11 06:54:08 -04:00
|
|
|
self.assertRaises(TypeError, self.loop.run_until_complete,
|
2014-06-27 20:19:11 -03:00
|
|
|
asyncio.as_completed(coro, loop=self.loop))
|
|
|
|
coro.close()
|
2014-02-11 06:54:08 -04:00
|
|
|
|
|
|
|
def test_wait_invalid_args(self):
|
|
|
|
fut = asyncio.Future(loop=self.loop)
|
|
|
|
|
|
|
|
# wait() expects a list of futures, not a future instance
|
|
|
|
self.assertRaises(TypeError, self.loop.run_until_complete,
|
|
|
|
asyncio.wait(fut, loop=self.loop))
|
2014-06-27 20:19:11 -03:00
|
|
|
coro = coroutine_function()
|
2014-02-11 06:54:08 -04:00
|
|
|
self.assertRaises(TypeError, self.loop.run_until_complete,
|
2014-06-27 20:19:11 -03:00
|
|
|
asyncio.wait(coro, loop=self.loop))
|
|
|
|
coro.close()
|
2014-02-11 06:54:08 -04:00
|
|
|
|
|
|
|
# wait() expects at least a future
|
|
|
|
self.assertRaises(ValueError, self.loop.run_until_complete,
|
|
|
|
asyncio.wait([], loop=self.loop))
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-04-15 16:06:34 -03:00
|
|
|
def test_corowrapper_mocks_generator(self):
|
|
|
|
|
|
|
|
def check():
|
|
|
|
# A function that asserts various things.
|
|
|
|
# Called twice, with different debug flag values.
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def coro():
|
|
|
|
# The actual coroutine.
|
|
|
|
self.assertTrue(gen.gi_running)
|
|
|
|
yield from fut
|
|
|
|
|
|
|
|
# A completed Future used to run the coroutine.
|
|
|
|
fut = asyncio.Future(loop=self.loop)
|
|
|
|
fut.set_result(None)
|
|
|
|
|
|
|
|
# Call the coroutine.
|
|
|
|
gen = coro()
|
|
|
|
|
|
|
|
# Check some properties.
|
|
|
|
self.assertTrue(asyncio.iscoroutine(gen))
|
|
|
|
self.assertIsInstance(gen.gi_frame, types.FrameType)
|
|
|
|
self.assertFalse(gen.gi_running)
|
|
|
|
self.assertIsInstance(gen.gi_code, types.CodeType)
|
|
|
|
|
|
|
|
# Run it.
|
|
|
|
self.loop.run_until_complete(gen)
|
|
|
|
|
|
|
|
# The frame should have changed.
|
|
|
|
self.assertIsNone(gen.gi_frame)
|
|
|
|
|
2015-05-02 22:38:24 -03:00
|
|
|
# Test with debug flag cleared.
|
|
|
|
with set_coroutine_debug(False):
|
2014-04-15 16:06:34 -03:00
|
|
|
check()
|
|
|
|
|
2015-05-02 22:38:24 -03:00
|
|
|
# Test with debug flag set.
|
|
|
|
with set_coroutine_debug(True):
|
2014-04-15 16:06:34 -03:00
|
|
|
check()
|
|
|
|
|
2014-04-14 23:21:52 -03:00
|
|
|
def test_yield_from_corowrapper(self):
|
2015-05-02 22:38:24 -03:00
|
|
|
with set_coroutine_debug(True):
|
2014-04-14 23:21:52 -03:00
|
|
|
@asyncio.coroutine
|
|
|
|
def t1():
|
|
|
|
return (yield from t2())
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def t2():
|
|
|
|
f = asyncio.Future(loop=self.loop)
|
|
|
|
asyncio.Task(t3(f), loop=self.loop)
|
|
|
|
return (yield from f)
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def t3(f):
|
|
|
|
f.set_result((1, 2, 3))
|
|
|
|
|
|
|
|
task = asyncio.Task(t1(), loop=self.loop)
|
|
|
|
val = self.loop.run_until_complete(task)
|
|
|
|
self.assertEqual(val, (1, 2, 3))
|
|
|
|
|
2014-04-15 13:01:16 -03:00
|
|
|
def test_yield_from_corowrapper_send(self):
|
|
|
|
def foo():
|
|
|
|
a = yield
|
|
|
|
return a
|
|
|
|
|
|
|
|
def call(arg):
|
2015-05-11 23:27:25 -03:00
|
|
|
cw = asyncio.coroutines.CoroWrapper(foo())
|
2014-04-15 13:01:16 -03:00
|
|
|
cw.send(None)
|
|
|
|
try:
|
|
|
|
cw.send(arg)
|
|
|
|
except StopIteration as ex:
|
|
|
|
return ex.args[0]
|
|
|
|
else:
|
|
|
|
raise AssertionError('StopIteration was expected')
|
|
|
|
|
|
|
|
self.assertEqual(call((1, 2)), (1, 2))
|
|
|
|
self.assertEqual(call('spam'), 'spam')
|
|
|
|
|
2014-04-27 14:44:22 -03:00
|
|
|
def test_corowrapper_weakref(self):
|
|
|
|
wd = weakref.WeakValueDictionary()
|
|
|
|
def foo(): yield from []
|
2015-05-11 23:27:25 -03:00
|
|
|
cw = asyncio.coroutines.CoroWrapper(foo())
|
2014-06-27 07:23:41 -03:00
|
|
|
wd['cw'] = cw # Would fail without __weakref__ slot.
|
|
|
|
cw.gen = None # Suppress warning from __del__.
|
2014-06-24 17:37:53 -03:00
|
|
|
|
|
|
|
@unittest.skipUnless(PY34,
|
|
|
|
'need python 3.4 or later')
|
|
|
|
def test_log_destroyed_pending_task(self):
|
|
|
|
@asyncio.coroutine
|
|
|
|
def kill_me(loop):
|
|
|
|
future = asyncio.Future(loop=loop)
|
|
|
|
yield from future
|
|
|
|
# at this point, the only reference to kill_me() task is
|
|
|
|
# the Task._wakeup() method in future._callbacks
|
|
|
|
raise Exception("code never reached")
|
|
|
|
|
|
|
|
mock_handler = mock.Mock()
|
2014-06-27 08:52:20 -03:00
|
|
|
self.loop.set_debug(True)
|
2014-06-24 17:37:53 -03:00
|
|
|
self.loop.set_exception_handler(mock_handler)
|
|
|
|
|
|
|
|
# schedule the task
|
|
|
|
coro = kill_me(self.loop)
|
2015-05-11 15:48:38 -03:00
|
|
|
task = asyncio.ensure_future(coro, loop=self.loop)
|
2014-06-24 17:37:53 -03:00
|
|
|
self.assertEqual(asyncio.Task.all_tasks(loop=self.loop), {task})
|
|
|
|
|
|
|
|
# execute the task so it waits for future
|
|
|
|
self.loop._run_once()
|
|
|
|
self.assertEqual(len(self.loop._ready), 0)
|
|
|
|
|
|
|
|
# remove the future used in kill_me(), and references to the task
|
|
|
|
del coro.gi_frame.f_locals['future']
|
|
|
|
coro = None
|
2014-06-27 08:52:20 -03:00
|
|
|
source_traceback = task._source_traceback
|
2014-06-24 17:37:53 -03:00
|
|
|
task = None
|
|
|
|
|
|
|
|
# no more reference to kill_me() task: the task is destroyed by the GC
|
|
|
|
support.gc_collect()
|
|
|
|
|
|
|
|
self.assertEqual(asyncio.Task.all_tasks(loop=self.loop), set())
|
|
|
|
|
|
|
|
mock_handler.assert_called_with(self.loop, {
|
|
|
|
'message': 'Task was destroyed but it is pending!',
|
|
|
|
'task': mock.ANY,
|
2014-06-27 08:52:20 -03:00
|
|
|
'source_traceback': source_traceback,
|
2014-06-24 17:37:53 -03:00
|
|
|
})
|
|
|
|
mock_handler.reset_mock()
|
2014-04-27 14:44:22 -03:00
|
|
|
|
2014-06-28 19:46:45 -03:00
|
|
|
@mock.patch('asyncio.coroutines.logger')
|
2014-06-27 07:28:41 -03:00
|
|
|
def test_coroutine_never_yielded(self, m_log):
|
2015-05-02 22:38:24 -03:00
|
|
|
with set_coroutine_debug(True):
|
2014-06-27 07:28:41 -03:00
|
|
|
@asyncio.coroutine
|
|
|
|
def coro_noop():
|
|
|
|
pass
|
|
|
|
|
|
|
|
tb_filename = __file__
|
2014-07-11 06:58:33 -03:00
|
|
|
tb_lineno = sys._getframe().f_lineno + 2
|
|
|
|
# create a coroutine object but don't use it
|
|
|
|
coro_noop()
|
2014-06-27 07:28:41 -03:00
|
|
|
support.gc_collect()
|
|
|
|
|
|
|
|
self.assertTrue(m_log.error.called)
|
|
|
|
message = m_log.error.call_args[0][0]
|
|
|
|
func_filename, func_lineno = test_utils.get_function_source(coro_noop)
|
2015-06-24 11:30:14 -03:00
|
|
|
|
|
|
|
regex = (r'^<CoroWrapper %s\(?\)? .* at %s:%s, .*> '
|
2015-01-08 19:09:10 -04:00
|
|
|
r'was never yielded from\n'
|
2014-06-27 07:28:41 -03:00
|
|
|
r'Coroutine object created at \(most recent call last\):\n'
|
|
|
|
r'.*\n'
|
|
|
|
r' File "%s", line %s, in test_coroutine_never_yielded\n'
|
2014-07-11 06:58:33 -03:00
|
|
|
r' coro_noop\(\)$'
|
2014-06-27 07:28:41 -03:00
|
|
|
% (re.escape(coro_noop.__qualname__),
|
2014-06-27 19:12:02 -03:00
|
|
|
re.escape(func_filename), func_lineno,
|
|
|
|
re.escape(tb_filename), tb_lineno))
|
2014-06-27 07:28:41 -03:00
|
|
|
|
|
|
|
self.assertRegex(message, re.compile(regex, re.DOTALL))
|
|
|
|
|
2014-06-27 08:52:20 -03:00
|
|
|
def test_task_source_traceback(self):
|
|
|
|
self.loop.set_debug(True)
|
|
|
|
|
|
|
|
task = asyncio.Task(coroutine_function(), loop=self.loop)
|
|
|
|
lineno = sys._getframe().f_lineno - 1
|
|
|
|
self.assertIsInstance(task._source_traceback, list)
|
|
|
|
self.assertEqual(task._source_traceback[-1][:3],
|
|
|
|
(__file__,
|
|
|
|
lineno,
|
|
|
|
'test_task_source_traceback'))
|
2014-06-30 09:39:11 -03:00
|
|
|
self.loop.run_until_complete(task)
|
2014-06-27 08:52:20 -03:00
|
|
|
|
2015-01-15 11:29:10 -04:00
|
|
|
def _test_cancel_wait_for(self, timeout):
|
|
|
|
loop = asyncio.new_event_loop()
|
|
|
|
self.addCleanup(loop.close)
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def blocking_coroutine():
|
|
|
|
fut = asyncio.Future(loop=loop)
|
|
|
|
# Block: fut result is never set
|
|
|
|
yield from fut
|
|
|
|
|
|
|
|
task = loop.create_task(blocking_coroutine())
|
|
|
|
|
|
|
|
wait = loop.create_task(asyncio.wait_for(task, timeout, loop=loop))
|
|
|
|
loop.call_soon(wait.cancel)
|
|
|
|
|
|
|
|
self.assertRaises(asyncio.CancelledError,
|
|
|
|
loop.run_until_complete, wait)
|
|
|
|
|
|
|
|
# Python issue #23219: cancelling the wait must also cancel the task
|
|
|
|
self.assertTrue(task.cancelled())
|
|
|
|
|
|
|
|
def test_cancel_blocking_wait_for(self):
|
|
|
|
self._test_cancel_wait_for(None)
|
|
|
|
|
|
|
|
def test_cancel_wait_for(self):
|
|
|
|
self._test_cancel_wait_for(60.0)
|
|
|
|
|
2014-04-14 23:21:52 -03:00
|
|
|
|
2013-10-17 17:40:50 -03:00
|
|
|
class GatherTestsBase:
|
|
|
|
|
|
|
|
def setUp(self):
|
2014-06-17 20:36:32 -03:00
|
|
|
self.one_loop = self.new_test_loop()
|
|
|
|
self.other_loop = self.new_test_loop()
|
|
|
|
self.set_event_loop(self.one_loop, cleanup=False)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def _run_loop(self, loop):
|
|
|
|
while loop._ready:
|
|
|
|
test_utils.run_briefly(loop)
|
|
|
|
|
|
|
|
def _check_success(self, **kwargs):
|
2014-01-25 10:32:06 -04:00
|
|
|
a, b, c = [asyncio.Future(loop=self.one_loop) for i in range(3)]
|
|
|
|
fut = asyncio.gather(*self.wrap_futures(a, b, c), **kwargs)
|
2014-02-11 06:34:30 -04:00
|
|
|
cb = test_utils.MockCallback()
|
2013-10-17 17:40:50 -03:00
|
|
|
fut.add_done_callback(cb)
|
|
|
|
b.set_result(1)
|
|
|
|
a.set_result(2)
|
|
|
|
self._run_loop(self.one_loop)
|
|
|
|
self.assertEqual(cb.called, False)
|
|
|
|
self.assertFalse(fut.done())
|
|
|
|
c.set_result(3)
|
|
|
|
self._run_loop(self.one_loop)
|
|
|
|
cb.assert_called_once_with(fut)
|
|
|
|
self.assertEqual(fut.result(), [2, 1, 3])
|
|
|
|
|
|
|
|
def test_success(self):
|
|
|
|
self._check_success()
|
|
|
|
self._check_success(return_exceptions=False)
|
|
|
|
|
|
|
|
def test_result_exception_success(self):
|
|
|
|
self._check_success(return_exceptions=True)
|
|
|
|
|
|
|
|
def test_one_exception(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
a, b, c, d, e = [asyncio.Future(loop=self.one_loop) for i in range(5)]
|
|
|
|
fut = asyncio.gather(*self.wrap_futures(a, b, c, d, e))
|
2014-02-11 06:34:30 -04:00
|
|
|
cb = test_utils.MockCallback()
|
2013-10-17 17:40:50 -03:00
|
|
|
fut.add_done_callback(cb)
|
|
|
|
exc = ZeroDivisionError()
|
|
|
|
a.set_result(1)
|
|
|
|
b.set_exception(exc)
|
|
|
|
self._run_loop(self.one_loop)
|
|
|
|
self.assertTrue(fut.done())
|
|
|
|
cb.assert_called_once_with(fut)
|
|
|
|
self.assertIs(fut.exception(), exc)
|
|
|
|
# Does nothing
|
|
|
|
c.set_result(3)
|
|
|
|
d.cancel()
|
|
|
|
e.set_exception(RuntimeError())
|
2013-12-19 17:42:40 -04:00
|
|
|
e.exception()
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def test_return_exceptions(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
a, b, c, d = [asyncio.Future(loop=self.one_loop) for i in range(4)]
|
|
|
|
fut = asyncio.gather(*self.wrap_futures(a, b, c, d),
|
2014-01-30 20:05:28 -04:00
|
|
|
return_exceptions=True)
|
2014-02-11 06:34:30 -04:00
|
|
|
cb = test_utils.MockCallback()
|
2013-10-17 17:40:50 -03:00
|
|
|
fut.add_done_callback(cb)
|
|
|
|
exc = ZeroDivisionError()
|
|
|
|
exc2 = RuntimeError()
|
|
|
|
b.set_result(1)
|
|
|
|
c.set_exception(exc)
|
|
|
|
a.set_result(3)
|
|
|
|
self._run_loop(self.one_loop)
|
|
|
|
self.assertFalse(fut.done())
|
|
|
|
d.set_exception(exc2)
|
|
|
|
self._run_loop(self.one_loop)
|
|
|
|
self.assertTrue(fut.done())
|
|
|
|
cb.assert_called_once_with(fut)
|
|
|
|
self.assertEqual(fut.result(), [3, 1, exc, exc2])
|
|
|
|
|
2014-02-19 18:15:02 -04:00
|
|
|
def test_env_var_debug(self):
|
2014-09-25 20:12:10 -03:00
|
|
|
aio_path = os.path.dirname(os.path.dirname(asyncio.__file__))
|
|
|
|
|
2014-02-19 18:15:02 -04:00
|
|
|
code = '\n'.join((
|
2014-06-28 19:46:45 -03:00
|
|
|
'import asyncio.coroutines',
|
|
|
|
'print(asyncio.coroutines._DEBUG)'))
|
2014-02-19 18:15:02 -04:00
|
|
|
|
|
|
|
# Test with -E to not fail if the unit test was run with
|
|
|
|
# PYTHONASYNCIODEBUG set to a non-empty string
|
2014-09-25 20:12:10 -03:00
|
|
|
sts, stdout, stderr = assert_python_ok('-E', '-c', code,
|
|
|
|
PYTHONPATH=aio_path)
|
2014-02-19 18:15:02 -04:00
|
|
|
self.assertEqual(stdout.rstrip(), b'False')
|
|
|
|
|
|
|
|
sts, stdout, stderr = assert_python_ok('-c', code,
|
2014-09-25 20:12:10 -03:00
|
|
|
PYTHONASYNCIODEBUG='',
|
|
|
|
PYTHONPATH=aio_path)
|
2014-02-19 18:15:02 -04:00
|
|
|
self.assertEqual(stdout.rstrip(), b'False')
|
|
|
|
|
|
|
|
sts, stdout, stderr = assert_python_ok('-c', code,
|
2014-09-25 20:12:10 -03:00
|
|
|
PYTHONASYNCIODEBUG='1',
|
|
|
|
PYTHONPATH=aio_path)
|
2014-02-19 18:15:02 -04:00
|
|
|
self.assertEqual(stdout.rstrip(), b'True')
|
|
|
|
|
|
|
|
sts, stdout, stderr = assert_python_ok('-E', '-c', code,
|
2014-09-25 20:12:10 -03:00
|
|
|
PYTHONASYNCIODEBUG='1',
|
|
|
|
PYTHONPATH=aio_path)
|
2014-02-19 18:15:02 -04:00
|
|
|
self.assertEqual(stdout.rstrip(), b'False')
|
|
|
|
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-06-17 20:36:32 -03:00
|
|
|
class FutureGatherTests(GatherTestsBase, test_utils.TestCase):
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def wrap_futures(self, *futures):
|
|
|
|
return futures
|
|
|
|
|
|
|
|
def _check_empty_sequence(self, seq_or_iter):
|
2014-01-25 10:32:06 -04:00
|
|
|
asyncio.set_event_loop(self.one_loop)
|
|
|
|
self.addCleanup(asyncio.set_event_loop, None)
|
|
|
|
fut = asyncio.gather(*seq_or_iter)
|
|
|
|
self.assertIsInstance(fut, asyncio.Future)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertIs(fut._loop, self.one_loop)
|
|
|
|
self._run_loop(self.one_loop)
|
|
|
|
self.assertTrue(fut.done())
|
|
|
|
self.assertEqual(fut.result(), [])
|
2014-01-25 10:32:06 -04:00
|
|
|
fut = asyncio.gather(*seq_or_iter, loop=self.other_loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertIs(fut._loop, self.other_loop)
|
|
|
|
|
|
|
|
def test_constructor_empty_sequence(self):
|
|
|
|
self._check_empty_sequence([])
|
|
|
|
self._check_empty_sequence(())
|
|
|
|
self._check_empty_sequence(set())
|
|
|
|
self._check_empty_sequence(iter(""))
|
|
|
|
|
|
|
|
def test_constructor_heterogenous_futures(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
fut1 = asyncio.Future(loop=self.one_loop)
|
|
|
|
fut2 = asyncio.Future(loop=self.other_loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
with self.assertRaises(ValueError):
|
2014-01-25 10:32:06 -04:00
|
|
|
asyncio.gather(fut1, fut2)
|
2013-10-17 17:40:50 -03:00
|
|
|
with self.assertRaises(ValueError):
|
2014-01-25 10:32:06 -04:00
|
|
|
asyncio.gather(fut1, loop=self.other_loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def test_constructor_homogenous_futures(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
children = [asyncio.Future(loop=self.other_loop) for i in range(3)]
|
|
|
|
fut = asyncio.gather(*children)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertIs(fut._loop, self.other_loop)
|
|
|
|
self._run_loop(self.other_loop)
|
|
|
|
self.assertFalse(fut.done())
|
2014-01-25 10:32:06 -04:00
|
|
|
fut = asyncio.gather(*children, loop=self.other_loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertIs(fut._loop, self.other_loop)
|
|
|
|
self._run_loop(self.other_loop)
|
|
|
|
self.assertFalse(fut.done())
|
|
|
|
|
|
|
|
def test_one_cancellation(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
a, b, c, d, e = [asyncio.Future(loop=self.one_loop) for i in range(5)]
|
|
|
|
fut = asyncio.gather(a, b, c, d, e)
|
2014-02-11 06:34:30 -04:00
|
|
|
cb = test_utils.MockCallback()
|
2013-10-17 17:40:50 -03:00
|
|
|
fut.add_done_callback(cb)
|
|
|
|
a.set_result(1)
|
|
|
|
b.cancel()
|
|
|
|
self._run_loop(self.one_loop)
|
|
|
|
self.assertTrue(fut.done())
|
|
|
|
cb.assert_called_once_with(fut)
|
|
|
|
self.assertFalse(fut.cancelled())
|
2014-01-25 10:32:06 -04:00
|
|
|
self.assertIsInstance(fut.exception(), asyncio.CancelledError)
|
2013-10-17 17:40:50 -03:00
|
|
|
# Does nothing
|
|
|
|
c.set_result(3)
|
|
|
|
d.cancel()
|
|
|
|
e.set_exception(RuntimeError())
|
2013-12-19 17:42:40 -04:00
|
|
|
e.exception()
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def test_result_exception_one_cancellation(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
a, b, c, d, e, f = [asyncio.Future(loop=self.one_loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
for i in range(6)]
|
2014-01-25 10:32:06 -04:00
|
|
|
fut = asyncio.gather(a, b, c, d, e, f, return_exceptions=True)
|
2014-02-11 06:34:30 -04:00
|
|
|
cb = test_utils.MockCallback()
|
2013-10-17 17:40:50 -03:00
|
|
|
fut.add_done_callback(cb)
|
|
|
|
a.set_result(1)
|
|
|
|
zde = ZeroDivisionError()
|
|
|
|
b.set_exception(zde)
|
|
|
|
c.cancel()
|
|
|
|
self._run_loop(self.one_loop)
|
|
|
|
self.assertFalse(fut.done())
|
|
|
|
d.set_result(3)
|
|
|
|
e.cancel()
|
|
|
|
rte = RuntimeError()
|
|
|
|
f.set_exception(rte)
|
|
|
|
res = self.one_loop.run_until_complete(fut)
|
2014-01-25 10:32:06 -04:00
|
|
|
self.assertIsInstance(res[2], asyncio.CancelledError)
|
|
|
|
self.assertIsInstance(res[4], asyncio.CancelledError)
|
2013-10-17 17:40:50 -03:00
|
|
|
res[2] = res[4] = None
|
|
|
|
self.assertEqual(res, [1, zde, None, 3, None, rte])
|
|
|
|
cb.assert_called_once_with(fut)
|
|
|
|
|
|
|
|
|
2014-06-17 20:36:32 -03:00
|
|
|
class CoroutineGatherTests(GatherTestsBase, test_utils.TestCase):
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
super().setUp()
|
2014-01-25 10:32:06 -04:00
|
|
|
asyncio.set_event_loop(self.one_loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def wrap_futures(self, *futures):
|
|
|
|
coros = []
|
|
|
|
for fut in futures:
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def coro(fut=fut):
|
|
|
|
return (yield from fut)
|
|
|
|
coros.append(coro())
|
|
|
|
return coros
|
|
|
|
|
|
|
|
def test_constructor_loop_selection(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def coro():
|
|
|
|
return 'abc'
|
|
|
|
gen1 = coro()
|
|
|
|
gen2 = coro()
|
2014-01-25 10:32:06 -04:00
|
|
|
fut = asyncio.gather(gen1, gen2)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertIs(fut._loop, self.one_loop)
|
2014-06-25 18:11:21 -03:00
|
|
|
self.one_loop.run_until_complete(fut)
|
2014-06-22 19:12:14 -03:00
|
|
|
|
|
|
|
self.set_event_loop(self.other_loop, cleanup=False)
|
2013-10-17 17:40:50 -03:00
|
|
|
gen3 = coro()
|
|
|
|
gen4 = coro()
|
2014-06-25 18:11:21 -03:00
|
|
|
fut2 = asyncio.gather(gen3, gen4, loop=self.other_loop)
|
|
|
|
self.assertIs(fut2._loop, self.other_loop)
|
|
|
|
self.other_loop.run_until_complete(fut2)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-02-06 23:06:16 -04:00
|
|
|
def test_duplicate_coroutines(self):
|
|
|
|
@asyncio.coroutine
|
|
|
|
def coro(s):
|
|
|
|
return s
|
|
|
|
c = coro('abc')
|
|
|
|
fut = asyncio.gather(c, c, coro('def'), c, loop=self.one_loop)
|
|
|
|
self._run_loop(self.one_loop)
|
|
|
|
self.assertEqual(fut.result(), ['abc', 'abc', 'def', 'abc'])
|
|
|
|
|
2013-10-17 17:40:50 -03:00
|
|
|
def test_cancellation_broadcast(self):
|
|
|
|
# Cancelling outer() cancels all children.
|
|
|
|
proof = 0
|
2014-01-25 10:32:06 -04:00
|
|
|
waiter = asyncio.Future(loop=self.one_loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def inner():
|
|
|
|
nonlocal proof
|
|
|
|
yield from waiter
|
|
|
|
proof += 1
|
|
|
|
|
2015-05-11 15:48:38 -03:00
|
|
|
child1 = asyncio.ensure_future(inner(), loop=self.one_loop)
|
|
|
|
child2 = asyncio.ensure_future(inner(), loop=self.one_loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
gatherer = None
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def outer():
|
|
|
|
nonlocal proof, gatherer
|
2014-01-25 10:32:06 -04:00
|
|
|
gatherer = asyncio.gather(child1, child2, loop=self.one_loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
yield from gatherer
|
|
|
|
proof += 100
|
|
|
|
|
2015-05-11 15:48:38 -03:00
|
|
|
f = asyncio.ensure_future(outer(), loop=self.one_loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
test_utils.run_briefly(self.one_loop)
|
|
|
|
self.assertTrue(f.cancel())
|
2014-01-25 10:32:06 -04:00
|
|
|
with self.assertRaises(asyncio.CancelledError):
|
2013-10-17 17:40:50 -03:00
|
|
|
self.one_loop.run_until_complete(f)
|
|
|
|
self.assertFalse(gatherer.cancel())
|
|
|
|
self.assertTrue(waiter.cancelled())
|
|
|
|
self.assertTrue(child1.cancelled())
|
|
|
|
self.assertTrue(child2.cancelled())
|
|
|
|
test_utils.run_briefly(self.one_loop)
|
|
|
|
self.assertEqual(proof, 0)
|
|
|
|
|
|
|
|
def test_exception_marking(self):
|
|
|
|
# Test for the first line marked "Mark exception retrieved."
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def inner(f):
|
|
|
|
yield from f
|
|
|
|
raise RuntimeError('should not be ignored')
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
a = asyncio.Future(loop=self.one_loop)
|
|
|
|
b = asyncio.Future(loop=self.one_loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
@asyncio.coroutine
|
2013-10-17 17:40:50 -03:00
|
|
|
def outer():
|
2014-01-25 10:32:06 -04:00
|
|
|
yield from asyncio.gather(inner(a), inner(b), loop=self.one_loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2015-05-11 15:48:38 -03:00
|
|
|
f = asyncio.ensure_future(outer(), loop=self.one_loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
test_utils.run_briefly(self.one_loop)
|
|
|
|
a.set_result(None)
|
|
|
|
test_utils.run_briefly(self.one_loop)
|
|
|
|
b.set_result(None)
|
|
|
|
test_utils.run_briefly(self.one_loop)
|
|
|
|
self.assertIsInstance(f.exception(), RuntimeError)
|
|
|
|
|
|
|
|
|
2015-10-03 12:31:42 -03:00
|
|
|
class RunCoroutineThreadsafeTests(test_utils.TestCase):
|
2015-11-13 13:28:48 -04:00
|
|
|
"""Test case for asyncio.run_coroutine_threadsafe."""
|
2015-10-03 12:31:42 -03:00
|
|
|
|
|
|
|
def setUp(self):
|
2015-11-13 13:28:48 -04:00
|
|
|
self.loop = asyncio.new_event_loop()
|
|
|
|
self.set_event_loop(self.loop) # Will cleanup properly
|
2015-10-03 12:31:42 -03:00
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def add(self, a, b, fail=False, cancel=False):
|
2015-11-13 13:28:48 -04:00
|
|
|
"""Wait 0.05 second and return a + b."""
|
|
|
|
yield from asyncio.sleep(0.05, loop=self.loop)
|
2015-10-03 12:31:42 -03:00
|
|
|
if fail:
|
|
|
|
raise RuntimeError("Fail!")
|
|
|
|
if cancel:
|
|
|
|
asyncio.tasks.Task.current_task(self.loop).cancel()
|
|
|
|
yield
|
|
|
|
return a + b
|
|
|
|
|
2015-11-13 13:28:48 -04:00
|
|
|
def target(self, fail=False, cancel=False, timeout=None,
|
|
|
|
advance_coro=False):
|
2015-10-03 12:31:42 -03:00
|
|
|
"""Run add coroutine in the event loop."""
|
|
|
|
coro = self.add(1, 2, fail=fail, cancel=cancel)
|
|
|
|
future = asyncio.run_coroutine_threadsafe(coro, self.loop)
|
2015-11-13 13:28:48 -04:00
|
|
|
if advance_coro:
|
|
|
|
# this is for test_run_coroutine_threadsafe_task_factory_exception;
|
|
|
|
# otherwise it spills errors and breaks **other** unittests, since
|
|
|
|
# 'target' is interacting with threads.
|
|
|
|
|
|
|
|
# With this call, `coro` will be advanced, so that
|
|
|
|
# CoroWrapper.__del__ won't do anything when asyncio tests run
|
|
|
|
# in debug mode.
|
|
|
|
self.loop.call_soon_threadsafe(coro.send, None)
|
2015-10-03 12:31:42 -03:00
|
|
|
try:
|
|
|
|
return future.result(timeout)
|
|
|
|
finally:
|
|
|
|
future.done() or future.cancel()
|
|
|
|
|
|
|
|
def test_run_coroutine_threadsafe(self):
|
|
|
|
"""Test coroutine submission from a thread to an event loop."""
|
|
|
|
future = self.loop.run_in_executor(None, self.target)
|
|
|
|
result = self.loop.run_until_complete(future)
|
|
|
|
self.assertEqual(result, 3)
|
|
|
|
|
|
|
|
def test_run_coroutine_threadsafe_with_exception(self):
|
|
|
|
"""Test coroutine submission from a thread to an event loop
|
|
|
|
when an exception is raised."""
|
|
|
|
future = self.loop.run_in_executor(None, self.target, True)
|
|
|
|
with self.assertRaises(RuntimeError) as exc_context:
|
|
|
|
self.loop.run_until_complete(future)
|
|
|
|
self.assertIn("Fail!", exc_context.exception.args)
|
|
|
|
|
|
|
|
def test_run_coroutine_threadsafe_with_timeout(self):
|
|
|
|
"""Test coroutine submission from a thread to an event loop
|
|
|
|
when a timeout is raised."""
|
|
|
|
callback = lambda: self.target(timeout=0)
|
|
|
|
future = self.loop.run_in_executor(None, callback)
|
|
|
|
with self.assertRaises(asyncio.TimeoutError):
|
|
|
|
self.loop.run_until_complete(future)
|
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
# Check that there's no pending task (add has been cancelled)
|
|
|
|
for task in asyncio.Task.all_tasks(self.loop):
|
|
|
|
self.assertTrue(task.done())
|
|
|
|
|
|
|
|
def test_run_coroutine_threadsafe_task_cancelled(self):
|
|
|
|
"""Test coroutine submission from a tread to an event loop
|
|
|
|
when the task is cancelled."""
|
|
|
|
callback = lambda: self.target(cancel=True)
|
|
|
|
future = self.loop.run_in_executor(None, callback)
|
|
|
|
with self.assertRaises(asyncio.CancelledError):
|
|
|
|
self.loop.run_until_complete(future)
|
|
|
|
|
2015-10-05 20:20:00 -03:00
|
|
|
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
|
2015-11-13 13:28:48 -04:00
|
|
|
future = self.loop.run_in_executor(
|
|
|
|
None, lambda: self.target(advance_coro=True))
|
2015-10-05 20:20:00 -03:00
|
|
|
# Set corrupted task factory
|
|
|
|
self.loop.set_task_factory(lambda loop, coro: wrong_name)
|
|
|
|
# Set exception handler
|
|
|
|
callback = test_utils.MockCallback()
|
|
|
|
self.loop.set_exception_handler(callback)
|
|
|
|
# Run event loop
|
|
|
|
with self.assertRaises(NameError) as exc_context:
|
|
|
|
self.loop.run_until_complete(future)
|
|
|
|
# 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)
|
|
|
|
|
2015-10-03 12:31:42 -03:00
|
|
|
|
2015-11-05 15:29:04 -04:00
|
|
|
class SleepTests(test_utils.TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.loop = asyncio.new_event_loop()
|
|
|
|
asyncio.set_event_loop(None)
|
|
|
|
|
|
|
|
def test_sleep_zero(self):
|
|
|
|
result = 0
|
|
|
|
|
|
|
|
def inc_result(num):
|
|
|
|
nonlocal result
|
|
|
|
result += num
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def coro():
|
|
|
|
self.loop.call_soon(inc_result, 1)
|
|
|
|
self.assertEqual(result, 0)
|
|
|
|
num = yield from asyncio.sleep(0, loop=self.loop, result=10)
|
|
|
|
self.assertEqual(result, 1) # inc'ed by call_soon
|
|
|
|
inc_result(num) # num should be 11
|
|
|
|
|
|
|
|
self.loop.run_until_complete(coro())
|
|
|
|
self.assertEqual(result, 11)
|
|
|
|
|
|
|
|
|
2013-10-17 17:40:50 -03:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|