Debian Codegraph @

Lib/test/test_asyncio/test_tasks.py

raw
1 """Tests for tasks.py."""
2
3 import collections
4 import contextlib
5 import contextvars
6 import gc
7 import io
8 import random
9 import re
10 import sys
11 import traceback
12 import types
13 import unittest
14 from unittest import mock
15 from types import GenericAlias
16
17 import asyncio
18 from asyncio import futures
19 from asyncio import tasks
20 from test.test_asyncio import utils as test_utils
21 from test import support
22 from test.support.script_helper import assert_python_ok
23 from test.support.warnings_helper import ignore_warnings
24
25
26 def tearDownModule():
27 asyncio.events._set_event_loop_policy(None)
28
29
30 async def coroutine_function():
31 pass
32
33
34 def format_coroutine(qualname, state, src, source_traceback, generator=False):
35 if generator:
36 state = '%s' % state
37 else:
38 state = '%s, defined' % state
39 if source_traceback is not None:
40 frame = source_traceback[-1]
41 return ('coro=<%s() %s at %s> created at %s:%s'
42 % (qualname, state, src, frame[0], frame[1]))
43 else:
44 return 'coro=<%s() %s at %s>' % (qualname, state, src)
45
46
47 def get_innermost_context(exc):
48 """
49 Return information about the innermost exception context in the chain.
50 """
51 depth = 0
52 while True:
53 context = exc.__context__
54 if context is None:
55 break
56
57 exc = context
58 depth += 1
59
60 return (type(exc), exc.args, depth)
61
62
63 class Dummy:
64
65 def __repr__(self):
66 return '<Dummy>'
67
68 def __call__(self, *args):
69 pass
70
71
72 class CoroLikeObject:
73 def send(self, v):
74 raise StopIteration(42)
75
76 def throw(self, *exc):
77 pass
78
79 def close(self):
80 pass
81
82 def __await__(self):
83 return self
84
85
86 class BaseTaskTests:
87
88 Task = None
89 Future = None
90 all_tasks = None
91
92 def new_task(self, loop, coro, name='TestTask', context=None, eager_start=None):
93 return self.__class__.Task(coro, loop=loop, name=name, context=context, eager_start=eager_start)
94
95 def new_future(self, loop):
96 return self.__class__.Future(loop=loop)
97
98 def setUp(self):
99 super().setUp()
100 self.loop = self.new_test_loop()
101 self.loop.set_task_factory(self.new_task)
102 self.loop.create_future = lambda: self.new_future(self.loop)
103
104 def test_generic_alias(self):
105 task = self.__class__.Task[str]
106 self.assertEqual(task.__args__, (str,))
107 self.assertIsInstance(task, GenericAlias)
108
109 def test_task_cancel_message_getter(self):
110 async def coro():
111 pass
112 t = self.new_task(self.loop, coro())
113 self.assertHasAttr(t, '_cancel_message')
114 self.assertEqual(t._cancel_message, None)
115
116 t.cancel('my message')
117 self.assertEqual(t._cancel_message, 'my message')
118
119 with self.assertRaises(asyncio.CancelledError) as cm:
120 self.loop.run_until_complete(t)
121
122 self.assertEqual('my message', cm.exception.args[0])
123
124 def test_task_cancel_message_setter(self):
125 async def coro():
126 pass
127 t = self.new_task(self.loop, coro())
128 t.cancel('my message')
129 t._cancel_message = 'my new message'
130 self.assertEqual(t._cancel_message, 'my new message')
131
132 with self.assertRaises(asyncio.CancelledError) as cm:
133 self.loop.run_until_complete(t)
134
135 self.assertEqual('my new message', cm.exception.args[0])
136
137 def test_task_del_collect(self):
138 class Evil:
139 def __del__(self):
140 gc.collect()
141
142 async def run():
143 return Evil()
144
145 self.loop.run_until_complete(
146 asyncio.gather(*[
147 self.new_task(self.loop, run()) for _ in range(100)
148 ]))
149
150 def test_other_loop_future(self):
151 other_loop = asyncio.new_event_loop()
152 fut = self.new_future(other_loop)
153
154 async def run(fut):
155 await fut
156
157 try:
158 with self.assertRaisesRegex(RuntimeError,
159 r'Task .* got Future .* attached'):
160 self.loop.run_until_complete(run(fut))
161 finally:
162 other_loop.close()
163
164 def test_task_awaits_on_itself(self):
165
166 async def test():
167 await task
168
169 task = asyncio.ensure_future(test(), loop=self.loop)
170
171 with self.assertRaisesRegex(RuntimeError,
172 'Task cannot await on itself'):
173 self.loop.run_until_complete(task)
174
175 def test_task_class(self):
176 async def notmuch():
177 return 'ok'
178 t = self.new_task(self.loop, notmuch())
179 self.loop.run_until_complete(t)
180 self.assertTrue(t.done())
181 self.assertEqual(t.result(), 'ok')
182 self.assertIs(t._loop, self.loop)
183 self.assertIs(t.get_loop(), self.loop)
184
185 loop = asyncio.new_event_loop()
186 self.set_event_loop(loop)
187 t = self.new_task(loop, notmuch())
188 self.assertIs(t._loop, loop)
189 loop.run_until_complete(t)
190 loop.close()
191
192 def test_ensure_future_coroutine(self):
193 async def notmuch():
194 return 'ok'
195 t = asyncio.ensure_future(notmuch(), loop=self.loop)
196 self.assertIs(t._loop, self.loop)
197 self.loop.run_until_complete(t)
198 self.assertTrue(t.done())
199 self.assertEqual(t.result(), 'ok')
200
201 a = notmuch()
202 self.addCleanup(a.close)
203 with self.assertRaisesRegex(RuntimeError, 'no current event loop'):
204 asyncio.ensure_future(a)
205
206 async def test():
207 return asyncio.ensure_future(notmuch())
208 t = self.loop.run_until_complete(test())
209 self.assertIs(t._loop, self.loop)
210 self.loop.run_until_complete(t)
211 self.assertTrue(t.done())
212 self.assertEqual(t.result(), 'ok')
213
214 # Deprecated in 3.10, undeprecated in 3.12
215 asyncio.set_event_loop(self.loop)
216 self.addCleanup(asyncio.set_event_loop, None)
217 t = asyncio.ensure_future(notmuch())
218 self.assertIs(t._loop, self.loop)
219 self.loop.run_until_complete(t)
220 self.assertTrue(t.done())
221 self.assertEqual(t.result(), 'ok')
222
223 def test_ensure_future_future(self):
224 f_orig = self.new_future(self.loop)
225 f_orig.set_result('ko')
226
227 f = asyncio.ensure_future(f_orig)
228 self.loop.run_until_complete(f)
229 self.assertTrue(f.done())
230 self.assertEqual(f.result(), 'ko')
231 self.assertIs(f, f_orig)
232
233 loop = asyncio.new_event_loop()
234 self.set_event_loop(loop)
235
236 with self.assertRaises(ValueError):
237 f = asyncio.ensure_future(f_orig, loop=loop)
238
239 loop.close()
240
241 f = asyncio.ensure_future(f_orig, loop=self.loop)
242 self.assertIs(f, f_orig)
243
244 def test_ensure_future_task(self):
245 async def notmuch():
246 return 'ok'
247 t_orig = self.new_task(self.loop, notmuch())
248 t = asyncio.ensure_future(t_orig)
249 self.loop.run_until_complete(t)
250 self.assertTrue(t.done())
251 self.assertEqual(t.result(), 'ok')
252 self.assertIs(t, t_orig)
253
254 loop = asyncio.new_event_loop()
255 self.set_event_loop(loop)
256
257 with self.assertRaises(ValueError):
258 t = asyncio.ensure_future(t_orig, loop=loop)
259
260 loop.close()
261
262 t = asyncio.ensure_future(t_orig, loop=self.loop)
263 self.assertIs(t, t_orig)
264
265 def test_ensure_future_awaitable(self):
266 class Aw:
267 def __init__(self, coro):
268 self.coro = coro
269 def __await__(self):
270 return self.coro.__await__()
271
272 async def coro():
273 return 'ok'
274
275 loop = asyncio.new_event_loop()
276 self.set_event_loop(loop)
277 fut = asyncio.ensure_future(Aw(coro()), loop=loop)
278 loop.run_until_complete(fut)
279 self.assertEqual(fut.result(), 'ok')
280
281 def test_ensure_future_task_awaitable(self):
282 class Aw:
283 def __await__(self):
284 return asyncio.sleep(0, result='ok').__await__()
285
286 loop = asyncio.new_event_loop()
287 self.set_event_loop(loop)
288 task = asyncio.ensure_future(Aw(), loop=loop)
289 loop.run_until_complete(task)
290 self.assertTrue(task.done())
291 self.assertEqual(task.result(), 'ok')
292 self.assertIsInstance(task.get_coro(), types.CoroutineType)
293 loop.close()
294
295 def test_ensure_future_neither(self):
296 with self.assertRaises(TypeError):
297 asyncio.ensure_future('ok')
298
299 def test_ensure_future_error_msg(self):
300 loop = asyncio.new_event_loop()
301 f = self.new_future(self.loop)
302 with self.assertRaisesRegex(ValueError, 'The future belongs to a '
303 'different loop than the one specified as '
304 'the loop argument'):
305 asyncio.ensure_future(f, loop=loop)
306 loop.close()
307
308 def test_get_stack(self):
309 T = None
310
311 async def foo():
312 await bar()
313
314 async def bar():
315 # test get_stack()
316 f = T.get_stack(limit=1)
317 try:
318 self.assertEqual(f[0].f_code.co_name, 'foo')
319 finally:
320 f = None
321
322 # test print_stack()
323 file = io.StringIO()
324 T.print_stack(limit=1, file=file)
325 file.seek(0)
326 tb = file.read()
327 self.assertRegex(tb, r'foo\(\) running')
328
329 async def runner():
330 nonlocal T
331 T = asyncio.ensure_future(foo(), loop=self.loop)
332 await T
333
334 self.loop.run_until_complete(runner())
335
336 def test_task_repr(self):
337 self.loop.set_debug(False)
338
339 async def notmuch():
340 return 'abc'
341
342 # test coroutine function
343 self.assertEqual(notmuch.__name__, 'notmuch')
344 self.assertRegex(notmuch.__qualname__,
345 r'\w+.test_task_repr.<locals>.notmuch')
346 self.assertEqual(notmuch.__module__, __name__)
347
348 filename, lineno = test_utils.get_function_source(notmuch)
349 src = "%s:%s" % (filename, lineno)
350
351 # test coroutine object
352 gen = notmuch()
353 coro_qualname = 'BaseTaskTests.test_task_repr.<locals>.notmuch'
354 self.assertEqual(gen.__name__, 'notmuch')
355 self.assertEqual(gen.__qualname__, coro_qualname)
356
357 # test pending Task
358 t = self.new_task(self.loop, gen)
359 t.add_done_callback(Dummy())
360
361 coro = format_coroutine(coro_qualname, 'running', src,
362 t._source_traceback, generator=True)
363 self.assertEqual(repr(t),
364 "<Task pending name='TestTask' %s cb=[<Dummy>()]>" % coro)
365
366 # test cancelling Task
367 t.cancel() # Does not take immediate effect!
368 self.assertEqual(repr(t),
369 "<Task cancelling name='TestTask' %s cb=[<Dummy>()]>" % coro)
370
371 # test cancelled Task
372 self.assertRaises(asyncio.CancelledError,
373 self.loop.run_until_complete, t)
374 coro = format_coroutine(coro_qualname, 'done', src,
375 t._source_traceback)
376 self.assertEqual(repr(t),
377 "<Task cancelled name='TestTask' %s>" % coro)
378
379 # test finished Task
380 t = self.new_task(self.loop, notmuch())
381 self.loop.run_until_complete(t)
382 coro = format_coroutine(coro_qualname, 'done', src,
383 t._source_traceback)
384 self.assertEqual(repr(t),
385 "<Task finished name='TestTask' %s result='abc'>" % coro)
386
387 def test_task_repr_autogenerated(self):
388 async def notmuch():
389 return 123
390
391 t1 = self.new_task(self.loop, notmuch(), None)
392 t2 = self.new_task(self.loop, notmuch(), None)
393 self.assertNotEqual(repr(t1), repr(t2))
394
395 match1 = re.match(r"^<Task pending name='Task-(\d+)'", repr(t1))
396 self.assertIsNotNone(match1)
397 match2 = re.match(r"^<Task pending name='Task-(\d+)'", repr(t2))
398 self.assertIsNotNone(match2)
399
400 # Autogenerated task names should have monotonically increasing numbers
401 self.assertLess(int(match1.group(1)), int(match2.group(1)))
402 self.loop.run_until_complete(t1)
403 self.loop.run_until_complete(t2)
404
405 def test_task_set_name_pylong(self):
406 # test that setting the task name to a PyLong explicitly doesn't
407 # incorrectly trigger the deferred name formatting logic
408 async def notmuch():
409 return 123
410
411 t = self.new_task(self.loop, notmuch(), name=987654321)
412 self.assertEqual(t.get_name(), '987654321')
413 t.set_name(123456789)
414 self.assertEqual(t.get_name(), '123456789')
415 self.loop.run_until_complete(t)
416
417 def test_task_repr_name_not_str(self):
418 async def notmuch():
419 return 123
420
421 t = self.new_task(self.loop, notmuch())
422 t.set_name({6})
423 self.assertEqual(t.get_name(), '{6}')
424 self.loop.run_until_complete(t)
425
426 def test_task_repr_wait_for(self):
427 self.loop.set_debug(False)
428
429 async def wait_for(fut):
430 return await fut
431
432 fut = self.new_future(self.loop)
433 task = self.new_task(self.loop, wait_for(fut))
434 test_utils.run_briefly(self.loop)
435 self.assertRegex(repr(task),
436 '<Task .* wait_for=%s>' % re.escape(repr(fut)))
437
438 fut.set_result(None)
439 self.loop.run_until_complete(task)
440
441 def test_task_basics(self):
442
443 async def outer():
444 a = await inner1()
445 b = await inner2()
446 return a+b
447
448 async def inner1():
449 return 42
450
451 async def inner2():
452 return 1000
453
454 t = outer()
455 self.assertEqual(self.loop.run_until_complete(t), 1042)
456
457 def test_exception_chaining_after_await(self):
458 # Test that when awaiting on a task when an exception is already
459 # active, if the task raises an exception it will be chained
460 # with the original.
461 loop = asyncio.new_event_loop()
462 self.set_event_loop(loop)
463
464 async def raise_error():
465 raise ValueError
466
467 async def run():
468 try:
469 raise KeyError(3)
470 except Exception as exc:
471 task = self.new_task(loop, raise_error())
472 try:
473 await task
474 except Exception as exc:
475 self.assertEqual(type(exc), ValueError)
476 chained = exc.__context__
477 self.assertEqual((type(chained), chained.args),
478 (KeyError, (3,)))
479
480 try:
481 task = self.new_task(loop, run())
482 loop.run_until_complete(task)
483 finally:
484 loop.close()
485
486 def test_exception_chaining_after_await_with_context_cycle(self):
487 # Check trying to create an exception context cycle:
488 # https://bugs.python.org/issue40696
489 has_cycle = None
490 loop = asyncio.new_event_loop()
491 self.set_event_loop(loop)
492
493 async def process_exc(exc):
494 raise exc
495
496 async def run():
497 nonlocal has_cycle
498 try:
499 raise KeyError('a')
500 except Exception as exc:
501 task = self.new_task(loop, process_exc(exc))
502 try:
503 await task
504 except BaseException as exc:
505 has_cycle = (exc is exc.__context__)
506 # Prevent a hang if has_cycle is True.
507 exc.__context__ = None
508
509 try:
510 task = self.new_task(loop, run())
511 loop.run_until_complete(task)
512 finally:
513 loop.close()
514 # This also distinguishes from the initial has_cycle=None.
515 self.assertEqual(has_cycle, False)
516
517
518 def test_cancelling(self):
519 loop = asyncio.new_event_loop()
520
521 async def task():
522 await asyncio.sleep(10)
523
524 try:
525 t = self.new_task(loop, task())
526 self.assertFalse(t.cancelling())
527 self.assertNotIn(" cancelling ", repr(t))
528 self.assertTrue(t.cancel())
529 self.assertTrue(t.cancelling())
530 self.assertIn(" cancelling ", repr(t))
531
532 # Since we commented out two lines from Task.cancel(),
533 # this t.cancel() call now returns True.
534 # self.assertFalse(t.cancel())
535 self.assertTrue(t.cancel())
536
537 with self.assertRaises(asyncio.CancelledError):
538 loop.run_until_complete(t)
539 finally:
540 loop.close()
541
542 def test_uncancel_basic(self):
543 loop = asyncio.new_event_loop()
544
545 async def task():
546 try:
547 await asyncio.sleep(10)
548 except asyncio.CancelledError:
549 self.current_task().uncancel()
550 await asyncio.sleep(10)
551
552 try:
553 t = self.new_task(loop, task())
554 loop.run_until_complete(asyncio.sleep(0.01))
555
556 # Cancel first sleep
557 self.assertTrue(t.cancel())
558 self.assertIn(" cancelling ", repr(t))
559 self.assertEqual(t.cancelling(), 1)
560 self.assertFalse(t.cancelled()) # Task is still not complete
561 loop.run_until_complete(asyncio.sleep(0.01))
562
563 # after .uncancel()
564 self.assertNotIn(" cancelling ", repr(t))
565 self.assertEqual(t.cancelling(), 0)
566 self.assertFalse(t.cancelled()) # Task is still not complete
567
568 # Cancel second sleep
569 self.assertTrue(t.cancel())
570 self.assertEqual(t.cancelling(), 1)
571 self.assertFalse(t.cancelled()) # Task is still not complete
572 with self.assertRaises(asyncio.CancelledError):
573 loop.run_until_complete(t)
574 self.assertTrue(t.cancelled()) # Finally, task complete
575 self.assertTrue(t.done())
576
577 # uncancel is no longer effective after the task is complete
578 t.uncancel()
579 self.assertTrue(t.cancelled())
580 self.assertTrue(t.done())
581 finally:
582 loop.close()
583
584 def test_uncancel_structured_blocks(self):
585 # This test recreates the following high-level structure using uncancel()::
586 #
587 # async def make_request_with_timeout():
588 # try:
589 # async with asyncio.timeout(1):
590 # # Structured block affected by the timeout:
591 # await make_request()
592 # await make_another_request()
593 # except TimeoutError:
594 # pass # There was a timeout
595 # # Outer code not affected by the timeout:
596 # await unrelated_code()
597
598 loop = asyncio.new_event_loop()
599
600 async def make_request_with_timeout(*, sleep: float, timeout: float):
601 task = self.current_task()
602 loop = task.get_loop()
603
604 timed_out = False
605 structured_block_finished = False
606 outer_code_reached = False
607
608 def on_timeout():
609 nonlocal timed_out
610 timed_out = True
611 task.cancel()
612
613 timeout_handle = loop.call_later(timeout, on_timeout)
614 try:
615 try:
616 # Structured block affected by the timeout
617 await asyncio.sleep(sleep)
618 structured_block_finished = True
619 finally:
620 timeout_handle.cancel()
621 if (
622 timed_out
623 and task.uncancel() == 0
624 and type(sys.exception()) is asyncio.CancelledError
625 ):
626 # Note the five rules that are needed here to satisfy proper
627 # uncancellation:
628 #
629 # 1. handle uncancellation in a `finally:` block to allow for
630 # plain returns;
631 # 2. our `timed_out` flag is set, meaning that it was our event
632 # that triggered the need to uncancel the task, regardless of
633 # what exception is raised;
634 # 3. we can call `uncancel()` because *we* called `cancel()`
635 # before;
636 # 4. we call `uncancel()` but we only continue converting the
637 # CancelledError to TimeoutError if `uncancel()` caused the
638 # cancellation request count go down to 0. We need to look
639 # at the counter vs having a simple boolean flag because our
640 # code might have been nested (think multiple timeouts). See
641 # commit 7fce1063b6e5a366f8504e039a8ccdd6944625cd for
642 # details.
643 # 5. we only convert CancelledError to TimeoutError; for other
644 # exceptions raised due to the cancellation (like
645 # a ConnectionLostError from a database client), simply
646 # propagate them.
647 #
648 # Those checks need to take place in this exact order to make
649 # sure the `cancelling()` counter always stays in sync.
650 #
651 # Additionally, the original stimulus to `cancel()` the task
652 # needs to be unscheduled to avoid re-cancelling the task later.
653 # Here we do it by cancelling `timeout_handle` in the `finally:`
654 # block.
655 raise TimeoutError
656 except TimeoutError:
657 self.assertTrue(timed_out)
658
659 # Outer code not affected by the timeout:
660 outer_code_reached = True
661 await asyncio.sleep(0)
662 return timed_out, structured_block_finished, outer_code_reached
663
664 try:
665 # Test which timed out.
666 t1 = self.new_task(loop, make_request_with_timeout(sleep=10.0, timeout=0.1))
667 timed_out, structured_block_finished, outer_code_reached = (
668 loop.run_until_complete(t1)
669 )
670 self.assertTrue(timed_out)
671 self.assertFalse(structured_block_finished) # it was cancelled
672 self.assertTrue(outer_code_reached) # task got uncancelled after leaving
673 # the structured block and continued until
674 # completion
675 self.assertEqual(t1.cancelling(), 0) # no pending cancellation of the outer task
676
677 # Test which did not time out.
678 t2 = self.new_task(loop, make_request_with_timeout(sleep=0, timeout=10.0))
679 timed_out, structured_block_finished, outer_code_reached = (
680 loop.run_until_complete(t2)
681 )
682 self.assertFalse(timed_out)
683 self.assertTrue(structured_block_finished)
684 self.assertTrue(outer_code_reached)
685 self.assertEqual(t2.cancelling(), 0)
686 finally:
687 loop.close()
688
689 def test_uncancel_resets_must_cancel(self):
690
691 async def coro():
692 await fut
693 return 42
694
695 loop = asyncio.new_event_loop()
696 fut = asyncio.Future(loop=loop)
697 task = self.new_task(loop, coro())
698 loop.run_until_complete(asyncio.sleep(0)) # Get task waiting for fut
699 fut.set_result(None) # Make task runnable
700 try:
701 task.cancel() # Enter cancelled state
702 self.assertEqual(task.cancelling(), 1)
703 self.assertTrue(task._must_cancel)
704
705 task.uncancel() # Undo cancellation
706 self.assertEqual(task.cancelling(), 0)
707 self.assertFalse(task._must_cancel)
708 finally:
709 res = loop.run_until_complete(task)
710 self.assertEqual(res, 42)
711 loop.close()
712
713 def test_cancel(self):
714
715 def gen():
716 when = yield
717 self.assertAlmostEqual(10.0, when)
718 yield 0
719
720 loop = self.new_test_loop(gen)
721
722 async def task():
723 await asyncio.sleep(10.0)
724 return 12
725
726 t = self.new_task(loop, task())
727 loop.call_soon(t.cancel)
728 with self.assertRaises(asyncio.CancelledError):
729 loop.run_until_complete(t)
730 self.assertTrue(t.done())
731 self.assertTrue(t.cancelled())
732 self.assertFalse(t.cancel())
733
734 def test_cancel_with_message_then_future_result(self):
735 # Test Future.result() after calling cancel() with a message.
736 cases = [
737 ((), ()),
738 ((None,), ()),
739 (('my message',), ('my message',)),
740 # Non-string values should roundtrip.
741 ((5,), (5,)),
742 ]
743 for cancel_args, expected_args in cases:
744 with self.subTest(cancel_args=cancel_args):
745 loop = asyncio.new_event_loop()
746 self.set_event_loop(loop)
747
748 async def sleep():
749 await asyncio.sleep(10)
750
751 async def coro():
752 task = self.new_task(loop, sleep())
753 await asyncio.sleep(0)
754 task.cancel(*cancel_args)
755 done, pending = await asyncio.wait([task])
756 task.result()
757
758 task = self.new_task(loop, coro())
759 with self.assertRaises(asyncio.CancelledError) as cm:
760 loop.run_until_complete(task)
761 exc = cm.exception
762 self.assertEqual(exc.args, expected_args)
763
764 actual = get_innermost_context(exc)
765 self.assertEqual(actual,
766 (asyncio.CancelledError, expected_args, 0))
767
768 def test_cancel_with_message_then_future_exception(self):
769 # Test Future.exception() after calling cancel() with a message.
770 cases = [
771 ((), ()),
772 ((None,), ()),
773 (('my message',), ('my message',)),
774 # Non-string values should roundtrip.
775 ((5,), (5,)),
776 ]
777 for cancel_args, expected_args in cases:
778 with self.subTest(cancel_args=cancel_args):
779 loop = asyncio.new_event_loop()
780 self.set_event_loop(loop)
781
782 async def sleep():
783 await asyncio.sleep(10)
784
785 async def coro():
786 task = self.new_task(loop, sleep())
787 await asyncio.sleep(0)
788 task.cancel(*cancel_args)
789 done, pending = await asyncio.wait([task])
790 task.exception()
791
792 task = self.new_task(loop, coro())
793 with self.assertRaises(asyncio.CancelledError) as cm:
794 loop.run_until_complete(task)
795 exc = cm.exception
796 self.assertEqual(exc.args, expected_args)
797
798 actual = get_innermost_context(exc)
799 self.assertEqual(actual,
800 (asyncio.CancelledError, expected_args, 0))
801
802 def test_cancellation_exception_context(self):
803 loop = asyncio.new_event_loop()
804 self.set_event_loop(loop)
805 fut = loop.create_future()
806
807 async def sleep():
808 fut.set_result(None)
809 await asyncio.sleep(10)
810
811 async def coro():
812 inner_task = self.new_task(loop, sleep())
813 await fut
814 loop.call_soon(inner_task.cancel, 'msg')
815 try:
816 await inner_task
817 except asyncio.CancelledError as ex:
818 raise ValueError("cancelled") from ex
819
820 task = self.new_task(loop, coro())
821 with self.assertRaises(ValueError) as cm:
822 loop.run_until_complete(task)
823 exc = cm.exception
824 self.assertEqual(exc.args, ('cancelled',))
825
826 actual = get_innermost_context(exc)
827 self.assertEqual(actual,
828 (asyncio.CancelledError, ('msg',), 1))
829
830 def test_cancel_with_message_before_starting_task(self):
831 loop = asyncio.new_event_loop()
832 self.set_event_loop(loop)
833
834 async def sleep():
835 await asyncio.sleep(10)
836
837 async def coro():
838 task = self.new_task(loop, sleep())
839 # We deliberately leave out the sleep here.
840 task.cancel('my message')
841 done, pending = await asyncio.wait([task])
842 task.exception()
843
844 task = self.new_task(loop, coro())
845 with self.assertRaises(asyncio.CancelledError) as cm:
846 loop.run_until_complete(task)
847 exc = cm.exception
848 self.assertEqual(exc.args, ('my message',))
849
850 actual = get_innermost_context(exc)
851 self.assertEqual(actual,
852 (asyncio.CancelledError, ('my message',), 0))
853
854 def test_cancel_yield(self):
855 async def task():
856 await asyncio.sleep(0)
857 await asyncio.sleep(0)
858 return 12
859
860 t = self.new_task(self.loop, task())
861 test_utils.run_briefly(self.loop) # start coro
862 t.cancel()
863 self.assertRaises(
864 asyncio.CancelledError, self.loop.run_until_complete, t)
865 self.assertTrue(t.done())
866 self.assertTrue(t.cancelled())
867 self.assertFalse(t.cancel())
868
869 def test_cancel_inner_future(self):
870 f = self.new_future(self.loop)
871
872 async def task():
873 await f
874 return 12
875
876 t = self.new_task(self.loop, task())
877 test_utils.run_briefly(self.loop) # start task
878 f.cancel()
879 with self.assertRaises(asyncio.CancelledError):
880 self.loop.run_until_complete(t)
881 self.assertTrue(f.cancelled())
882 self.assertTrue(t.cancelled())
883
884 def test_cancel_both_task_and_inner_future(self):
885 f = self.new_future(self.loop)
886
887 async def task():
888 await f
889 return 12
890
891 t = self.new_task(self.loop, task())
892 test_utils.run_briefly(self.loop)
893
894 f.cancel()
895 t.cancel()
896
897 with self.assertRaises(asyncio.CancelledError):
898 self.loop.run_until_complete(t)
899
900 self.assertTrue(t.done())
901 self.assertTrue(f.cancelled())
902 self.assertTrue(t.cancelled())
903
904 def test_cancel_task_catching(self):
905 fut1 = self.new_future(self.loop)
906 fut2 = self.new_future(self.loop)
907
908 async def task():
909 await fut1
910 try:
911 await fut2
912 except asyncio.CancelledError:
913 return 42
914
915 t = self.new_task(self.loop, task())
916 test_utils.run_briefly(self.loop)
917 self.assertIs(t._fut_waiter, fut1) # White-box test.
918 fut1.set_result(None)
919 test_utils.run_briefly(self.loop)
920 self.assertIs(t._fut_waiter, fut2) # White-box test.
921 t.cancel()
922 self.assertTrue(fut2.cancelled())
923 res = self.loop.run_until_complete(t)
924 self.assertEqual(res, 42)
925 self.assertFalse(t.cancelled())
926
927 def test_cancel_task_ignoring(self):
928 fut1 = self.new_future(self.loop)
929 fut2 = self.new_future(self.loop)
930 fut3 = self.new_future(self.loop)
931
932 async def task():
933 await fut1
934 try:
935 await fut2
936 except asyncio.CancelledError:
937 pass
938 res = await fut3
939 return res
940
941 t = self.new_task(self.loop, task())
942 test_utils.run_briefly(self.loop)
943 self.assertIs(t._fut_waiter, fut1) # White-box test.
944 fut1.set_result(None)
945 test_utils.run_briefly(self.loop)
946 self.assertIs(t._fut_waiter, fut2) # White-box test.
947 t.cancel()
948 self.assertTrue(fut2.cancelled())
949 test_utils.run_briefly(self.loop)
950 self.assertIs(t._fut_waiter, fut3) # White-box test.
951 fut3.set_result(42)
952 res = self.loop.run_until_complete(t)
953 self.assertEqual(res, 42)
954 self.assertFalse(fut3.cancelled())
955 self.assertFalse(t.cancelled())
956
957 def test_cancel_current_task(self):
958 loop = asyncio.new_event_loop()
959 self.set_event_loop(loop)
960
961 async def task():
962 t.cancel()
963 self.assertTrue(t._must_cancel) # White-box test.
964 # The sleep should be cancelled immediately.
965 await asyncio.sleep(100)
966 return 12
967
968 t = self.new_task(loop, task())
969 self.assertFalse(t.cancelled())
970 self.assertRaises(
971 asyncio.CancelledError, loop.run_until_complete, t)
972 self.assertTrue(t.done())
973 self.assertTrue(t.cancelled())
974 self.assertFalse(t._must_cancel) # White-box test.
975 self.assertFalse(t.cancel())
976
977 def test_cancel_at_end(self):
978 """coroutine end right after task is cancelled"""
979 loop = asyncio.new_event_loop()
980 self.set_event_loop(loop)
981
982 async def task():
983 t.cancel()
984 self.assertTrue(t._must_cancel) # White-box test.
985 return 12
986
987 t = self.new_task(loop, task())
988 self.assertFalse(t.cancelled())
989 self.assertRaises(
990 asyncio.CancelledError, loop.run_until_complete, t)
991 self.assertTrue(t.done())
992 self.assertTrue(t.cancelled())
993 self.assertFalse(t._must_cancel) # White-box test.
994 self.assertFalse(t.cancel())
995
996 def test_cancel_awaited_task(self):
997 # This tests for a relatively rare condition when
998 # a task cancellation is requested for a task which is not
999 # currently blocked, such as a task cancelling itself.
1000 # In this situation we must ensure that whatever next future
1001 # or task the cancelled task blocks on is cancelled correctly
1002 # as well. See also bpo-34872.
1003 loop = asyncio.new_event_loop()
1004 self.addCleanup(lambda: loop.close())
1005
1006 task = nested_task = None
1007 fut = self.new_future(loop)
1008
1009 async def nested():
1010 await fut
1011
1012 async def coro():
1013 nonlocal nested_task
1014 # Create a sub-task and wait for it to run.
1015 nested_task = self.new_task(loop, nested())
1016 await asyncio.sleep(0)
1017
1018 # Request the current task to be cancelled.
1019 task.cancel()
1020 # Block on the nested task, which should be immediately
1021 # cancelled.
1022 await nested_task
1023
1024 task = self.new_task(loop, coro())
1025 with self.assertRaises(asyncio.CancelledError):
1026 loop.run_until_complete(task)
1027
1028 self.assertTrue(task.cancelled())
1029 self.assertTrue(nested_task.cancelled())
1030 self.assertTrue(fut.cancelled())
1031
1032 def assert_text_contains(self, text, substr):
1033 if substr not in text:
1034 raise RuntimeError(f'text {substr!r} not found in:\n>>>{text}<<<')
1035
1036 def test_cancel_traceback_for_future_result(self):
1037 # When calling Future.result() on a cancelled task, check that the
1038 # line of code that was interrupted is included in the traceback.
1039 loop = asyncio.new_event_loop()
1040 self.set_event_loop(loop)
1041
1042 async def nested():
1043 # This will get cancelled immediately.
1044 await asyncio.sleep(10)
1045
1046 async def coro():
1047 task = self.new_task(loop, nested())
1048 await asyncio.sleep(0)
1049 task.cancel()
1050 await task # search target
1051
1052 task = self.new_task(loop, coro())
1053 try:
1054 loop.run_until_complete(task)
1055 except asyncio.CancelledError:
1056 tb = traceback.format_exc()
1057 self.assert_text_contains(tb, "await asyncio.sleep(10)")
1058 # The intermediate await should also be included.
1059 self.assert_text_contains(tb, "await task # search target")
1060 else:
1061 self.fail('CancelledError did not occur')
1062
1063 def test_cancel_traceback_for_future_exception(self):
1064 # When calling Future.exception() on a cancelled task, check that the
1065 # line of code that was interrupted is included in the traceback.
1066 loop = asyncio.new_event_loop()
1067 self.set_event_loop(loop)
1068
1069 async def nested():
1070 # This will get cancelled immediately.
1071 await asyncio.sleep(10)
1072
1073 async def coro():
1074 task = self.new_task(loop, nested())
1075 await asyncio.sleep(0)
1076 task.cancel()
1077 done, pending = await asyncio.wait([task])
1078 task.exception() # search target
1079
1080 task = self.new_task(loop, coro())
1081 try:
1082 loop.run_until_complete(task)
1083 except asyncio.CancelledError:
1084 tb = traceback.format_exc()
1085 self.assert_text_contains(tb, "await asyncio.sleep(10)")
1086 # The intermediate await should also be included.
1087 self.assert_text_contains(tb,
1088 "task.exception() # search target")
1089 else:
1090 self.fail('CancelledError did not occur')
1091
1092 def test_stop_while_run_in_complete(self):
1093
1094 def gen():
1095 when = yield
1096 self.assertAlmostEqual(0.1, when)
1097 when = yield 0.1
1098 self.assertAlmostEqual(0.2, when)
1099 when = yield 0.1
1100 self.assertAlmostEqual(0.3, when)
1101 yield 0.1
1102
1103 loop = self.new_test_loop(gen)
1104
1105 x = 0
1106
1107 async def task():
1108 nonlocal x
1109 while x < 10:
1110 await asyncio.sleep(0.1)
1111 x += 1
1112 if x == 2:
1113 loop.stop()
1114
1115 t = self.new_task(loop, task())
1116 with self.assertRaises(RuntimeError) as cm:
1117 loop.run_until_complete(t)
1118 self.assertEqual(str(cm.exception),
1119 'Event loop stopped before Future completed.')
1120 self.assertFalse(t.done())
1121 self.assertEqual(x, 2)
1122 self.assertAlmostEqual(0.3, loop.time())
1123
1124 t.cancel()
1125 self.assertRaises(asyncio.CancelledError, loop.run_until_complete, t)
1126
1127 def test_log_traceback(self):
1128 async def coro():
1129 pass
1130
1131 task = self.new_task(self.loop, coro())
1132 with self.assertRaisesRegex(ValueError, 'can only be set to False'):
1133 task._log_traceback = True
1134 self.loop.run_until_complete(task)
1135
1136 def test_wait(self):
1137
1138 def gen():
1139 when = yield
1140 self.assertAlmostEqual(0.1, when)
1141 when = yield 0
1142 self.assertAlmostEqual(0.15, when)
1143 yield 0.15
1144
1145 loop = self.new_test_loop(gen)
1146
1147 a = self.new_task(loop, asyncio.sleep(0.1))
1148 b = self.new_task(loop, asyncio.sleep(0.15))
1149
1150 async def foo():
1151 done, pending = await asyncio.wait([b, a])
1152 self.assertEqual(done, set([a, b]))
1153 self.assertEqual(pending, set())
1154 return 42
1155
1156 res = loop.run_until_complete(self.new_task(loop, foo()))
1157 self.assertEqual(res, 42)
1158 self.assertAlmostEqual(0.15, loop.time())
1159
1160 # Doing it again should take no time and exercise a different path.
1161 res = loop.run_until_complete(self.new_task(loop, foo()))
1162 self.assertAlmostEqual(0.15, loop.time())
1163 self.assertEqual(res, 42)
1164
1165 def test_wait_duplicate_coroutines(self):
1166
1167 async def coro(s):
1168 return s
1169 c = self.loop.create_task(coro('test'))
1170 task = self.new_task(
1171 self.loop,
1172 asyncio.wait([c, c, self.loop.create_task(coro('spam'))]))
1173
1174 done, pending = self.loop.run_until_complete(task)
1175
1176 self.assertFalse(pending)
1177 self.assertEqual(set(f.result() for f in done), {'test', 'spam'})
1178
1179 def test_wait_errors(self):
1180 self.assertRaises(
1181 ValueError, self.loop.run_until_complete,
1182 asyncio.wait(set()))
1183
1184 # -1 is an invalid return_when value
1185 sleep_coro = asyncio.sleep(10.0)
1186 wait_coro = asyncio.wait([sleep_coro], return_when=-1)
1187 self.assertRaises(ValueError,
1188 self.loop.run_until_complete, wait_coro)
1189
1190 sleep_coro.close()
1191
1192 def test_wait_first_completed(self):
1193
1194 def gen():
1195 when = yield
1196 self.assertAlmostEqual(10.0, when)
1197 when = yield 0
1198 self.assertAlmostEqual(0.1, when)
1199 yield 0.1
1200
1201 loop = self.new_test_loop(gen)
1202
1203 a = self.new_task(loop, asyncio.sleep(10.0))
1204 b = self.new_task(loop, asyncio.sleep(0.1))
1205 task = self.new_task(
1206 loop,
1207 asyncio.wait([b, a], return_when=asyncio.FIRST_COMPLETED))
1208
1209 done, pending = loop.run_until_complete(task)
1210 self.assertEqual({b}, done)
1211 self.assertEqual({a}, pending)
1212 self.assertFalse(a.done())
1213 self.assertTrue(b.done())
1214 self.assertIsNone(b.result())
1215 self.assertAlmostEqual(0.1, loop.time())
1216
1217 # move forward to close generator
1218 loop.advance_time(10)
1219 loop.run_until_complete(asyncio.wait([a, b]))
1220
1221 def test_wait_really_done(self):
1222 # there is possibility that some tasks in the pending list
1223 # became done but their callbacks haven't all been called yet
1224
1225 async def coro1():
1226 await asyncio.sleep(0)
1227
1228 async def coro2():
1229 await asyncio.sleep(0)
1230 await asyncio.sleep(0)
1231
1232 a = self.new_task(self.loop, coro1())
1233 b = self.new_task(self.loop, coro2())
1234 task = self.new_task(
1235 self.loop,
1236 asyncio.wait([b, a], return_when=asyncio.FIRST_COMPLETED))
1237
1238 done, pending = self.loop.run_until_complete(task)
1239 self.assertEqual({a, b}, done)
1240 self.assertTrue(a.done())
1241 self.assertIsNone(a.result())
1242 self.assertTrue(b.done())
1243 self.assertIsNone(b.result())
1244
1245 def test_wait_first_exception(self):
1246
1247 def gen():
1248 when = yield
1249 self.assertAlmostEqual(10.0, when)
1250 yield 0
1251
1252 loop = self.new_test_loop(gen)
1253
1254 # first_exception, task already has exception
1255 a = self.new_task(loop, asyncio.sleep(10.0))
1256
1257 async def exc():
1258 raise ZeroDivisionError('err')
1259
1260 b = self.new_task(loop, exc())
1261 task = self.new_task(
1262 loop,
1263 asyncio.wait([b, a], return_when=asyncio.FIRST_EXCEPTION))
1264
1265 done, pending = loop.run_until_complete(task)
1266 self.assertEqual({b}, done)
1267 self.assertEqual({a}, pending)
1268 self.assertAlmostEqual(0, loop.time())
1269
1270 # move forward to close generator
1271 loop.advance_time(10)
1272 loop.run_until_complete(asyncio.wait([a, b]))
1273
1274 def test_wait_first_exception_in_wait(self):
1275
1276 def gen():
1277 when = yield
1278 self.assertAlmostEqual(10.0, when)
1279 when = yield 0
1280 self.assertAlmostEqual(0.01, when)
1281 yield 0.01
1282
1283 loop = self.new_test_loop(gen)
1284
1285 # first_exception, exception during waiting
1286 a = self.new_task(loop, asyncio.sleep(10.0))
1287
1288 async def exc():
1289 await asyncio.sleep(0.01)
1290 raise ZeroDivisionError('err')
1291
1292 b = self.new_task(loop, exc())
1293 task = asyncio.wait([b, a], return_when=asyncio.FIRST_EXCEPTION)
1294
1295 done, pending = loop.run_until_complete(task)
1296 self.assertEqual({b}, done)
1297 self.assertEqual({a}, pending)
1298 self.assertAlmostEqual(0.01, loop.time())
1299
1300 # move forward to close generator
1301 loop.advance_time(10)
1302 loop.run_until_complete(asyncio.wait([a, b]))
1303
1304 def test_wait_with_exception(self):
1305
1306 def gen():
1307 when = yield
1308 self.assertAlmostEqual(0.1, when)
1309 when = yield 0
1310 self.assertAlmostEqual(0.15, when)
1311 yield 0.15
1312
1313 loop = self.new_test_loop(gen)
1314
1315 a = self.new_task(loop, asyncio.sleep(0.1))
1316
1317 async def sleeper():
1318 await asyncio.sleep(0.15)
1319 raise ZeroDivisionError('really')
1320
1321 b = self.new_task(loop, sleeper())
1322
1323 async def foo():
1324 done, pending = await asyncio.wait([b, a])
1325 self.assertEqual(len(done), 2)
1326 self.assertEqual(pending, set())
1327 errors = set(f for f in done if f.exception() is not None)
1328 self.assertEqual(len(errors), 1)
1329
1330 loop.run_until_complete(self.new_task(loop, foo()))
1331 self.assertAlmostEqual(0.15, loop.time())
1332
1333 loop.run_until_complete(self.new_task(loop, foo()))
1334 self.assertAlmostEqual(0.15, loop.time())
1335
1336 def test_wait_with_timeout(self):
1337
1338 def gen():
1339 when = yield
1340 self.assertAlmostEqual(0.1, when)
1341 when = yield 0
1342 self.assertAlmostEqual(0.15, when)
1343 when = yield 0
1344 self.assertAlmostEqual(0.11, when)
1345 yield 0.11
1346
1347 loop = self.new_test_loop(gen)
1348
1349 a = self.new_task(loop, asyncio.sleep(0.1))
1350 b = self.new_task(loop, asyncio.sleep(0.15))
1351
1352 async def foo():
1353 done, pending = await asyncio.wait([b, a], timeout=0.11)
1354 self.assertEqual(done, set([a]))
1355 self.assertEqual(pending, set([b]))
1356
1357 loop.run_until_complete(self.new_task(loop, foo()))
1358 self.assertAlmostEqual(0.11, loop.time())
1359
1360 # move forward to close generator
1361 loop.advance_time(10)
1362 loop.run_until_complete(asyncio.wait([a, b]))
1363
1364 def test_wait_concurrent_complete(self):
1365
1366 def gen():
1367 when = yield
1368 self.assertAlmostEqual(0.1, when)
1369 when = yield 0
1370 self.assertAlmostEqual(0.15, when)
1371 when = yield 0
1372 self.assertAlmostEqual(0.1, when)
1373 yield 0.1
1374
1375 loop = self.new_test_loop(gen)
1376
1377 a = self.new_task(loop, asyncio.sleep(0.1))
1378 b = self.new_task(loop, asyncio.sleep(0.15))
1379
1380 done, pending = loop.run_until_complete(
1381 asyncio.wait([b, a], timeout=0.1))
1382
1383 self.assertEqual(done, set([a]))
1384 self.assertEqual(pending, set([b]))
1385 self.assertAlmostEqual(0.1, loop.time())
1386
1387 # move forward to close generator
1388 loop.advance_time(10)
1389 loop.run_until_complete(asyncio.wait([a, b]))
1390
1391 def test_wait_with_iterator_of_tasks(self):
1392
1393 def gen():
1394 when = yield
1395 self.assertAlmostEqual(0.1, when)
1396 when = yield 0
1397 self.assertAlmostEqual(0.15, when)
1398 yield 0.15
1399
1400 loop = self.new_test_loop(gen)
1401
1402 a = self.new_task(loop, asyncio.sleep(0.1))
1403 b = self.new_task(loop, asyncio.sleep(0.15))
1404
1405 async def foo():
1406 done, pending = await asyncio.wait(iter([b, a]))
1407 self.assertEqual(done, set([a, b]))
1408 self.assertEqual(pending, set())
1409 return 42
1410
1411 res = loop.run_until_complete(self.new_task(loop, foo()))
1412 self.assertEqual(res, 42)
1413 self.assertAlmostEqual(0.15, loop.time())
1414
1415
1416 def test_wait_generator(self):
1417 async def func(a):
1418 return a
1419
1420 loop = self.new_test_loop()
1421
1422 async def main():
1423 tasks = (self.new_task(loop, func(i)) for i in range(10))
1424 done, pending = await asyncio.wait(tasks, return_when=asyncio.ALL_COMPLETED)
1425 self.assertEqual(len(done), 10)
1426 self.assertEqual(len(pending), 0)
1427
1428 loop.run_until_complete(main())
1429
1430
1431 def test_as_completed(self):
1432
1433 def gen():
1434 yield 0
1435 yield 0
1436 yield 0.01
1437 yield 0
1438
1439 async def sleeper(dt, x):
1440 nonlocal time_shifted
1441 await asyncio.sleep(dt)
1442 completed.add(x)
1443 if not time_shifted and 'a' in completed and 'b' in completed:
1444 time_shifted = True
1445 loop.advance_time(0.14)
1446 return x
1447
1448 async def try_iterator(awaitables):
1449 values = []
1450 for f in asyncio.as_completed(awaitables):
1451 values.append(await f)
1452 return values
1453
1454 async def try_async_iterator(awaitables):
1455 values = []
1456 async for f in asyncio.as_completed(awaitables):
1457 values.append(await f)
1458 return values
1459
1460 for foo in try_iterator, try_async_iterator:
1461 with self.subTest(method=foo.__name__):
1462 loop = self.new_test_loop(gen)
1463 # disable "slow callback" warning
1464 loop.slow_callback_duration = 1.0
1465
1466 completed = set()
1467 time_shifted = False
1468
1469 a = sleeper(0.01, 'a')
1470 b = sleeper(0.01, 'b')
1471 c = sleeper(0.15, 'c')
1472
1473 res = loop.run_until_complete(self.new_task(loop, foo([b, c, a])))
1474 self.assertAlmostEqual(0.15, loop.time())
1475 self.assertTrue('a' in res[:2])
1476 self.assertTrue('b' in res[:2])
1477 self.assertEqual(res[2], 'c')
1478
1479 def test_as_completed_same_tasks_in_as_out(self):
1480 # Ensures that asynchronously iterating as_completed's iterator
1481 # yields awaitables are the same awaitables that were passed in when
1482 # those awaitables are futures.
1483 async def try_async_iterator(awaitables):
1484 awaitables_out = set()
1485 async for out_aw in asyncio.as_completed(awaitables):
1486 awaitables_out.add(out_aw)
1487 return awaitables_out
1488
1489 async def coro(i):
1490 return i
1491
1492 with contextlib.closing(asyncio.new_event_loop()) as loop:
1493 # Coroutines shouldn't be yielded back as finished coroutines
1494 # can't be re-used.
1495 awaitables_in = frozenset(
1496 (coro(0), coro(1), coro(2), coro(3))
1497 )
1498 awaitables_out = loop.run_until_complete(
1499 try_async_iterator(awaitables_in)
1500 )
1501 if awaitables_in - awaitables_out != awaitables_in:
1502 raise self.failureException('Got original coroutines '
1503 'out of as_completed iterator.')
1504
1505 # Tasks should be yielded back.
1506 coro_obj_a = coro('a')
1507 task_b = loop.create_task(coro('b'))
1508 coro_obj_c = coro('c')
1509 task_d = loop.create_task(coro('d'))
1510 awaitables_in = frozenset(
1511 (coro_obj_a, task_b, coro_obj_c, task_d)
1512 )
1513 awaitables_out = loop.run_until_complete(
1514 try_async_iterator(awaitables_in)
1515 )
1516 if awaitables_in & awaitables_out != {task_b, task_d}:
1517 raise self.failureException('Only tasks should be yielded '
1518 'from as_completed iterator '
1519 'as-is.')
1520
1521 def test_as_completed_with_timeout(self):
1522
1523 def gen():
1524 yield
1525 yield 0
1526 yield 0
1527 yield 0.1
1528
1529 async def try_iterator():
1530 values = []
1531 for f in asyncio.as_completed([a, b], timeout=0.12):
1532 if values:
1533 loop.advance_time(0.02)
1534 try:
1535 v = await f
1536 values.append((1, v))
1537 except asyncio.TimeoutError as exc:
1538 values.append((2, exc))
1539 return values
1540
1541 async def try_async_iterator():
1542 values = []
1543 try:
1544 async for f in asyncio.as_completed([a, b], timeout=0.12):
1545 v = await f
1546 values.append((1, v))
1547 loop.advance_time(0.02)
1548 except asyncio.TimeoutError as exc:
1549 values.append((2, exc))
1550 return values
1551
1552 for foo in try_iterator, try_async_iterator:
1553 with self.subTest(method=foo.__name__):
1554 loop = self.new_test_loop(gen)
1555 a = loop.create_task(asyncio.sleep(0.1, 'a'))
1556 b = loop.create_task(asyncio.sleep(0.15, 'b'))
1557
1558 res = loop.run_until_complete(self.new_task(loop, foo()))
1559 self.assertEqual(len(res), 2, res)
1560 self.assertEqual(res[0], (1, 'a'))
1561 self.assertEqual(res[1][0], 2)
1562 self.assertIsInstance(res[1][1], asyncio.TimeoutError)
1563 self.assertAlmostEqual(0.12, loop.time())
1564
1565 # move forward to close generator
1566 loop.advance_time(10)
1567 loop.run_until_complete(asyncio.wait([a, b]))
1568
1569 def test_as_completed_with_unused_timeout(self):
1570
1571 def gen():
1572 yield
1573 yield 0
1574 yield 0.01
1575
1576 async def try_iterator():
1577 for f in asyncio.as_completed([a], timeout=1):
1578 v = await f
1579 self.assertEqual(v, 'a')
1580
1581 async def try_async_iterator():
1582 async for f in asyncio.as_completed([a], timeout=1):
1583 v = await f
1584 self.assertEqual(v, 'a')
1585
1586 for foo in try_iterator, try_async_iterator:
1587 with self.subTest(method=foo.__name__):
1588 a = asyncio.sleep(0.01, 'a')
1589 loop = self.new_test_loop(gen)
1590 loop.run_until_complete(self.new_task(loop, foo()))
1591 loop.close()
1592
1593 def test_as_completed_resume_iterator(self):
1594 # Test that as_completed returns an iterator that can be resumed
1595 # the next time iteration is performed (i.e. if __iter__ is called
1596 # again)
1597 async def try_iterator(awaitables):
1598 iterations = 0
1599 iterator = asyncio.as_completed(awaitables)
1600 collected = []
1601 for f in iterator:
1602 collected.append(await f)
1603 iterations += 1
1604 if iterations == 2:
1605 break
1606 self.assertEqual(len(collected), 2)
1607
1608 # Resume same iterator:
1609 for f in iterator:
1610 collected.append(await f)
1611 return collected
1612
1613 async def try_async_iterator(awaitables):
1614 iterations = 0
1615 iterator = asyncio.as_completed(awaitables)
1616 collected = []
1617 async for f in iterator:
1618 collected.append(await f)
1619 iterations += 1
1620 if iterations == 2:
1621 break
1622 self.assertEqual(len(collected), 2)
1623
1624 # Resume same iterator:
1625 async for f in iterator:
1626 collected.append(await f)
1627 return collected
1628
1629 async def coro(i):
1630 return i
1631
1632 with contextlib.closing(asyncio.new_event_loop()) as loop:
1633 for foo in try_iterator, try_async_iterator:
1634 with self.subTest(method=foo.__name__):
1635 results = loop.run_until_complete(
1636 foo((coro(0), coro(1), coro(2), coro(3)))
1637 )
1638 self.assertCountEqual(results, (0, 1, 2, 3))
1639
1640 def test_as_completed_reverse_wait(self):
1641 # Tests the plain iterator style of as_completed iteration to
1642 # ensure that the first future awaited resolves to the first
1643 # completed awaitable from the set we passed in, even if it wasn't
1644 # the first future generated by as_completed.
1645 def gen():
1646 yield 0
1647 yield 0.05
1648 yield 0
1649
1650 loop = self.new_test_loop(gen)
1651
1652 a = asyncio.sleep(0.05, 'a')
1653 b = asyncio.sleep(0.10, 'b')
1654 fs = {a, b}
1655
1656 async def test():
1657 futs = list(asyncio.as_completed(fs))
1658 self.assertEqual(len(futs), 2)
1659
1660 x = await futs[1]
1661 self.assertEqual(x, 'a')
1662 self.assertAlmostEqual(0.05, loop.time())
1663 loop.advance_time(0.05)
1664 y = await futs[0]
1665 self.assertEqual(y, 'b')
1666 self.assertAlmostEqual(0.10, loop.time())
1667
1668 loop.run_until_complete(test())
1669
1670 def test_as_completed_concurrent(self):
1671 # Ensure that more than one future or coroutine yielded from
1672 # as_completed can be awaited concurrently.
1673 def gen():
1674 when = yield
1675 self.assertAlmostEqual(0.05, when)
1676 when = yield 0
1677 self.assertAlmostEqual(0.05, when)
1678 yield 0.05
1679
1680 async def try_iterator(fs):
1681 return list(asyncio.as_completed(fs))
1682
1683 async def try_async_iterator(fs):
1684 return [f async for f in asyncio.as_completed(fs)]
1685
1686 for runner in try_iterator, try_async_iterator:
1687 with self.subTest(method=runner.__name__):
1688 a = asyncio.sleep(0.05, 'a')
1689 b = asyncio.sleep(0.05, 'b')
1690 fs = {a, b}
1691
1692 async def test():
1693 futs = await runner(fs)
1694 self.assertEqual(len(futs), 2)
1695 done, pending = await asyncio.wait(
1696 [asyncio.ensure_future(fut) for fut in futs]
1697 )
1698 self.assertEqual(set(f.result() for f in done), {'a', 'b'})
1699
1700 loop = self.new_test_loop(gen)
1701 loop.run_until_complete(test())
1702
1703 def test_as_completed_duplicate_coroutines(self):
1704
1705 async def coro(s):
1706 return s
1707
1708 async def try_iterator():
1709 result = []
1710 c = coro('ham')
1711 for f in asyncio.as_completed([c, c, coro('spam')]):
1712 result.append(await f)
1713 return result
1714
1715 async def try_async_iterator():
1716 result = []
1717 c = coro('ham')
1718 async for f in asyncio.as_completed([c, c, coro('spam')]):
1719 result.append(await f)
1720 return result
1721
1722 for runner in try_iterator, try_async_iterator:
1723 with self.subTest(method=runner.__name__):
1724 fut = self.new_task(self.loop, runner())
1725 self.loop.run_until_complete(fut)
1726 result = fut.result()
1727 self.assertEqual(set(result), {'ham', 'spam'})
1728 self.assertEqual(len(result), 2)
1729
1730 def test_as_completed_coroutine_without_loop(self):
1731 async def coro():
1732 return 42
1733
1734 a = coro()
1735 self.addCleanup(a.close)
1736
1737 with self.assertRaisesRegex(RuntimeError, 'no current event loop'):
1738 futs = asyncio.as_completed([a])
1739 list(futs)
1740
1741 def test_as_completed_coroutine_use_running_loop(self):
1742 loop = self.new_test_loop()
1743
1744 async def coro():
1745 return 42
1746
1747 async def test():
1748 futs = list(asyncio.as_completed([coro()]))
1749 self.assertEqual(len(futs), 1)
1750 self.assertEqual(await futs[0], 42)
1751
1752 loop.run_until_complete(test())
1753
1754 def test_sleep(self):
1755
1756 def gen():
1757 when = yield
1758 self.assertAlmostEqual(0.05, when)
1759 when = yield 0.05
1760 self.assertAlmostEqual(0.1, when)
1761 yield 0.05
1762
1763 loop = self.new_test_loop(gen)
1764
1765 async def sleeper(dt, arg):
1766 await asyncio.sleep(dt/2)
1767 res = await asyncio.sleep(dt/2, arg)
1768 return res
1769
1770 t = self.new_task(loop, sleeper(0.1, 'yeah'))
1771 loop.run_until_complete(t)
1772 self.assertTrue(t.done())
1773 self.assertEqual(t.result(), 'yeah')
1774 self.assertAlmostEqual(0.1, loop.time())
1775
1776 def test_sleep_when_delay_is_nan(self):
1777
1778 def gen():
1779 yield
1780
1781 loop = self.new_test_loop(gen)
1782
1783 async def sleeper():
1784 await asyncio.sleep(float("nan"))
1785
1786 t = self.new_task(loop, sleeper())
1787
1788 with self.assertRaises(ValueError):
1789 loop.run_until_complete(t)
1790
1791 def test_sleep_cancel(self):
1792
1793 def gen():
1794 when = yield
1795 self.assertAlmostEqual(10.0, when)
1796 yield 0
1797
1798 loop = self.new_test_loop(gen)
1799
1800 t = self.new_task(loop, asyncio.sleep(10.0, 'yeah'))
1801
1802 handle = None
1803 orig_call_later = loop.call_later
1804
1805 def call_later(delay, callback, *args):
1806 nonlocal handle
1807 handle = orig_call_later(delay, callback, *args)
1808 return handle
1809
1810 loop.call_later = call_later
1811 test_utils.run_briefly(loop)
1812
1813 self.assertFalse(handle._cancelled)
1814
1815 t.cancel()
1816 test_utils.run_briefly(loop)
1817 self.assertTrue(handle._cancelled)
1818
1819 def test_task_cancel_sleeping_task(self):
1820
1821 def gen():
1822 when = yield
1823 self.assertAlmostEqual(0.1, when)
1824 when = yield 0
1825 self.assertAlmostEqual(5000, when)
1826 yield 0.1
1827
1828 loop = self.new_test_loop(gen)
1829
1830 async def sleep(dt):
1831 await asyncio.sleep(dt)
1832
1833 async def doit():
1834 sleeper = self.new_task(loop, sleep(5000))
1835 loop.call_later(0.1, sleeper.cancel)
1836 try:
1837 await sleeper
1838 except asyncio.CancelledError:
1839 return 'cancelled'
1840 else:
1841 return 'slept in'
1842
1843 doer = doit()
1844 self.assertEqual(loop.run_until_complete(doer), 'cancelled')
1845 self.assertAlmostEqual(0.1, loop.time())
1846
1847 def test_task_cancel_waiter_future(self):
1848 fut = self.new_future(self.loop)
1849
1850 async def coro():
1851 await fut
1852
1853 task = self.new_task(self.loop, coro())
1854 test_utils.run_briefly(self.loop)
1855 self.assertIs(task._fut_waiter, fut)
1856
1857 task.cancel()
1858 test_utils.run_briefly(self.loop)
1859 self.assertRaises(
1860 asyncio.CancelledError, self.loop.run_until_complete, task)
1861 self.assertIsNone(task._fut_waiter)
1862 self.assertTrue(fut.cancelled())
1863
1864 def test_task_set_methods(self):
1865 async def notmuch():
1866 return 'ko'
1867
1868 gen = notmuch()
1869 task = self.new_task(self.loop, gen)
1870
1871 with self.assertRaisesRegex(RuntimeError, 'not support set_result'):
1872 task.set_result('ok')
1873
1874 with self.assertRaisesRegex(RuntimeError, 'not support set_exception'):
1875 task.set_exception(ValueError())
1876
1877 self.assertEqual(
1878 self.loop.run_until_complete(task),
1879 'ko')
1880
1881 def test_step_result_future(self):
1882 # If coroutine returns future, task waits on this future.
1883
1884 class Fut(asyncio.Future):
1885 def __init__(self, *args, **kwds):
1886 self.cb_added = False
1887 super().__init__(*args, **kwds)
1888
1889 def add_done_callback(self, *args, **kwargs):
1890 self.cb_added = True
1891 super().add_done_callback(*args, **kwargs)
1892
1893 fut = Fut(loop=self.loop)
1894 result = None
1895
1896 async def wait_for_future():
1897 nonlocal result
1898 result = await fut
1899
1900 t = self.new_task(self.loop, wait_for_future())
1901 test_utils.run_briefly(self.loop)
1902 self.assertTrue(fut.cb_added)
1903
1904 res = object()
1905 fut.set_result(res)
1906 test_utils.run_briefly(self.loop)
1907 self.assertIs(res, result)
1908 self.assertTrue(t.done())
1909 self.assertIsNone(t.result())
1910
1911 def test_baseexception_during_cancel(self):
1912
1913 def gen():
1914 when = yield
1915 self.assertAlmostEqual(10.0, when)
1916 yield 0
1917
1918 loop = self.new_test_loop(gen)
1919
1920 async def sleeper():
1921 await asyncio.sleep(10)
1922
1923 base_exc = SystemExit()
1924
1925 async def notmutch():
1926 try:
1927 await sleeper()
1928 except asyncio.CancelledError:
1929 raise base_exc
1930
1931 task = self.new_task(loop, notmutch())
1932 test_utils.run_briefly(loop)
1933
1934 task.cancel()
1935 self.assertFalse(task.done())
1936
1937 self.assertRaises(SystemExit, test_utils.run_briefly, loop)
1938
1939 self.assertTrue(task.done())
1940 self.assertFalse(task.cancelled())
1941 self.assertIs(task.exception(), base_exc)
1942
1943 @ignore_warnings(category=DeprecationWarning)
1944 def test_iscoroutinefunction(self):
1945 def fn():
1946 pass
1947
1948 self.assertFalse(asyncio.iscoroutinefunction(fn))
1949
1950 def fn1():
1951 yield
1952 self.assertFalse(asyncio.iscoroutinefunction(fn1))
1953
1954 async def fn2():
1955 pass
1956 self.assertTrue(asyncio.iscoroutinefunction(fn2))
1957
1958 self.assertFalse(asyncio.iscoroutinefunction(mock.Mock()))
1959 self.assertTrue(asyncio.iscoroutinefunction(mock.AsyncMock()))
1960
1961 @ignore_warnings(category=DeprecationWarning)
1962 def test_coroutine_non_gen_function(self):
1963 async def func():
1964 return 'test'
1965
1966 self.assertTrue(asyncio.iscoroutinefunction(func))
1967
1968 coro = func()
1969 self.assertTrue(asyncio.iscoroutine(coro))
1970
1971 res = self.loop.run_until_complete(coro)
1972 self.assertEqual(res, 'test')
1973
1974 def test_coroutine_non_gen_function_return_future(self):
1975 fut = self.new_future(self.loop)
1976
1977 async def func():
1978 return fut
1979
1980 async def coro():
1981 fut.set_result('test')
1982
1983 t1 = self.new_task(self.loop, func())
1984 t2 = self.new_task(self.loop, coro())
1985 res = self.loop.run_until_complete(t1)
1986 self.assertEqual(res, fut)
1987 self.assertIsNone(t2.result())
1988
1989 def test_current_task(self):
1990 self.assertIsNone(self.current_task(loop=self.loop))
1991
1992 async def coro(loop):
1993 self.assertIs(self.current_task(), task)
1994
1995 self.assertIs(self.current_task(None), task)
1996 self.assertIs(self.current_task(), task)
1997
1998 task = self.new_task(self.loop, coro(self.loop))
1999 self.loop.run_until_complete(task)
2000 self.assertIsNone(self.current_task(loop=self.loop))
2001
2002 def test_current_task_with_interleaving_tasks(self):
2003 self.assertIsNone(self.current_task(loop=self.loop))
2004
2005 fut1 = self.new_future(self.loop)
2006 fut2 = self.new_future(self.loop)
2007
2008 async def coro1(loop):
2009 self.assertTrue(self.current_task() is task1)
2010 await fut1
2011 self.assertTrue(self.current_task() is task1)
2012 fut2.set_result(True)
2013
2014 async def coro2(loop):
2015 self.assertTrue(self.current_task() is task2)
2016 fut1.set_result(True)
2017 await fut2
2018 self.assertTrue(self.current_task() is task2)
2019
2020 task1 = self.new_task(self.loop, coro1(self.loop))
2021 task2 = self.new_task(self.loop, coro2(self.loop))
2022
2023 self.loop.run_until_complete(asyncio.wait((task1, task2)))
2024 self.assertIsNone(self.current_task(loop=self.loop))
2025
2026 # Some thorough tests for cancellation propagation through
2027 # coroutines, tasks and wait().
2028
2029 def test_yield_future_passes_cancel(self):
2030 # Cancelling outer() cancels inner() cancels waiter.
2031 proof = 0
2032 waiter = self.new_future(self.loop)
2033
2034 async def inner():
2035 nonlocal proof
2036 try:
2037 await waiter
2038 except asyncio.CancelledError:
2039 proof += 1
2040 raise
2041 else:
2042 self.fail('got past sleep() in inner()')
2043
2044 async def outer():
2045 nonlocal proof
2046 try:
2047 await inner()
2048 except asyncio.CancelledError:
2049 proof += 100 # Expect this path.
2050 else:
2051 proof += 10
2052
2053 f = asyncio.ensure_future(outer(), loop=self.loop)
2054 test_utils.run_briefly(self.loop)
2055 f.cancel()
2056 self.loop.run_until_complete(f)
2057 self.assertEqual(proof, 101)
2058 self.assertTrue(waiter.cancelled())
2059
2060 def test_yield_wait_does_not_shield_cancel(self):
2061 # Cancelling outer() makes wait() return early, leaves inner()
2062 # running.
2063 proof = 0
2064 waiter = self.new_future(self.loop)
2065
2066 async def inner():
2067 nonlocal proof
2068 await waiter
2069 proof += 1
2070
2071 async def outer():
2072 nonlocal proof
2073 with self.assertWarns(DeprecationWarning):
2074 d, p = await asyncio.wait([asyncio.create_task(inner())])
2075 proof += 100
2076
2077 f = asyncio.ensure_future(outer(), loop=self.loop)
2078 test_utils.run_briefly(self.loop)
2079 f.cancel()
2080 self.assertRaises(
2081 asyncio.CancelledError, self.loop.run_until_complete, f)
2082 waiter.set_result(None)
2083 test_utils.run_briefly(self.loop)
2084 self.assertEqual(proof, 1)
2085
2086 def test_shield_result(self):
2087 inner = self.new_future(self.loop)
2088 outer = asyncio.shield(inner)
2089 inner.set_result(42)
2090 res = self.loop.run_until_complete(outer)
2091 self.assertEqual(res, 42)
2092
2093 def test_shield_exception(self):
2094 inner = self.new_future(self.loop)
2095 outer = asyncio.shield(inner)
2096 test_utils.run_briefly(self.loop)
2097 exc = RuntimeError('expected')
2098 inner.set_exception(exc)
2099 test_utils.run_briefly(self.loop)
2100 self.assertIs(outer.exception(), exc)
2101
2102 def test_shield_cancel_inner(self):
2103 inner = self.new_future(self.loop)
2104 outer = asyncio.shield(inner)
2105 test_utils.run_briefly(self.loop)
2106 inner.cancel()
2107 test_utils.run_briefly(self.loop)
2108 self.assertTrue(outer.cancelled())
2109
2110 def test_shield_cancel_outer(self):
2111 inner = self.new_future(self.loop)
2112 outer = asyncio.shield(inner)
2113 test_utils.run_briefly(self.loop)
2114 outer.cancel()
2115 test_utils.run_briefly(self.loop)
2116 self.assertTrue(outer.cancelled())
2117 self.assertEqual(0, 0 if outer._callbacks is None else len(outer._callbacks))
2118
2119 def test_shield_cancel_outer_result(self):
2120 mock_handler = mock.Mock()
2121 self.loop.set_exception_handler(mock_handler)
2122 inner = self.new_future(self.loop)
2123 outer = asyncio.shield(inner)
2124 test_utils.run_briefly(self.loop)
2125 outer.cancel()
2126 test_utils.run_briefly(self.loop)
2127 inner.set_result(1)
2128 test_utils.run_briefly(self.loop)
2129 mock_handler.assert_not_called()
2130
2131 def test_shield_cancel_outer_exception(self):
2132 mock_handler = mock.Mock()
2133 self.loop.set_exception_handler(mock_handler)
2134 inner = self.new_future(self.loop)
2135 outer = asyncio.shield(inner)
2136 test_utils.run_briefly(self.loop)
2137 outer.cancel()
2138 test_utils.run_briefly(self.loop)
2139 inner.set_exception(Exception('foo'))
2140 test_utils.run_briefly(self.loop)
2141 mock_handler.assert_called_once()
2142
2143 def test_shield_duplicate_log_once(self):
2144 mock_handler = mock.Mock()
2145 self.loop.set_exception_handler(mock_handler)
2146 inner = self.new_future(self.loop)
2147 outer = asyncio.shield(inner)
2148 test_utils.run_briefly(self.loop)
2149 outer.cancel()
2150 test_utils.run_briefly(self.loop)
2151 outer = asyncio.shield(inner)
2152 test_utils.run_briefly(self.loop)
2153 outer.cancel()
2154 test_utils.run_briefly(self.loop)
2155 inner.set_exception(Exception('foo'))
2156 test_utils.run_briefly(self.loop)
2157 mock_handler.assert_called_once()
2158
2159 def test_shield_shortcut(self):
2160 fut = self.new_future(self.loop)
2161 fut.set_result(42)
2162 res = self.loop.run_until_complete(asyncio.shield(fut))
2163 self.assertEqual(res, 42)
2164
2165 def test_shield_effect(self):
2166 # Cancelling outer() does not affect inner().
2167 proof = 0
2168 waiter = self.new_future(self.loop)
2169
2170 async def inner():
2171 nonlocal proof
2172 await waiter
2173 proof += 1
2174
2175 async def outer():
2176 nonlocal proof
2177 await asyncio.shield(inner())
2178 proof += 100
2179
2180 f = asyncio.ensure_future(outer(), loop=self.loop)
2181 test_utils.run_briefly(self.loop)
2182 f.cancel()
2183 with self.assertRaises(asyncio.CancelledError):
2184 self.loop.run_until_complete(f)
2185 waiter.set_result(None)
2186 test_utils.run_briefly(self.loop)
2187 self.assertEqual(proof, 1)
2188
2189 def test_shield_gather(self):
2190 child1 = self.new_future(self.loop)
2191 child2 = self.new_future(self.loop)
2192 parent = asyncio.gather(child1, child2)
2193 outer = asyncio.shield(parent)
2194 test_utils.run_briefly(self.loop)
2195 outer.cancel()
2196 test_utils.run_briefly(self.loop)
2197 self.assertTrue(outer.cancelled())
2198 child1.set_result(1)
2199 child2.set_result(2)
2200 test_utils.run_briefly(self.loop)
2201 self.assertEqual(parent.result(), [1, 2])
2202
2203 def test_gather_shield(self):
2204 child1 = self.new_future(self.loop)
2205 child2 = self.new_future(self.loop)
2206 inner1 = asyncio.shield(child1)
2207 inner2 = asyncio.shield(child2)
2208 parent = asyncio.gather(inner1, inner2)
2209 test_utils.run_briefly(self.loop)
2210 parent.cancel()
2211 # This should cancel inner1 and inner2 but bot child1 and child2.
2212 test_utils.run_briefly(self.loop)
2213 self.assertIsInstance(parent.exception(), asyncio.CancelledError)
2214 self.assertTrue(inner1.cancelled())
2215 self.assertTrue(inner2.cancelled())
2216 child1.set_result(1)
2217 child2.set_result(2)
2218 test_utils.run_briefly(self.loop)
2219
2220 def test_shield_coroutine_without_loop(self):
2221 async def coro():
2222 return 42
2223
2224 inner = coro()
2225 self.addCleanup(inner.close)
2226 with self.assertRaisesRegex(RuntimeError, 'no current event loop'):
2227 asyncio.shield(inner)
2228
2229 def test_shield_coroutine_use_running_loop(self):
2230 async def coro():
2231 return 42
2232
2233 async def test():
2234 return asyncio.shield(coro())
2235 outer = self.loop.run_until_complete(test())
2236 self.assertEqual(outer._loop, self.loop)
2237 res = self.loop.run_until_complete(outer)
2238 self.assertEqual(res, 42)
2239
2240 def test_shield_coroutine_use_global_loop(self):
2241 # Deprecated in 3.10, undeprecated in 3.12
2242 async def coro():
2243 return 42
2244
2245 asyncio.set_event_loop(self.loop)
2246 self.addCleanup(asyncio.set_event_loop, None)
2247 outer = asyncio.shield(coro())
2248 self.assertEqual(outer._loop, self.loop)
2249 res = self.loop.run_until_complete(outer)
2250 self.assertEqual(res, 42)
2251
2252 def test_as_completed_invalid_args(self):
2253 # as_completed() expects a list of futures, not a future instance
2254 # TypeError should be raised either on iterator construction or first
2255 # iteration
2256
2257 # Plain iterator
2258 fut = self.new_future(self.loop)
2259 with self.assertRaises(TypeError):
2260 iterator = asyncio.as_completed(fut)
2261 next(iterator)
2262 coro = coroutine_function()
2263 with self.assertRaises(TypeError):
2264 iterator = asyncio.as_completed(coro)
2265 next(iterator)
2266 coro.close()
2267
2268 # Async iterator
2269 async def try_async_iterator(aw):
2270 async for f in asyncio.as_completed(aw):
2271 break
2272
2273 fut = self.new_future(self.loop)
2274 with self.assertRaises(TypeError):
2275 self.loop.run_until_complete(try_async_iterator(fut))
2276 coro = coroutine_function()
2277 with self.assertRaises(TypeError):
2278 self.loop.run_until_complete(try_async_iterator(coro))
2279 coro.close()
2280
2281 def test_wait_invalid_args(self):
2282 fut = self.new_future(self.loop)
2283
2284 # wait() expects a list of futures, not a future instance
2285 self.assertRaises(TypeError, self.loop.run_until_complete,
2286 asyncio.wait(fut))
2287 coro = coroutine_function()
2288 self.assertRaises(TypeError, self.loop.run_until_complete,
2289 asyncio.wait(coro))
2290 coro.close()
2291
2292 # wait() expects at least a future
2293 self.assertRaises(ValueError, self.loop.run_until_complete,
2294 asyncio.wait([]))
2295
2296 def test_log_destroyed_pending_task(self):
2297
2298 async def kill_me(loop):
2299 future = self.new_future(loop)
2300 await future
2301 # at this point, the only reference to kill_me() task is
2302 # the Task._wakeup() method in future._callbacks
2303 raise Exception("code never reached")
2304
2305 mock_handler = mock.Mock()
2306 self.loop.set_debug(True)
2307 self.loop.set_exception_handler(mock_handler)
2308
2309 # schedule the task
2310 coro = kill_me(self.loop)
2311 task = self.new_task(self.loop, coro)
2312
2313 self.assertEqual(self.all_tasks(loop=self.loop), {task})
2314
2315 # execute the task so it waits for future
2316 self.loop.run_until_complete(asyncio.sleep(0))
2317 self.assertEqual(len(self.loop._ready), 0)
2318
2319 coro = None
2320 source_traceback = task._source_traceback
2321 task = None
2322
2323 # no more reference to kill_me() task: the task is destroyed by the GC
2324 support.gc_collect()
2325
2326 mock_handler.assert_called_with(self.loop, {
2327 'message': 'Task was destroyed but it is pending!',
2328 'task': mock.ANY,
2329 'source_traceback': source_traceback,
2330 })
2331 mock_handler.reset_mock()
2332 # task got resurrected by the exception handler
2333 support.gc_collect()
2334
2335 self.assertEqual(self.all_tasks(loop=self.loop), set())
2336
2337 def test_task_not_crash_without_finalization(self):
2338 Task = self.__class__.Task
2339
2340 class Subclass(Task):
2341 def __del__(self):
2342 pass
2343
2344 async def corofn():
2345 await asyncio.sleep(0.01)
2346
2347 coro = corofn()
2348 task = Subclass(coro, loop = self.loop)
2349 task._log_destroy_pending = False
2350
2351 del task
2352
2353 support.gc_collect()
2354
2355 coro.close()
2356
2357 @mock.patch('asyncio.base_events.logger')
2358 def test_tb_logger_not_called_after_cancel(self, m_log):
2359 loop = asyncio.new_event_loop()
2360 self.set_event_loop(loop)
2361
2362 async def coro():
2363 raise TypeError
2364
2365 async def runner():
2366 task = self.new_task(loop, coro())
2367 await asyncio.sleep(0.05)
2368 task.cancel()
2369 task = None
2370
2371 loop.run_until_complete(runner())
2372 self.assertFalse(m_log.error.called)
2373
2374 def test_task_source_traceback(self):
2375 self.loop.set_debug(True)
2376
2377 task = self.new_task(self.loop, coroutine_function())
2378 lineno = sys._getframe().f_lineno - 1
2379 self.assertIsInstance(task._source_traceback, list)
2380 self.assertEqual(task._source_traceback[-2][:3],
2381 (__file__,
2382 lineno,
2383 'test_task_source_traceback'))
2384 self.loop.run_until_complete(task)
2385
2386 def test_cancel_gather_1(self):
2387 """Ensure that a gathering future refuses to be cancelled once all
2388 children are done"""
2389 loop = asyncio.new_event_loop()
2390 self.addCleanup(loop.close)
2391
2392 fut = self.new_future(loop)
2393 async def create():
2394 # The indirection fut->child_coro is needed since otherwise the
2395 # gathering task is done at the same time as the child future
2396 async def child_coro():
2397 return await fut
2398 gather_future = asyncio.gather(child_coro())
2399 return asyncio.ensure_future(gather_future)
2400 gather_task = loop.run_until_complete(create())
2401
2402 cancel_result = None
2403 def cancelling_callback(_):
2404 nonlocal cancel_result
2405 cancel_result = gather_task.cancel()
2406 fut.add_done_callback(cancelling_callback)
2407
2408 fut.set_result(42) # calls the cancelling_callback after fut is done()
2409
2410 # At this point the task should complete.
2411 loop.run_until_complete(gather_task)
2412
2413 # Python issue #26923: asyncio.gather drops cancellation
2414 self.assertEqual(cancel_result, False)
2415 self.assertFalse(gather_task.cancelled())
2416 self.assertEqual(gather_task.result(), [42])
2417
2418 def test_cancel_gather_2(self):
2419 cases = [
2420 ((), ()),
2421 ((None,), ()),
2422 (('my message',), ('my message',)),
2423 # Non-string values should roundtrip.
2424 ((5,), (5,)),
2425 ]
2426 for cancel_args, expected_args in cases:
2427 with self.subTest(cancel_args=cancel_args):
2428 loop = asyncio.new_event_loop()
2429 self.addCleanup(loop.close)
2430
2431 async def test():
2432 time = 0
2433 while True:
2434 time += 0.05
2435 await asyncio.gather(asyncio.sleep(0.05),
2436 return_exceptions=True)
2437 if time > 1:
2438 return
2439
2440 async def main():
2441 qwe = self.new_task(loop, test())
2442 await asyncio.sleep(0.2)
2443 qwe.cancel(*cancel_args)
2444 await qwe
2445
2446 try:
2447 loop.run_until_complete(main())
2448 except asyncio.CancelledError as exc:
2449 self.assertEqual(exc.args, expected_args)
2450 actual = get_innermost_context(exc)
2451 self.assertEqual(
2452 actual,
2453 (asyncio.CancelledError, expected_args, 0),
2454 )
2455 else:
2456 self.fail(
2457 'gather() does not propagate CancelledError '
2458 'raised by inner task to the gather() caller.'
2459 )
2460
2461 def test_exception_traceback(self):
2462 # See http://bugs.python.org/issue28843
2463
2464 async def foo():
2465 1 / 0
2466
2467 async def main():
2468 task = self.new_task(self.loop, foo())
2469 await asyncio.sleep(0) # skip one loop iteration
2470 self.assertIsNotNone(task.exception().__traceback__)
2471
2472 self.loop.run_until_complete(main())
2473
2474 @mock.patch('asyncio.base_events.logger')
2475 def test_error_in_call_soon(self, m_log):
2476 def call_soon(callback, *args, **kwargs):
2477 raise ValueError
2478 self.loop.call_soon = call_soon
2479
2480 async def coro():
2481 pass
2482
2483 self.assertFalse(m_log.error.called)
2484
2485 with self.assertRaises(ValueError):
2486 gen = coro()
2487 try:
2488 self.new_task(self.loop, gen)
2489 finally:
2490 gen.close()
2491 gc.collect() # For PyPy or other GCs.
2492
2493 self.assertTrue(m_log.error.called)
2494 message = m_log.error.call_args[0][0]
2495 self.assertIn('Task was destroyed but it is pending', message)
2496
2497 self.assertEqual(self.all_tasks(self.loop), set())
2498
2499 def test_create_task_with_noncoroutine(self):
2500 with self.assertRaisesRegex(TypeError,
2501 "a coroutine was expected, got 123"):
2502 self.new_task(self.loop, 123)
2503
2504 # test it for the second time to ensure that caching
2505 # in asyncio.iscoroutine() doesn't break things.
2506 with self.assertRaisesRegex(TypeError,
2507 "a coroutine was expected, got 123"):
2508 self.new_task(self.loop, 123)
2509
2510 def test_create_task_with_async_function(self):
2511
2512 async def coro():
2513 pass
2514
2515 task = self.new_task(self.loop, coro())
2516 self.assertIsInstance(task, self.Task)
2517 self.loop.run_until_complete(task)
2518
2519 # test it for the second time to ensure that caching
2520 # in asyncio.iscoroutine() doesn't break things.
2521 task = self.new_task(self.loop, coro())
2522 self.assertIsInstance(task, self.Task)
2523 self.loop.run_until_complete(task)
2524
2525 def test_create_task_with_asynclike_function(self):
2526 task = self.new_task(self.loop, CoroLikeObject())
2527 self.assertIsInstance(task, self.Task)
2528 self.assertEqual(self.loop.run_until_complete(task), 42)
2529
2530 # test it for the second time to ensure that caching
2531 # in asyncio.iscoroutine() doesn't break things.
2532 task = self.new_task(self.loop, CoroLikeObject())
2533 self.assertIsInstance(task, self.Task)
2534 self.assertEqual(self.loop.run_until_complete(task), 42)
2535
2536 def test_bare_create_task(self):
2537
2538 async def inner():
2539 return 1
2540
2541 async def coro():
2542 task = asyncio.create_task(inner())
2543 self.assertIsInstance(task, self.Task)
2544 ret = await task
2545 self.assertEqual(1, ret)
2546
2547 self.loop.run_until_complete(coro())
2548
2549 def test_bare_create_named_task(self):
2550
2551 async def coro_noop():
2552 pass
2553
2554 async def coro():
2555 task = asyncio.create_task(coro_noop(), name='No-op')
2556 self.assertEqual(task.get_name(), 'No-op')
2557 await task
2558
2559 self.loop.run_until_complete(coro())
2560
2561 def test_context_1(self):
2562 cvar = contextvars.ContextVar('cvar', default='nope')
2563
2564 async def sub():
2565 await asyncio.sleep(0.01)
2566 self.assertEqual(cvar.get(), 'nope')
2567 cvar.set('something else')
2568
2569 async def main():
2570 self.assertEqual(cvar.get(), 'nope')
2571 subtask = self.new_task(loop, sub())
2572 cvar.set('yes')
2573 self.assertEqual(cvar.get(), 'yes')
2574 await subtask
2575 self.assertEqual(cvar.get(), 'yes')
2576
2577 loop = asyncio.new_event_loop()
2578 try:
2579 task = self.new_task(loop, main())
2580 loop.run_until_complete(task)
2581 finally:
2582 loop.close()
2583
2584 def test_context_2(self):
2585 cvar = contextvars.ContextVar('cvar', default='nope')
2586
2587 async def main():
2588 def fut_on_done(fut):
2589 # This change must not pollute the context
2590 # of the "main()" task.
2591 cvar.set('something else')
2592
2593 self.assertEqual(cvar.get(), 'nope')
2594
2595 for j in range(2):
2596 fut = self.new_future(loop)
2597 fut.add_done_callback(fut_on_done)
2598 cvar.set(f'yes{j}')
2599 loop.call_soon(fut.set_result, None)
2600 await fut
2601 self.assertEqual(cvar.get(), f'yes{j}')
2602
2603 for i in range(3):
2604 # Test that task passed its context to add_done_callback:
2605 cvar.set(f'yes{i}-{j}')
2606 await asyncio.sleep(0.001)
2607 self.assertEqual(cvar.get(), f'yes{i}-{j}')
2608
2609 loop = asyncio.new_event_loop()
2610 try:
2611 task = self.new_task(loop, main())
2612 loop.run_until_complete(task)
2613 finally:
2614 loop.close()
2615
2616 self.assertEqual(cvar.get(), 'nope')
2617
2618 def test_context_3(self):
2619 # Run 100 Tasks in parallel, each modifying cvar.
2620
2621 cvar = contextvars.ContextVar('cvar', default=-1)
2622
2623 async def sub(num):
2624 for i in range(10):
2625 cvar.set(num + i)
2626 await asyncio.sleep(random.uniform(0.001, 0.05))
2627 self.assertEqual(cvar.get(), num + i)
2628
2629 async def main():
2630 tasks = []
2631 for i in range(100):
2632 task = loop.create_task(sub(random.randint(0, 10)))
2633 tasks.append(task)
2634
2635 await asyncio.gather(*tasks)
2636
2637 loop = asyncio.new_event_loop()
2638 try:
2639 loop.run_until_complete(main())
2640 finally:
2641 loop.close()
2642
2643 self.assertEqual(cvar.get(), -1)
2644
2645 def test_context_4(self):
2646 cvar = contextvars.ContextVar('cvar')
2647
2648 async def coro(val):
2649 await asyncio.sleep(0)
2650 cvar.set(val)
2651
2652 async def main():
2653 ret = []
2654 ctx = contextvars.copy_context()
2655 ret.append(ctx.get(cvar))
2656 t1 = self.new_task(loop, coro(1), context=ctx)
2657 await t1
2658 ret.append(ctx.get(cvar))
2659 t2 = self.new_task(loop, coro(2), context=ctx)
2660 await t2
2661 ret.append(ctx.get(cvar))
2662 return ret
2663
2664 loop = asyncio.new_event_loop()
2665 try:
2666 task = self.new_task(loop, main())
2667 ret = loop.run_until_complete(task)
2668 finally:
2669 loop.close()
2670
2671 self.assertEqual([None, 1, 2], ret)
2672
2673 def test_context_5(self):
2674 cvar = contextvars.ContextVar('cvar')
2675
2676 async def coro(val):
2677 await asyncio.sleep(0)
2678 cvar.set(val)
2679
2680 async def main():
2681 ret = []
2682 ctx = contextvars.copy_context()
2683 ret.append(ctx.get(cvar))
2684 t1 = asyncio.create_task(coro(1), context=ctx)
2685 await t1
2686 ret.append(ctx.get(cvar))
2687 t2 = asyncio.create_task(coro(2), context=ctx)
2688 await t2
2689 ret.append(ctx.get(cvar))
2690 return ret
2691
2692 loop = asyncio.new_event_loop()
2693 try:
2694 task = self.new_task(loop, main())
2695 ret = loop.run_until_complete(task)
2696 finally:
2697 loop.close()
2698
2699 self.assertEqual([None, 1, 2], ret)
2700
2701 def test_context_6(self):
2702 cvar = contextvars.ContextVar('cvar')
2703
2704 async def coro(val):
2705 await asyncio.sleep(0)
2706 cvar.set(val)
2707
2708 async def main():
2709 ret = []
2710 ctx = contextvars.copy_context()
2711 ret.append(ctx.get(cvar))
2712 t1 = loop.create_task(coro(1), context=ctx)
2713 await t1
2714 ret.append(ctx.get(cvar))
2715 t2 = loop.create_task(coro(2), context=ctx)
2716 await t2
2717 ret.append(ctx.get(cvar))
2718 return ret
2719
2720 loop = asyncio.new_event_loop()
2721 try:
2722 task = loop.create_task(main())
2723 ret = loop.run_until_complete(task)
2724 finally:
2725 loop.close()
2726
2727 self.assertEqual([None, 1, 2], ret)
2728
2729 def test_eager_start_true(self):
2730 name = None
2731
2732 async def asyncfn():
2733 nonlocal name
2734 name = self.current_task().get_name()
2735
2736 async def main():
2737 t = self.new_task(coro=asyncfn(), loop=asyncio.get_running_loop(), eager_start=True, name="example")
2738 self.assertTrue(t.done())
2739 self.assertEqual(name, "example")
2740 await t
2741
2742 def test_eager_start_false(self):
2743 name = None
2744
2745 async def asyncfn():
2746 nonlocal name
2747 name = self.current_task().get_name()
2748
2749 async def main():
2750 t = self.new_task(coro=asyncfn(), loop=asyncio.get_running_loop(), eager_start=False, name="example")
2751 self.assertFalse(t.done())
2752 self.assertIsNone(name)
2753 await t
2754 self.assertEqual(name, "example")
2755
2756 asyncio.run(main(), loop_factory=asyncio.EventLoop)
2757
2758 def test_get_coro(self):
2759 loop = asyncio.new_event_loop()
2760 coro = coroutine_function()
2761 try:
2762 task = self.new_task(loop, coro)
2763 loop.run_until_complete(task)
2764 self.assertIs(task.get_coro(), coro)
2765 finally:
2766 loop.close()
2767
2768 def test_get_context(self):
2769 loop = asyncio.new_event_loop()
2770 coro = coroutine_function()
2771 context = contextvars.copy_context()
2772 try:
2773 task = self.new_task(loop, coro, context=context)
2774 loop.run_until_complete(task)
2775 self.assertIs(task.get_context(), context)
2776 finally:
2777 loop.close()
2778
2779 def test_proper_refcounts(self):
2780 # see: https://github.com/python/cpython/issues/126083
2781 class Break:
2782 def __str__(self):
2783 raise RuntimeError("break")
2784
2785 obj = object()
2786 initial_refcount = sys.getrefcount(obj)
2787
2788 coro = coroutine_function()
2789 with contextlib.closing(asyncio.EventLoop()) as loop:
2790 task = asyncio.Task.__new__(asyncio.Task)
2791 for _ in range(5):
2792 with self.assertRaisesRegex(RuntimeError, 'break'):
2793 task.__init__(coro, loop=loop, context=obj, name=Break())
2794
2795 coro.close()
2796 task._log_destroy_pending = False
2797 del task
2798
2799 self.assertEqual(sys.getrefcount(obj), initial_refcount)
2800
2801
2802 def add_subclass_tests(cls):
2803 BaseTask = cls.Task
2804 BaseFuture = cls.Future
2805
2806 if BaseTask is None or BaseFuture is None:
2807 return cls
2808
2809 class CommonFuture:
2810 def __init__(self, *args, **kwargs):
2811 self.calls = collections.defaultdict(lambda: 0)
2812 super().__init__(*args, **kwargs)
2813
2814 def add_done_callback(self, *args, **kwargs):
2815 self.calls['add_done_callback'] += 1
2816 return super().add_done_callback(*args, **kwargs)
2817
2818 class Task(CommonFuture, BaseTask):
2819 pass
2820
2821 class Future(CommonFuture, BaseFuture):
2822 pass
2823
2824 def test_subclasses_ctask_cfuture(self):
2825 fut = self.Future(loop=self.loop)
2826
2827 async def func():
2828 self.loop.call_soon(lambda: fut.set_result('spam'))
2829 return await fut
2830
2831 task = self.Task(func(), loop=self.loop)
2832
2833 result = self.loop.run_until_complete(task)
2834
2835 self.assertEqual(result, 'spam')
2836
2837 self.assertEqual(
2838 dict(task.calls),
2839 {'add_done_callback': 1})
2840
2841 self.assertEqual(
2842 dict(fut.calls),
2843 {'add_done_callback': 1})
2844
2845 # Add patched Task & Future back to the test case
2846 cls.Task = Task
2847 cls.Future = Future
2848
2849 # Add an extra unit-test
2850 cls.test_subclasses_ctask_cfuture = test_subclasses_ctask_cfuture
2851
2852 # Disable the "test_task_source_traceback" test
2853 # (the test is hardcoded for a particular call stack, which
2854 # is slightly different for Task subclasses)
2855 cls.test_task_source_traceback = None
2856
2857 return cls
2858
2859
2860 class SetMethodsTest:
2861
2862 def test_set_result_causes_invalid_state(self):
2863 Future = type(self).Future
2864 self.loop.call_exception_handler = exc_handler = mock.Mock()
2865
2866 async def foo():
2867 await asyncio.sleep(0.1)
2868 return 10
2869
2870 coro = foo()
2871 task = self.new_task(self.loop, coro)
2872 Future.set_result(task, 'spam')
2873
2874 self.assertEqual(
2875 self.loop.run_until_complete(task),
2876 'spam')
2877
2878 exc_handler.assert_called_once()
2879 exc = exc_handler.call_args[0][0]['exception']
2880 with self.assertRaisesRegex(asyncio.InvalidStateError,
2881 r'step\(\): already done'):
2882 raise exc
2883
2884 coro.close()
2885
2886 def test_set_exception_causes_invalid_state(self):
2887 class MyExc(Exception):
2888 pass
2889
2890 Future = type(self).Future
2891 self.loop.call_exception_handler = exc_handler = mock.Mock()
2892
2893 async def foo():
2894 await asyncio.sleep(0.1)
2895 return 10
2896
2897 coro = foo()
2898 task = self.new_task(self.loop, coro)
2899 Future.set_exception(task, MyExc())
2900
2901 with self.assertRaises(MyExc):
2902 self.loop.run_until_complete(task)
2903
2904 exc_handler.assert_called_once()
2905 exc = exc_handler.call_args[0][0]['exception']
2906 with self.assertRaisesRegex(asyncio.InvalidStateError,
2907 r'step\(\): already done'):
2908 raise exc
2909
2910 coro.close()
2911
2912
2913 @unittest.skipUnless(hasattr(futures, '_CFuture') and
2914 hasattr(tasks, '_CTask'),
2915 'requires the C _asyncio module')
2916 class CTask_CFuture_Tests(BaseTaskTests, SetMethodsTest,
2917 test_utils.TestCase):
2918
2919 Task = getattr(tasks, '_CTask', None)
2920 Future = getattr(futures, '_CFuture', None)
2921 all_tasks = getattr(tasks, '_c_all_tasks', None)
2922 current_task = staticmethod(getattr(tasks, '_c_current_task', None))
2923
2924 @support.refcount_test
2925 def test_refleaks_in_task___init__(self):
2926 gettotalrefcount = support.get_attribute(sys, 'gettotalrefcount')
2927 async def coro():
2928 pass
2929 task = self.new_task(self.loop, coro())
2930 self.loop.run_until_complete(task)
2931 refs_before = gettotalrefcount()
2932 for i in range(100):
2933 task.__init__(coro(), loop=self.loop)
2934 self.loop.run_until_complete(task)
2935 self.assertAlmostEqual(gettotalrefcount() - refs_before, 0, delta=10)
2936
2937 def test_del__log_destroy_pending_segfault(self):
2938 async def coro():
2939 pass
2940 task = self.new_task(self.loop, coro())
2941 self.loop.run_until_complete(task)
2942 with self.assertRaises(AttributeError):
2943 del task._log_destroy_pending
2944
2945
2946 @unittest.skipUnless(hasattr(futures, '_CFuture') and
2947 hasattr(tasks, '_CTask'),
2948 'requires the C _asyncio module')
2949 @add_subclass_tests
2950 class CTask_CFuture_SubclassTests(BaseTaskTests, test_utils.TestCase):
2951
2952 Task = getattr(tasks, '_CTask', None)
2953 Future = getattr(futures, '_CFuture', None)
2954 all_tasks = getattr(tasks, '_c_all_tasks', None)
2955 current_task = staticmethod(getattr(tasks, '_c_current_task', None))
2956
2957
2958 @unittest.skipUnless(hasattr(tasks, '_CTask'),
2959 'requires the C _asyncio module')
2960 @add_subclass_tests
2961 class CTaskSubclass_PyFuture_Tests(BaseTaskTests, test_utils.TestCase):
2962
2963 Task = getattr(tasks, '_CTask', None)
2964 Future = futures._PyFuture
2965 all_tasks = getattr(tasks, '_c_all_tasks', None)
2966 current_task = staticmethod(getattr(tasks, '_c_current_task', None))
2967
2968
2969 @unittest.skipUnless(hasattr(futures, '_CFuture'),
2970 'requires the C _asyncio module')
2971 @add_subclass_tests
2972 class PyTask_CFutureSubclass_Tests(BaseTaskTests, test_utils.TestCase):
2973
2974 Future = getattr(futures, '_CFuture', None)
2975 Task = tasks._PyTask
2976 all_tasks = staticmethod(tasks._py_all_tasks)
2977 current_task = staticmethod(tasks._py_current_task)
2978
2979
2980 @unittest.skipUnless(hasattr(tasks, '_CTask'),
2981 'requires the C _asyncio module')
2982 class CTask_PyFuture_Tests(BaseTaskTests, test_utils.TestCase):
2983
2984 Task = getattr(tasks, '_CTask', None)
2985 Future = futures._PyFuture
2986 all_tasks = getattr(tasks, '_c_all_tasks', None)
2987 current_task = staticmethod(getattr(tasks, '_c_current_task', None))
2988
2989
2990 @unittest.skipUnless(hasattr(futures, '_CFuture'),
2991 'requires the C _asyncio module')
2992 class PyTask_CFuture_Tests(BaseTaskTests, test_utils.TestCase):
2993
2994 Task = tasks._PyTask
2995 Future = getattr(futures, '_CFuture', None)
2996 all_tasks = staticmethod(tasks._py_all_tasks)
2997 current_task = staticmethod(tasks._py_current_task)
2998
2999
3000 class PyTask_PyFuture_Tests(BaseTaskTests, SetMethodsTest,
3001 test_utils.TestCase):
3002
3003 Task = tasks._PyTask
3004 Future = futures._PyFuture
3005 all_tasks = staticmethod(tasks._py_all_tasks)
3006 current_task = staticmethod(tasks._py_current_task)
3007
3008
3009 @add_subclass_tests
3010 class PyTask_PyFuture_SubclassTests(BaseTaskTests, test_utils.TestCase):
3011 Task = tasks._PyTask
3012 Future = futures._PyFuture
3013 all_tasks = staticmethod(tasks._py_all_tasks)
3014 current_task = staticmethod(tasks._py_current_task)
3015
3016 @unittest.skipUnless(hasattr(tasks, '_CTask'),
3017 'requires the C _asyncio module')
3018 class CTask_Future_Tests(test_utils.TestCase):
3019
3020 def test_foobar(self):
3021 class Fut(asyncio.Future):
3022 @property
3023 def get_loop(self):
3024 raise AttributeError
3025
3026 async def coro():
3027 await fut
3028 return 'spam'
3029
3030 self.loop = asyncio.new_event_loop()
3031 try:
3032 fut = Fut(loop=self.loop)
3033 self.loop.call_later(0.1, fut.set_result, 1)
3034 task = self.loop.create_task(coro())
3035 res = self.loop.run_until_complete(task)
3036 finally:
3037 self.loop.close()
3038
3039 self.assertEqual(res, 'spam')
3040
3041
3042 class BaseTaskIntrospectionTests:
3043 _register_task = None
3044 _unregister_task = None
3045 _enter_task = None
3046 _leave_task = None
3047 all_tasks = None
3048 Task = None
3049
3050 def test_register_task_resurrection(self):
3051 register_task = self._register_task
3052 class EvilLoop:
3053 def get_debug(self):
3054 return False
3055
3056 def call_exception_handler(self, context):
3057 register_task(context["task"])
3058
3059 async def coro_fn ():
3060 pass
3061
3062 coro = coro_fn()
3063 self.addCleanup(coro.close)
3064 loop = EvilLoop()
3065 with self.assertRaises(AttributeError):
3066 self.Task(coro, loop=loop)
3067
3068
3069 def test__register_task_1(self):
3070 class TaskLike:
3071 @property
3072 def _loop(self):
3073 return loop
3074
3075 def done(self):
3076 return False
3077
3078 task = TaskLike()
3079 loop = mock.Mock()
3080
3081 self.assertEqual(self.all_tasks(loop), set())
3082 self._register_task(task)
3083 self.assertEqual(self.all_tasks(loop), {task})
3084 self._unregister_task(task)
3085
3086 def test__register_task_2(self):
3087 class TaskLike:
3088 def get_loop(self):
3089 return loop
3090
3091 def done(self):
3092 return False
3093
3094 task = TaskLike()
3095 loop = mock.Mock()
3096
3097 self.assertEqual(self.all_tasks(loop), set())
3098 self._register_task(task)
3099 self.assertEqual(self.all_tasks(loop), {task})
3100 self._unregister_task(task)
3101
3102 def test__register_task_3(self):
3103 class TaskLike:
3104 def get_loop(self):
3105 return loop
3106
3107 def done(self):
3108 return True
3109
3110 task = TaskLike()
3111 loop = mock.Mock()
3112
3113 self.assertEqual(self.all_tasks(loop), set())
3114 self._register_task(task)
3115 self.assertEqual(self.all_tasks(loop), set())
3116 self._unregister_task(task)
3117
3118 def test__enter_task(self):
3119 task = mock.Mock()
3120 loop = mock.Mock()
3121 # _enter_task is called by Task.__step while the loop
3122 # is running, so set the loop as the running loop
3123 # for a more realistic test.
3124 asyncio._set_running_loop(loop)
3125 self.assertIsNone(self.current_task(loop))
3126 self._enter_task(loop, task)
3127 self.assertIs(self.current_task(loop), task)
3128 self._leave_task(loop, task)
3129 asyncio._set_running_loop(None)
3130
3131 def test__enter_task_failure(self):
3132 task1 = mock.Mock()
3133 task2 = mock.Mock()
3134 loop = mock.Mock()
3135 asyncio._set_running_loop(loop)
3136 self._enter_task(loop, task1)
3137 with self.assertRaises(RuntimeError):
3138 self._enter_task(loop, task2)
3139 self.assertIs(self.current_task(loop), task1)
3140 self._leave_task(loop, task1)
3141 asyncio._set_running_loop(None)
3142
3143 def test__leave_task(self):
3144 task = mock.Mock()
3145 loop = mock.Mock()
3146 asyncio._set_running_loop(loop)
3147 self._enter_task(loop, task)
3148 self._leave_task(loop, task)
3149 self.assertIsNone(self.current_task(loop))
3150 asyncio._set_running_loop(None)
3151
3152 def test__leave_task_failure1(self):
3153 task1 = mock.Mock()
3154 task2 = mock.Mock()
3155 loop = mock.Mock()
3156 # _leave_task is called by Task.__step while the loop
3157 # is running, so set the loop as the running loop
3158 # for a more realistic test.
3159 asyncio._set_running_loop(loop)
3160 self._enter_task(loop, task1)
3161 with self.assertRaises(RuntimeError):
3162 self._leave_task(loop, task2)
3163 self.assertIs(self.current_task(loop), task1)
3164 self._leave_task(loop, task1)
3165 asyncio._set_running_loop(None)
3166
3167 def test__leave_task_failure2(self):
3168 task = mock.Mock()
3169 loop = mock.Mock()
3170 asyncio._set_running_loop(loop)
3171 with self.assertRaises(RuntimeError):
3172 self._leave_task(loop, task)
3173 self.assertIsNone(self.current_task(loop))
3174 asyncio._set_running_loop(None)
3175
3176 def test__unregister_task(self):
3177 task = mock.Mock()
3178 loop = mock.Mock()
3179 task.get_loop = lambda: loop
3180 self._register_task(task)
3181 self._unregister_task(task)
3182 self.assertEqual(self.all_tasks(loop), set())
3183
3184 def test__unregister_task_not_registered(self):
3185 task = mock.Mock()
3186 loop = mock.Mock()
3187 self._unregister_task(task)
3188 self.assertEqual(self.all_tasks(loop), set())
3189
3190
3191 class PyIntrospectionTests(test_utils.TestCase, BaseTaskIntrospectionTests):
3192 _register_task = staticmethod(tasks._py_register_task)
3193 _unregister_task = staticmethod(tasks._py_unregister_task)
3194 _enter_task = staticmethod(tasks._py_enter_task)
3195 _leave_task = staticmethod(tasks._py_leave_task)
3196 all_tasks = staticmethod(tasks._py_all_tasks)
3197 current_task = staticmethod(tasks._py_current_task)
3198 Task = tasks._PyTask
3199
3200
3201 @unittest.skipUnless(hasattr(tasks, '_c_register_task'),
3202 'requires the C _asyncio module')
3203 class CIntrospectionTests(test_utils.TestCase, BaseTaskIntrospectionTests):
3204 if hasattr(tasks, '_c_register_task'):
3205 _register_task = staticmethod(tasks._c_register_task)
3206 _unregister_task = staticmethod(tasks._c_unregister_task)
3207 _enter_task = staticmethod(tasks._c_enter_task)
3208 _leave_task = staticmethod(tasks._c_leave_task)
3209 all_tasks = staticmethod(tasks._c_all_tasks)
3210 current_task = staticmethod(tasks._c_current_task)
3211 Task = tasks._CTask
3212 else:
3213 _register_task = _unregister_task = _enter_task = _leave_task = None
3214
3215
3216
3217 class BaseCurrentLoopTests:
3218 current_task = None
3219
3220 def setUp(self):
3221 super().setUp()
3222 self.loop = asyncio.new_event_loop()
3223 self.set_event_loop(self.loop)
3224
3225 def new_task(self, coro):
3226 raise NotImplementedError
3227
3228 def test_current_task_no_running_loop(self):
3229 self.assertIsNone(self.current_task(loop=self.loop))
3230
3231 def test_current_task_no_running_loop_implicit(self):
3232 with self.assertRaisesRegex(RuntimeError, 'no running event loop'):
3233 self.current_task()
3234
3235 def test_current_task_with_implicit_loop(self):
3236 async def coro():
3237 self.assertIs(self.current_task(loop=self.loop), task)
3238
3239 self.assertIs(self.current_task(None), task)
3240 self.assertIs(self.current_task(), task)
3241
3242 task = self.new_task(coro())
3243 self.loop.run_until_complete(task)
3244 self.assertIsNone(self.current_task(loop=self.loop))
3245
3246
3247 class PyCurrentLoopTests(BaseCurrentLoopTests, test_utils.TestCase):
3248 current_task = staticmethod(tasks._py_current_task)
3249
3250 def new_task(self, coro):
3251 return tasks._PyTask(coro, loop=self.loop)
3252
3253
3254 @unittest.skipUnless(hasattr(tasks, '_CTask') and
3255 hasattr(tasks, '_c_current_task'),
3256 'requires the C _asyncio module')
3257 class CCurrentLoopTests(BaseCurrentLoopTests, test_utils.TestCase):
3258 if hasattr(tasks, '_c_current_task'):
3259 current_task = staticmethod(tasks._c_current_task)
3260 else:
3261 current_task = None
3262
3263 def new_task(self, coro):
3264 return getattr(tasks, '_CTask')(coro, loop=self.loop)
3265
3266
3267 class GenericTaskTests(test_utils.TestCase):
3268
3269 def test_future_subclass(self):
3270 self.assertIsSubclass(asyncio.Task, asyncio.Future)
3271
3272 @support.cpython_only
3273 def test_asyncio_module_compiled(self):
3274 # Because of circular imports it's easy to make _asyncio
3275 # module non-importable. This is a simple test that will
3276 # fail on systems where C modules were successfully compiled
3277 # (hence the test for _functools etc), but _asyncio somehow didn't.
3278 try:
3279 import _functools # noqa: F401
3280 import _json # noqa: F401
3281 import _pickle # noqa: F401
3282 except ImportError:
3283 self.skipTest('C modules are not available')
3284 else:
3285 try:
3286 import _asyncio # noqa: F401
3287 except ImportError:
3288 self.fail('_asyncio module is missing')
3289
3290
3291 class GatherTestsBase:
3292
3293 def setUp(self):
3294 super().setUp()
3295 self.one_loop = self.new_test_loop()
3296 self.other_loop = self.new_test_loop()
3297 self.set_event_loop(self.one_loop, cleanup=False)
3298
3299 def _run_loop(self, loop):
3300 while loop._ready:
3301 test_utils.run_briefly(loop)
3302
3303 def _check_success(self, **kwargs):
3304 a, b, c = [self.one_loop.create_future() for i in range(3)]
3305 fut = self._gather(*self.wrap_futures(a, b, c), **kwargs)
3306 cb = test_utils.MockCallback()
3307 fut.add_done_callback(cb)
3308 b.set_result(1)
3309 a.set_result(2)
3310 self._run_loop(self.one_loop)
3311 self.assertEqual(cb.called, False)
3312 self.assertFalse(fut.done())
3313 c.set_result(3)
3314 self._run_loop(self.one_loop)
3315 cb.assert_called_once_with(fut)
3316 self.assertEqual(fut.result(), [2, 1, 3])
3317
3318 def test_success(self):
3319 self._check_success()
3320 self._check_success(return_exceptions=False)
3321
3322 def test_result_exception_success(self):
3323 self._check_success(return_exceptions=True)
3324
3325 def test_one_exception(self):
3326 a, b, c, d, e = [self.one_loop.create_future() for i in range(5)]
3327 fut = self._gather(*self.wrap_futures(a, b, c, d, e))
3328 cb = test_utils.MockCallback()
3329 fut.add_done_callback(cb)
3330 exc = ZeroDivisionError()
3331 a.set_result(1)
3332 b.set_exception(exc)
3333 self._run_loop(self.one_loop)
3334 self.assertTrue(fut.done())
3335 cb.assert_called_once_with(fut)
3336 self.assertIs(fut.exception(), exc)
3337 # Does nothing
3338 c.set_result(3)
3339 d.cancel()
3340 e.set_exception(RuntimeError())
3341 e.exception()
3342
3343 def test_return_exceptions(self):
3344 a, b, c, d = [self.one_loop.create_future() for i in range(4)]
3345 fut = self._gather(*self.wrap_futures(a, b, c, d),
3346 return_exceptions=True)
3347 cb = test_utils.MockCallback()
3348 fut.add_done_callback(cb)
3349 exc = ZeroDivisionError()
3350 exc2 = RuntimeError()
3351 b.set_result(1)
3352 c.set_exception(exc)
3353 a.set_result(3)
3354 self._run_loop(self.one_loop)
3355 self.assertFalse(fut.done())
3356 d.set_exception(exc2)
3357 self._run_loop(self.one_loop)
3358 self.assertTrue(fut.done())
3359 cb.assert_called_once_with(fut)
3360 self.assertEqual(fut.result(), [3, 1, exc, exc2])
3361
3362 def test_env_var_debug(self):
3363 code = '\n'.join((
3364 'import asyncio.coroutines',
3365 'print(asyncio.coroutines._is_debug_mode())'))
3366
3367 # Test with -E to not fail if the unit test was run with
3368 # PYTHONASYNCIODEBUG set to a non-empty string
3369 sts, stdout, stderr = assert_python_ok('-E', '-c', code)
3370 self.assertEqual(stdout.rstrip(), b'False')
3371
3372 sts, stdout, stderr = assert_python_ok('-c', code,
3373 PYTHONASYNCIODEBUG='',
3374 PYTHONDEVMODE='')
3375 self.assertEqual(stdout.rstrip(), b'False')
3376
3377 sts, stdout, stderr = assert_python_ok('-c', code,
3378 PYTHONASYNCIODEBUG='1',
3379 PYTHONDEVMODE='')
3380 self.assertEqual(stdout.rstrip(), b'True')
3381
3382 sts, stdout, stderr = assert_python_ok('-E', '-c', code,
3383 PYTHONASYNCIODEBUG='1',
3384 PYTHONDEVMODE='')
3385 self.assertEqual(stdout.rstrip(), b'False')
3386
3387 # -X dev
3388 sts, stdout, stderr = assert_python_ok('-E', '-X', 'dev',
3389 '-c', code)
3390 self.assertEqual(stdout.rstrip(), b'True')
3391
3392
3393 class FutureGatherTests(GatherTestsBase, test_utils.TestCase):
3394
3395 def wrap_futures(self, *futures):
3396 return futures
3397
3398 def _gather(self, *args, **kwargs):
3399 return asyncio.gather(*args, **kwargs)
3400
3401 def test_constructor_empty_sequence_without_loop(self):
3402 with self.assertRaisesRegex(RuntimeError, 'no current event loop'):
3403 asyncio.gather()
3404
3405 def test_constructor_empty_sequence_use_running_loop(self):
3406 async def gather():
3407 return asyncio.gather()
3408 fut = self.one_loop.run_until_complete(gather())
3409 self.assertIsInstance(fut, asyncio.Future)
3410 self.assertIs(fut._loop, self.one_loop)
3411 self._run_loop(self.one_loop)
3412 self.assertTrue(fut.done())
3413 self.assertEqual(fut.result(), [])
3414
3415 def test_constructor_empty_sequence_use_global_loop(self):
3416 # Deprecated in 3.10, undeprecated in 3.12
3417 asyncio.set_event_loop(self.one_loop)
3418 self.addCleanup(asyncio.set_event_loop, None)
3419 fut = asyncio.gather()
3420 self.assertIsInstance(fut, asyncio.Future)
3421 self.assertIs(fut._loop, self.one_loop)
3422 self._run_loop(self.one_loop)
3423 self.assertTrue(fut.done())
3424 self.assertEqual(fut.result(), [])
3425
3426 def test_constructor_heterogenous_futures(self):
3427 fut1 = self.one_loop.create_future()
3428 fut2 = self.other_loop.create_future()
3429 with self.assertRaises(ValueError):
3430 asyncio.gather(fut1, fut2)
3431
3432 def test_constructor_homogenous_futures(self):
3433 children = [self.other_loop.create_future() for i in range(3)]
3434 fut = asyncio.gather(*children)
3435 self.assertIs(fut._loop, self.other_loop)
3436 self._run_loop(self.other_loop)
3437 self.assertFalse(fut.done())
3438 fut = asyncio.gather(*children)
3439 self.assertIs(fut._loop, self.other_loop)
3440 self._run_loop(self.other_loop)
3441 self.assertFalse(fut.done())
3442
3443 def test_one_cancellation(self):
3444 a, b, c, d, e = [self.one_loop.create_future() for i in range(5)]
3445 fut = asyncio.gather(a, b, c, d, e)
3446 cb = test_utils.MockCallback()
3447 fut.add_done_callback(cb)
3448 a.set_result(1)
3449 b.cancel()
3450 self._run_loop(self.one_loop)
3451 self.assertTrue(fut.done())
3452 cb.assert_called_once_with(fut)
3453 self.assertFalse(fut.cancelled())
3454 self.assertIsInstance(fut.exception(), asyncio.CancelledError)
3455 # Does nothing
3456 c.set_result(3)
3457 d.cancel()
3458 e.set_exception(RuntimeError())
3459 e.exception()
3460
3461 def test_result_exception_one_cancellation(self):
3462 a, b, c, d, e, f = [self.one_loop.create_future()
3463 for i in range(6)]
3464 fut = asyncio.gather(a, b, c, d, e, f, return_exceptions=True)
3465 cb = test_utils.MockCallback()
3466 fut.add_done_callback(cb)
3467 a.set_result(1)
3468 zde = ZeroDivisionError()
3469 b.set_exception(zde)
3470 c.cancel()
3471 self._run_loop(self.one_loop)
3472 self.assertFalse(fut.done())
3473 d.set_result(3)
3474 e.cancel()
3475 rte = RuntimeError()
3476 f.set_exception(rte)
3477 res = self.one_loop.run_until_complete(fut)
3478 self.assertIsInstance(res[2], asyncio.CancelledError)
3479 self.assertIsInstance(res[4], asyncio.CancelledError)
3480 res[2] = res[4] = None
3481 self.assertEqual(res, [1, zde, None, 3, None, rte])
3482 cb.assert_called_once_with(fut)
3483
3484
3485 class CoroutineGatherTests(GatherTestsBase, test_utils.TestCase):
3486
3487 def wrap_futures(self, *futures):
3488 coros = []
3489 for fut in futures:
3490 async def coro(fut=fut):
3491 return await fut
3492 coros.append(coro())
3493 return coros
3494
3495 def _gather(self, *args, **kwargs):
3496 async def coro():
3497 return asyncio.gather(*args, **kwargs)
3498 return self.one_loop.run_until_complete(coro())
3499
3500 def test_constructor_without_loop(self):
3501 async def coro():
3502 return 'abc'
3503 gen1 = coro()
3504 self.addCleanup(gen1.close)
3505 gen2 = coro()
3506 self.addCleanup(gen2.close)
3507 with self.assertRaisesRegex(RuntimeError, 'no current event loop'):
3508 asyncio.gather(gen1, gen2)
3509
3510 def test_constructor_use_running_loop(self):
3511 async def coro():
3512 return 'abc'
3513 gen1 = coro()
3514 gen2 = coro()
3515 async def gather():
3516 return asyncio.gather(gen1, gen2)
3517 fut = self.one_loop.run_until_complete(gather())
3518 self.assertIs(fut._loop, self.one_loop)
3519 self.one_loop.run_until_complete(fut)
3520
3521 def test_constructor_use_global_loop(self):
3522 # Deprecated in 3.10, undeprecated in 3.12
3523 async def coro():
3524 return 'abc'
3525 asyncio.set_event_loop(self.other_loop)
3526 self.addCleanup(asyncio.set_event_loop, None)
3527 gen1 = coro()
3528 gen2 = coro()
3529 fut = asyncio.gather(gen1, gen2)
3530 self.assertIs(fut._loop, self.other_loop)
3531 self.other_loop.run_until_complete(fut)
3532
3533 def test_duplicate_coroutines(self):
3534 async def coro(s):
3535 return s
3536 c = coro('abc')
3537 fut = self._gather(c, c, coro('def'), c)
3538 self._run_loop(self.one_loop)
3539 self.assertEqual(fut.result(), ['abc', 'abc', 'def', 'abc'])
3540
3541 def test_cancellation_broadcast(self):
3542 # Cancelling outer() cancels all children.
3543 proof = 0
3544 waiter = self.one_loop.create_future()
3545
3546 async def inner():
3547 nonlocal proof
3548 await waiter
3549 proof += 1
3550
3551 child1 = asyncio.ensure_future(inner(), loop=self.one_loop)
3552 child2 = asyncio.ensure_future(inner(), loop=self.one_loop)
3553 gatherer = None
3554
3555 async def outer():
3556 nonlocal proof, gatherer
3557 gatherer = asyncio.gather(child1, child2)
3558 await gatherer
3559 proof += 100
3560
3561 f = asyncio.ensure_future(outer(), loop=self.one_loop)
3562 test_utils.run_briefly(self.one_loop)
3563 self.assertTrue(f.cancel())
3564 with self.assertRaises(asyncio.CancelledError):
3565 self.one_loop.run_until_complete(f)
3566 self.assertFalse(gatherer.cancel())
3567 self.assertTrue(waiter.cancelled())
3568 self.assertTrue(child1.cancelled())
3569 self.assertTrue(child2.cancelled())
3570 test_utils.run_briefly(self.one_loop)
3571 self.assertEqual(proof, 0)
3572
3573 def test_exception_marking(self):
3574 # Test for the first line marked "Mark exception retrieved."
3575
3576 async def inner(f):
3577 await f
3578 raise RuntimeError('should not be ignored')
3579
3580 a = self.one_loop.create_future()
3581 b = self.one_loop.create_future()
3582
3583 async def outer():
3584 await asyncio.gather(inner(a), inner(b))
3585
3586 f = asyncio.ensure_future(outer(), loop=self.one_loop)
3587 test_utils.run_briefly(self.one_loop)
3588 a.set_result(None)
3589 test_utils.run_briefly(self.one_loop)
3590 b.set_result(None)
3591 test_utils.run_briefly(self.one_loop)
3592 self.assertIsInstance(f.exception(), RuntimeError)
3593
3594 def test_issue46672(self):
3595 with mock.patch(
3596 'asyncio.base_events.BaseEventLoop.call_exception_handler',
3597 ):
3598 async def coro(s):
3599 return s
3600 c = coro('abc')
3601
3602 with self.assertRaises(TypeError):
3603 self._gather(c, {})
3604 self._run_loop(self.one_loop)
3605 # NameError should not happen:
3606 self.one_loop.call_exception_handler.assert_not_called()
3607
3608
3609 class RunCoroutineThreadsafeTests(test_utils.TestCase):
3610 """Test case for asyncio.run_coroutine_threadsafe."""
3611
3612 def setUp(self):
3613 super().setUp()
3614 self.loop = asyncio.new_event_loop()
3615 self.set_event_loop(self.loop) # Will cleanup properly
3616
3617 async def add(self, a, b, fail=False, cancel=False):
3618 """Wait 0.05 second and return a + b."""
3619 await asyncio.sleep(0.05)
3620 if fail:
3621 raise RuntimeError("Fail!")
3622 if cancel:
3623 asyncio.current_task(self.loop).cancel()
3624 await asyncio.sleep(0)
3625 return a + b
3626
3627 def target(self, fail=False, cancel=False, timeout=None,
3628 advance_coro=False):
3629 """Run add coroutine in the event loop."""
3630 coro = self.add(1, 2, fail=fail, cancel=cancel)
3631 future = asyncio.run_coroutine_threadsafe(coro, self.loop)
3632 if advance_coro:
3633 # this is for test_run_coroutine_threadsafe_task_factory_exception;
3634 # otherwise it spills errors and breaks **other** unittests, since
3635 # 'target' is interacting with threads.
3636
3637 # With this call, `coro` will be advanced.
3638 self.loop.call_soon_threadsafe(coro.send, None)
3639 try:
3640 return future.result(timeout)
3641 finally:
3642 future.done() or future.cancel()
3643
3644 def test_run_coroutine_threadsafe(self):
3645 """Test coroutine submission from a thread to an event loop."""
3646 future = self.loop.run_in_executor(None, self.target)
3647 result = self.loop.run_until_complete(future)
3648 self.assertEqual(result, 3)
3649
3650 def test_run_coroutine_threadsafe_with_exception(self):
3651 """Test coroutine submission from a thread to an event loop
3652 when an exception is raised."""
3653 future = self.loop.run_in_executor(None, self.target, True)
3654 with self.assertRaises(RuntimeError) as exc_context:
3655 self.loop.run_until_complete(future)
3656 self.assertIn("Fail!", exc_context.exception.args)
3657
3658 def test_run_coroutine_threadsafe_with_timeout(self):
3659 """Test coroutine submission from a thread to an event loop
3660 when a timeout is raised."""
3661 callback = lambda: self.target(timeout=0)
3662 future = self.loop.run_in_executor(None, callback)
3663 with self.assertRaises(asyncio.TimeoutError):
3664 self.loop.run_until_complete(future)
3665 test_utils.run_briefly(self.loop)
3666 # Check that there's no pending task (add has been cancelled)
3667 for task in asyncio.all_tasks(self.loop):
3668 self.assertTrue(task.done())
3669
3670 def test_run_coroutine_threadsafe_task_cancelled(self):
3671 """Test coroutine submission from a thread to an event loop
3672 when the task is cancelled."""
3673 callback = lambda: self.target(cancel=True)
3674 future = self.loop.run_in_executor(None, callback)
3675 with self.assertRaises(asyncio.CancelledError):
3676 self.loop.run_until_complete(future)
3677
3678 def test_run_coroutine_threadsafe_task_factory_exception(self):
3679 """Test coroutine submission from a thread to an event loop
3680 when the task factory raise an exception."""
3681
3682 def task_factory(loop, coro):
3683 raise NameError
3684
3685 run = self.loop.run_in_executor(
3686 None, lambda: self.target(advance_coro=True))
3687
3688 # Set exception handler
3689 callback = test_utils.MockCallback()
3690 self.loop.set_exception_handler(callback)
3691
3692 # Set corrupted task factory
3693 self.addCleanup(self.loop.set_task_factory,
3694 self.loop.get_task_factory())
3695 self.loop.set_task_factory(task_factory)
3696
3697 # Run event loop
3698 with self.assertRaises(NameError) as exc_context:
3699 self.loop.run_until_complete(run)
3700
3701 # Check exceptions
3702 self.assertEqual(len(callback.call_args_list), 1)
3703 (loop, context), kwargs = callback.call_args
3704 self.assertEqual(context['exception'], exc_context.exception)
3705
3706 def test_run_coroutine_threadsafe_and_cancel(self):
3707 task = None
3708 thread_future = None
3709 # Use a custom task factory to capture the created Task
3710 def task_factory(loop, coro):
3711 nonlocal task
3712 task = asyncio.Task(coro, loop=loop)
3713 return task
3714
3715 self.addCleanup(self.loop.set_task_factory,
3716 self.loop.get_task_factory())
3717
3718 async def target():
3719 nonlocal thread_future
3720 self.loop.set_task_factory(task_factory)
3721 thread_future = asyncio.run_coroutine_threadsafe(asyncio.sleep(10), self.loop)
3722 await asyncio.sleep(0)
3723
3724 thread_future.cancel()
3725
3726 self.loop.run_until_complete(target())
3727 self.assertTrue(task.cancelled())
3728 self.assertTrue(thread_future.cancelled())
3729
3730
3731 class SleepTests(test_utils.TestCase):
3732 def setUp(self):
3733 super().setUp()
3734 self.loop = asyncio.new_event_loop()
3735 self.set_event_loop(self.loop)
3736
3737 def tearDown(self):
3738 self.loop.close()
3739 self.loop = None
3740 super().tearDown()
3741
3742 def test_sleep_zero(self):
3743 result = 0
3744
3745 def inc_result(num):
3746 nonlocal result
3747 result += num
3748
3749 async def coro():
3750 self.loop.call_soon(inc_result, 1)
3751 self.assertEqual(result, 0)
3752 num = await asyncio.sleep(0, result=10)
3753 self.assertEqual(result, 1) # inc'ed by call_soon
3754 inc_result(num) # num should be 11
3755
3756 self.loop.run_until_complete(coro())
3757 self.assertEqual(result, 11)
3758
3759
3760 class CompatibilityTests(test_utils.TestCase):
3761 # Tests for checking a bridge between old-styled coroutines
3762 # and async/await syntax
3763
3764 def setUp(self):
3765 super().setUp()
3766 self.loop = asyncio.new_event_loop()
3767 self.set_event_loop(self.loop)
3768
3769 def tearDown(self):
3770 self.loop.close()
3771 self.loop = None
3772 super().tearDown()
3773
3774
3775 if __name__ == '__main__':
3776 unittest.main()