summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_asyncio/test_tasks.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/test/test_asyncio/test_tasks.py')
-rw-r--r--Lib/test/test_asyncio/test_tasks.py596
1 files changed, 297 insertions, 299 deletions
diff --git a/Lib/test/test_asyncio/test_tasks.py b/Lib/test/test_asyncio/test_tasks.py
index 3d08ad8..dbf130c 100644
--- a/Lib/test/test_asyncio/test_tasks.py
+++ b/Lib/test/test_asyncio/test_tasks.py
@@ -5,9 +5,7 @@ import unittest
import unittest.mock
from unittest.mock import Mock
-from asyncio import events
-from asyncio import futures
-from asyncio import tasks
+import asyncio
from asyncio import test_utils
@@ -24,115 +22,115 @@ class TaskTests(unittest.TestCase):
def setUp(self):
self.loop = test_utils.TestLoop()
- events.set_event_loop(None)
+ asyncio.set_event_loop(None)
def tearDown(self):
self.loop.close()
gc.collect()
def test_task_class(self):
- @tasks.coroutine
+ @asyncio.coroutine
def notmuch():
return 'ok'
- t = tasks.Task(notmuch(), loop=self.loop)
+ t = asyncio.Task(notmuch(), loop=self.loop)
self.loop.run_until_complete(t)
self.assertTrue(t.done())
self.assertEqual(t.result(), 'ok')
self.assertIs(t._loop, self.loop)
- loop = events.new_event_loop()
- t = tasks.Task(notmuch(), loop=loop)
+ loop = asyncio.new_event_loop()
+ t = asyncio.Task(notmuch(), loop=loop)
self.assertIs(t._loop, loop)
loop.close()
def test_async_coroutine(self):
- @tasks.coroutine
+ @asyncio.coroutine
def notmuch():
return 'ok'
- t = tasks.async(notmuch(), loop=self.loop)
+ t = asyncio.async(notmuch(), loop=self.loop)
self.loop.run_until_complete(t)
self.assertTrue(t.done())
self.assertEqual(t.result(), 'ok')
self.assertIs(t._loop, self.loop)
- loop = events.new_event_loop()
- t = tasks.async(notmuch(), loop=loop)
+ loop = asyncio.new_event_loop()
+ t = asyncio.async(notmuch(), loop=loop)
self.assertIs(t._loop, loop)
loop.close()
def test_async_future(self):
- f_orig = futures.Future(loop=self.loop)
+ f_orig = asyncio.Future(loop=self.loop)
f_orig.set_result('ko')
- f = tasks.async(f_orig)
+ f = asyncio.async(f_orig)
self.loop.run_until_complete(f)
self.assertTrue(f.done())
self.assertEqual(f.result(), 'ko')
self.assertIs(f, f_orig)
- loop = events.new_event_loop()
+ loop = asyncio.new_event_loop()
with self.assertRaises(ValueError):
- f = tasks.async(f_orig, loop=loop)
+ f = asyncio.async(f_orig, loop=loop)
loop.close()
- f = tasks.async(f_orig, loop=self.loop)
+ f = asyncio.async(f_orig, loop=self.loop)
self.assertIs(f, f_orig)
def test_async_task(self):
- @tasks.coroutine
+ @asyncio.coroutine
def notmuch():
return 'ok'
- t_orig = tasks.Task(notmuch(), loop=self.loop)
- t = tasks.async(t_orig)
+ t_orig = asyncio.Task(notmuch(), loop=self.loop)
+ t = asyncio.async(t_orig)
self.loop.run_until_complete(t)
self.assertTrue(t.done())
self.assertEqual(t.result(), 'ok')
self.assertIs(t, t_orig)
- loop = events.new_event_loop()
+ loop = asyncio.new_event_loop()
with self.assertRaises(ValueError):
- t = tasks.async(t_orig, loop=loop)
+ t = asyncio.async(t_orig, loop=loop)
loop.close()
- t = tasks.async(t_orig, loop=self.loop)
+ t = asyncio.async(t_orig, loop=self.loop)
self.assertIs(t, t_orig)
def test_async_neither(self):
with self.assertRaises(TypeError):
- tasks.async('ok')
+ asyncio.async('ok')
def test_task_repr(self):
- @tasks.coroutine
+ @asyncio.coroutine
def notmuch():
yield from []
return 'abc'
- t = tasks.Task(notmuch(), loop=self.loop)
+ t = asyncio.Task(notmuch(), loop=self.loop)
t.add_done_callback(Dummy())
self.assertEqual(repr(t), 'Task(<notmuch>)<PENDING, [Dummy()]>')
t.cancel() # Does not take immediate effect!
self.assertEqual(repr(t), 'Task(<notmuch>)<CANCELLING, [Dummy()]>')
- self.assertRaises(futures.CancelledError,
+ self.assertRaises(asyncio.CancelledError,
self.loop.run_until_complete, t)
self.assertEqual(repr(t), 'Task(<notmuch>)<CANCELLED>')
- t = tasks.Task(notmuch(), loop=self.loop)
+ t = asyncio.Task(notmuch(), loop=self.loop)
self.loop.run_until_complete(t)
self.assertEqual(repr(t), "Task(<notmuch>)<result='abc'>")
def test_task_repr_custom(self):
- @tasks.coroutine
+ @asyncio.coroutine
def coro():
pass
- class T(futures.Future):
+ class T(asyncio.Future):
def __repr__(self):
return 'T[]'
- class MyTask(tasks.Task, T):
+ class MyTask(asyncio.Task, T):
def __repr__(self):
return super().__repr__()
@@ -142,17 +140,17 @@ class TaskTests(unittest.TestCase):
gen.close()
def test_task_basics(self):
- @tasks.coroutine
+ @asyncio.coroutine
def outer():
a = yield from inner1()
b = yield from inner2()
return a+b
- @tasks.coroutine
+ @asyncio.coroutine
def inner1():
return 42
- @tasks.coroutine
+ @asyncio.coroutine
def inner2():
return 1000
@@ -169,66 +167,66 @@ class TaskTests(unittest.TestCase):
loop = test_utils.TestLoop(gen)
self.addCleanup(loop.close)
- @tasks.coroutine
+ @asyncio.coroutine
def task():
- yield from tasks.sleep(10.0, loop=loop)
+ yield from asyncio.sleep(10.0, loop=loop)
return 12
- t = tasks.Task(task(), loop=loop)
+ t = asyncio.Task(task(), loop=loop)
loop.call_soon(t.cancel)
- with self.assertRaises(futures.CancelledError):
+ with self.assertRaises(asyncio.CancelledError):
loop.run_until_complete(t)
self.assertTrue(t.done())
self.assertTrue(t.cancelled())
self.assertFalse(t.cancel())
def test_cancel_yield(self):
- @tasks.coroutine
+ @asyncio.coroutine
def task():
yield
yield
return 12
- t = tasks.Task(task(), loop=self.loop)
+ t = asyncio.Task(task(), loop=self.loop)
test_utils.run_briefly(self.loop) # start coro
t.cancel()
self.assertRaises(
- futures.CancelledError, self.loop.run_until_complete, t)
+ asyncio.CancelledError, self.loop.run_until_complete, t)
self.assertTrue(t.done())
self.assertTrue(t.cancelled())
self.assertFalse(t.cancel())
def test_cancel_inner_future(self):
- f = futures.Future(loop=self.loop)
+ f = asyncio.Future(loop=self.loop)
- @tasks.coroutine
+ @asyncio.coroutine
def task():
yield from f
return 12
- t = tasks.Task(task(), loop=self.loop)
+ t = asyncio.Task(task(), loop=self.loop)
test_utils.run_briefly(self.loop) # start task
f.cancel()
- with self.assertRaises(futures.CancelledError):
+ with self.assertRaises(asyncio.CancelledError):
self.loop.run_until_complete(t)
self.assertTrue(f.cancelled())
self.assertTrue(t.cancelled())
def test_cancel_both_task_and_inner_future(self):
- f = futures.Future(loop=self.loop)
+ f = asyncio.Future(loop=self.loop)
- @tasks.coroutine
+ @asyncio.coroutine
def task():
yield from f
return 12
- t = tasks.Task(task(), loop=self.loop)
+ t = asyncio.Task(task(), loop=self.loop)
test_utils.run_briefly(self.loop)
f.cancel()
t.cancel()
- with self.assertRaises(futures.CancelledError):
+ with self.assertRaises(asyncio.CancelledError):
self.loop.run_until_complete(t)
self.assertTrue(t.done())
@@ -236,18 +234,18 @@ class TaskTests(unittest.TestCase):
self.assertTrue(t.cancelled())
def test_cancel_task_catching(self):
- fut1 = futures.Future(loop=self.loop)
- fut2 = futures.Future(loop=self.loop)
+ fut1 = asyncio.Future(loop=self.loop)
+ fut2 = asyncio.Future(loop=self.loop)
- @tasks.coroutine
+ @asyncio.coroutine
def task():
yield from fut1
try:
yield from fut2
- except futures.CancelledError:
+ except asyncio.CancelledError:
return 42
- t = tasks.Task(task(), loop=self.loop)
+ t = asyncio.Task(task(), loop=self.loop)
test_utils.run_briefly(self.loop)
self.assertIs(t._fut_waiter, fut1) # White-box test.
fut1.set_result(None)
@@ -260,21 +258,21 @@ class TaskTests(unittest.TestCase):
self.assertFalse(t.cancelled())
def test_cancel_task_ignoring(self):
- fut1 = futures.Future(loop=self.loop)
- fut2 = futures.Future(loop=self.loop)
- fut3 = futures.Future(loop=self.loop)
+ fut1 = asyncio.Future(loop=self.loop)
+ fut2 = asyncio.Future(loop=self.loop)
+ fut3 = asyncio.Future(loop=self.loop)
- @tasks.coroutine
+ @asyncio.coroutine
def task():
yield from fut1
try:
yield from fut2
- except futures.CancelledError:
+ except asyncio.CancelledError:
pass
res = yield from fut3
return res
- t = tasks.Task(task(), loop=self.loop)
+ t = asyncio.Task(task(), loop=self.loop)
test_utils.run_briefly(self.loop)
self.assertIs(t._fut_waiter, fut1) # White-box test.
fut1.set_result(None)
@@ -291,20 +289,20 @@ class TaskTests(unittest.TestCase):
self.assertFalse(t.cancelled())
def test_cancel_current_task(self):
- loop = events.new_event_loop()
+ loop = asyncio.new_event_loop()
self.addCleanup(loop.close)
- @tasks.coroutine
+ @asyncio.coroutine
def task():
t.cancel()
self.assertTrue(t._must_cancel) # White-box test.
# The sleep should be cancelled immediately.
- yield from tasks.sleep(100, loop=loop)
+ yield from asyncio.sleep(100, loop=loop)
return 12
- t = tasks.Task(task(), loop=loop)
+ t = asyncio.Task(task(), loop=loop)
self.assertRaises(
- futures.CancelledError, loop.run_until_complete, t)
+ asyncio.CancelledError, loop.run_until_complete, t)
self.assertTrue(t.done())
self.assertFalse(t._must_cancel) # White-box test.
self.assertFalse(t.cancel())
@@ -326,17 +324,17 @@ class TaskTests(unittest.TestCase):
x = 0
waiters = []
- @tasks.coroutine
+ @asyncio.coroutine
def task():
nonlocal x
while x < 10:
- waiters.append(tasks.sleep(0.1, loop=loop))
+ waiters.append(asyncio.sleep(0.1, loop=loop))
yield from waiters[-1]
x += 1
if x == 2:
loop.stop()
- t = tasks.Task(task(), loop=loop)
+ t = asyncio.Task(task(), loop=loop)
self.assertRaises(
RuntimeError, loop.run_until_complete, t)
self.assertFalse(t.done())
@@ -361,20 +359,20 @@ class TaskTests(unittest.TestCase):
foo_running = None
- @tasks.coroutine
+ @asyncio.coroutine
def foo():
nonlocal foo_running
foo_running = True
try:
- yield from tasks.sleep(0.2, loop=loop)
+ yield from asyncio.sleep(0.2, loop=loop)
finally:
foo_running = False
return 'done'
- fut = tasks.Task(foo(), loop=loop)
+ fut = asyncio.Task(foo(), loop=loop)
- with self.assertRaises(futures.TimeoutError):
- loop.run_until_complete(tasks.wait_for(fut, 0.1, loop=loop))
+ with self.assertRaises(asyncio.TimeoutError):
+ loop.run_until_complete(asyncio.wait_for(fut, 0.1, loop=loop))
self.assertTrue(fut.done())
# it should have been cancelled due to the timeout
self.assertTrue(fut.cancelled())
@@ -394,18 +392,18 @@ class TaskTests(unittest.TestCase):
loop = test_utils.TestLoop(gen)
self.addCleanup(loop.close)
- @tasks.coroutine
+ @asyncio.coroutine
def foo():
- yield from tasks.sleep(0.2, loop=loop)
+ yield from asyncio.sleep(0.2, loop=loop)
return 'done'
- events.set_event_loop(loop)
+ asyncio.set_event_loop(loop)
try:
- fut = tasks.Task(foo(), loop=loop)
- with self.assertRaises(futures.TimeoutError):
- loop.run_until_complete(tasks.wait_for(fut, 0.01))
+ fut = asyncio.Task(foo(), loop=loop)
+ with self.assertRaises(asyncio.TimeoutError):
+ loop.run_until_complete(asyncio.wait_for(fut, 0.01))
finally:
- events.set_event_loop(None)
+ asyncio.set_event_loop(None)
self.assertAlmostEqual(0.01, loop.time())
self.assertTrue(fut.done())
@@ -423,22 +421,22 @@ class TaskTests(unittest.TestCase):
loop = test_utils.TestLoop(gen)
self.addCleanup(loop.close)
- a = tasks.Task(tasks.sleep(0.1, loop=loop), loop=loop)
- b = tasks.Task(tasks.sleep(0.15, loop=loop), loop=loop)
+ a = asyncio.Task(asyncio.sleep(0.1, loop=loop), loop=loop)
+ b = asyncio.Task(asyncio.sleep(0.15, loop=loop), loop=loop)
- @tasks.coroutine
+ @asyncio.coroutine
def foo():
- done, pending = yield from tasks.wait([b, a], loop=loop)
+ done, pending = yield from asyncio.wait([b, a], loop=loop)
self.assertEqual(done, set([a, b]))
self.assertEqual(pending, set())
return 42
- res = loop.run_until_complete(tasks.Task(foo(), loop=loop))
+ res = loop.run_until_complete(asyncio.Task(foo(), loop=loop))
self.assertEqual(res, 42)
self.assertAlmostEqual(0.15, loop.time())
# Doing it again should take no time and exercise a different path.
- res = loop.run_until_complete(tasks.Task(foo(), loop=loop))
+ res = loop.run_until_complete(asyncio.Task(foo(), loop=loop))
self.assertAlmostEqual(0.15, loop.time())
self.assertEqual(res, 42)
@@ -454,33 +452,33 @@ class TaskTests(unittest.TestCase):
loop = test_utils.TestLoop(gen)
self.addCleanup(loop.close)
- a = tasks.Task(tasks.sleep(0.01, loop=loop), loop=loop)
- b = tasks.Task(tasks.sleep(0.015, loop=loop), loop=loop)
+ a = asyncio.Task(asyncio.sleep(0.01, loop=loop), loop=loop)
+ b = asyncio.Task(asyncio.sleep(0.015, loop=loop), loop=loop)
- @tasks.coroutine
+ @asyncio.coroutine
def foo():
- done, pending = yield from tasks.wait([b, a])
+ done, pending = yield from asyncio.wait([b, a])
self.assertEqual(done, set([a, b]))
self.assertEqual(pending, set())
return 42
- events.set_event_loop(loop)
+ asyncio.set_event_loop(loop)
try:
res = loop.run_until_complete(
- tasks.Task(foo(), loop=loop))
+ asyncio.Task(foo(), loop=loop))
finally:
- events.set_event_loop(None)
+ asyncio.set_event_loop(None)
self.assertEqual(res, 42)
def test_wait_errors(self):
self.assertRaises(
ValueError, self.loop.run_until_complete,
- tasks.wait(set(), loop=self.loop))
+ asyncio.wait(set(), loop=self.loop))
self.assertRaises(
ValueError, self.loop.run_until_complete,
- tasks.wait([tasks.sleep(10.0, loop=self.loop)],
+ asyncio.wait([asyncio.sleep(10.0, loop=self.loop)],
return_when=-1, loop=self.loop))
def test_wait_first_completed(self):
@@ -495,10 +493,10 @@ class TaskTests(unittest.TestCase):
loop = test_utils.TestLoop(gen)
self.addCleanup(loop.close)
- a = tasks.Task(tasks.sleep(10.0, loop=loop), loop=loop)
- b = tasks.Task(tasks.sleep(0.1, loop=loop), loop=loop)
- task = tasks.Task(
- tasks.wait([b, a], return_when=tasks.FIRST_COMPLETED,
+ 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,
loop=loop),
loop=loop)
@@ -512,25 +510,25 @@ class TaskTests(unittest.TestCase):
# move forward to close generator
loop.advance_time(10)
- loop.run_until_complete(tasks.wait([a, b], loop=loop))
+ loop.run_until_complete(asyncio.wait([a, b], loop=loop))
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
- @tasks.coroutine
+ @asyncio.coroutine
def coro1():
yield
- @tasks.coroutine
+ @asyncio.coroutine
def coro2():
yield
yield
- a = tasks.Task(coro1(), loop=self.loop)
- b = tasks.Task(coro2(), loop=self.loop)
- task = tasks.Task(
- tasks.wait([b, a], return_when=tasks.FIRST_COMPLETED,
+ 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,
loop=self.loop),
loop=self.loop)
@@ -552,15 +550,15 @@ class TaskTests(unittest.TestCase):
self.addCleanup(loop.close)
# first_exception, task already has exception
- a = tasks.Task(tasks.sleep(10.0, loop=loop), loop=loop)
+ a = asyncio.Task(asyncio.sleep(10.0, loop=loop), loop=loop)
- @tasks.coroutine
+ @asyncio.coroutine
def exc():
raise ZeroDivisionError('err')
- b = tasks.Task(exc(), loop=loop)
- task = tasks.Task(
- tasks.wait([b, a], return_when=tasks.FIRST_EXCEPTION,
+ b = asyncio.Task(exc(), loop=loop)
+ task = asyncio.Task(
+ asyncio.wait([b, a], return_when=asyncio.FIRST_EXCEPTION,
loop=loop),
loop=loop)
@@ -571,7 +569,7 @@ class TaskTests(unittest.TestCase):
# move forward to close generator
loop.advance_time(10)
- loop.run_until_complete(tasks.wait([a, b], loop=loop))
+ loop.run_until_complete(asyncio.wait([a, b], loop=loop))
def test_wait_first_exception_in_wait(self):
@@ -586,15 +584,15 @@ class TaskTests(unittest.TestCase):
self.addCleanup(loop.close)
# first_exception, exception during waiting
- a = tasks.Task(tasks.sleep(10.0, loop=loop), loop=loop)
+ a = asyncio.Task(asyncio.sleep(10.0, loop=loop), loop=loop)
- @tasks.coroutine
+ @asyncio.coroutine
def exc():
- yield from tasks.sleep(0.01, loop=loop)
+ yield from asyncio.sleep(0.01, loop=loop)
raise ZeroDivisionError('err')
- b = tasks.Task(exc(), loop=loop)
- task = tasks.wait([b, a], return_when=tasks.FIRST_EXCEPTION,
+ b = asyncio.Task(exc(), loop=loop)
+ task = asyncio.wait([b, a], return_when=asyncio.FIRST_EXCEPTION,
loop=loop)
done, pending = loop.run_until_complete(task)
@@ -604,7 +602,7 @@ class TaskTests(unittest.TestCase):
# move forward to close generator
loop.advance_time(10)
- loop.run_until_complete(tasks.wait([a, b], loop=loop))
+ loop.run_until_complete(asyncio.wait([a, b], loop=loop))
def test_wait_with_exception(self):
@@ -618,27 +616,27 @@ class TaskTests(unittest.TestCase):
loop = test_utils.TestLoop(gen)
self.addCleanup(loop.close)
- a = tasks.Task(tasks.sleep(0.1, loop=loop), loop=loop)
+ a = asyncio.Task(asyncio.sleep(0.1, loop=loop), loop=loop)
- @tasks.coroutine
+ @asyncio.coroutine
def sleeper():
- yield from tasks.sleep(0.15, loop=loop)
+ yield from asyncio.sleep(0.15, loop=loop)
raise ZeroDivisionError('really')
- b = tasks.Task(sleeper(), loop=loop)
+ b = asyncio.Task(sleeper(), loop=loop)
- @tasks.coroutine
+ @asyncio.coroutine
def foo():
- done, pending = yield from tasks.wait([b, a], loop=loop)
+ done, pending = yield from asyncio.wait([b, a], loop=loop)
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)
- loop.run_until_complete(tasks.Task(foo(), loop=loop))
+ loop.run_until_complete(asyncio.Task(foo(), loop=loop))
self.assertAlmostEqual(0.15, loop.time())
- loop.run_until_complete(tasks.Task(foo(), loop=loop))
+ loop.run_until_complete(asyncio.Task(foo(), loop=loop))
self.assertAlmostEqual(0.15, loop.time())
def test_wait_with_timeout(self):
@@ -655,22 +653,22 @@ class TaskTests(unittest.TestCase):
loop = test_utils.TestLoop(gen)
self.addCleanup(loop.close)
- a = tasks.Task(tasks.sleep(0.1, loop=loop), loop=loop)
- b = tasks.Task(tasks.sleep(0.15, loop=loop), loop=loop)
+ a = asyncio.Task(asyncio.sleep(0.1, loop=loop), loop=loop)
+ b = asyncio.Task(asyncio.sleep(0.15, loop=loop), loop=loop)
- @tasks.coroutine
+ @asyncio.coroutine
def foo():
- done, pending = yield from tasks.wait([b, a], timeout=0.11,
+ done, pending = yield from asyncio.wait([b, a], timeout=0.11,
loop=loop)
self.assertEqual(done, set([a]))
self.assertEqual(pending, set([b]))
- loop.run_until_complete(tasks.Task(foo(), loop=loop))
+ loop.run_until_complete(asyncio.Task(foo(), loop=loop))
self.assertAlmostEqual(0.11, loop.time())
# move forward to close generator
loop.advance_time(10)
- loop.run_until_complete(tasks.wait([a, b], loop=loop))
+ loop.run_until_complete(asyncio.wait([a, b], loop=loop))
def test_wait_concurrent_complete(self):
@@ -686,11 +684,11 @@ class TaskTests(unittest.TestCase):
loop = test_utils.TestLoop(gen)
self.addCleanup(loop.close)
- a = tasks.Task(tasks.sleep(0.1, loop=loop), loop=loop)
- b = tasks.Task(tasks.sleep(0.15, loop=loop), loop=loop)
+ a = asyncio.Task(asyncio.sleep(0.1, loop=loop), loop=loop)
+ b = asyncio.Task(asyncio.sleep(0.15, loop=loop), loop=loop)
done, pending = loop.run_until_complete(
- tasks.wait([b, a], timeout=0.1, loop=loop))
+ asyncio.wait([b, a], timeout=0.1, loop=loop))
self.assertEqual(done, set([a]))
self.assertEqual(pending, set([b]))
@@ -698,7 +696,7 @@ class TaskTests(unittest.TestCase):
# move forward to close generator
loop.advance_time(10)
- loop.run_until_complete(tasks.wait([a, b], loop=loop))
+ loop.run_until_complete(asyncio.wait([a, b], loop=loop))
def test_as_completed(self):
@@ -713,10 +711,10 @@ class TaskTests(unittest.TestCase):
completed = set()
time_shifted = False
- @tasks.coroutine
+ @asyncio.coroutine
def sleeper(dt, x):
nonlocal time_shifted
- yield from tasks.sleep(dt, loop=loop)
+ yield from asyncio.sleep(dt, loop=loop)
completed.add(x)
if not time_shifted and 'a' in completed and 'b' in completed:
time_shifted = True
@@ -727,21 +725,21 @@ class TaskTests(unittest.TestCase):
b = sleeper(0.01, 'b')
c = sleeper(0.15, 'c')
- @tasks.coroutine
+ @asyncio.coroutine
def foo():
values = []
- for f in tasks.as_completed([b, c, a], loop=loop):
+ for f in asyncio.as_completed([b, c, a], loop=loop):
values.append((yield from f))
return values
- res = loop.run_until_complete(tasks.Task(foo(), loop=loop))
+ res = loop.run_until_complete(asyncio.Task(foo(), loop=loop))
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.
- res = loop.run_until_complete(tasks.Task(foo(), loop=loop))
+ res = loop.run_until_complete(asyncio.Task(foo(), loop=loop))
self.assertAlmostEqual(0.15, loop.time())
def test_as_completed_with_timeout(self):
@@ -760,30 +758,30 @@ class TaskTests(unittest.TestCase):
loop = test_utils.TestLoop(gen)
self.addCleanup(loop.close)
- a = tasks.sleep(0.1, 'a', loop=loop)
- b = tasks.sleep(0.15, 'b', loop=loop)
+ a = asyncio.sleep(0.1, 'a', loop=loop)
+ b = asyncio.sleep(0.15, 'b', loop=loop)
- @tasks.coroutine
+ @asyncio.coroutine
def foo():
values = []
- for f in tasks.as_completed([a, b], timeout=0.12, loop=loop):
+ for f in asyncio.as_completed([a, b], timeout=0.12, loop=loop):
try:
v = yield from f
values.append((1, v))
- except futures.TimeoutError as exc:
+ except asyncio.TimeoutError as exc:
values.append((2, exc))
return values
- res = loop.run_until_complete(tasks.Task(foo(), loop=loop))
+ res = loop.run_until_complete(asyncio.Task(foo(), loop=loop))
self.assertEqual(len(res), 2, res)
self.assertEqual(res[0], (1, 'a'))
self.assertEqual(res[1][0], 2)
- self.assertIsInstance(res[1][1], futures.TimeoutError)
+ self.assertIsInstance(res[1][1], asyncio.TimeoutError)
self.assertAlmostEqual(0.12, loop.time())
# move forward to close generator
loop.advance_time(10)
- loop.run_until_complete(tasks.wait([a, b], loop=loop))
+ loop.run_until_complete(asyncio.wait([a, b], loop=loop))
def test_as_completed_reverse_wait(self):
@@ -795,10 +793,10 @@ class TaskTests(unittest.TestCase):
loop = test_utils.TestLoop(gen)
self.addCleanup(loop.close)
- a = tasks.sleep(0.05, 'a', loop=loop)
- b = tasks.sleep(0.10, 'b', loop=loop)
+ a = asyncio.sleep(0.05, 'a', loop=loop)
+ b = asyncio.sleep(0.10, 'b', loop=loop)
fs = {a, b}
- futs = list(tasks.as_completed(fs, loop=loop))
+ futs = list(asyncio.as_completed(fs, loop=loop))
self.assertEqual(len(futs), 2)
x = loop.run_until_complete(futs[1])
@@ -821,12 +819,12 @@ class TaskTests(unittest.TestCase):
loop = test_utils.TestLoop(gen)
self.addCleanup(loop.close)
- a = tasks.sleep(0.05, 'a', loop=loop)
- b = tasks.sleep(0.05, 'b', loop=loop)
+ a = asyncio.sleep(0.05, 'a', loop=loop)
+ b = asyncio.sleep(0.05, 'b', loop=loop)
fs = {a, b}
- futs = list(tasks.as_completed(fs, loop=loop))
+ futs = list(asyncio.as_completed(fs, loop=loop))
self.assertEqual(len(futs), 2)
- waiter = tasks.wait(futs, loop=loop)
+ waiter = asyncio.wait(futs, loop=loop)
done, pending = loop.run_until_complete(waiter)
self.assertEqual(set(f.result() for f in done), {'a', 'b'})
@@ -842,13 +840,13 @@ class TaskTests(unittest.TestCase):
loop = test_utils.TestLoop(gen)
self.addCleanup(loop.close)
- @tasks.coroutine
+ @asyncio.coroutine
def sleeper(dt, arg):
- yield from tasks.sleep(dt/2, loop=loop)
- res = yield from tasks.sleep(dt/2, arg, loop=loop)
+ yield from asyncio.sleep(dt/2, loop=loop)
+ res = yield from asyncio.sleep(dt/2, arg, loop=loop)
return res
- t = tasks.Task(sleeper(0.1, 'yeah'), loop=loop)
+ t = asyncio.Task(sleeper(0.1, 'yeah'), loop=loop)
loop.run_until_complete(t)
self.assertTrue(t.done())
self.assertEqual(t.result(), 'yeah')
@@ -864,7 +862,7 @@ class TaskTests(unittest.TestCase):
loop = test_utils.TestLoop(gen)
self.addCleanup(loop.close)
- t = tasks.Task(tasks.sleep(10.0, 'yeah', loop=loop),
+ t = asyncio.Task(asyncio.sleep(10.0, 'yeah', loop=loop),
loop=loop)
handle = None
@@ -898,19 +896,19 @@ class TaskTests(unittest.TestCase):
sleepfut = None
- @tasks.coroutine
+ @asyncio.coroutine
def sleep(dt):
nonlocal sleepfut
- sleepfut = tasks.sleep(dt, loop=loop)
+ sleepfut = asyncio.sleep(dt, loop=loop)
yield from sleepfut
- @tasks.coroutine
+ @asyncio.coroutine
def doit():
- sleeper = tasks.Task(sleep(5000), loop=loop)
+ sleeper = asyncio.Task(sleep(5000), loop=loop)
loop.call_later(0.1, sleeper.cancel)
try:
yield from sleeper
- except futures.CancelledError:
+ except asyncio.CancelledError:
return 'cancelled'
else:
return 'slept in'
@@ -920,37 +918,37 @@ class TaskTests(unittest.TestCase):
self.assertAlmostEqual(0.1, loop.time())
def test_task_cancel_waiter_future(self):
- fut = futures.Future(loop=self.loop)
+ fut = asyncio.Future(loop=self.loop)
- @tasks.coroutine
+ @asyncio.coroutine
def coro():
yield from fut
- task = tasks.Task(coro(), loop=self.loop)
+ task = asyncio.Task(coro(), loop=self.loop)
test_utils.run_briefly(self.loop)
self.assertIs(task._fut_waiter, fut)
task.cancel()
test_utils.run_briefly(self.loop)
self.assertRaises(
- futures.CancelledError, self.loop.run_until_complete, task)
+ asyncio.CancelledError, self.loop.run_until_complete, task)
self.assertIsNone(task._fut_waiter)
self.assertTrue(fut.cancelled())
def test_step_in_completed_task(self):
- @tasks.coroutine
+ @asyncio.coroutine
def notmuch():
return 'ko'
gen = notmuch()
- task = tasks.Task(gen, loop=self.loop)
+ task = asyncio.Task(gen, loop=self.loop)
task.set_result('ok')
self.assertRaises(AssertionError, task._step)
gen.close()
def test_step_result(self):
- @tasks.coroutine
+ @asyncio.coroutine
def notmuch():
yield None
yield 1
@@ -962,7 +960,7 @@ class TaskTests(unittest.TestCase):
def test_step_result_future(self):
# If coroutine returns future, task waits on this future.
- class Fut(futures.Future):
+ class Fut(asyncio.Future):
def __init__(self, *args, **kwds):
self.cb_added = False
super().__init__(*args, **kwds)
@@ -974,12 +972,12 @@ class TaskTests(unittest.TestCase):
fut = Fut(loop=self.loop)
result = None
- @tasks.coroutine
+ @asyncio.coroutine
def wait_for_future():
nonlocal result
result = yield from fut
- t = tasks.Task(wait_for_future(), loop=self.loop)
+ t = asyncio.Task(wait_for_future(), loop=self.loop)
test_utils.run_briefly(self.loop)
self.assertTrue(fut.cb_added)
@@ -991,11 +989,11 @@ class TaskTests(unittest.TestCase):
self.assertIsNone(t.result())
def test_step_with_baseexception(self):
- @tasks.coroutine
+ @asyncio.coroutine
def notmutch():
raise BaseException()
- task = tasks.Task(notmutch(), loop=self.loop)
+ task = asyncio.Task(notmutch(), loop=self.loop)
self.assertRaises(BaseException, task._step)
self.assertTrue(task.done())
@@ -1011,20 +1009,20 @@ class TaskTests(unittest.TestCase):
loop = test_utils.TestLoop(gen)
self.addCleanup(loop.close)
- @tasks.coroutine
+ @asyncio.coroutine
def sleeper():
- yield from tasks.sleep(10, loop=loop)
+ yield from asyncio.sleep(10, loop=loop)
base_exc = BaseException()
- @tasks.coroutine
+ @asyncio.coroutine
def notmutch():
try:
yield from sleeper()
- except futures.CancelledError:
+ except asyncio.CancelledError:
raise base_exc
- task = tasks.Task(notmutch(), loop=loop)
+ task = asyncio.Task(notmutch(), loop=loop)
test_utils.run_briefly(loop)
task.cancel()
@@ -1040,21 +1038,21 @@ class TaskTests(unittest.TestCase):
def fn():
pass
- self.assertFalse(tasks.iscoroutinefunction(fn))
+ self.assertFalse(asyncio.iscoroutinefunction(fn))
def fn1():
yield
- self.assertFalse(tasks.iscoroutinefunction(fn1))
+ self.assertFalse(asyncio.iscoroutinefunction(fn1))
- @tasks.coroutine
+ @asyncio.coroutine
def fn2():
yield
- self.assertTrue(tasks.iscoroutinefunction(fn2))
+ self.assertTrue(asyncio.iscoroutinefunction(fn2))
def test_yield_vs_yield_from(self):
- fut = futures.Future(loop=self.loop)
+ fut = asyncio.Future(loop=self.loop)
- @tasks.coroutine
+ @asyncio.coroutine
def wait_for_future():
yield fut
@@ -1065,11 +1063,11 @@ class TaskTests(unittest.TestCase):
self.assertFalse(fut.done())
def test_yield_vs_yield_from_generator(self):
- @tasks.coroutine
+ @asyncio.coroutine
def coro():
yield
- @tasks.coroutine
+ @asyncio.coroutine
def wait_for_future():
gen = coro()
try:
@@ -1083,72 +1081,72 @@ class TaskTests(unittest.TestCase):
self.loop.run_until_complete, task)
def test_coroutine_non_gen_function(self):
- @tasks.coroutine
+ @asyncio.coroutine
def func():
return 'test'
- self.assertTrue(tasks.iscoroutinefunction(func))
+ self.assertTrue(asyncio.iscoroutinefunction(func))
coro = func()
- self.assertTrue(tasks.iscoroutine(coro))
+ self.assertTrue(asyncio.iscoroutine(coro))
res = self.loop.run_until_complete(coro)
self.assertEqual(res, 'test')
def test_coroutine_non_gen_function_return_future(self):
- fut = futures.Future(loop=self.loop)
+ fut = asyncio.Future(loop=self.loop)
- @tasks.coroutine
+ @asyncio.coroutine
def func():
return fut
- @tasks.coroutine
+ @asyncio.coroutine
def coro():
fut.set_result('test')
- t1 = tasks.Task(func(), loop=self.loop)
- t2 = tasks.Task(coro(), loop=self.loop)
+ t1 = asyncio.Task(func(), loop=self.loop)
+ t2 = asyncio.Task(coro(), loop=self.loop)
res = self.loop.run_until_complete(t1)
self.assertEqual(res, 'test')
self.assertIsNone(t2.result())
def test_current_task(self):
- self.assertIsNone(tasks.Task.current_task(loop=self.loop))
+ self.assertIsNone(asyncio.Task.current_task(loop=self.loop))
- @tasks.coroutine
+ @asyncio.coroutine
def coro(loop):
- self.assertTrue(tasks.Task.current_task(loop=loop) is task)
+ self.assertTrue(asyncio.Task.current_task(loop=loop) is task)
- task = tasks.Task(coro(self.loop), loop=self.loop)
+ task = asyncio.Task(coro(self.loop), loop=self.loop)
self.loop.run_until_complete(task)
- self.assertIsNone(tasks.Task.current_task(loop=self.loop))
+ self.assertIsNone(asyncio.Task.current_task(loop=self.loop))
def test_current_task_with_interleaving_tasks(self):
- self.assertIsNone(tasks.Task.current_task(loop=self.loop))
+ self.assertIsNone(asyncio.Task.current_task(loop=self.loop))
- fut1 = futures.Future(loop=self.loop)
- fut2 = futures.Future(loop=self.loop)
+ fut1 = asyncio.Future(loop=self.loop)
+ fut2 = asyncio.Future(loop=self.loop)
- @tasks.coroutine
+ @asyncio.coroutine
def coro1(loop):
- self.assertTrue(tasks.Task.current_task(loop=loop) is task1)
+ self.assertTrue(asyncio.Task.current_task(loop=loop) is task1)
yield from fut1
- self.assertTrue(tasks.Task.current_task(loop=loop) is task1)
+ self.assertTrue(asyncio.Task.current_task(loop=loop) is task1)
fut2.set_result(True)
- @tasks.coroutine
+ @asyncio.coroutine
def coro2(loop):
- self.assertTrue(tasks.Task.current_task(loop=loop) is task2)
+ self.assertTrue(asyncio.Task.current_task(loop=loop) is task2)
fut1.set_result(True)
yield from fut2
- self.assertTrue(tasks.Task.current_task(loop=loop) is task2)
+ self.assertTrue(asyncio.Task.current_task(loop=loop) is task2)
- task1 = tasks.Task(coro1(self.loop), loop=self.loop)
- task2 = tasks.Task(coro2(self.loop), loop=self.loop)
+ task1 = asyncio.Task(coro1(self.loop), loop=self.loop)
+ task2 = asyncio.Task(coro2(self.loop), loop=self.loop)
- self.loop.run_until_complete(tasks.wait((task1, task2),
+ self.loop.run_until_complete(asyncio.wait((task1, task2),
loop=self.loop))
- self.assertIsNone(tasks.Task.current_task(loop=self.loop))
+ self.assertIsNone(asyncio.Task.current_task(loop=self.loop))
# Some thorough tests for cancellation propagation through
# coroutines, tasks and wait().
@@ -1156,30 +1154,30 @@ class TaskTests(unittest.TestCase):
def test_yield_future_passes_cancel(self):
# Cancelling outer() cancels inner() cancels waiter.
proof = 0
- waiter = futures.Future(loop=self.loop)
+ waiter = asyncio.Future(loop=self.loop)
- @tasks.coroutine
+ @asyncio.coroutine
def inner():
nonlocal proof
try:
yield from waiter
- except futures.CancelledError:
+ except asyncio.CancelledError:
proof += 1
raise
else:
self.fail('got past sleep() in inner()')
- @tasks.coroutine
+ @asyncio.coroutine
def outer():
nonlocal proof
try:
yield from inner()
- except futures.CancelledError:
+ except asyncio.CancelledError:
proof += 100 # Expect this path.
else:
proof += 10
- f = tasks.async(outer(), loop=self.loop)
+ f = asyncio.async(outer(), loop=self.loop)
test_utils.run_briefly(self.loop)
f.cancel()
self.loop.run_until_complete(f)
@@ -1190,39 +1188,39 @@ class TaskTests(unittest.TestCase):
# Cancelling outer() makes wait() return early, leaves inner()
# running.
proof = 0
- waiter = futures.Future(loop=self.loop)
+ waiter = asyncio.Future(loop=self.loop)
- @tasks.coroutine
+ @asyncio.coroutine
def inner():
nonlocal proof
yield from waiter
proof += 1
- @tasks.coroutine
+ @asyncio.coroutine
def outer():
nonlocal proof
- d, p = yield from tasks.wait([inner()], loop=self.loop)
+ d, p = yield from asyncio.wait([inner()], loop=self.loop)
proof += 100
- f = tasks.async(outer(), loop=self.loop)
+ f = asyncio.async(outer(), loop=self.loop)
test_utils.run_briefly(self.loop)
f.cancel()
self.assertRaises(
- futures.CancelledError, self.loop.run_until_complete, f)
+ asyncio.CancelledError, self.loop.run_until_complete, f)
waiter.set_result(None)
test_utils.run_briefly(self.loop)
self.assertEqual(proof, 1)
def test_shield_result(self):
- inner = futures.Future(loop=self.loop)
- outer = tasks.shield(inner)
+ inner = asyncio.Future(loop=self.loop)
+ outer = asyncio.shield(inner)
inner.set_result(42)
res = self.loop.run_until_complete(outer)
self.assertEqual(res, 42)
def test_shield_exception(self):
- inner = futures.Future(loop=self.loop)
- outer = tasks.shield(inner)
+ inner = asyncio.Future(loop=self.loop)
+ outer = asyncio.shield(inner)
test_utils.run_briefly(self.loop)
exc = RuntimeError('expected')
inner.set_exception(exc)
@@ -1230,50 +1228,50 @@ class TaskTests(unittest.TestCase):
self.assertIs(outer.exception(), exc)
def test_shield_cancel(self):
- inner = futures.Future(loop=self.loop)
- outer = tasks.shield(inner)
+ inner = asyncio.Future(loop=self.loop)
+ outer = asyncio.shield(inner)
test_utils.run_briefly(self.loop)
inner.cancel()
test_utils.run_briefly(self.loop)
self.assertTrue(outer.cancelled())
def test_shield_shortcut(self):
- fut = futures.Future(loop=self.loop)
+ fut = asyncio.Future(loop=self.loop)
fut.set_result(42)
- res = self.loop.run_until_complete(tasks.shield(fut))
+ res = self.loop.run_until_complete(asyncio.shield(fut))
self.assertEqual(res, 42)
def test_shield_effect(self):
# Cancelling outer() does not affect inner().
proof = 0
- waiter = futures.Future(loop=self.loop)
+ waiter = asyncio.Future(loop=self.loop)
- @tasks.coroutine
+ @asyncio.coroutine
def inner():
nonlocal proof
yield from waiter
proof += 1
- @tasks.coroutine
+ @asyncio.coroutine
def outer():
nonlocal proof
- yield from tasks.shield(inner(), loop=self.loop)
+ yield from asyncio.shield(inner(), loop=self.loop)
proof += 100
- f = tasks.async(outer(), loop=self.loop)
+ f = asyncio.async(outer(), loop=self.loop)
test_utils.run_briefly(self.loop)
f.cancel()
- with self.assertRaises(futures.CancelledError):
+ with self.assertRaises(asyncio.CancelledError):
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):
- child1 = futures.Future(loop=self.loop)
- child2 = futures.Future(loop=self.loop)
- parent = tasks.gather(child1, child2, loop=self.loop)
- outer = tasks.shield(parent, loop=self.loop)
+ 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)
test_utils.run_briefly(self.loop)
outer.cancel()
test_utils.run_briefly(self.loop)
@@ -1284,16 +1282,16 @@ class TaskTests(unittest.TestCase):
self.assertEqual(parent.result(), [1, 2])
def test_gather_shield(self):
- child1 = futures.Future(loop=self.loop)
- child2 = futures.Future(loop=self.loop)
- inner1 = tasks.shield(child1, loop=self.loop)
- inner2 = tasks.shield(child2, loop=self.loop)
- parent = tasks.gather(inner1, inner2, loop=self.loop)
+ 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)
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)
- self.assertIsInstance(parent.exception(), futures.CancelledError)
+ self.assertIsInstance(parent.exception(), asyncio.CancelledError)
self.assertTrue(inner1.cancelled())
self.assertTrue(inner2.cancelled())
child1.set_result(1)
@@ -1316,8 +1314,8 @@ class GatherTestsBase:
test_utils.run_briefly(loop)
def _check_success(self, **kwargs):
- a, b, c = [futures.Future(loop=self.one_loop) for i in range(3)]
- fut = tasks.gather(*self.wrap_futures(a, b, c), **kwargs)
+ a, b, c = [asyncio.Future(loop=self.one_loop) for i in range(3)]
+ fut = asyncio.gather(*self.wrap_futures(a, b, c), **kwargs)
cb = Mock()
fut.add_done_callback(cb)
b.set_result(1)
@@ -1338,8 +1336,8 @@ class GatherTestsBase:
self._check_success(return_exceptions=True)
def test_one_exception(self):
- a, b, c, d, e = [futures.Future(loop=self.one_loop) for i in range(5)]
- fut = tasks.gather(*self.wrap_futures(a, b, c, d, e))
+ 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))
cb = Mock()
fut.add_done_callback(cb)
exc = ZeroDivisionError()
@@ -1356,8 +1354,8 @@ class GatherTestsBase:
e.exception()
def test_return_exceptions(self):
- a, b, c, d = [futures.Future(loop=self.one_loop) for i in range(4)]
- fut = tasks.gather(*self.wrap_futures(a, b, c, d),
+ 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),
return_exceptions=True)
cb = Mock()
fut.add_done_callback(cb)
@@ -1381,15 +1379,15 @@ class FutureGatherTests(GatherTestsBase, unittest.TestCase):
return futures
def _check_empty_sequence(self, seq_or_iter):
- events.set_event_loop(self.one_loop)
- self.addCleanup(events.set_event_loop, None)
- fut = tasks.gather(*seq_or_iter)
- self.assertIsInstance(fut, futures.Future)
+ 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)
self.assertIs(fut._loop, self.one_loop)
self._run_loop(self.one_loop)
self.assertTrue(fut.done())
self.assertEqual(fut.result(), [])
- fut = tasks.gather(*seq_or_iter, loop=self.other_loop)
+ fut = asyncio.gather(*seq_or_iter, loop=self.other_loop)
self.assertIs(fut._loop, self.other_loop)
def test_constructor_empty_sequence(self):
@@ -1399,27 +1397,27 @@ class FutureGatherTests(GatherTestsBase, unittest.TestCase):
self._check_empty_sequence(iter(""))
def test_constructor_heterogenous_futures(self):
- fut1 = futures.Future(loop=self.one_loop)
- fut2 = futures.Future(loop=self.other_loop)
+ fut1 = asyncio.Future(loop=self.one_loop)
+ fut2 = asyncio.Future(loop=self.other_loop)
with self.assertRaises(ValueError):
- tasks.gather(fut1, fut2)
+ asyncio.gather(fut1, fut2)
with self.assertRaises(ValueError):
- tasks.gather(fut1, loop=self.other_loop)
+ asyncio.gather(fut1, loop=self.other_loop)
def test_constructor_homogenous_futures(self):
- children = [futures.Future(loop=self.other_loop) for i in range(3)]
- fut = tasks.gather(*children)
+ children = [asyncio.Future(loop=self.other_loop) for i in range(3)]
+ fut = asyncio.gather(*children)
self.assertIs(fut._loop, self.other_loop)
self._run_loop(self.other_loop)
self.assertFalse(fut.done())
- fut = tasks.gather(*children, loop=self.other_loop)
+ fut = asyncio.gather(*children, loop=self.other_loop)
self.assertIs(fut._loop, self.other_loop)
self._run_loop(self.other_loop)
self.assertFalse(fut.done())
def test_one_cancellation(self):
- a, b, c, d, e = [futures.Future(loop=self.one_loop) for i in range(5)]
- fut = tasks.gather(a, b, c, d, e)
+ a, b, c, d, e = [asyncio.Future(loop=self.one_loop) for i in range(5)]
+ fut = asyncio.gather(a, b, c, d, e)
cb = Mock()
fut.add_done_callback(cb)
a.set_result(1)
@@ -1428,7 +1426,7 @@ class FutureGatherTests(GatherTestsBase, unittest.TestCase):
self.assertTrue(fut.done())
cb.assert_called_once_with(fut)
self.assertFalse(fut.cancelled())
- self.assertIsInstance(fut.exception(), futures.CancelledError)
+ self.assertIsInstance(fut.exception(), asyncio.CancelledError)
# Does nothing
c.set_result(3)
d.cancel()
@@ -1436,9 +1434,9 @@ class FutureGatherTests(GatherTestsBase, unittest.TestCase):
e.exception()
def test_result_exception_one_cancellation(self):
- a, b, c, d, e, f = [futures.Future(loop=self.one_loop)
+ a, b, c, d, e, f = [asyncio.Future(loop=self.one_loop)
for i in range(6)]
- fut = tasks.gather(a, b, c, d, e, f, return_exceptions=True)
+ fut = asyncio.gather(a, b, c, d, e, f, return_exceptions=True)
cb = Mock()
fut.add_done_callback(cb)
a.set_result(1)
@@ -1452,8 +1450,8 @@ class FutureGatherTests(GatherTestsBase, unittest.TestCase):
rte = RuntimeError()
f.set_exception(rte)
res = self.one_loop.run_until_complete(fut)
- self.assertIsInstance(res[2], futures.CancelledError)
- self.assertIsInstance(res[4], futures.CancelledError)
+ self.assertIsInstance(res[2], asyncio.CancelledError)
+ self.assertIsInstance(res[4], asyncio.CancelledError)
res[2] = res[4] = None
self.assertEqual(res, [1, zde, None, 3, None, rte])
cb.assert_called_once_with(fut)
@@ -1463,34 +1461,34 @@ class CoroutineGatherTests(GatherTestsBase, unittest.TestCase):
def setUp(self):
super().setUp()
- events.set_event_loop(self.one_loop)
+ asyncio.set_event_loop(self.one_loop)
def tearDown(self):
- events.set_event_loop(None)
+ asyncio.set_event_loop(None)
super().tearDown()
def wrap_futures(self, *futures):
coros = []
for fut in futures:
- @tasks.coroutine
+ @asyncio.coroutine
def coro(fut=fut):
return (yield from fut)
coros.append(coro())
return coros
def test_constructor_loop_selection(self):
- @tasks.coroutine
+ @asyncio.coroutine
def coro():
return 'abc'
gen1 = coro()
gen2 = coro()
- fut = tasks.gather(gen1, gen2)
+ fut = asyncio.gather(gen1, gen2)
self.assertIs(fut._loop, self.one_loop)
gen1.close()
gen2.close()
gen3 = coro()
gen4 = coro()
- fut = tasks.gather(gen3, gen4, loop=self.other_loop)
+ fut = asyncio.gather(gen3, gen4, loop=self.other_loop)
self.assertIs(fut._loop, self.other_loop)
gen3.close()
gen4.close()
@@ -1498,29 +1496,29 @@ class CoroutineGatherTests(GatherTestsBase, unittest.TestCase):
def test_cancellation_broadcast(self):
# Cancelling outer() cancels all children.
proof = 0
- waiter = futures.Future(loop=self.one_loop)
+ waiter = asyncio.Future(loop=self.one_loop)
- @tasks.coroutine
+ @asyncio.coroutine
def inner():
nonlocal proof
yield from waiter
proof += 1
- child1 = tasks.async(inner(), loop=self.one_loop)
- child2 = tasks.async(inner(), loop=self.one_loop)
+ child1 = asyncio.async(inner(), loop=self.one_loop)
+ child2 = asyncio.async(inner(), loop=self.one_loop)
gatherer = None
- @tasks.coroutine
+ @asyncio.coroutine
def outer():
nonlocal proof, gatherer
- gatherer = tasks.gather(child1, child2, loop=self.one_loop)
+ gatherer = asyncio.gather(child1, child2, loop=self.one_loop)
yield from gatherer
proof += 100
- f = tasks.async(outer(), loop=self.one_loop)
+ f = asyncio.async(outer(), loop=self.one_loop)
test_utils.run_briefly(self.one_loop)
self.assertTrue(f.cancel())
- with self.assertRaises(futures.CancelledError):
+ with self.assertRaises(asyncio.CancelledError):
self.one_loop.run_until_complete(f)
self.assertFalse(gatherer.cancel())
self.assertTrue(waiter.cancelled())
@@ -1532,19 +1530,19 @@ class CoroutineGatherTests(GatherTestsBase, unittest.TestCase):
def test_exception_marking(self):
# Test for the first line marked "Mark exception retrieved."
- @tasks.coroutine
+ @asyncio.coroutine
def inner(f):
yield from f
raise RuntimeError('should not be ignored')
- a = futures.Future(loop=self.one_loop)
- b = futures.Future(loop=self.one_loop)
+ a = asyncio.Future(loop=self.one_loop)
+ b = asyncio.Future(loop=self.one_loop)
- @tasks.coroutine
+ @asyncio.coroutine
def outer():
- yield from tasks.gather(inner(a), inner(b), loop=self.one_loop)
+ yield from asyncio.gather(inner(a), inner(b), loop=self.one_loop)
- f = tasks.async(outer(), loop=self.one_loop)
+ f = asyncio.async(outer(), loop=self.one_loop)
test_utils.run_briefly(self.one_loop)
a.set_result(None)
test_utils.run_briefly(self.one_loop)