Best Python code snippet using localstack_python
test_tasks.py
Source:test_tasks.py
...66 def new_future(self, loop):67 return self.__class__.Future(loop=loop)68 def setUp(self):69 super().setUp()70 self.loop = self.new_test_loop()71 self.loop.set_task_factory(self.new_task)72 self.loop.create_future = lambda: self.new_future(self.loop)73 def test_task_del_collect(self):74 class Evil:75 def __del__(self):76 gc.collect()77 async def run():78 return Evil()79 self.loop.run_until_complete(80 asyncio.gather(*[81 self.new_task(self.loop, run()) for _ in range(100)82 ], loop=self.loop))83 def test_other_loop_future(self):84 other_loop = asyncio.new_event_loop()85 fut = self.new_future(other_loop)86 async def run(fut):87 await fut88 try:89 with self.assertRaisesRegex(RuntimeError,90 r'Task .* got Future .* attached'):91 self.loop.run_until_complete(run(fut))92 finally:93 other_loop.close()94 def test_task_awaits_on_itself(self):95 async def test():96 await task97 task = asyncio.ensure_future(test(), loop=self.loop)98 with self.assertRaisesRegex(RuntimeError,99 'Task cannot await on itself'):100 self.loop.run_until_complete(task)101 def test_task_class(self):102 async def notmuch():103 return 'ok'104 t = self.new_task(self.loop, notmuch())105 self.loop.run_until_complete(t)106 self.assertTrue(t.done())107 self.assertEqual(t.result(), 'ok')108 self.assertIs(t._loop, self.loop)109 self.assertIs(t.get_loop(), self.loop)110 loop = asyncio.new_event_loop()111 self.set_event_loop(loop)112 t = self.new_task(loop, notmuch())113 self.assertIs(t._loop, loop)114 loop.run_until_complete(t)115 loop.close()116 def test_ensure_future_coroutine(self):117 with self.assertWarns(DeprecationWarning):118 @asyncio.coroutine119 def notmuch():120 return 'ok'121 t = asyncio.ensure_future(notmuch(), loop=self.loop)122 self.loop.run_until_complete(t)123 self.assertTrue(t.done())124 self.assertEqual(t.result(), 'ok')125 self.assertIs(t._loop, self.loop)126 loop = asyncio.new_event_loop()127 self.set_event_loop(loop)128 t = asyncio.ensure_future(notmuch(), loop=loop)129 self.assertIs(t._loop, loop)130 loop.run_until_complete(t)131 loop.close()132 def test_ensure_future_future(self):133 f_orig = self.new_future(self.loop)134 f_orig.set_result('ko')135 f = asyncio.ensure_future(f_orig)136 self.loop.run_until_complete(f)137 self.assertTrue(f.done())138 self.assertEqual(f.result(), 'ko')139 self.assertIs(f, f_orig)140 loop = asyncio.new_event_loop()141 self.set_event_loop(loop)142 with self.assertRaises(ValueError):143 f = asyncio.ensure_future(f_orig, loop=loop)144 loop.close()145 f = asyncio.ensure_future(f_orig, loop=self.loop)146 self.assertIs(f, f_orig)147 def test_ensure_future_task(self):148 async def notmuch():149 return 'ok'150 t_orig = self.new_task(self.loop, notmuch())151 t = asyncio.ensure_future(t_orig)152 self.loop.run_until_complete(t)153 self.assertTrue(t.done())154 self.assertEqual(t.result(), 'ok')155 self.assertIs(t, t_orig)156 loop = asyncio.new_event_loop()157 self.set_event_loop(loop)158 with self.assertRaises(ValueError):159 t = asyncio.ensure_future(t_orig, loop=loop)160 loop.close()161 t = asyncio.ensure_future(t_orig, loop=self.loop)162 self.assertIs(t, t_orig)163 def test_ensure_future_awaitable(self):164 class Aw:165 def __init__(self, coro):166 self.coro = coro167 def __await__(self):168 return (yield from self.coro)169 with self.assertWarns(DeprecationWarning):170 @asyncio.coroutine171 def coro():172 return 'ok'173 loop = asyncio.new_event_loop()174 self.set_event_loop(loop)175 fut = asyncio.ensure_future(Aw(coro()), loop=loop)176 loop.run_until_complete(fut)177 assert fut.result() == 'ok'178 def test_ensure_future_neither(self):179 with self.assertRaises(TypeError):180 asyncio.ensure_future('ok')181 def test_ensure_future_error_msg(self):182 loop = asyncio.new_event_loop()183 f = self.new_future(self.loop)184 with self.assertRaisesRegex(ValueError, 'The future belongs to a '185 'different loop than the one specified as '186 'the loop argument'):187 asyncio.ensure_future(f, loop=loop)188 loop.close()189 def test_get_stack(self):190 T = None191 async def foo():192 await bar()193 async def bar():194 # test get_stack()195 f = T.get_stack(limit=1)196 try:197 self.assertEqual(f[0].f_code.co_name, 'foo')198 finally:199 f = None200 # test print_stack()201 file = io.StringIO()202 T.print_stack(limit=1, file=file)203 file.seek(0)204 tb = file.read()205 self.assertRegex(tb, r'foo\(\) running')206 async def runner():207 nonlocal T208 T = asyncio.ensure_future(foo(), loop=self.loop)209 await T210 self.loop.run_until_complete(runner())211 def test_task_repr(self):212 self.loop.set_debug(False)213 async def notmuch():214 return 'abc'215 # test coroutine function216 self.assertEqual(notmuch.__name__, 'notmuch')217 self.assertRegex(notmuch.__qualname__,218 r'\w+.test_task_repr.<locals>.notmuch')219 self.assertEqual(notmuch.__module__, __name__)220 filename, lineno = test_utils.get_function_source(notmuch)221 src = "%s:%s" % (filename, lineno)222 # test coroutine object223 gen = notmuch()224 coro_qualname = 'BaseTaskTests.test_task_repr.<locals>.notmuch'225 self.assertEqual(gen.__name__, 'notmuch')226 self.assertEqual(gen.__qualname__, coro_qualname)227 # test pending Task228 t = self.new_task(self.loop, gen)229 t.add_done_callback(Dummy())230 coro = format_coroutine(coro_qualname, 'running', src,231 t._source_traceback, generator=True)232 self.assertEqual(repr(t),233 "<Task pending name='TestTask' %s cb=[<Dummy>()]>" % coro)234 # test cancelling Task235 t.cancel() # Does not take immediate effect!236 self.assertEqual(repr(t),237 "<Task cancelling name='TestTask' %s cb=[<Dummy>()]>" % coro)238 # test cancelled Task239 self.assertRaises(asyncio.CancelledError,240 self.loop.run_until_complete, t)241 coro = format_coroutine(coro_qualname, 'done', src,242 t._source_traceback)243 self.assertEqual(repr(t),244 "<Task cancelled name='TestTask' %s>" % coro)245 # test finished Task246 t = self.new_task(self.loop, notmuch())247 self.loop.run_until_complete(t)248 coro = format_coroutine(coro_qualname, 'done', src,249 t._source_traceback)250 self.assertEqual(repr(t),251 "<Task finished name='TestTask' %s result='abc'>" % coro)252 def test_task_repr_autogenerated(self):253 async def notmuch():254 return 123255 t1 = self.new_task(self.loop, notmuch(), None)256 t2 = self.new_task(self.loop, notmuch(), None)257 self.assertNotEqual(repr(t1), repr(t2))258 match1 = re.match(r"^<Task pending name='Task-(\d+)'", repr(t1))259 self.assertIsNotNone(match1)260 match2 = re.match(r"^<Task pending name='Task-(\d+)'", repr(t2))261 self.assertIsNotNone(match2)262 # Autogenerated task names should have monotonically increasing numbers263 self.assertLess(int(match1.group(1)), int(match2.group(1)))264 self.loop.run_until_complete(t1)265 self.loop.run_until_complete(t2)266 def test_task_repr_name_not_str(self):267 async def notmuch():268 return 123269 t = self.new_task(self.loop, notmuch())270 t.set_name({6})271 self.assertEqual(t.get_name(), '{6}')272 self.loop.run_until_complete(t)273 def test_task_repr_coro_decorator(self):274 self.loop.set_debug(False)275 with self.assertWarns(DeprecationWarning):276 @asyncio.coroutine277 def notmuch():278 # notmuch() function doesn't use yield from: it will be wrapped by279 # @coroutine decorator280 return 123281 # test coroutine function282 self.assertEqual(notmuch.__name__, 'notmuch')283 self.assertRegex(notmuch.__qualname__,284 r'\w+.test_task_repr_coro_decorator'285 r'\.<locals>\.notmuch')286 self.assertEqual(notmuch.__module__, __name__)287 # test coroutine object288 gen = notmuch()289 # On Python >= 3.5, generators now inherit the name of the290 # function, as expected, and have a qualified name (__qualname__291 # attribute).292 coro_name = 'notmuch'293 coro_qualname = ('BaseTaskTests.test_task_repr_coro_decorator'294 '.<locals>.notmuch')295 self.assertEqual(gen.__name__, coro_name)296 self.assertEqual(gen.__qualname__, coro_qualname)297 # test repr(CoroWrapper)298 if coroutines._DEBUG:299 # format the coroutine object300 if coroutines._DEBUG:301 filename, lineno = test_utils.get_function_source(notmuch)302 frame = gen._source_traceback[-1]303 coro = ('%s() running, defined at %s:%s, created at %s:%s'304 % (coro_qualname, filename, lineno,305 frame[0], frame[1]))306 else:307 code = gen.gi_code308 coro = ('%s() running at %s:%s'309 % (coro_qualname, code.co_filename,310 code.co_firstlineno))311 self.assertEqual(repr(gen), '<CoroWrapper %s>' % coro)312 # test pending Task313 t = self.new_task(self.loop, gen)314 t.add_done_callback(Dummy())315 # format the coroutine object316 if coroutines._DEBUG:317 src = '%s:%s' % test_utils.get_function_source(notmuch)318 else:319 code = gen.gi_code320 src = '%s:%s' % (code.co_filename, code.co_firstlineno)321 coro = format_coroutine(coro_qualname, 'running', src,322 t._source_traceback,323 generator=not coroutines._DEBUG)324 self.assertEqual(repr(t),325 "<Task pending name='TestTask' %s cb=[<Dummy>()]>" % coro)326 self.loop.run_until_complete(t)327 def test_task_repr_wait_for(self):328 self.loop.set_debug(False)329 async def wait_for(fut):330 return await fut331 fut = self.new_future(self.loop)332 task = self.new_task(self.loop, wait_for(fut))333 test_utils.run_briefly(self.loop)334 self.assertRegex(repr(task),335 '<Task .* wait_for=%s>' % re.escape(repr(fut)))336 fut.set_result(None)337 self.loop.run_until_complete(task)338 def test_task_repr_partial_corowrapper(self):339 # Issue #222: repr(CoroWrapper) must not fail in debug mode if the340 # coroutine is a partial function341 with set_coroutine_debug(True):342 self.loop.set_debug(True)343 async def func(x, y):344 await asyncio.sleep(0)345 with self.assertWarns(DeprecationWarning):346 partial_func = asyncio.coroutine(functools.partial(func, 1))347 task = self.loop.create_task(partial_func(2))348 # make warnings quiet349 task._log_destroy_pending = False350 self.addCleanup(task._coro.close)351 coro_repr = repr(task._coro)352 expected = (353 r'<coroutine object \w+\.test_task_repr_partial_corowrapper'354 r'\.<locals>\.func at'355 )356 self.assertRegex(coro_repr, expected)357 def test_task_basics(self):358 async def outer():359 a = await inner1()360 b = await inner2()361 return a+b362 async def inner1():363 return 42364 async def inner2():365 return 1000366 t = outer()367 self.assertEqual(self.loop.run_until_complete(t), 1042)368 def test_cancel(self):369 def gen():370 when = yield371 self.assertAlmostEqual(10.0, when)372 yield 0373 loop = self.new_test_loop(gen)374 async def task():375 await asyncio.sleep(10.0)376 return 12377 t = self.new_task(loop, task())378 loop.call_soon(t.cancel)379 with self.assertRaises(asyncio.CancelledError):380 loop.run_until_complete(t)381 self.assertTrue(t.done())382 self.assertTrue(t.cancelled())383 self.assertFalse(t.cancel())384 def test_cancel_yield(self):385 with self.assertWarns(DeprecationWarning):386 @asyncio.coroutine387 def task():388 yield389 yield390 return 12391 t = self.new_task(self.loop, task())392 test_utils.run_briefly(self.loop) # start coro393 t.cancel()394 self.assertRaises(395 asyncio.CancelledError, self.loop.run_until_complete, t)396 self.assertTrue(t.done())397 self.assertTrue(t.cancelled())398 self.assertFalse(t.cancel())399 def test_cancel_inner_future(self):400 f = self.new_future(self.loop)401 async def task():402 await f403 return 12404 t = self.new_task(self.loop, task())405 test_utils.run_briefly(self.loop) # start task406 f.cancel()407 with self.assertRaises(asyncio.CancelledError):408 self.loop.run_until_complete(t)409 self.assertTrue(f.cancelled())410 self.assertTrue(t.cancelled())411 def test_cancel_both_task_and_inner_future(self):412 f = self.new_future(self.loop)413 async def task():414 await f415 return 12416 t = self.new_task(self.loop, task())417 test_utils.run_briefly(self.loop)418 f.cancel()419 t.cancel()420 with self.assertRaises(asyncio.CancelledError):421 self.loop.run_until_complete(t)422 self.assertTrue(t.done())423 self.assertTrue(f.cancelled())424 self.assertTrue(t.cancelled())425 def test_cancel_task_catching(self):426 fut1 = self.new_future(self.loop)427 fut2 = self.new_future(self.loop)428 async def task():429 await fut1430 try:431 await fut2432 except asyncio.CancelledError:433 return 42434 t = self.new_task(self.loop, task())435 test_utils.run_briefly(self.loop)436 self.assertIs(t._fut_waiter, fut1) # White-box test.437 fut1.set_result(None)438 test_utils.run_briefly(self.loop)439 self.assertIs(t._fut_waiter, fut2) # White-box test.440 t.cancel()441 self.assertTrue(fut2.cancelled())442 res = self.loop.run_until_complete(t)443 self.assertEqual(res, 42)444 self.assertFalse(t.cancelled())445 def test_cancel_task_ignoring(self):446 fut1 = self.new_future(self.loop)447 fut2 = self.new_future(self.loop)448 fut3 = self.new_future(self.loop)449 async def task():450 await fut1451 try:452 await fut2453 except asyncio.CancelledError:454 pass455 res = await fut3456 return res457 t = self.new_task(self.loop, task())458 test_utils.run_briefly(self.loop)459 self.assertIs(t._fut_waiter, fut1) # White-box test.460 fut1.set_result(None)461 test_utils.run_briefly(self.loop)462 self.assertIs(t._fut_waiter, fut2) # White-box test.463 t.cancel()464 self.assertTrue(fut2.cancelled())465 test_utils.run_briefly(self.loop)466 self.assertIs(t._fut_waiter, fut3) # White-box test.467 fut3.set_result(42)468 res = self.loop.run_until_complete(t)469 self.assertEqual(res, 42)470 self.assertFalse(fut3.cancelled())471 self.assertFalse(t.cancelled())472 def test_cancel_current_task(self):473 loop = asyncio.new_event_loop()474 self.set_event_loop(loop)475 async def task():476 t.cancel()477 self.assertTrue(t._must_cancel) # White-box test.478 # The sleep should be cancelled immediately.479 await asyncio.sleep(100)480 return 12481 t = self.new_task(loop, task())482 self.assertFalse(t.cancelled())483 self.assertRaises(484 asyncio.CancelledError, loop.run_until_complete, t)485 self.assertTrue(t.done())486 self.assertTrue(t.cancelled())487 self.assertFalse(t._must_cancel) # White-box test.488 self.assertFalse(t.cancel())489 def test_cancel_at_end(self):490 """coroutine end right after task is cancelled"""491 loop = asyncio.new_event_loop()492 self.set_event_loop(loop)493 async def task():494 t.cancel()495 self.assertTrue(t._must_cancel) # White-box test.496 return 12497 t = self.new_task(loop, task())498 self.assertFalse(t.cancelled())499 self.assertRaises(500 asyncio.CancelledError, loop.run_until_complete, t)501 self.assertTrue(t.done())502 self.assertTrue(t.cancelled())503 self.assertFalse(t._must_cancel) # White-box test.504 self.assertFalse(t.cancel())505 def test_cancel_awaited_task(self):506 # This tests for a relatively rare condition when507 # a task cancellation is requested for a task which is not508 # currently blocked, such as a task cancelling itself.509 # In this situation we must ensure that whatever next future510 # or task the cancelled task blocks on is cancelled correctly511 # as well. See also bpo-34872.512 loop = asyncio.new_event_loop()513 self.addCleanup(lambda: loop.close())514 task = nested_task = None515 fut = self.new_future(loop)516 async def nested():517 await fut518 async def coro():519 nonlocal nested_task520 # Create a sub-task and wait for it to run.521 nested_task = self.new_task(loop, nested())522 await asyncio.sleep(0)523 # Request the current task to be cancelled.524 task.cancel()525 # Block on the nested task, which should be immediately526 # cancelled.527 await nested_task528 task = self.new_task(loop, coro())529 with self.assertRaises(asyncio.CancelledError):530 loop.run_until_complete(task)531 self.assertTrue(task.cancelled())532 self.assertTrue(nested_task.cancelled())533 self.assertTrue(fut.cancelled())534 def test_stop_while_run_in_complete(self):535 def gen():536 when = yield537 self.assertAlmostEqual(0.1, when)538 when = yield 0.1539 self.assertAlmostEqual(0.2, when)540 when = yield 0.1541 self.assertAlmostEqual(0.3, when)542 yield 0.1543 loop = self.new_test_loop(gen)544 x = 0545 async def task():546 nonlocal x547 while x < 10:548 await asyncio.sleep(0.1)549 x += 1550 if x == 2:551 loop.stop()552 t = self.new_task(loop, task())553 with self.assertRaises(RuntimeError) as cm:554 loop.run_until_complete(t)555 self.assertEqual(str(cm.exception),556 'Event loop stopped before Future completed.')557 self.assertFalse(t.done())558 self.assertEqual(x, 2)559 self.assertAlmostEqual(0.3, loop.time())560 t.cancel()561 self.assertRaises(asyncio.CancelledError, loop.run_until_complete, t)562 def test_log_traceback(self):563 async def coro():564 pass565 task = self.new_task(self.loop, coro())566 with self.assertRaisesRegex(ValueError, 'can only be set to False'):567 task._log_traceback = True568 self.loop.run_until_complete(task)569 def test_wait_for_timeout_less_then_0_or_0_future_done(self):570 def gen():571 when = yield572 self.assertAlmostEqual(0, when)573 loop = self.new_test_loop(gen)574 fut = self.new_future(loop)575 fut.set_result('done')576 ret = loop.run_until_complete(asyncio.wait_for(fut, 0))577 self.assertEqual(ret, 'done')578 self.assertTrue(fut.done())579 self.assertAlmostEqual(0, loop.time())580 def test_wait_for_timeout_less_then_0_or_0_coroutine_do_not_started(self):581 def gen():582 when = yield583 self.assertAlmostEqual(0, when)584 loop = self.new_test_loop(gen)585 foo_started = False586 async def foo():587 nonlocal foo_started588 foo_started = True589 with self.assertRaises(asyncio.TimeoutError):590 loop.run_until_complete(asyncio.wait_for(foo(), 0))591 self.assertAlmostEqual(0, loop.time())592 self.assertEqual(foo_started, False)593 def test_wait_for_timeout_less_then_0_or_0(self):594 def gen():595 when = yield596 self.assertAlmostEqual(0.2, when)597 when = yield 0598 self.assertAlmostEqual(0, when)599 for timeout in [0, -1]:600 with self.subTest(timeout=timeout):601 loop = self.new_test_loop(gen)602 foo_running = None603 async def foo():604 nonlocal foo_running605 foo_running = True606 try:607 await asyncio.sleep(0.2)608 finally:609 foo_running = False610 return 'done'611 fut = self.new_task(loop, foo())612 with self.assertRaises(asyncio.TimeoutError):613 loop.run_until_complete(asyncio.wait_for(fut, timeout))614 self.assertTrue(fut.done())615 # it should have been cancelled due to the timeout616 self.assertTrue(fut.cancelled())617 self.assertAlmostEqual(0, loop.time())618 self.assertEqual(foo_running, False)619 def test_wait_for(self):620 def gen():621 when = yield622 self.assertAlmostEqual(0.2, when)623 when = yield 0624 self.assertAlmostEqual(0.1, when)625 when = yield 0.1626 loop = self.new_test_loop(gen)627 foo_running = None628 async def foo():629 nonlocal foo_running630 foo_running = True631 try:632 await asyncio.sleep(0.2)633 finally:634 foo_running = False635 return 'done'636 fut = self.new_task(loop, foo())637 with self.assertRaises(asyncio.TimeoutError):638 loop.run_until_complete(asyncio.wait_for(fut, 0.1))639 self.assertTrue(fut.done())640 # it should have been cancelled due to the timeout641 self.assertTrue(fut.cancelled())642 self.assertAlmostEqual(0.1, loop.time())643 self.assertEqual(foo_running, False)644 def test_wait_for_blocking(self):645 loop = self.new_test_loop()646 async def coro():647 return 'done'648 res = loop.run_until_complete(asyncio.wait_for(coro(), timeout=None))649 self.assertEqual(res, 'done')650 def test_wait_for_with_global_loop(self):651 def gen():652 when = yield653 self.assertAlmostEqual(0.2, when)654 when = yield 0655 self.assertAlmostEqual(0.01, when)656 yield 0.01657 loop = self.new_test_loop(gen)658 async def foo():659 await asyncio.sleep(0.2)660 return 'done'661 asyncio.set_event_loop(loop)662 try:663 fut = self.new_task(loop, foo())664 with self.assertRaises(asyncio.TimeoutError):665 loop.run_until_complete(asyncio.wait_for(fut, 0.01))666 finally:667 asyncio.set_event_loop(None)668 self.assertAlmostEqual(0.01, loop.time())669 self.assertTrue(fut.done())670 self.assertTrue(fut.cancelled())671 def test_wait_for_race_condition(self):672 def gen():673 yield 0.1674 yield 0.1675 yield 0.1676 loop = self.new_test_loop(gen)677 fut = self.new_future(loop)678 task = asyncio.wait_for(fut, timeout=0.2)679 loop.call_later(0.1, fut.set_result, "ok")680 res = loop.run_until_complete(task)681 self.assertEqual(res, "ok")682 def test_wait_for_waits_for_task_cancellation(self):683 loop = asyncio.new_event_loop()684 self.addCleanup(loop.close)685 task_done = False686 async def foo():687 async def inner():688 nonlocal task_done689 try:690 await asyncio.sleep(0.2)691 finally:692 task_done = True693 inner_task = self.new_task(loop, inner())694 with self.assertRaises(asyncio.TimeoutError):695 await asyncio.wait_for(inner_task, timeout=0.1)696 self.assertTrue(task_done)697 loop.run_until_complete(foo())698 def test_wait_for_self_cancellation(self):699 loop = asyncio.new_event_loop()700 self.addCleanup(loop.close)701 async def foo():702 async def inner():703 try:704 await asyncio.sleep(0.3)705 except asyncio.CancelledError:706 try:707 await asyncio.sleep(0.3)708 except asyncio.CancelledError:709 await asyncio.sleep(0.3)710 return 42711 inner_task = self.new_task(loop, inner())712 wait = asyncio.wait_for(inner_task, timeout=0.1)713 # Test that wait_for itself is properly cancellable714 # even when the initial task holds up the initial cancellation.715 task = self.new_task(loop, wait)716 await asyncio.sleep(0.2)717 task.cancel()718 with self.assertRaises(asyncio.CancelledError):719 await task720 self.assertEqual(await inner_task, 42)721 loop.run_until_complete(foo())722 def test_wait(self):723 def gen():724 when = yield725 self.assertAlmostEqual(0.1, when)726 when = yield 0727 self.assertAlmostEqual(0.15, when)728 yield 0.15729 loop = self.new_test_loop(gen)730 a = self.new_task(loop, asyncio.sleep(0.1))731 b = self.new_task(loop, asyncio.sleep(0.15))732 async def foo():733 done, pending = await asyncio.wait([b, a])734 self.assertEqual(done, set([a, b]))735 self.assertEqual(pending, set())736 return 42737 res = loop.run_until_complete(self.new_task(loop, foo()))738 self.assertEqual(res, 42)739 self.assertAlmostEqual(0.15, loop.time())740 # Doing it again should take no time and exercise a different path.741 res = loop.run_until_complete(self.new_task(loop, foo()))742 self.assertAlmostEqual(0.15, loop.time())743 self.assertEqual(res, 42)744 def test_wait_with_global_loop(self):745 def gen():746 when = yield747 self.assertAlmostEqual(0.01, when)748 when = yield 0749 self.assertAlmostEqual(0.015, when)750 yield 0.015751 loop = self.new_test_loop(gen)752 a = self.new_task(loop, asyncio.sleep(0.01))753 b = self.new_task(loop, asyncio.sleep(0.015))754 async def foo():755 done, pending = await asyncio.wait([b, a])756 self.assertEqual(done, set([a, b]))757 self.assertEqual(pending, set())758 return 42759 asyncio.set_event_loop(loop)760 res = loop.run_until_complete(761 self.new_task(loop, foo()))762 self.assertEqual(res, 42)763 def test_wait_duplicate_coroutines(self):764 with self.assertWarns(DeprecationWarning):765 @asyncio.coroutine766 def coro(s):767 return s768 c = coro('test')769 task =self.new_task(770 self.loop,771 asyncio.wait([c, c, coro('spam')]))772 done, pending = self.loop.run_until_complete(task)773 self.assertFalse(pending)774 self.assertEqual(set(f.result() for f in done), {'test', 'spam'})775 def test_wait_errors(self):776 self.assertRaises(777 ValueError, self.loop.run_until_complete,778 asyncio.wait(set()))779 # -1 is an invalid return_when value780 sleep_coro = asyncio.sleep(10.0)781 wait_coro = asyncio.wait([sleep_coro], return_when=-1)782 self.assertRaises(ValueError,783 self.loop.run_until_complete, wait_coro)784 sleep_coro.close()785 def test_wait_first_completed(self):786 def gen():787 when = yield788 self.assertAlmostEqual(10.0, when)789 when = yield 0790 self.assertAlmostEqual(0.1, when)791 yield 0.1792 loop = self.new_test_loop(gen)793 a = self.new_task(loop, asyncio.sleep(10.0))794 b = self.new_task(loop, asyncio.sleep(0.1))795 task = self.new_task(796 loop,797 asyncio.wait([b, a], return_when=asyncio.FIRST_COMPLETED))798 done, pending = loop.run_until_complete(task)799 self.assertEqual({b}, done)800 self.assertEqual({a}, pending)801 self.assertFalse(a.done())802 self.assertTrue(b.done())803 self.assertIsNone(b.result())804 self.assertAlmostEqual(0.1, loop.time())805 # move forward to close generator806 loop.advance_time(10)807 loop.run_until_complete(asyncio.wait([a, b]))808 def test_wait_really_done(self):809 # there is possibility that some tasks in the pending list810 # became done but their callbacks haven't all been called yet811 async def coro1():812 await asyncio.sleep(0)813 async def coro2():814 await asyncio.sleep(0)815 await asyncio.sleep(0)816 a = self.new_task(self.loop, coro1())817 b = self.new_task(self.loop, coro2())818 task = self.new_task(819 self.loop,820 asyncio.wait([b, a], return_when=asyncio.FIRST_COMPLETED))821 done, pending = self.loop.run_until_complete(task)822 self.assertEqual({a, b}, done)823 self.assertTrue(a.done())824 self.assertIsNone(a.result())825 self.assertTrue(b.done())826 self.assertIsNone(b.result())827 def test_wait_first_exception(self):828 def gen():829 when = yield830 self.assertAlmostEqual(10.0, when)831 yield 0832 loop = self.new_test_loop(gen)833 # first_exception, task already has exception834 a = self.new_task(loop, asyncio.sleep(10.0))835 async def exc():836 raise ZeroDivisionError('err')837 b = self.new_task(loop, exc())838 task = self.new_task(839 loop,840 asyncio.wait([b, a], return_when=asyncio.FIRST_EXCEPTION))841 done, pending = loop.run_until_complete(task)842 self.assertEqual({b}, done)843 self.assertEqual({a}, pending)844 self.assertAlmostEqual(0, loop.time())845 # move forward to close generator846 loop.advance_time(10)847 loop.run_until_complete(asyncio.wait([a, b]))848 def test_wait_first_exception_in_wait(self):849 def gen():850 when = yield851 self.assertAlmostEqual(10.0, when)852 when = yield 0853 self.assertAlmostEqual(0.01, when)854 yield 0.01855 loop = self.new_test_loop(gen)856 # first_exception, exception during waiting857 a = self.new_task(loop, asyncio.sleep(10.0))858 async def exc():859 await asyncio.sleep(0.01)860 raise ZeroDivisionError('err')861 b = self.new_task(loop, exc())862 task = asyncio.wait([b, a], return_when=asyncio.FIRST_EXCEPTION)863 done, pending = loop.run_until_complete(task)864 self.assertEqual({b}, done)865 self.assertEqual({a}, pending)866 self.assertAlmostEqual(0.01, loop.time())867 # move forward to close generator868 loop.advance_time(10)869 loop.run_until_complete(asyncio.wait([a, b]))870 def test_wait_with_exception(self):871 def gen():872 when = yield873 self.assertAlmostEqual(0.1, when)874 when = yield 0875 self.assertAlmostEqual(0.15, when)876 yield 0.15877 loop = self.new_test_loop(gen)878 a = self.new_task(loop, asyncio.sleep(0.1))879 async def sleeper():880 await asyncio.sleep(0.15)881 raise ZeroDivisionError('really')882 b = self.new_task(loop, sleeper())883 async def foo():884 done, pending = await asyncio.wait([b, a])885 self.assertEqual(len(done), 2)886 self.assertEqual(pending, set())887 errors = set(f for f in done if f.exception() is not None)888 self.assertEqual(len(errors), 1)889 loop.run_until_complete(self.new_task(loop, foo()))890 self.assertAlmostEqual(0.15, loop.time())891 loop.run_until_complete(self.new_task(loop, foo()))892 self.assertAlmostEqual(0.15, loop.time())893 def test_wait_with_timeout(self):894 def gen():895 when = yield896 self.assertAlmostEqual(0.1, when)897 when = yield 0898 self.assertAlmostEqual(0.15, when)899 when = yield 0900 self.assertAlmostEqual(0.11, when)901 yield 0.11902 loop = self.new_test_loop(gen)903 a = self.new_task(loop, asyncio.sleep(0.1))904 b = self.new_task(loop, asyncio.sleep(0.15))905 async def foo():906 done, pending = await asyncio.wait([b, a], timeout=0.11)907 self.assertEqual(done, set([a]))908 self.assertEqual(pending, set([b]))909 loop.run_until_complete(self.new_task(loop, foo()))910 self.assertAlmostEqual(0.11, loop.time())911 # move forward to close generator912 loop.advance_time(10)913 loop.run_until_complete(asyncio.wait([a, b]))914 def test_wait_concurrent_complete(self):915 def gen():916 when = yield917 self.assertAlmostEqual(0.1, when)918 when = yield 0919 self.assertAlmostEqual(0.15, when)920 when = yield 0921 self.assertAlmostEqual(0.1, when)922 yield 0.1923 loop = self.new_test_loop(gen)924 a = self.new_task(loop, asyncio.sleep(0.1))925 b = self.new_task(loop, asyncio.sleep(0.15))926 done, pending = loop.run_until_complete(927 asyncio.wait([b, a], timeout=0.1))928 self.assertEqual(done, set([a]))929 self.assertEqual(pending, set([b]))930 self.assertAlmostEqual(0.1, loop.time())931 # move forward to close generator932 loop.advance_time(10)933 loop.run_until_complete(asyncio.wait([a, b]))934 def test_as_completed(self):935 def gen():936 yield 0937 yield 0938 yield 0.01939 yield 0940 loop = self.new_test_loop(gen)941 # disable "slow callback" warning942 loop.slow_callback_duration = 1.0943 completed = set()944 time_shifted = False945 with self.assertWarns(DeprecationWarning):946 @asyncio.coroutine947 def sleeper(dt, x):948 nonlocal time_shifted949 yield from asyncio.sleep(dt)950 completed.add(x)951 if not time_shifted and 'a' in completed and 'b' in completed:952 time_shifted = True953 loop.advance_time(0.14)954 return x955 a = sleeper(0.01, 'a')956 b = sleeper(0.01, 'b')957 c = sleeper(0.15, 'c')958 async def foo():959 values = []960 for f in asyncio.as_completed([b, c, a], loop=loop):961 values.append(await f)962 return values963 with self.assertWarns(DeprecationWarning):964 res = loop.run_until_complete(self.new_task(loop, foo()))965 self.assertAlmostEqual(0.15, loop.time())966 self.assertTrue('a' in res[:2])967 self.assertTrue('b' in res[:2])968 self.assertEqual(res[2], 'c')969 # Doing it again should take no time and exercise a different path.970 with self.assertWarns(DeprecationWarning):971 res = loop.run_until_complete(self.new_task(loop, foo()))972 self.assertAlmostEqual(0.15, loop.time())973 def test_as_completed_with_timeout(self):974 def gen():975 yield976 yield 0977 yield 0978 yield 0.1979 loop = self.new_test_loop(gen)980 a = loop.create_task(asyncio.sleep(0.1, 'a'))981 b = loop.create_task(asyncio.sleep(0.15, 'b'))982 async def foo():983 values = []984 for f in asyncio.as_completed([a, b], timeout=0.12, loop=loop):985 if values:986 loop.advance_time(0.02)987 try:988 v = await f989 values.append((1, v))990 except asyncio.TimeoutError as exc:991 values.append((2, exc))992 return values993 with self.assertWarns(DeprecationWarning):994 res = loop.run_until_complete(self.new_task(loop, foo()))995 self.assertEqual(len(res), 2, res)996 self.assertEqual(res[0], (1, 'a'))997 self.assertEqual(res[1][0], 2)998 self.assertIsInstance(res[1][1], asyncio.TimeoutError)999 self.assertAlmostEqual(0.12, loop.time())1000 # move forward to close generator1001 loop.advance_time(10)1002 loop.run_until_complete(asyncio.wait([a, b]))1003 def test_as_completed_with_unused_timeout(self):1004 def gen():1005 yield1006 yield 01007 yield 0.011008 loop = self.new_test_loop(gen)1009 a = asyncio.sleep(0.01, 'a')1010 async def foo():1011 for f in asyncio.as_completed([a], timeout=1, loop=loop):1012 v = await f1013 self.assertEqual(v, 'a')1014 with self.assertWarns(DeprecationWarning):1015 loop.run_until_complete(self.new_task(loop, foo()))1016 def test_as_completed_reverse_wait(self):1017 def gen():1018 yield 01019 yield 0.051020 yield 01021 loop = self.new_test_loop(gen)1022 a = asyncio.sleep(0.05, 'a')1023 b = asyncio.sleep(0.10, 'b')1024 fs = {a, b}1025 with self.assertWarns(DeprecationWarning):1026 futs = list(asyncio.as_completed(fs, loop=loop))1027 self.assertEqual(len(futs), 2)1028 x = loop.run_until_complete(futs[1])1029 self.assertEqual(x, 'a')1030 self.assertAlmostEqual(0.05, loop.time())1031 loop.advance_time(0.05)1032 y = loop.run_until_complete(futs[0])1033 self.assertEqual(y, 'b')1034 self.assertAlmostEqual(0.10, loop.time())1035 def test_as_completed_concurrent(self):1036 def gen():1037 when = yield1038 self.assertAlmostEqual(0.05, when)1039 when = yield 01040 self.assertAlmostEqual(0.05, when)1041 yield 0.051042 loop = self.new_test_loop(gen)1043 a = asyncio.sleep(0.05, 'a')1044 b = asyncio.sleep(0.05, 'b')1045 fs = {a, b}1046 with self.assertWarns(DeprecationWarning):1047 futs = list(asyncio.as_completed(fs, loop=loop))1048 self.assertEqual(len(futs), 2)1049 waiter = asyncio.wait(futs)1050 done, pending = loop.run_until_complete(waiter)1051 self.assertEqual(set(f.result() for f in done), {'a', 'b'})1052 def test_as_completed_duplicate_coroutines(self):1053 with self.assertWarns(DeprecationWarning):1054 @asyncio.coroutine1055 def coro(s):1056 return s1057 with self.assertWarns(DeprecationWarning):1058 @asyncio.coroutine1059 def runner():1060 result = []1061 c = coro('ham')1062 for f in asyncio.as_completed([c, c, coro('spam')],1063 loop=self.loop):1064 result.append((yield from f))1065 return result1066 with self.assertWarns(DeprecationWarning):1067 fut = self.new_task(self.loop, runner())1068 self.loop.run_until_complete(fut)1069 result = fut.result()1070 self.assertEqual(set(result), {'ham', 'spam'})1071 self.assertEqual(len(result), 2)1072 def test_sleep(self):1073 def gen():1074 when = yield1075 self.assertAlmostEqual(0.05, when)1076 when = yield 0.051077 self.assertAlmostEqual(0.1, when)1078 yield 0.051079 loop = self.new_test_loop(gen)1080 async def sleeper(dt, arg):1081 await asyncio.sleep(dt/2)1082 res = await asyncio.sleep(dt/2, arg)1083 return res1084 t = self.new_task(loop, sleeper(0.1, 'yeah'))1085 loop.run_until_complete(t)1086 self.assertTrue(t.done())1087 self.assertEqual(t.result(), 'yeah')1088 self.assertAlmostEqual(0.1, loop.time())1089 def test_sleep_cancel(self):1090 def gen():1091 when = yield1092 self.assertAlmostEqual(10.0, when)1093 yield 01094 loop = self.new_test_loop(gen)1095 t = self.new_task(loop, asyncio.sleep(10.0, 'yeah'))1096 handle = None1097 orig_call_later = loop.call_later1098 def call_later(delay, callback, *args):1099 nonlocal handle1100 handle = orig_call_later(delay, callback, *args)1101 return handle1102 loop.call_later = call_later1103 test_utils.run_briefly(loop)1104 self.assertFalse(handle._cancelled)1105 t.cancel()1106 test_utils.run_briefly(loop)1107 self.assertTrue(handle._cancelled)1108 def test_task_cancel_sleeping_task(self):1109 def gen():1110 when = yield1111 self.assertAlmostEqual(0.1, when)1112 when = yield 01113 self.assertAlmostEqual(5000, when)1114 yield 0.11115 loop = self.new_test_loop(gen)1116 async def sleep(dt):1117 await asyncio.sleep(dt)1118 async def doit():1119 sleeper = self.new_task(loop, sleep(5000))1120 loop.call_later(0.1, sleeper.cancel)1121 try:1122 await sleeper1123 except asyncio.CancelledError:1124 return 'cancelled'1125 else:1126 return 'slept in'1127 doer = doit()1128 self.assertEqual(loop.run_until_complete(doer), 'cancelled')1129 self.assertAlmostEqual(0.1, loop.time())1130 def test_task_cancel_waiter_future(self):1131 fut = self.new_future(self.loop)1132 async def coro():1133 await fut1134 task = self.new_task(self.loop, coro())1135 test_utils.run_briefly(self.loop)1136 self.assertIs(task._fut_waiter, fut)1137 task.cancel()1138 test_utils.run_briefly(self.loop)1139 self.assertRaises(1140 asyncio.CancelledError, self.loop.run_until_complete, task)1141 self.assertIsNone(task._fut_waiter)1142 self.assertTrue(fut.cancelled())1143 def test_task_set_methods(self):1144 async def notmuch():1145 return 'ko'1146 gen = notmuch()1147 task = self.new_task(self.loop, gen)1148 with self.assertRaisesRegex(RuntimeError, 'not support set_result'):1149 task.set_result('ok')1150 with self.assertRaisesRegex(RuntimeError, 'not support set_exception'):1151 task.set_exception(ValueError())1152 self.assertEqual(1153 self.loop.run_until_complete(task),1154 'ko')1155 def test_step_result(self):1156 with self.assertWarns(DeprecationWarning):1157 @asyncio.coroutine1158 def notmuch():1159 yield None1160 yield 11161 return 'ko'1162 self.assertRaises(1163 RuntimeError, self.loop.run_until_complete, notmuch())1164 def test_step_result_future(self):1165 # If coroutine returns future, task waits on this future.1166 class Fut(asyncio.Future):1167 def __init__(self, *args, **kwds):1168 self.cb_added = False1169 super().__init__(*args, **kwds)1170 def add_done_callback(self, *args, **kwargs):1171 self.cb_added = True1172 super().add_done_callback(*args, **kwargs)1173 fut = Fut(loop=self.loop)1174 result = None1175 async def wait_for_future():1176 nonlocal result1177 result = await fut1178 t = self.new_task(self.loop, wait_for_future())1179 test_utils.run_briefly(self.loop)1180 self.assertTrue(fut.cb_added)1181 res = object()1182 fut.set_result(res)1183 test_utils.run_briefly(self.loop)1184 self.assertIs(res, result)1185 self.assertTrue(t.done())1186 self.assertIsNone(t.result())1187 def test_baseexception_during_cancel(self):1188 def gen():1189 when = yield1190 self.assertAlmostEqual(10.0, when)1191 yield 01192 loop = self.new_test_loop(gen)1193 async def sleeper():1194 await asyncio.sleep(10)1195 base_exc = SystemExit()1196 async def notmutch():1197 try:1198 await sleeper()1199 except asyncio.CancelledError:1200 raise base_exc1201 task = self.new_task(loop, notmutch())1202 test_utils.run_briefly(loop)1203 task.cancel()1204 self.assertFalse(task.done())1205 self.assertRaises(SystemExit, test_utils.run_briefly, loop)1206 self.assertTrue(task.done())1207 self.assertFalse(task.cancelled())1208 self.assertIs(task.exception(), base_exc)1209 def test_iscoroutinefunction(self):1210 def fn():1211 pass1212 self.assertFalse(asyncio.iscoroutinefunction(fn))1213 def fn1():1214 yield1215 self.assertFalse(asyncio.iscoroutinefunction(fn1))1216 with self.assertWarns(DeprecationWarning):1217 @asyncio.coroutine1218 def fn2():1219 yield1220 self.assertTrue(asyncio.iscoroutinefunction(fn2))1221 self.assertFalse(asyncio.iscoroutinefunction(mock.Mock()))1222 def test_yield_vs_yield_from(self):1223 fut = self.new_future(self.loop)1224 with self.assertWarns(DeprecationWarning):1225 @asyncio.coroutine1226 def wait_for_future():1227 yield fut1228 task = wait_for_future()1229 with self.assertRaises(RuntimeError):1230 self.loop.run_until_complete(task)1231 self.assertFalse(fut.done())1232 def test_yield_vs_yield_from_generator(self):1233 with self.assertWarns(DeprecationWarning):1234 @asyncio.coroutine1235 def coro():1236 yield1237 with self.assertWarns(DeprecationWarning):1238 @asyncio.coroutine1239 def wait_for_future():1240 gen = coro()1241 try:1242 yield gen1243 finally:1244 gen.close()1245 task = wait_for_future()1246 self.assertRaises(1247 RuntimeError,1248 self.loop.run_until_complete, task)1249 def test_coroutine_non_gen_function(self):1250 with self.assertWarns(DeprecationWarning):1251 @asyncio.coroutine1252 def func():1253 return 'test'1254 self.assertTrue(asyncio.iscoroutinefunction(func))1255 coro = func()1256 self.assertTrue(asyncio.iscoroutine(coro))1257 res = self.loop.run_until_complete(coro)1258 self.assertEqual(res, 'test')1259 def test_coroutine_non_gen_function_return_future(self):1260 fut = self.new_future(self.loop)1261 with self.assertWarns(DeprecationWarning):1262 @asyncio.coroutine1263 def func():1264 return fut1265 async def coro():1266 fut.set_result('test')1267 t1 = self.new_task(self.loop, func())1268 t2 = self.new_task(self.loop, coro())1269 res = self.loop.run_until_complete(t1)1270 self.assertEqual(res, 'test')1271 self.assertIsNone(t2.result())1272 def test_current_task_deprecated(self):1273 Task = self.__class__.Task1274 with self.assertWarns(DeprecationWarning):1275 self.assertIsNone(Task.current_task(loop=self.loop))1276 async def coro(loop):1277 with self.assertWarns(DeprecationWarning):1278 self.assertIs(Task.current_task(loop=loop), task)1279 # See http://bugs.python.org/issue29271 for details:1280 asyncio.set_event_loop(loop)1281 try:1282 with self.assertWarns(DeprecationWarning):1283 self.assertIs(Task.current_task(None), task)1284 with self.assertWarns(DeprecationWarning):1285 self.assertIs(Task.current_task(), task)1286 finally:1287 asyncio.set_event_loop(None)1288 task = self.new_task(self.loop, coro(self.loop))1289 self.loop.run_until_complete(task)1290 with self.assertWarns(DeprecationWarning):1291 self.assertIsNone(Task.current_task(loop=self.loop))1292 def test_current_task(self):1293 self.assertIsNone(asyncio.current_task(loop=self.loop))1294 async def coro(loop):1295 self.assertIs(asyncio.current_task(loop=loop), task)1296 self.assertIs(asyncio.current_task(None), task)1297 self.assertIs(asyncio.current_task(), task)1298 task = self.new_task(self.loop, coro(self.loop))1299 self.loop.run_until_complete(task)1300 self.assertIsNone(asyncio.current_task(loop=self.loop))1301 def test_current_task_with_interleaving_tasks(self):1302 self.assertIsNone(asyncio.current_task(loop=self.loop))1303 fut1 = self.new_future(self.loop)1304 fut2 = self.new_future(self.loop)1305 async def coro1(loop):1306 self.assertTrue(asyncio.current_task(loop=loop) is task1)1307 await fut11308 self.assertTrue(asyncio.current_task(loop=loop) is task1)1309 fut2.set_result(True)1310 async def coro2(loop):1311 self.assertTrue(asyncio.current_task(loop=loop) is task2)1312 fut1.set_result(True)1313 await fut21314 self.assertTrue(asyncio.current_task(loop=loop) is task2)1315 task1 = self.new_task(self.loop, coro1(self.loop))1316 task2 = self.new_task(self.loop, coro2(self.loop))1317 self.loop.run_until_complete(asyncio.wait((task1, task2)))1318 self.assertIsNone(asyncio.current_task(loop=self.loop))1319 # Some thorough tests for cancellation propagation through1320 # coroutines, tasks and wait().1321 def test_yield_future_passes_cancel(self):1322 # Cancelling outer() cancels inner() cancels waiter.1323 proof = 01324 waiter = self.new_future(self.loop)1325 async def inner():1326 nonlocal proof1327 try:1328 await waiter1329 except asyncio.CancelledError:1330 proof += 11331 raise1332 else:1333 self.fail('got past sleep() in inner()')1334 async def outer():1335 nonlocal proof1336 try:1337 await inner()1338 except asyncio.CancelledError:1339 proof += 100 # Expect this path.1340 else:1341 proof += 101342 f = asyncio.ensure_future(outer(), loop=self.loop)1343 test_utils.run_briefly(self.loop)1344 f.cancel()1345 self.loop.run_until_complete(f)1346 self.assertEqual(proof, 101)1347 self.assertTrue(waiter.cancelled())1348 def test_yield_wait_does_not_shield_cancel(self):1349 # Cancelling outer() makes wait() return early, leaves inner()1350 # running.1351 proof = 01352 waiter = self.new_future(self.loop)1353 async def inner():1354 nonlocal proof1355 await waiter1356 proof += 11357 async def outer():1358 nonlocal proof1359 d, p = await asyncio.wait([inner()])1360 proof += 1001361 f = asyncio.ensure_future(outer(), loop=self.loop)1362 test_utils.run_briefly(self.loop)1363 f.cancel()1364 self.assertRaises(1365 asyncio.CancelledError, self.loop.run_until_complete, f)1366 waiter.set_result(None)1367 test_utils.run_briefly(self.loop)1368 self.assertEqual(proof, 1)1369 def test_shield_result(self):1370 inner = self.new_future(self.loop)1371 outer = asyncio.shield(inner)1372 inner.set_result(42)1373 res = self.loop.run_until_complete(outer)1374 self.assertEqual(res, 42)1375 def test_shield_exception(self):1376 inner = self.new_future(self.loop)1377 outer = asyncio.shield(inner)1378 test_utils.run_briefly(self.loop)1379 exc = RuntimeError('expected')1380 inner.set_exception(exc)1381 test_utils.run_briefly(self.loop)1382 self.assertIs(outer.exception(), exc)1383 def test_shield_cancel_inner(self):1384 inner = self.new_future(self.loop)1385 outer = asyncio.shield(inner)1386 test_utils.run_briefly(self.loop)1387 inner.cancel()1388 test_utils.run_briefly(self.loop)1389 self.assertTrue(outer.cancelled())1390 def test_shield_cancel_outer(self):1391 inner = self.new_future(self.loop)1392 outer = asyncio.shield(inner)1393 test_utils.run_briefly(self.loop)1394 outer.cancel()1395 test_utils.run_briefly(self.loop)1396 self.assertTrue(outer.cancelled())1397 self.assertEqual(0, 0 if outer._callbacks is None else len(outer._callbacks))1398 def test_shield_shortcut(self):1399 fut = self.new_future(self.loop)1400 fut.set_result(42)1401 res = self.loop.run_until_complete(asyncio.shield(fut))1402 self.assertEqual(res, 42)1403 def test_shield_effect(self):1404 # Cancelling outer() does not affect inner().1405 proof = 01406 waiter = self.new_future(self.loop)1407 async def inner():1408 nonlocal proof1409 await waiter1410 proof += 11411 async def outer():1412 nonlocal proof1413 await asyncio.shield(inner())1414 proof += 1001415 f = asyncio.ensure_future(outer(), loop=self.loop)1416 test_utils.run_briefly(self.loop)1417 f.cancel()1418 with self.assertRaises(asyncio.CancelledError):1419 self.loop.run_until_complete(f)1420 waiter.set_result(None)1421 test_utils.run_briefly(self.loop)1422 self.assertEqual(proof, 1)1423 def test_shield_gather(self):1424 child1 = self.new_future(self.loop)1425 child2 = self.new_future(self.loop)1426 parent = asyncio.gather(child1, child2)1427 outer = asyncio.shield(parent)1428 test_utils.run_briefly(self.loop)1429 outer.cancel()1430 test_utils.run_briefly(self.loop)1431 self.assertTrue(outer.cancelled())1432 child1.set_result(1)1433 child2.set_result(2)1434 test_utils.run_briefly(self.loop)1435 self.assertEqual(parent.result(), [1, 2])1436 def test_gather_shield(self):1437 child1 = self.new_future(self.loop)1438 child2 = self.new_future(self.loop)1439 inner1 = asyncio.shield(child1)1440 inner2 = asyncio.shield(child2)1441 parent = asyncio.gather(inner1, inner2)1442 test_utils.run_briefly(self.loop)1443 parent.cancel()1444 # This should cancel inner1 and inner2 but bot child1 and child2.1445 test_utils.run_briefly(self.loop)1446 self.assertIsInstance(parent.exception(), asyncio.CancelledError)1447 self.assertTrue(inner1.cancelled())1448 self.assertTrue(inner2.cancelled())1449 child1.set_result(1)1450 child2.set_result(2)1451 test_utils.run_briefly(self.loop)1452 def test_as_completed_invalid_args(self):1453 fut = self.new_future(self.loop)1454 # as_completed() expects a list of futures, not a future instance1455 self.assertRaises(TypeError, self.loop.run_until_complete,1456 asyncio.as_completed(fut, loop=self.loop))1457 coro = coroutine_function()1458 self.assertRaises(TypeError, self.loop.run_until_complete,1459 asyncio.as_completed(coro, loop=self.loop))1460 coro.close()1461 def test_wait_invalid_args(self):1462 fut = self.new_future(self.loop)1463 # wait() expects a list of futures, not a future instance1464 self.assertRaises(TypeError, self.loop.run_until_complete,1465 asyncio.wait(fut))1466 coro = coroutine_function()1467 self.assertRaises(TypeError, self.loop.run_until_complete,1468 asyncio.wait(coro))1469 coro.close()1470 # wait() expects at least a future1471 self.assertRaises(ValueError, self.loop.run_until_complete,1472 asyncio.wait([]))1473 def test_corowrapper_mocks_generator(self):1474 def check():1475 # A function that asserts various things.1476 # Called twice, with different debug flag values.1477 with self.assertWarns(DeprecationWarning):1478 @asyncio.coroutine1479 def coro():1480 # The actual coroutine.1481 self.assertTrue(gen.gi_running)1482 yield from fut1483 # A completed Future used to run the coroutine.1484 fut = self.new_future(self.loop)1485 fut.set_result(None)1486 # Call the coroutine.1487 gen = coro()1488 # Check some properties.1489 self.assertTrue(asyncio.iscoroutine(gen))1490 self.assertIsInstance(gen.gi_frame, types.FrameType)1491 self.assertFalse(gen.gi_running)1492 self.assertIsInstance(gen.gi_code, types.CodeType)1493 # Run it.1494 self.loop.run_until_complete(gen)1495 # The frame should have changed.1496 self.assertIsNone(gen.gi_frame)1497 # Test with debug flag cleared.1498 with set_coroutine_debug(False):1499 check()1500 # Test with debug flag set.1501 with set_coroutine_debug(True):1502 check()1503 def test_yield_from_corowrapper(self):1504 with set_coroutine_debug(True):1505 with self.assertWarns(DeprecationWarning):1506 @asyncio.coroutine1507 def t1():1508 return (yield from t2())1509 with self.assertWarns(DeprecationWarning):1510 @asyncio.coroutine1511 def t2():1512 f = self.new_future(self.loop)1513 self.new_task(self.loop, t3(f))1514 return (yield from f)1515 with self.assertWarns(DeprecationWarning):1516 @asyncio.coroutine1517 def t3(f):1518 f.set_result((1, 2, 3))1519 task = self.new_task(self.loop, t1())1520 val = self.loop.run_until_complete(task)1521 self.assertEqual(val, (1, 2, 3))1522 def test_yield_from_corowrapper_send(self):1523 def foo():1524 a = yield1525 return a1526 def call(arg):1527 cw = asyncio.coroutines.CoroWrapper(foo())1528 cw.send(None)1529 try:1530 cw.send(arg)1531 except StopIteration as ex:1532 return ex.args[0]1533 else:1534 raise AssertionError('StopIteration was expected')1535 self.assertEqual(call((1, 2)), (1, 2))1536 self.assertEqual(call('spam'), 'spam')1537 def test_corowrapper_weakref(self):1538 wd = weakref.WeakValueDictionary()1539 def foo(): yield from []1540 cw = asyncio.coroutines.CoroWrapper(foo())1541 wd['cw'] = cw # Would fail without __weakref__ slot.1542 cw.gen = None # Suppress warning from __del__.1543 def test_corowrapper_throw(self):1544 # Issue 429: CoroWrapper.throw must be compatible with gen.throw1545 def foo():1546 value = None1547 while True:1548 try:1549 value = yield value1550 except Exception as e:1551 value = e1552 exception = Exception("foo")1553 cw = asyncio.coroutines.CoroWrapper(foo())1554 cw.send(None)1555 self.assertIs(exception, cw.throw(exception))1556 cw = asyncio.coroutines.CoroWrapper(foo())1557 cw.send(None)1558 self.assertIs(exception, cw.throw(Exception, exception))1559 cw = asyncio.coroutines.CoroWrapper(foo())1560 cw.send(None)1561 exception = cw.throw(Exception, "foo")1562 self.assertIsInstance(exception, Exception)1563 self.assertEqual(exception.args, ("foo", ))1564 cw = asyncio.coroutines.CoroWrapper(foo())1565 cw.send(None)1566 exception = cw.throw(Exception, "foo", None)1567 self.assertIsInstance(exception, Exception)1568 self.assertEqual(exception.args, ("foo", ))1569 def test_all_tasks_deprecated(self):1570 Task = self.__class__.Task1571 async def coro():1572 with self.assertWarns(DeprecationWarning):1573 assert Task.all_tasks(self.loop) == {t}1574 t = self.new_task(self.loop, coro())1575 self.loop.run_until_complete(t)1576 def test_log_destroyed_pending_task(self):1577 Task = self.__class__.Task1578 with self.assertWarns(DeprecationWarning):1579 @asyncio.coroutine1580 def kill_me(loop):1581 future = self.new_future(loop)1582 yield from future1583 # at this point, the only reference to kill_me() task is1584 # the Task._wakeup() method in future._callbacks1585 raise Exception("code never reached")1586 mock_handler = mock.Mock()1587 self.loop.set_debug(True)1588 self.loop.set_exception_handler(mock_handler)1589 # schedule the task1590 coro = kill_me(self.loop)1591 task = asyncio.ensure_future(coro, loop=self.loop)1592 self.assertEqual(asyncio.all_tasks(loop=self.loop), {task})1593 # See http://bugs.python.org/issue29271 for details:1594 asyncio.set_event_loop(self.loop)1595 try:1596 with self.assertWarns(DeprecationWarning):1597 self.assertEqual(Task.all_tasks(), {task})1598 with self.assertWarns(DeprecationWarning):1599 self.assertEqual(Task.all_tasks(None), {task})1600 finally:1601 asyncio.set_event_loop(None)1602 # execute the task so it waits for future1603 self.loop._run_once()1604 self.assertEqual(len(self.loop._ready), 0)1605 # remove the future used in kill_me(), and references to the task1606 del coro.gi_frame.f_locals['future']1607 coro = None1608 source_traceback = task._source_traceback1609 task = None1610 # no more reference to kill_me() task: the task is destroyed by the GC1611 support.gc_collect()1612 self.assertEqual(asyncio.all_tasks(loop=self.loop), set())1613 mock_handler.assert_called_with(self.loop, {1614 'message': 'Task was destroyed but it is pending!',1615 'task': mock.ANY,1616 'source_traceback': source_traceback,1617 })1618 mock_handler.reset_mock()1619 @mock.patch('asyncio.base_events.logger')1620 def test_tb_logger_not_called_after_cancel(self, m_log):1621 loop = asyncio.new_event_loop()1622 self.set_event_loop(loop)1623 async def coro():1624 raise TypeError1625 async def runner():1626 task = self.new_task(loop, coro())1627 await asyncio.sleep(0.05)1628 task.cancel()1629 task = None1630 loop.run_until_complete(runner())1631 self.assertFalse(m_log.error.called)1632 @mock.patch('asyncio.coroutines.logger')1633 def test_coroutine_never_yielded(self, m_log):1634 with set_coroutine_debug(True):1635 with self.assertWarns(DeprecationWarning):1636 @asyncio.coroutine1637 def coro_noop():1638 pass1639 tb_filename = __file__1640 tb_lineno = sys._getframe().f_lineno + 21641 # create a coroutine object but don't use it1642 coro_noop()1643 support.gc_collect()1644 self.assertTrue(m_log.error.called)1645 message = m_log.error.call_args[0][0]1646 func_filename, func_lineno = test_utils.get_function_source(coro_noop)1647 regex = (r'^<CoroWrapper %s\(?\)? .* at %s:%s, .*> '1648 r'was never yielded from\n'1649 r'Coroutine object created at \(most recent call last, truncated to \d+ last lines\):\n'1650 r'.*\n'1651 r' File "%s", line %s, in test_coroutine_never_yielded\n'1652 r' coro_noop\(\)$'1653 % (re.escape(coro_noop.__qualname__),1654 re.escape(func_filename), func_lineno,1655 re.escape(tb_filename), tb_lineno))1656 self.assertRegex(message, re.compile(regex, re.DOTALL))1657 def test_return_coroutine_from_coroutine(self):1658 """Return of @asyncio.coroutine()-wrapped function generator object1659 from @asyncio.coroutine()-wrapped function should have same effect as1660 returning generator object or Future."""1661 def check():1662 with self.assertWarns(DeprecationWarning):1663 @asyncio.coroutine1664 def outer_coro():1665 with self.assertWarns(DeprecationWarning):1666 @asyncio.coroutine1667 def inner_coro():1668 return 11669 return inner_coro()1670 result = self.loop.run_until_complete(outer_coro())1671 self.assertEqual(result, 1)1672 # Test with debug flag cleared.1673 with set_coroutine_debug(False):1674 check()1675 # Test with debug flag set.1676 with set_coroutine_debug(True):1677 check()1678 def test_task_source_traceback(self):1679 self.loop.set_debug(True)1680 task = self.new_task(self.loop, coroutine_function())1681 lineno = sys._getframe().f_lineno - 11682 self.assertIsInstance(task._source_traceback, list)1683 self.assertEqual(task._source_traceback[-2][:3],1684 (__file__,1685 lineno,1686 'test_task_source_traceback'))1687 self.loop.run_until_complete(task)1688 def _test_cancel_wait_for(self, timeout):1689 loop = asyncio.new_event_loop()1690 self.addCleanup(loop.close)1691 async def blocking_coroutine():1692 fut = self.new_future(loop)1693 # Block: fut result is never set1694 await fut1695 task = loop.create_task(blocking_coroutine())1696 wait = loop.create_task(asyncio.wait_for(task, timeout))1697 loop.call_soon(wait.cancel)1698 self.assertRaises(asyncio.CancelledError,1699 loop.run_until_complete, wait)1700 # Python issue #23219: cancelling the wait must also cancel the task1701 self.assertTrue(task.cancelled())1702 def test_cancel_blocking_wait_for(self):1703 self._test_cancel_wait_for(None)1704 def test_cancel_wait_for(self):1705 self._test_cancel_wait_for(60.0)1706 def test_cancel_gather_1(self):1707 """Ensure that a gathering future refuses to be cancelled once all1708 children are done"""1709 loop = asyncio.new_event_loop()1710 self.addCleanup(loop.close)1711 fut = self.new_future(loop)1712 # The indirection fut->child_coro is needed since otherwise the1713 # gathering task is done at the same time as the child future1714 def child_coro():1715 return (yield from fut)1716 gather_future = asyncio.gather(child_coro(), loop=loop)1717 gather_task = asyncio.ensure_future(gather_future, loop=loop)1718 cancel_result = None1719 def cancelling_callback(_):1720 nonlocal cancel_result1721 cancel_result = gather_task.cancel()1722 fut.add_done_callback(cancelling_callback)1723 fut.set_result(42) # calls the cancelling_callback after fut is done()1724 # At this point the task should complete.1725 loop.run_until_complete(gather_task)1726 # Python issue #26923: asyncio.gather drops cancellation1727 self.assertEqual(cancel_result, False)1728 self.assertFalse(gather_task.cancelled())1729 self.assertEqual(gather_task.result(), [42])1730 def test_cancel_gather_2(self):1731 loop = asyncio.new_event_loop()1732 self.addCleanup(loop.close)1733 async def test():1734 time = 01735 while True:1736 time += 0.051737 await asyncio.gather(asyncio.sleep(0.05),1738 return_exceptions=True,1739 loop=loop)1740 if time > 1:1741 return1742 async def main():1743 qwe = self.new_task(loop, test())1744 await asyncio.sleep(0.2)1745 qwe.cancel()1746 try:1747 await qwe1748 except asyncio.CancelledError:1749 pass1750 else:1751 self.fail('gather did not propagate the cancellation request')1752 loop.run_until_complete(main())1753 def test_exception_traceback(self):1754 # See http://bugs.python.org/issue288431755 async def foo():1756 1 / 01757 async def main():1758 task = self.new_task(self.loop, foo())1759 await asyncio.sleep(0) # skip one loop iteration1760 self.assertIsNotNone(task.exception().__traceback__)1761 self.loop.run_until_complete(main())1762 @mock.patch('asyncio.base_events.logger')1763 def test_error_in_call_soon(self, m_log):1764 def call_soon(callback, *args, **kwargs):1765 raise ValueError1766 self.loop.call_soon = call_soon1767 with self.assertWarns(DeprecationWarning):1768 @asyncio.coroutine1769 def coro():1770 pass1771 self.assertFalse(m_log.error.called)1772 with self.assertRaises(ValueError):1773 gen = coro()1774 try:1775 self.new_task(self.loop, gen)1776 finally:1777 gen.close()1778 self.assertTrue(m_log.error.called)1779 message = m_log.error.call_args[0][0]1780 self.assertIn('Task was destroyed but it is pending', message)1781 self.assertEqual(asyncio.all_tasks(self.loop), set())1782 def test_create_task_with_noncoroutine(self):1783 with self.assertRaisesRegex(TypeError,1784 "a coroutine was expected, got 123"):1785 self.new_task(self.loop, 123)1786 # test it for the second time to ensure that caching1787 # in asyncio.iscoroutine() doesn't break things.1788 with self.assertRaisesRegex(TypeError,1789 "a coroutine was expected, got 123"):1790 self.new_task(self.loop, 123)1791 def test_create_task_with_oldstyle_coroutine(self):1792 with self.assertWarns(DeprecationWarning):1793 @asyncio.coroutine1794 def coro():1795 pass1796 task = self.new_task(self.loop, coro())1797 self.assertIsInstance(task, self.Task)1798 self.loop.run_until_complete(task)1799 # test it for the second time to ensure that caching1800 # in asyncio.iscoroutine() doesn't break things.1801 task = self.new_task(self.loop, coro())1802 self.assertIsInstance(task, self.Task)1803 self.loop.run_until_complete(task)1804 def test_create_task_with_async_function(self):1805 async def coro():1806 pass1807 task = self.new_task(self.loop, coro())1808 self.assertIsInstance(task, self.Task)1809 self.loop.run_until_complete(task)1810 # test it for the second time to ensure that caching1811 # in asyncio.iscoroutine() doesn't break things.1812 task = self.new_task(self.loop, coro())1813 self.assertIsInstance(task, self.Task)1814 self.loop.run_until_complete(task)1815 def test_create_task_with_asynclike_function(self):1816 task = self.new_task(self.loop, CoroLikeObject())1817 self.assertIsInstance(task, self.Task)1818 self.assertEqual(self.loop.run_until_complete(task), 42)1819 # test it for the second time to ensure that caching1820 # in asyncio.iscoroutine() doesn't break things.1821 task = self.new_task(self.loop, CoroLikeObject())1822 self.assertIsInstance(task, self.Task)1823 self.assertEqual(self.loop.run_until_complete(task), 42)1824 def test_bare_create_task(self):1825 async def inner():1826 return 11827 async def coro():1828 task = asyncio.create_task(inner())1829 self.assertIsInstance(task, self.Task)1830 ret = await task1831 self.assertEqual(1, ret)1832 self.loop.run_until_complete(coro())1833 def test_bare_create_named_task(self):1834 async def coro_noop():1835 pass1836 async def coro():1837 task = asyncio.create_task(coro_noop(), name='No-op')1838 self.assertEqual(task.get_name(), 'No-op')1839 await task1840 self.loop.run_until_complete(coro())1841 def test_context_1(self):1842 cvar = contextvars.ContextVar('cvar', default='nope')1843 async def sub():1844 await asyncio.sleep(0.01)1845 self.assertEqual(cvar.get(), 'nope')1846 cvar.set('something else')1847 async def main():1848 self.assertEqual(cvar.get(), 'nope')1849 subtask = self.new_task(loop, sub())1850 cvar.set('yes')1851 self.assertEqual(cvar.get(), 'yes')1852 await subtask1853 self.assertEqual(cvar.get(), 'yes')1854 loop = asyncio.new_event_loop()1855 try:1856 task = self.new_task(loop, main())1857 loop.run_until_complete(task)1858 finally:1859 loop.close()1860 def test_context_2(self):1861 cvar = contextvars.ContextVar('cvar', default='nope')1862 async def main():1863 def fut_on_done(fut):1864 # This change must not pollute the context1865 # of the "main()" task.1866 cvar.set('something else')1867 self.assertEqual(cvar.get(), 'nope')1868 for j in range(2):1869 fut = self.new_future(loop)1870 fut.add_done_callback(fut_on_done)1871 cvar.set(f'yes{j}')1872 loop.call_soon(fut.set_result, None)1873 await fut1874 self.assertEqual(cvar.get(), f'yes{j}')1875 for i in range(3):1876 # Test that task passed its context to add_done_callback:1877 cvar.set(f'yes{i}-{j}')1878 await asyncio.sleep(0.001)1879 self.assertEqual(cvar.get(), f'yes{i}-{j}')1880 loop = asyncio.new_event_loop()1881 try:1882 task = self.new_task(loop, main())1883 loop.run_until_complete(task)1884 finally:1885 loop.close()1886 self.assertEqual(cvar.get(), 'nope')1887 def test_context_3(self):1888 # Run 100 Tasks in parallel, each modifying cvar.1889 cvar = contextvars.ContextVar('cvar', default=-1)1890 async def sub(num):1891 for i in range(10):1892 cvar.set(num + i)1893 await asyncio.sleep(random.uniform(0.001, 0.05))1894 self.assertEqual(cvar.get(), num + i)1895 async def main():1896 tasks = []1897 for i in range(100):1898 task = loop.create_task(sub(random.randint(0, 10)))1899 tasks.append(task)1900 await asyncio.gather(*tasks, loop=loop)1901 loop = asyncio.new_event_loop()1902 try:1903 loop.run_until_complete(main())1904 finally:1905 loop.close()1906 self.assertEqual(cvar.get(), -1)1907 def test_get_coro(self):1908 loop = asyncio.new_event_loop()1909 coro = coroutine_function()1910 try:1911 task = self.new_task(loop, coro)1912 loop.run_until_complete(task)1913 self.assertIs(task.get_coro(), coro)1914 finally:1915 loop.close()1916def add_subclass_tests(cls):1917 BaseTask = cls.Task1918 BaseFuture = cls.Future1919 if BaseTask is None or BaseFuture is None:1920 return cls1921 class CommonFuture:1922 def __init__(self, *args, **kwargs):1923 self.calls = collections.defaultdict(lambda: 0)1924 super().__init__(*args, **kwargs)1925 def add_done_callback(self, *args, **kwargs):1926 self.calls['add_done_callback'] += 11927 return super().add_done_callback(*args, **kwargs)1928 class Task(CommonFuture, BaseTask):1929 pass1930 class Future(CommonFuture, BaseFuture):1931 pass1932 def test_subclasses_ctask_cfuture(self):1933 fut = self.Future(loop=self.loop)1934 async def func():1935 self.loop.call_soon(lambda: fut.set_result('spam'))1936 return await fut1937 task = self.Task(func(), loop=self.loop)1938 result = self.loop.run_until_complete(task)1939 self.assertEqual(result, 'spam')1940 self.assertEqual(1941 dict(task.calls),1942 {'add_done_callback': 1})1943 self.assertEqual(1944 dict(fut.calls),1945 {'add_done_callback': 1})1946 # Add patched Task & Future back to the test case1947 cls.Task = Task1948 cls.Future = Future1949 # Add an extra unit-test1950 cls.test_subclasses_ctask_cfuture = test_subclasses_ctask_cfuture1951 # Disable the "test_task_source_traceback" test1952 # (the test is hardcoded for a particular call stack, which1953 # is slightly different for Task subclasses)1954 cls.test_task_source_traceback = None1955 return cls1956class SetMethodsTest:1957 def test_set_result_causes_invalid_state(self):1958 Future = type(self).Future1959 self.loop.call_exception_handler = exc_handler = mock.Mock()1960 async def foo():1961 await asyncio.sleep(0.1)1962 return 101963 coro = foo()1964 task = self.new_task(self.loop, coro)1965 Future.set_result(task, 'spam')1966 self.assertEqual(1967 self.loop.run_until_complete(task),1968 'spam')1969 exc_handler.assert_called_once()1970 exc = exc_handler.call_args[0][0]['exception']1971 with self.assertRaisesRegex(asyncio.InvalidStateError,1972 r'step\(\): already done'):1973 raise exc1974 coro.close()1975 def test_set_exception_causes_invalid_state(self):1976 class MyExc(Exception):1977 pass1978 Future = type(self).Future1979 self.loop.call_exception_handler = exc_handler = mock.Mock()1980 async def foo():1981 await asyncio.sleep(0.1)1982 return 101983 coro = foo()1984 task = self.new_task(self.loop, coro)1985 Future.set_exception(task, MyExc())1986 with self.assertRaises(MyExc):1987 self.loop.run_until_complete(task)1988 exc_handler.assert_called_once()1989 exc = exc_handler.call_args[0][0]['exception']1990 with self.assertRaisesRegex(asyncio.InvalidStateError,1991 r'step\(\): already done'):1992 raise exc1993 coro.close()1994@unittest.skipUnless(hasattr(futures, '_CFuture') and1995 hasattr(tasks, '_CTask'),1996 'requires the C _asyncio module')1997class CTask_CFuture_Tests(BaseTaskTests, SetMethodsTest,1998 test_utils.TestCase):1999 Task = getattr(tasks, '_CTask', None)2000 Future = getattr(futures, '_CFuture', None)2001 @support.refcount_test2002 def test_refleaks_in_task___init__(self):2003 gettotalrefcount = support.get_attribute(sys, 'gettotalrefcount')2004 async def coro():2005 pass2006 task = self.new_task(self.loop, coro())2007 self.loop.run_until_complete(task)2008 refs_before = gettotalrefcount()2009 for i in range(100):2010 task.__init__(coro(), loop=self.loop)2011 self.loop.run_until_complete(task)2012 self.assertAlmostEqual(gettotalrefcount() - refs_before, 0, delta=10)2013 def test_del__log_destroy_pending_segfault(self):2014 async def coro():2015 pass2016 task = self.new_task(self.loop, coro())2017 self.loop.run_until_complete(task)2018 with self.assertRaises(AttributeError):2019 del task._log_destroy_pending2020@unittest.skipUnless(hasattr(futures, '_CFuture') and2021 hasattr(tasks, '_CTask'),2022 'requires the C _asyncio module')2023@add_subclass_tests2024class CTask_CFuture_SubclassTests(BaseTaskTests, test_utils.TestCase):2025 Task = getattr(tasks, '_CTask', None)2026 Future = getattr(futures, '_CFuture', None)2027@unittest.skipUnless(hasattr(tasks, '_CTask'),2028 'requires the C _asyncio module')2029@add_subclass_tests2030class CTaskSubclass_PyFuture_Tests(BaseTaskTests, test_utils.TestCase):2031 Task = getattr(tasks, '_CTask', None)2032 Future = futures._PyFuture2033@unittest.skipUnless(hasattr(futures, '_CFuture'),2034 'requires the C _asyncio module')2035@add_subclass_tests2036class PyTask_CFutureSubclass_Tests(BaseTaskTests, test_utils.TestCase):2037 Future = getattr(futures, '_CFuture', None)2038 Task = tasks._PyTask2039@unittest.skipUnless(hasattr(tasks, '_CTask'),2040 'requires the C _asyncio module')2041class CTask_PyFuture_Tests(BaseTaskTests, test_utils.TestCase):2042 Task = getattr(tasks, '_CTask', None)2043 Future = futures._PyFuture2044@unittest.skipUnless(hasattr(futures, '_CFuture'),2045 'requires the C _asyncio module')2046class PyTask_CFuture_Tests(BaseTaskTests, test_utils.TestCase):2047 Task = tasks._PyTask2048 Future = getattr(futures, '_CFuture', None)2049class PyTask_PyFuture_Tests(BaseTaskTests, SetMethodsTest,2050 test_utils.TestCase):2051 Task = tasks._PyTask2052 Future = futures._PyFuture2053@add_subclass_tests2054class PyTask_PyFuture_SubclassTests(BaseTaskTests, test_utils.TestCase):2055 Task = tasks._PyTask2056 Future = futures._PyFuture2057@unittest.skipUnless(hasattr(tasks, '_CTask'),2058 'requires the C _asyncio module')2059class CTask_Future_Tests(test_utils.TestCase):2060 def test_foobar(self):2061 class Fut(asyncio.Future):2062 @property2063 def get_loop(self):2064 raise AttributeError2065 async def coro():2066 await fut2067 return 'spam'2068 self.loop = asyncio.new_event_loop()2069 try:2070 fut = Fut(loop=self.loop)2071 self.loop.call_later(0.1, fut.set_result, 1)2072 task = self.loop.create_task(coro())2073 res = self.loop.run_until_complete(task)2074 finally:2075 self.loop.close()2076 self.assertEqual(res, 'spam')2077class BaseTaskIntrospectionTests:2078 _register_task = None2079 _unregister_task = None2080 _enter_task = None2081 _leave_task = None2082 def test__register_task_1(self):2083 class TaskLike:2084 @property2085 def _loop(self):2086 return loop2087 def done(self):2088 return False2089 task = TaskLike()2090 loop = mock.Mock()2091 self.assertEqual(asyncio.all_tasks(loop), set())2092 self._register_task(task)2093 self.assertEqual(asyncio.all_tasks(loop), {task})2094 self._unregister_task(task)2095 def test__register_task_2(self):2096 class TaskLike:2097 def get_loop(self):2098 return loop2099 def done(self):2100 return False2101 task = TaskLike()2102 loop = mock.Mock()2103 self.assertEqual(asyncio.all_tasks(loop), set())2104 self._register_task(task)2105 self.assertEqual(asyncio.all_tasks(loop), {task})2106 self._unregister_task(task)2107 def test__register_task_3(self):2108 class TaskLike:2109 def get_loop(self):2110 return loop2111 def done(self):2112 return True2113 task = TaskLike()2114 loop = mock.Mock()2115 self.assertEqual(asyncio.all_tasks(loop), set())2116 self._register_task(task)2117 self.assertEqual(asyncio.all_tasks(loop), set())2118 with self.assertWarns(DeprecationWarning):2119 self.assertEqual(asyncio.Task.all_tasks(loop), {task})2120 self._unregister_task(task)2121 def test__enter_task(self):2122 task = mock.Mock()2123 loop = mock.Mock()2124 self.assertIsNone(asyncio.current_task(loop))2125 self._enter_task(loop, task)2126 self.assertIs(asyncio.current_task(loop), task)2127 self._leave_task(loop, task)2128 def test__enter_task_failure(self):2129 task1 = mock.Mock()2130 task2 = mock.Mock()2131 loop = mock.Mock()2132 self._enter_task(loop, task1)2133 with self.assertRaises(RuntimeError):2134 self._enter_task(loop, task2)2135 self.assertIs(asyncio.current_task(loop), task1)2136 self._leave_task(loop, task1)2137 def test__leave_task(self):2138 task = mock.Mock()2139 loop = mock.Mock()2140 self._enter_task(loop, task)2141 self._leave_task(loop, task)2142 self.assertIsNone(asyncio.current_task(loop))2143 def test__leave_task_failure1(self):2144 task1 = mock.Mock()2145 task2 = mock.Mock()2146 loop = mock.Mock()2147 self._enter_task(loop, task1)2148 with self.assertRaises(RuntimeError):2149 self._leave_task(loop, task2)2150 self.assertIs(asyncio.current_task(loop), task1)2151 self._leave_task(loop, task1)2152 def test__leave_task_failure2(self):2153 task = mock.Mock()2154 loop = mock.Mock()2155 with self.assertRaises(RuntimeError):2156 self._leave_task(loop, task)2157 self.assertIsNone(asyncio.current_task(loop))2158 def test__unregister_task(self):2159 task = mock.Mock()2160 loop = mock.Mock()2161 task.get_loop = lambda: loop2162 self._register_task(task)2163 self._unregister_task(task)2164 self.assertEqual(asyncio.all_tasks(loop), set())2165 def test__unregister_task_not_registered(self):2166 task = mock.Mock()2167 loop = mock.Mock()2168 self._unregister_task(task)2169 self.assertEqual(asyncio.all_tasks(loop), set())2170class PyIntrospectionTests(test_utils.TestCase, BaseTaskIntrospectionTests):2171 _register_task = staticmethod(tasks._py_register_task)2172 _unregister_task = staticmethod(tasks._py_unregister_task)2173 _enter_task = staticmethod(tasks._py_enter_task)2174 _leave_task = staticmethod(tasks._py_leave_task)2175@unittest.skipUnless(hasattr(tasks, '_c_register_task'),2176 'requires the C _asyncio module')2177class CIntrospectionTests(test_utils.TestCase, BaseTaskIntrospectionTests):2178 if hasattr(tasks, '_c_register_task'):2179 _register_task = staticmethod(tasks._c_register_task)2180 _unregister_task = staticmethod(tasks._c_unregister_task)2181 _enter_task = staticmethod(tasks._c_enter_task)2182 _leave_task = staticmethod(tasks._c_leave_task)2183 else:2184 _register_task = _unregister_task = _enter_task = _leave_task = None2185class BaseCurrentLoopTests:2186 def setUp(self):2187 super().setUp()2188 self.loop = asyncio.new_event_loop()2189 self.set_event_loop(self.loop)2190 def new_task(self, coro):2191 raise NotImplementedError2192 def test_current_task_no_running_loop(self):2193 self.assertIsNone(asyncio.current_task(loop=self.loop))2194 def test_current_task_no_running_loop_implicit(self):2195 with self.assertRaises(RuntimeError):2196 asyncio.current_task()2197 def test_current_task_with_implicit_loop(self):2198 async def coro():2199 self.assertIs(asyncio.current_task(loop=self.loop), task)2200 self.assertIs(asyncio.current_task(None), task)2201 self.assertIs(asyncio.current_task(), task)2202 task = self.new_task(coro())2203 self.loop.run_until_complete(task)2204 self.assertIsNone(asyncio.current_task(loop=self.loop))2205class PyCurrentLoopTests(BaseCurrentLoopTests, test_utils.TestCase):2206 def new_task(self, coro):2207 return tasks._PyTask(coro, loop=self.loop)2208@unittest.skipUnless(hasattr(tasks, '_CTask'),2209 'requires the C _asyncio module')2210class CCurrentLoopTests(BaseCurrentLoopTests, test_utils.TestCase):2211 def new_task(self, coro):2212 return getattr(tasks, '_CTask')(coro, loop=self.loop)2213class GenericTaskTests(test_utils.TestCase):2214 def test_future_subclass(self):2215 self.assertTrue(issubclass(asyncio.Task, asyncio.Future))2216 def test_asyncio_module_compiled(self):2217 # Because of circular imports it's easy to make _asyncio2218 # module non-importable. This is a simple test that will2219 # fail on systems where C modules were successfully compiled2220 # (hence the test for _functools), but _asyncio somehow didn't.2221 try:2222 import _functools2223 except ImportError:2224 pass2225 else:2226 try:2227 import _asyncio2228 except ImportError:2229 self.fail('_asyncio module is missing')2230class GatherTestsBase:2231 def setUp(self):2232 super().setUp()2233 self.one_loop = self.new_test_loop()2234 self.other_loop = self.new_test_loop()2235 self.set_event_loop(self.one_loop, cleanup=False)2236 def _run_loop(self, loop):2237 while loop._ready:2238 test_utils.run_briefly(loop)2239 def _check_success(self, **kwargs):2240 a, b, c = [self.one_loop.create_future() for i in range(3)]2241 fut = asyncio.gather(*self.wrap_futures(a, b, c), **kwargs)2242 cb = test_utils.MockCallback()2243 fut.add_done_callback(cb)2244 b.set_result(1)2245 a.set_result(2)2246 self._run_loop(self.one_loop)2247 self.assertEqual(cb.called, False)2248 self.assertFalse(fut.done())2249 c.set_result(3)2250 self._run_loop(self.one_loop)2251 cb.assert_called_once_with(fut)2252 self.assertEqual(fut.result(), [2, 1, 3])2253 def test_success(self):2254 self._check_success()2255 self._check_success(return_exceptions=False)2256 def test_result_exception_success(self):2257 self._check_success(return_exceptions=True)2258 def test_one_exception(self):2259 a, b, c, d, e = [self.one_loop.create_future() for i in range(5)]2260 fut = asyncio.gather(*self.wrap_futures(a, b, c, d, e))2261 cb = test_utils.MockCallback()2262 fut.add_done_callback(cb)2263 exc = ZeroDivisionError()2264 a.set_result(1)2265 b.set_exception(exc)2266 self._run_loop(self.one_loop)2267 self.assertTrue(fut.done())2268 cb.assert_called_once_with(fut)2269 self.assertIs(fut.exception(), exc)2270 # Does nothing2271 c.set_result(3)2272 d.cancel()2273 e.set_exception(RuntimeError())2274 e.exception()2275 def test_return_exceptions(self):2276 a, b, c, d = [self.one_loop.create_future() for i in range(4)]2277 fut = asyncio.gather(*self.wrap_futures(a, b, c, d),2278 return_exceptions=True)2279 cb = test_utils.MockCallback()2280 fut.add_done_callback(cb)2281 exc = ZeroDivisionError()2282 exc2 = RuntimeError()2283 b.set_result(1)2284 c.set_exception(exc)2285 a.set_result(3)2286 self._run_loop(self.one_loop)2287 self.assertFalse(fut.done())2288 d.set_exception(exc2)2289 self._run_loop(self.one_loop)2290 self.assertTrue(fut.done())2291 cb.assert_called_once_with(fut)2292 self.assertEqual(fut.result(), [3, 1, exc, exc2])2293 def test_env_var_debug(self):2294 code = '\n'.join((2295 'import asyncio.coroutines',2296 'print(asyncio.coroutines._DEBUG)'))2297 # Test with -E to not fail if the unit test was run with2298 # PYTHONASYNCIODEBUG set to a non-empty string2299 sts, stdout, stderr = assert_python_ok('-E', '-c', code)2300 self.assertEqual(stdout.rstrip(), b'False')2301 sts, stdout, stderr = assert_python_ok('-c', code,2302 PYTHONASYNCIODEBUG='',2303 PYTHONDEVMODE='')2304 self.assertEqual(stdout.rstrip(), b'False')2305 sts, stdout, stderr = assert_python_ok('-c', code,2306 PYTHONASYNCIODEBUG='1',2307 PYTHONDEVMODE='')2308 self.assertEqual(stdout.rstrip(), b'True')2309 sts, stdout, stderr = assert_python_ok('-E', '-c', code,2310 PYTHONASYNCIODEBUG='1',2311 PYTHONDEVMODE='')2312 self.assertEqual(stdout.rstrip(), b'False')2313 # -X dev2314 sts, stdout, stderr = assert_python_ok('-E', '-X', 'dev',2315 '-c', code)2316 self.assertEqual(stdout.rstrip(), b'True')2317class FutureGatherTests(GatherTestsBase, test_utils.TestCase):2318 def wrap_futures(self, *futures):2319 return futures2320 def _check_empty_sequence(self, seq_or_iter):2321 asyncio.set_event_loop(self.one_loop)2322 self.addCleanup(asyncio.set_event_loop, None)2323 fut = asyncio.gather(*seq_or_iter)2324 self.assertIsInstance(fut, asyncio.Future)2325 self.assertIs(fut._loop, self.one_loop)2326 self._run_loop(self.one_loop)2327 self.assertTrue(fut.done())2328 self.assertEqual(fut.result(), [])2329 with self.assertWarns(DeprecationWarning):2330 fut = asyncio.gather(*seq_or_iter, loop=self.other_loop)2331 self.assertIs(fut._loop, self.other_loop)2332 def test_constructor_empty_sequence(self):2333 self._check_empty_sequence([])2334 self._check_empty_sequence(())2335 self._check_empty_sequence(set())2336 self._check_empty_sequence(iter(""))2337 def test_constructor_heterogenous_futures(self):2338 fut1 = self.one_loop.create_future()2339 fut2 = self.other_loop.create_future()2340 with self.assertRaises(ValueError):2341 asyncio.gather(fut1, fut2)2342 with self.assertRaises(ValueError):2343 asyncio.gather(fut1, loop=self.other_loop)2344 def test_constructor_homogenous_futures(self):2345 children = [self.other_loop.create_future() for i in range(3)]2346 fut = asyncio.gather(*children)2347 self.assertIs(fut._loop, self.other_loop)2348 self._run_loop(self.other_loop)2349 self.assertFalse(fut.done())2350 fut = asyncio.gather(*children, loop=self.other_loop)2351 self.assertIs(fut._loop, self.other_loop)2352 self._run_loop(self.other_loop)2353 self.assertFalse(fut.done())2354 def test_one_cancellation(self):2355 a, b, c, d, e = [self.one_loop.create_future() for i in range(5)]2356 fut = asyncio.gather(a, b, c, d, e)2357 cb = test_utils.MockCallback()2358 fut.add_done_callback(cb)2359 a.set_result(1)2360 b.cancel()2361 self._run_loop(self.one_loop)2362 self.assertTrue(fut.done())2363 cb.assert_called_once_with(fut)2364 self.assertFalse(fut.cancelled())2365 self.assertIsInstance(fut.exception(), asyncio.CancelledError)2366 # Does nothing2367 c.set_result(3)2368 d.cancel()2369 e.set_exception(RuntimeError())2370 e.exception()2371 def test_result_exception_one_cancellation(self):2372 a, b, c, d, e, f = [self.one_loop.create_future()2373 for i in range(6)]2374 fut = asyncio.gather(a, b, c, d, e, f, return_exceptions=True)2375 cb = test_utils.MockCallback()2376 fut.add_done_callback(cb)2377 a.set_result(1)2378 zde = ZeroDivisionError()2379 b.set_exception(zde)2380 c.cancel()2381 self._run_loop(self.one_loop)2382 self.assertFalse(fut.done())2383 d.set_result(3)2384 e.cancel()2385 rte = RuntimeError()2386 f.set_exception(rte)2387 res = self.one_loop.run_until_complete(fut)2388 self.assertIsInstance(res[2], asyncio.CancelledError)2389 self.assertIsInstance(res[4], asyncio.CancelledError)2390 res[2] = res[4] = None2391 self.assertEqual(res, [1, zde, None, 3, None, rte])2392 cb.assert_called_once_with(fut)2393class CoroutineGatherTests(GatherTestsBase, test_utils.TestCase):2394 def setUp(self):2395 super().setUp()2396 asyncio.set_event_loop(self.one_loop)2397 def wrap_futures(self, *futures):2398 coros = []2399 for fut in futures:2400 async def coro(fut=fut):2401 return await fut2402 coros.append(coro())2403 return coros2404 def test_constructor_loop_selection(self):2405 async def coro():2406 return 'abc'2407 gen1 = coro()2408 gen2 = coro()2409 fut = asyncio.gather(gen1, gen2)2410 self.assertIs(fut._loop, self.one_loop)2411 self.one_loop.run_until_complete(fut)2412 self.set_event_loop(self.other_loop, cleanup=False)2413 gen3 = coro()2414 gen4 = coro()2415 fut2 = asyncio.gather(gen3, gen4, loop=self.other_loop)2416 self.assertIs(fut2._loop, self.other_loop)2417 self.other_loop.run_until_complete(fut2)2418 def test_duplicate_coroutines(self):2419 with self.assertWarns(DeprecationWarning):2420 @asyncio.coroutine2421 def coro(s):2422 return s2423 c = coro('abc')2424 fut = asyncio.gather(c, c, coro('def'), c, loop=self.one_loop)2425 self._run_loop(self.one_loop)2426 self.assertEqual(fut.result(), ['abc', 'abc', 'def', 'abc'])2427 def test_cancellation_broadcast(self):2428 # Cancelling outer() cancels all children.2429 proof = 02430 waiter = self.one_loop.create_future()2431 async def inner():2432 nonlocal proof2433 await waiter2434 proof += 12435 child1 = asyncio.ensure_future(inner(), loop=self.one_loop)2436 child2 = asyncio.ensure_future(inner(), loop=self.one_loop)2437 gatherer = None2438 async def outer():2439 nonlocal proof, gatherer2440 gatherer = asyncio.gather(child1, child2, loop=self.one_loop)2441 await gatherer2442 proof += 1002443 f = asyncio.ensure_future(outer(), loop=self.one_loop)2444 test_utils.run_briefly(self.one_loop)2445 self.assertTrue(f.cancel())2446 with self.assertRaises(asyncio.CancelledError):2447 self.one_loop.run_until_complete(f)2448 self.assertFalse(gatherer.cancel())2449 self.assertTrue(waiter.cancelled())2450 self.assertTrue(child1.cancelled())2451 self.assertTrue(child2.cancelled())2452 test_utils.run_briefly(self.one_loop)2453 self.assertEqual(proof, 0)2454 def test_exception_marking(self):2455 # Test for the first line marked "Mark exception retrieved."2456 async def inner(f):2457 await f2458 raise RuntimeError('should not be ignored')2459 a = self.one_loop.create_future()2460 b = self.one_loop.create_future()2461 async def outer():2462 await asyncio.gather(inner(a), inner(b), loop=self.one_loop)2463 f = asyncio.ensure_future(outer(), loop=self.one_loop)2464 test_utils.run_briefly(self.one_loop)2465 a.set_result(None)2466 test_utils.run_briefly(self.one_loop)2467 b.set_result(None)2468 test_utils.run_briefly(self.one_loop)2469 self.assertIsInstance(f.exception(), RuntimeError)2470class RunCoroutineThreadsafeTests(test_utils.TestCase):2471 """Test case for asyncio.run_coroutine_threadsafe."""2472 def setUp(self):2473 super().setUp()2474 self.loop = asyncio.new_event_loop()2475 self.set_event_loop(self.loop) # Will cleanup properly2476 async def add(self, a, b, fail=False, cancel=False):2477 """Wait 0.05 second and return a + b."""2478 await asyncio.sleep(0.05)2479 if fail:2480 raise RuntimeError("Fail!")2481 if cancel:2482 asyncio.current_task(self.loop).cancel()2483 await asyncio.sleep(0)2484 return a + b2485 def target(self, fail=False, cancel=False, timeout=None,2486 advance_coro=False):2487 """Run add coroutine in the event loop."""2488 coro = self.add(1, 2, fail=fail, cancel=cancel)2489 future = asyncio.run_coroutine_threadsafe(coro, self.loop)2490 if advance_coro:2491 # this is for test_run_coroutine_threadsafe_task_factory_exception;2492 # otherwise it spills errors and breaks **other** unittests, since2493 # 'target' is interacting with threads.2494 # With this call, `coro` will be advanced, so that2495 # CoroWrapper.__del__ won't do anything when asyncio tests run2496 # in debug mode.2497 self.loop.call_soon_threadsafe(coro.send, None)2498 try:2499 return future.result(timeout)2500 finally:2501 future.done() or future.cancel()2502 def test_run_coroutine_threadsafe(self):2503 """Test coroutine submission from a thread to an event loop."""2504 future = self.loop.run_in_executor(None, self.target)2505 result = self.loop.run_until_complete(future)2506 self.assertEqual(result, 3)2507 def test_run_coroutine_threadsafe_with_exception(self):2508 """Test coroutine submission from a thread to an event loop2509 when an exception is raised."""2510 future = self.loop.run_in_executor(None, self.target, True)2511 with self.assertRaises(RuntimeError) as exc_context:2512 self.loop.run_until_complete(future)2513 self.assertIn("Fail!", exc_context.exception.args)2514 def test_run_coroutine_threadsafe_with_timeout(self):2515 """Test coroutine submission from a thread to an event loop2516 when a timeout is raised."""2517 callback = lambda: self.target(timeout=0)2518 future = self.loop.run_in_executor(None, callback)2519 with self.assertRaises(asyncio.TimeoutError):2520 self.loop.run_until_complete(future)2521 test_utils.run_briefly(self.loop)2522 # Check that there's no pending task (add has been cancelled)2523 for task in asyncio.all_tasks(self.loop):2524 self.assertTrue(task.done())2525 def test_run_coroutine_threadsafe_task_cancelled(self):2526 """Test coroutine submission from a tread to an event loop2527 when the task is cancelled."""2528 callback = lambda: self.target(cancel=True)2529 future = self.loop.run_in_executor(None, callback)2530 with self.assertRaises(asyncio.CancelledError):2531 self.loop.run_until_complete(future)2532 def test_run_coroutine_threadsafe_task_factory_exception(self):2533 """Test coroutine submission from a tread to an event loop2534 when the task factory raise an exception."""2535 def task_factory(loop, coro):2536 raise NameError2537 run = self.loop.run_in_executor(2538 None, lambda: self.target(advance_coro=True))2539 # Set exception handler2540 callback = test_utils.MockCallback()2541 self.loop.set_exception_handler(callback)2542 # Set corrupted task factory2543 self.loop.set_task_factory(task_factory)2544 # Run event loop2545 with self.assertRaises(NameError) as exc_context:2546 self.loop.run_until_complete(run)2547 # Check exceptions2548 self.assertEqual(len(callback.call_args_list), 1)2549 (loop, context), kwargs = callback.call_args2550 self.assertEqual(context['exception'], exc_context.exception)2551class SleepTests(test_utils.TestCase):2552 def setUp(self):2553 super().setUp()2554 self.loop = asyncio.new_event_loop()2555 self.set_event_loop(self.loop)2556 def tearDown(self):2557 self.loop.close()2558 self.loop = None2559 super().tearDown()2560 def test_sleep_zero(self):2561 result = 02562 def inc_result(num):2563 nonlocal result2564 result += num2565 async def coro():2566 self.loop.call_soon(inc_result, 1)2567 self.assertEqual(result, 0)2568 num = await asyncio.sleep(0, result=10)2569 self.assertEqual(result, 1) # inc'ed by call_soon2570 inc_result(num) # num should be 112571 self.loop.run_until_complete(coro())2572 self.assertEqual(result, 11)2573 def test_loop_argument_is_deprecated(self):2574 # Remove test when loop argument is removed in Python 3.102575 with self.assertWarns(DeprecationWarning):2576 self.loop.run_until_complete(asyncio.sleep(0.01, loop=self.loop))2577class WaitTests(test_utils.TestCase):2578 def setUp(self):2579 super().setUp()2580 self.loop = asyncio.new_event_loop()2581 self.set_event_loop(self.loop)2582 def tearDown(self):2583 self.loop.close()2584 self.loop = None2585 super().tearDown()2586 def test_loop_argument_is_deprecated_in_wait(self):2587 # Remove test when loop argument is removed in Python 3.102588 with self.assertWarns(DeprecationWarning):2589 self.loop.run_until_complete(2590 asyncio.wait([coroutine_function()], loop=self.loop))2591 def test_loop_argument_is_deprecated_in_wait_for(self):2592 # Remove test when loop argument is removed in Python 3.102593 with self.assertWarns(DeprecationWarning):2594 self.loop.run_until_complete(2595 asyncio.wait_for(coroutine_function(), 0.01, loop=self.loop))2596class CompatibilityTests(test_utils.TestCase):2597 # Tests for checking a bridge between old-styled coroutines2598 # and async/await syntax2599 def setUp(self):2600 super().setUp()2601 self.loop = asyncio.new_event_loop()2602 self.set_event_loop(self.loop)2603 def tearDown(self):2604 self.loop.close()2605 self.loop = None2606 super().tearDown()2607 def test_yield_from_awaitable(self):2608 with self.assertWarns(DeprecationWarning):2609 @asyncio.coroutine2610 def coro():2611 yield from asyncio.sleep(0)2612 return 'ok'2613 result = self.loop.run_until_complete(coro())2614 self.assertEqual('ok', result)2615 def test_await_old_style_coro(self):2616 with self.assertWarns(DeprecationWarning):...
es3fShaderLoopTests.js
Source:es3fShaderLoopTests.js
1/*-------------------------------------------------------------------------2 * drawElements Quality Program OpenGL ES Utilities3 * ------------------------------------------------4 *5 * Copyright 2014 The Android Open Source Project6 *7 * Licensed under the Apache License, Version 2.0 (the 'License');8 * you may not use this file except in compliance with the License.9 * You may obtain a copy of the License at10 *11 * http://www.apache.org/licenses/LICENSE-2.012 *13 * Unless required by applicable law or agreed to in writing, software14 * distributed under the License is distributed on an 'AS IS' BASIS,15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.16 * See the License for the specific language governing permissions and17 * limitations under the License.18 *19 */20'use strict';21goog.provide('functional.gles3.es3fShaderLoopTests');22goog.require('framework.common.tcuStringTemplate');23goog.require('framework.common.tcuTestCase');24goog.require('framework.delibs.debase.deMath');25goog.require('framework.opengl.gluShaderUtil');26goog.require('framework.opengl.gluShaderProgram');27goog.require('modules.shared.glsShaderRenderCase');28goog.scope(function() {29var es3fShaderLoopTests = functional.gles3.es3fShaderLoopTests;30var tcuTestCase = framework.common.tcuTestCase;31var deMath = framework.delibs.debase.deMath;32var gluShaderUtil = framework.opengl.gluShaderUtil;33var gluShaderProgram = framework.opengl.gluShaderProgram;34var glsShaderRenderCase = modules.shared.glsShaderRenderCase;35var tcuStringTemplate = framework.common.tcuStringTemplate;36// Repeated with for, while, do-while. Examples given as 'for' loops.37// Repeated for const, uniform, dynamic loops.38/**39 * @enum {number}40 */41es3fShaderLoopTests.LoopCase = {42 LOOPCASE_EMPTY_BODY: 0, // for (...) { }43 LOOPCASE_INFINITE_WITH_UNCONDITIONAL_BREAK_FIRST: 1, // for (...) { break; <body>; }44 LOOPCASE_INFINITE_WITH_UNCONDITIONAL_BREAK_LAST: 2, // for (...) { <body>; break; }45 LOOPCASE_INFINITE_WITH_CONDITIONAL_BREAK: 3, // for (...) { <body>; if (cond) break; }46 LOOPCASE_SINGLE_STATEMENT: 4, // for (...) statement;47 LOOPCASE_COMPOUND_STATEMENT: 5, // for (...) { statement; statement; }48 LOOPCASE_SEQUENCE_STATEMENT: 6, // for (...) statement, statement;49 LOOPCASE_NO_ITERATIONS: 7, // for (i=0; i<0; i++) ...50 LOOPCASE_SINGLE_ITERATION: 8, // for (i=0; i<1; i++) ...51 LOOPCASE_SELECT_ITERATION_COUNT: 9, // for (i=0; i<a?b:c; i++) ...52 LOOPCASE_CONDITIONAL_CONTINUE: 10, // for (...) { if (cond) continue; }53 LOOPCASE_UNCONDITIONAL_CONTINUE: 11, // for (...) { <body>; continue; }54 LOOPCASE_ONLY_CONTINUE: 12, // for (...) { continue; }55 LOOPCASE_DOUBLE_CONTINUE: 13, // for (...) { if (cond) continue; <body>; continue; }56 LOOPCASE_CONDITIONAL_BREAK: 14, // for (...) { if (cond) break; }57 LOOPCASE_UNCONDITIONAL_BREAK: 15, // for (...) { <body>; break; }58 LOOPCASE_PRE_INCREMENT: 16, // for (...; ++i) { <body>; }59 LOOPCASE_POST_INCREMENT: 17, // for (...; i++) { <body>; }60 LOOPCASE_MIXED_BREAK_CONTINUE: 18,61 LOOPCASE_VECTOR_COUNTER: 19, // for (ivec3 ndx = ...; ndx.x < ndx.y; ndx.x += ndx.z) { ... }62 LOOPCASE_101_ITERATIONS: 20, // loop for 101 iterations63 LOOPCASE_SEQUENCE: 21, // two loops in sequence64 LOOPCASE_NESTED: 22, // two nested loops65 LOOPCASE_NESTED_SEQUENCE: 23, // two loops in sequence nested inside a third66 LOOPCASE_NESTED_TRICKY_DATAFLOW_1: 24, // nested loops with tricky data flow67 LOOPCASE_NESTED_TRICKY_DATAFLOW_2: 25 // nested loops with tricky data flow68};69/**70 * @param {es3fShaderLoopTests.LoopCase} loopCase71 * @return {string}72 */73es3fShaderLoopTests.getLoopCaseName = function(loopCase) {74 /** @type {Array<string>} */ var s_names = [75 'empty_body',76 'infinite_with_unconditional_break_first',77 'infinite_with_unconditional_break_last',78 'infinite_with_conditional_break',79 'single_statement',80 'compound_statement',81 'sequence_statement',82 'no_iterations',83 'single_iteration',84 'select_iteration_count',85 'conditional_continue',86 'unconditional_continue',87 'only_continue',88 'double_continue',89 'conditional_break',90 'unconditional_break',91 'pre_increment',92 'post_increment',93 'mixed_break_continue',94 'vector_counter',95 '101_iterations',96 'sequence',97 'nested',98 'nested_sequence',99 'nested_tricky_dataflow_1',100 'nested_tricky_dataflow_2'101 ];102 // DE_STATIC_ASSERT(DE_LENGTH_OF_ARRAY(s_names) == es3fShaderLoopTests.LoopCase.LOOPCASE_LAST);103 // DE_ASSERT(deInBounds32((int)loopCase, 0, LOOPCASE_LAST));104 return s_names[loopCase];105};106// Complex loop cases.107/*enum LoopBody108{109 LOOPBODY_READ_UNIFORM = 0,110 LOOPBODY_READ_UNIFORM_ARRAY,111 LOOPBODY_READ_112};*/113/**114 * @enum {number}115 */116es3fShaderLoopTests.LoopType = {117 LOOPTYPE_FOR: 0,118 LOOPTYPE_WHILE: 1,119 LOOPTYPE_DO_WHILE: 2120};121/**122 * @param {es3fShaderLoopTests.LoopType} loopType123 * @return {string}124 */125es3fShaderLoopTests.getLoopTypeName = function(loopType) {126 /** @type {Array<string>} */ var s_names = [127 'for',128 'while',129 'do_while'130 ];131 // DE_STATIC_ASSERT(DE_LENGTH_OF_ARRAY(s_names) === es3fShaderLoopTests.LoopType.LOOPTYPE_LAST);132 // DE_ASSERT(deInBounds32((int)loopType, 0, LOOPTYPE_LAST));133 return s_names[loopType];134};135/**136 * @enum {number}137 */138es3fShaderLoopTests.LoopCountType = {139 LOOPCOUNT_CONSTANT: 0,140 LOOPCOUNT_UNIFORM: 1,141 LOOPCOUNT_DYNAMIC: 2142};143/**144 * @param {es3fShaderLoopTests.LoopCountType} countType145 * @return {string}146 */147es3fShaderLoopTests.getLoopCountTypeName = function(countType) {148 /** @type {Array<string>} */ var s_names = [149 'constant',150 'uniform',151 'dynamic'152 ];153 // DE_STATIC_ASSERT(DE_LENGTH_OF_ARRAY(s_names) == es3fShaderLoopTests.LoopCountType.LOOPCOUNT_LAST);154 // DE_ASSERT(deInBounds32((int)countType, 0, es3fShaderLoopTests.LoopCountType.LOOPCOUNT_LAST));155 return s_names[countType];156};157/**158 * @param {glsShaderRenderCase.ShaderEvalContext} c159 */160es3fShaderLoopTests.evalLoop0Iters = function(c) {161 var swizzled = deMath.swizzle(c.coords, [0, 1, 2]);162 c.color[0] = swizzled[0];163 c.color[1] = swizzled[1];164 c.color[2] = swizzled[2];165};166/**167 * @param {glsShaderRenderCase.ShaderEvalContext} c168 */169es3fShaderLoopTests.evalLoop1Iters = function(c) {170 var swizzled = deMath.swizzle(c.coords, [1, 2, 3]);171 c.color[0] = swizzled[0];172 c.color[1] = swizzled[1];173 c.color[2] = swizzled[2];174};175/**176 * @param {glsShaderRenderCase.ShaderEvalContext} c177 */178es3fShaderLoopTests.evalLoop2Iters = function(c) {179 var swizzled = deMath.swizzle(c.coords, [2, 3, 0]);180 c.color[0] = swizzled[0];181 c.color[1] = swizzled[1];182 c.color[2] = swizzled[2];183};184/**185 * @param {glsShaderRenderCase.ShaderEvalContext} c186 */187es3fShaderLoopTests.evalLoop3Iters = function(c) {188 var swizzled = deMath.swizzle(c.coords, [3, 0, 1]);189 c.color[0] = swizzled[0];190 c.color[1] = swizzled[1];191 c.color[2] = swizzled[2];192};193/**194 * @param {number} numIters195 * @return {glsShaderRenderCase.ShaderEvalFunc}196 */197es3fShaderLoopTests.getLoopEvalFunc = function(numIters) {198 switch (numIters % 4) {199 case 0: return es3fShaderLoopTests.evalLoop0Iters;200 case 1: return es3fShaderLoopTests.evalLoop1Iters;201 case 2: return es3fShaderLoopTests.evalLoop2Iters;202 case 3: return es3fShaderLoopTests.evalLoop3Iters;203 }204 throw new Error('Invalid loop iteration count.');205};206// ShaderLoopCase207/**208 * @constructor209 * @extends {glsShaderRenderCase.ShaderRenderCase}210 * @param {string} name211 * @param {string} description212 * @param {boolean} isVertexCase213 * @param {glsShaderRenderCase.ShaderEvalFunc} evalFunc214 * @param {string} vertShaderSource215 * @param {string} fragShaderSource216 */217es3fShaderLoopTests.ShaderLoopCase = function(name, description, isVertexCase, evalFunc, vertShaderSource, fragShaderSource) {218 glsShaderRenderCase.ShaderRenderCase.call(this, name, description, isVertexCase, evalFunc);219 /** @type {string} */ this.m_vertShaderSource = vertShaderSource;220 /** @type {string} */ this.m_fragShaderSource = fragShaderSource;221};222es3fShaderLoopTests.ShaderLoopCase.prototype = Object.create(glsShaderRenderCase.ShaderRenderCase.prototype);223es3fShaderLoopTests.ShaderLoopCase.prototype.constructor = es3fShaderLoopTests.ShaderLoopCase;224// Test case creation.225/**226 * @param {string} caseName227 * @param {string} description228 * @param {boolean} isVertexCase229 * @param {es3fShaderLoopTests.LoopType} loopType230 * @param {es3fShaderLoopTests.LoopCountType} loopCountType231 * @param {gluShaderUtil.precision} loopCountPrecision232 * @param {gluShaderUtil.DataType} loopCountDataType233 * @return {es3fShaderLoopTests.ShaderLoopCase}234 */235es3fShaderLoopTests.createGenericLoopCase = function(caseName, description, isVertexCase, loopType, loopCountType, loopCountPrecision, loopCountDataType) {236 /** @type {string} */ var vtx = '';237 /** @type {string} */ var frag = '';238 /** @type {string} */ var op = '';239 vtx += '#version 300 es\n';240 frag += '#version 300 es\n';241 vtx += 'in highp vec4 a_position;\n';242 vtx += 'in highp vec4 a_coords;\n';243 frag += 'layout(location = 0) out mediump vec4 o_color;\n';244 if (loopCountType === es3fShaderLoopTests.LoopCountType.LOOPCOUNT_DYNAMIC)245 vtx += 'in mediump float a_one;\n';246 if (isVertexCase) {247 vtx += 'out mediump vec3 v_color;\n';248 frag += 'in mediump vec3 v_color;\n';249 }250 else {251 vtx += 'out mediump vec4 v_coords;\n';252 frag += 'in mediump vec4 v_coords;\n';253 if (loopCountType === es3fShaderLoopTests.LoopCountType.LOOPCOUNT_DYNAMIC) {254 vtx += 'out mediump float v_one;\n';255 frag += 'in mediump float v_one;\n';256 }257 }258 // \todo [petri] Pass numLoopIters from outside?259 /** @type {number} */ var numLoopIters = 3;260 /** @type {boolean} */ var isIntCounter = gluShaderUtil.isDataTypeIntOrIVec(loopCountDataType);261 if (isIntCounter) {262 if (loopCountType === es3fShaderLoopTests.LoopCountType.LOOPCOUNT_UNIFORM || loopCountType === es3fShaderLoopTests.LoopCountType.LOOPCOUNT_DYNAMIC)263 op += 'uniform ${COUNTER_PRECISION} int ' + glsShaderRenderCase.getIntUniformName(numLoopIters) + ';\n';264 }265 else {266 if (loopCountType === es3fShaderLoopTests.LoopCountType.LOOPCOUNT_UNIFORM || loopCountType === es3fShaderLoopTests.LoopCountType.LOOPCOUNT_DYNAMIC)267 op += 'uniform ${COUNTER_PRECISION} float ' + glsShaderRenderCase.getFloatFractionUniformName(numLoopIters) + ';\n';268 if (numLoopIters != 1)269 op += 'uniform ${COUNTER_PRECISION} float uf_one;\n';270 }271 vtx += isVertexCase ? op : '';272 frag += isVertexCase ? '' : op;273 op = '';274 vtx += "\n" +275 "void main()\n" +276 "{\n" +277 " gl_Position = a_position;\n";278 frag += "\n" +279 "void main()\n" +280 "{\n";281 if (isVertexCase)282 vtx += ' ${PRECISION} vec4 coords = a_coords;\n';283 else284 frag += ' ${PRECISION} vec4 coords = v_coords;\n';285 if (loopCountType === es3fShaderLoopTests.LoopCountType.LOOPCOUNT_DYNAMIC) {286 if (isIntCounter) {287 if (isVertexCase)288 vtx += ' ${COUNTER_PRECISION} int one = int(a_one + 0.5);\n';289 else290 frag += ' ${COUNTER_PRECISION} int one = int(v_one + 0.5);\n';291 }292 else {293 if (isVertexCase)294 vtx += ' ${COUNTER_PRECISION} float one = a_one;\n';295 else296 frag += ' ${COUNTER_PRECISION} float one = v_one;\n';297 }298 }299 // Read array.300 op += ' ${PRECISION} vec4 res = coords;\n';301 // Loop iteration count.302 /** @type {string} */ var iterMaxStr;303 if (isIntCounter) {304 if (loopCountType === es3fShaderLoopTests.LoopCountType.LOOPCOUNT_CONSTANT)305 iterMaxStr = numLoopIters.toString();306 else if (loopCountType === es3fShaderLoopTests.LoopCountType.LOOPCOUNT_UNIFORM)307 iterMaxStr = glsShaderRenderCase.getIntUniformName(numLoopIters);308 else if (loopCountType === es3fShaderLoopTests.LoopCountType.LOOPCOUNT_DYNAMIC)309 iterMaxStr = glsShaderRenderCase.getIntUniformName(numLoopIters) + '*one';310 else311 throw new Error('Loop Count Type not supported: ' + loopCountType);312 }313 else {314 if (loopCountType === es3fShaderLoopTests.LoopCountType.LOOPCOUNT_CONSTANT)315 iterMaxStr = '1.0';316 else if (loopCountType === es3fShaderLoopTests.LoopCountType.LOOPCOUNT_UNIFORM)317 iterMaxStr = 'uf_one';318 else if (loopCountType === es3fShaderLoopTests.LoopCountType.LOOPCOUNT_DYNAMIC)319 iterMaxStr = 'uf_one*one';320 else321 throw new Error('Loop Count Type not supported: ' + loopCountType);322 }323 // Loop operations.324 /** @type {string} */ var initValue = isIntCounter ? '0' : '0.05';325 /** @type {string} */ var loopCountDeclStr = '' + gluShaderUtil.getPrecisionName(loopCountPrecision) + ' ' + gluShaderUtil.getDataTypeName(loopCountDataType) + ' ndx = ' + initValue;326 /** @type {string} */ var loopCmpStr = 'ndx < ' + iterMaxStr;327 /** @type {string} */ var incrementStr;328 if (isIntCounter)329 incrementStr = 'ndx++';330 else {331 if (loopCountType === es3fShaderLoopTests.LoopCountType.LOOPCOUNT_CONSTANT)332 incrementStr = 'ndx += ' + (1.0 / numLoopIters);333 else if (loopCountType === es3fShaderLoopTests.LoopCountType.LOOPCOUNT_UNIFORM)334 incrementStr = 'ndx += ' + glsShaderRenderCase.getFloatFractionUniformName(numLoopIters);335 else if (loopCountType === es3fShaderLoopTests.LoopCountType.LOOPCOUNT_DYNAMIC)336 incrementStr = 'ndx += ' + glsShaderRenderCase.getFloatFractionUniformName(numLoopIters) + '*one';337 else338 throw new Error('Loop Count Type not supported: ' + loopCountType);339 }340 // Loop body.341 /** @type {string} */ var loopBody = ' res = res.yzwx;\n';;342 if (loopType === es3fShaderLoopTests.LoopType.LOOPTYPE_FOR) {343 op += ' for (' + loopCountDeclStr + '; ' + loopCmpStr + '; ' + incrementStr + ')\n' +344 ' {\n' +345 loopBody +346 ' }\n';347 }348 else if (loopType === es3fShaderLoopTests.LoopType.LOOPTYPE_WHILE) {349 op += '\t' + loopCountDeclStr + ';\n' +350 ' while (' + loopCmpStr + ')\n' +351 ' {\n' +352 loopBody +353 '\t\t' + incrementStr + ';\n' +354 ' }\n';355 }356 else if (loopType === es3fShaderLoopTests.LoopType.LOOPTYPE_DO_WHILE)357 {358 op += '\t' + loopCountDeclStr + ';\n' +359 ' do\n' +360 ' {\n' +361 loopBody +362 '\t\t' + incrementStr + ';\n' +363 ' } while (' + loopCmpStr + ');\n';364 }365 else366 throw new Error('Loop Type not supported: ' + loopType);367 vtx += isVertexCase ? op : '';368 frag += isVertexCase ? '' : op;369 op = '';370 if (isVertexCase) {371 vtx += ' v_color = res.rgb;\n';372 frag += ' o_color = vec4(v_color.rgb, 1.0);\n';373 }374 else {375 vtx += ' v_coords = a_coords;\n';376 frag += ' o_color = vec4(res.rgb, 1.0);\n';377 if (loopCountType === es3fShaderLoopTests.LoopCountType.LOOPCOUNT_DYNAMIC)378 vtx += ' v_one = a_one;\n';379 }380 vtx += '}\n';381 frag += '}\n';382 // Fill in shader templates.383 /** @type {Object} */ var params = {};384 params['LOOP_VAR_TYPE'] = gluShaderUtil.getDataTypeName(loopCountDataType);385 params['PRECISION'] = 'mediump';386 params['COUNTER_PRECISION'] = gluShaderUtil.getPrecisionName(loopCountPrecision);387 /** @type {string} */ var vertexShaderSource = tcuStringTemplate.specialize(vtx, params);388 /** @type {string} */ var fragmentShaderSource = tcuStringTemplate.specialize(frag, params);389 // Create the case.390 /** @type {glsShaderRenderCase.ShaderEvalFunc} */391 var evalFunc = es3fShaderLoopTests.getLoopEvalFunc(numLoopIters);392 return new es3fShaderLoopTests.ShaderLoopCase(caseName, description, isVertexCase, evalFunc, vertexShaderSource, fragmentShaderSource);393};394// \todo [petri] Generalize to float as well?395/**396 * @param {string} caseName397 * @param {string} description398 * @param {boolean} isVertexCase399 * @param {es3fShaderLoopTests.LoopCase} loopCase400 * @param {es3fShaderLoopTests.LoopType} loopType401 * @param {es3fShaderLoopTests.LoopCountType} loopCountType402 * @return {es3fShaderLoopTests.ShaderLoopCase}403 */404es3fShaderLoopTests.createSpecialLoopCase = function(caseName, description, isVertexCase, loopCase, loopType, loopCountType) {405 /** @type {string} */ var vtx = '';406 /** @type {string} */ var frag = '';407 /** @type {string} */ var op = '';408 vtx += '#version 300 es\n';409 frag += '#version 300 es\n';410 vtx += 'in highp vec4 a_position;\n';411 vtx += 'in highp vec4 a_coords;\n';412 frag += 'layout(location = 0) out mediump vec4 o_color;\n';413 if (loopCountType === es3fShaderLoopTests.LoopCountType.LOOPCOUNT_DYNAMIC)414 vtx += 'in mediump float a_one;\n';415 // Attribute and varyings.416 if (isVertexCase) {417 vtx += 'out mediump vec3 v_color;\n';418 frag += 'in mediump vec3 v_color;\n';419 }420 else {421 vtx += 'out mediump vec4 v_coords;\n';422 frag += 'in mediump vec4 v_coords;\n';423 if (loopCountType === es3fShaderLoopTests.LoopCountType.LOOPCOUNT_DYNAMIC) {424 vtx += 'out mediump float v_one;\n';425 frag += 'in mediump float v_one;\n';426 }427 }428 if (loopCase === es3fShaderLoopTests.LoopCase.LOOPCASE_SELECT_ITERATION_COUNT)429 op += 'uniform bool ub_true;\n';430 op += 'uniform ${COUNTER_PRECISION} int ui_zero, ui_one, ui_two, ui_three, ui_four, ui_five, ui_six;\n';431 if (loopCase === es3fShaderLoopTests.LoopCase.LOOPCASE_101_ITERATIONS)432 op += 'uniform ${COUNTER_PRECISION} int ui_oneHundredOne;\n';433 vtx += isVertexCase ? op : '';434 frag += isVertexCase ? '' : op;435 op = '';436 /** @type {number} */ var iterCount = 3; // value to use in loop437 /** @type {number} */ var numIters = 3; // actual number of iterations438 vtx += '\n' +439 'void main()\n' +440 '{\n' +441 ' gl_Position = a_position;\n';442 frag += '\n' +443 'void main()\n' +444 '{\n';445 if (loopCountType === es3fShaderLoopTests.LoopCountType.LOOPCOUNT_DYNAMIC) {446 if (isVertexCase)447 vtx += ' ${COUNTER_PRECISION} int one = int(a_one + 0.5);\n';448 else449 frag += ' ${COUNTER_PRECISION} int one = int(v_one + 0.5);\n';450 }451 if (isVertexCase)452 vtx += ' ${PRECISION} vec4 coords = a_coords;\n';453 else454 frag += ' ${PRECISION} vec4 coords = v_coords;\n';455 // Read array.456 op += ' ${PRECISION} vec4 res = coords;\n';457 // Handle all loop types.458 /** @type {string} */ var counterPrecisionStr = 'mediump';459 /** @type {string} */ var forLoopStr = '';460 /** @type {string} */ var whileLoopStr = '';461 /** @type {string} */ var doWhileLoopPreStr = '';462 /** @type {string} */ var doWhileLoopPostStr = '';463 if (loopType === es3fShaderLoopTests.LoopType.LOOPTYPE_FOR) {464 switch (loopCase) {465 case es3fShaderLoopTests.LoopCase.LOOPCASE_EMPTY_BODY:466 numIters = 0;467 op += ' ${FOR_LOOP} {}\n';468 break;469 case es3fShaderLoopTests.LoopCase.LOOPCASE_INFINITE_WITH_UNCONDITIONAL_BREAK_FIRST:470 numIters = 0;471 op += ' for (;;) { break; res = res.yzwx; }\n';472 break;473 case es3fShaderLoopTests.LoopCase.LOOPCASE_INFINITE_WITH_UNCONDITIONAL_BREAK_LAST:474 numIters = 1;475 op += ' for (;;) { res = res.yzwx; break; }\n';476 break;477 case es3fShaderLoopTests.LoopCase.LOOPCASE_INFINITE_WITH_CONDITIONAL_BREAK:478 numIters = 2;479 op += ' ${COUNTER_PRECISION} int i = 0;\n' +480 ' for (;;) { res = res.yzwx; if (i == ${ONE}) break; i++; }\n';481 break;482 case es3fShaderLoopTests.LoopCase.LOOPCASE_SINGLE_STATEMENT:483 op += ' ${FOR_LOOP} res = res.yzwx;\n';484 break;485 case es3fShaderLoopTests.LoopCase.LOOPCASE_COMPOUND_STATEMENT:486 iterCount = 2;487 numIters = 2 * iterCount;488 op += ' ${FOR_LOOP} { res = res.yzwx; res = res.yzwx; }\n';489 break;490 case es3fShaderLoopTests.LoopCase.LOOPCASE_SEQUENCE_STATEMENT:491 iterCount = 2;492 numIters = 2 * iterCount;493 op += ' ${FOR_LOOP} res = res.yzwx, res = res.yzwx;\n';494 break;495 case es3fShaderLoopTests.LoopCase.LOOPCASE_NO_ITERATIONS:496 iterCount = 0;497 numIters = 0;498 op += ' ${FOR_LOOP} res = res.yzwx;\n';499 break;500 case es3fShaderLoopTests.LoopCase.LOOPCASE_SINGLE_ITERATION:501 iterCount = 1;502 numIters = 1;503 op += ' ${FOR_LOOP} res = res.yzwx;\n';504 break;505 case es3fShaderLoopTests.LoopCase.LOOPCASE_SELECT_ITERATION_COUNT:506 op += ' for (int i = 0; i < (ub_true ? ${ITER_COUNT} : 0); i++) res = res.yzwx;\n';507 break;508 case es3fShaderLoopTests.LoopCase.LOOPCASE_CONDITIONAL_CONTINUE:509 numIters = iterCount - 1;510 op += ' ${FOR_LOOP} { if (i == ${TWO}) continue; res = res.yzwx; }\n';511 break;512 case es3fShaderLoopTests.LoopCase.LOOPCASE_UNCONDITIONAL_CONTINUE:513 op += ' ${FOR_LOOP} { res = res.yzwx; continue; }\n';514 break;515 case es3fShaderLoopTests.LoopCase.LOOPCASE_ONLY_CONTINUE:516 numIters = 0;517 op += ' ${FOR_LOOP} { continue; }\n';518 break;519 case es3fShaderLoopTests.LoopCase.LOOPCASE_DOUBLE_CONTINUE:520 numIters = iterCount - 1;521 op += ' ${FOR_LOOP} { if (i == ${TWO}) continue; res = res.yzwx; continue; }\n';522 break;523 case es3fShaderLoopTests.LoopCase.LOOPCASE_CONDITIONAL_BREAK:524 numIters = 2;525 op += ' ${FOR_LOOP} { if (i == ${TWO}) break; res = res.yzwx; }\n';526 break;527 case es3fShaderLoopTests.LoopCase.LOOPCASE_UNCONDITIONAL_BREAK:528 numIters = 1;529 op += ' ${FOR_LOOP} { res = res.yzwx; break; }\n';530 break;531 case es3fShaderLoopTests.LoopCase.LOOPCASE_PRE_INCREMENT:532 op += ' for (int i = 0; i < ${ITER_COUNT}; ++i) { res = res.yzwx; }\n';533 break;534 case es3fShaderLoopTests.LoopCase.LOOPCASE_POST_INCREMENT:535 op += ' ${FOR_LOOP} { res = res.yzwx; }\n';536 break;537 case es3fShaderLoopTests.LoopCase.LOOPCASE_MIXED_BREAK_CONTINUE:538 numIters = 2;539 iterCount = 5;540 op += ' ${FOR_LOOP} { if (i == 0) continue; else if (i == 3) break; res = res.yzwx; }\n';541 break;542 case es3fShaderLoopTests.LoopCase.LOOPCASE_VECTOR_COUNTER:543 op += ' for (${COUNTER_PRECISION} ivec4 i = ivec4(0, 1, ${ITER_COUNT}, 0); i.x < i.z; i.x += i.y) { res = res.yzwx; }\n';544 break;545 case es3fShaderLoopTests.LoopCase.LOOPCASE_101_ITERATIONS:546 numIters = iterCount = 101;547 op += ' ${FOR_LOOP} res = res.yzwx;\n';548 break;549 case es3fShaderLoopTests.LoopCase.LOOPCASE_SEQUENCE:550 iterCount = 5;551 numIters = 5;552 op += ' ${COUNTER_PRECISION} int i;\n' +553 ' for (i = 0; i < ${TWO}; i++) { res = res.yzwx; }\n' +554 ' for (; i < ${ITER_COUNT}; i++) { res = res.yzwx; }\n';555 break;556 case es3fShaderLoopTests.LoopCase.LOOPCASE_NESTED:557 numIters = 2 * iterCount;558 op += ' for (${COUNTER_PRECISION} int i = 0; i < ${TWO}; i++)\n' +559 ' {\n' +560 ' for (${COUNTER_PRECISION} int j = 0; j < ${ITER_COUNT}; j++)\n' +561 ' res = res.yzwx;\n' +562 ' }\n';563 break;564 case es3fShaderLoopTests.LoopCase.LOOPCASE_NESTED_SEQUENCE:565 numIters = 3 * iterCount;566 op += ' for (${COUNTER_PRECISION} int i = 0; i < ${ITER_COUNT}; i++)\n' +567 ' {\n' +568 ' for (${COUNTER_PRECISION} int j = 0; j < ${TWO}; j++)\n' +569 ' res = res.yzwx;\n' +570 ' for (${COUNTER_PRECISION} int j = 0; j < ${ONE}; j++)\n' +571 ' res = res.yzwx;\n' +572 ' }\n';573 break;574 case es3fShaderLoopTests.LoopCase.LOOPCASE_NESTED_TRICKY_DATAFLOW_1:575 numIters = 2;576 op += ' ${FOR_LOOP}\n' +577 ' {\n' +578 ' res = coords; // ignore outer loop effect \n' +579 ' for (${COUNTER_PRECISION} int j = 0; j < ${TWO}; j++)\n' +580 ' res = res.yzwx;\n' +581 ' }\n';582 break;583 case es3fShaderLoopTests.LoopCase.LOOPCASE_NESTED_TRICKY_DATAFLOW_2:584 numIters = iterCount;585 op += ' ${FOR_LOOP}\n' +586 ' {\n' +587 ' res = coords.wxyz;\n' +588 ' for (${COUNTER_PRECISION} int j = 0; j < ${TWO}; j++)\n' +589 ' res = res.yzwx;\n' +590 ' coords = res;\n' +591 ' }\n';592 break;593 default:594 throw new Error('Case not supported: ' + loopCase);595 }596 if (loopCountType === es3fShaderLoopTests.LoopCountType.LOOPCOUNT_CONSTANT)597 forLoopStr = 'for (' + counterPrecisionStr + ' int i = 0; i < ' + iterCount + '; i++)';598 else if (loopCountType === es3fShaderLoopTests.LoopCountType.LOOPCOUNT_UNIFORM)599 forLoopStr = 'for (' + counterPrecisionStr + ' int i = 0; i < ' + glsShaderRenderCase.getIntUniformName(iterCount) + '; i++)';600 else if (loopCountType === es3fShaderLoopTests.LoopCountType.LOOPCOUNT_DYNAMIC)601 forLoopStr = 'for (' + counterPrecisionStr + ' int i = 0; i < one*' + glsShaderRenderCase.getIntUniformName(iterCount) + '; i++)';602 else603 throw new Error('Loop Count Type not supported: ' + loopCountType);604 }605 else if (loopType === es3fShaderLoopTests.LoopType.LOOPTYPE_WHILE) {606 switch (loopCase) {607 case es3fShaderLoopTests.LoopCase.LOOPCASE_EMPTY_BODY:608 numIters = 0;609 op += ' ${WHILE_LOOP} {}\n';610 break;611 case es3fShaderLoopTests.LoopCase.LOOPCASE_INFINITE_WITH_UNCONDITIONAL_BREAK_FIRST:612 numIters = 0;613 op += ' while (true) { break; res = res.yzwx; }\n';614 break;615 case es3fShaderLoopTests.LoopCase.LOOPCASE_INFINITE_WITH_UNCONDITIONAL_BREAK_LAST:616 numIters = 1;617 op += ' while (true) { res = res.yzwx; break; }\n';618 break;619 case es3fShaderLoopTests.LoopCase.LOOPCASE_INFINITE_WITH_CONDITIONAL_BREAK:620 numIters = 2;621 op += ' ${COUNTER_PRECISION} int i = 0;\n' +622 ' while (true) { res = res.yzwx; if (i == ${ONE}) break; i++; }\n';623 break;624 case es3fShaderLoopTests.LoopCase.LOOPCASE_SINGLE_STATEMENT:625 op += ' ${WHILE_LOOP} res = res.yzwx;\n';626 break;627 case es3fShaderLoopTests.LoopCase.LOOPCASE_COMPOUND_STATEMENT:628 iterCount = 2;629 numIters = 2 * iterCount;630 op += ' ${WHILE_LOOP} { res = res.yzwx; res = res.yzwx; }\n';631 break;632 case es3fShaderLoopTests.LoopCase.LOOPCASE_SEQUENCE_STATEMENT:633 iterCount = 2;634 numIters = 2 * iterCount;635 op += ' ${WHILE_LOOP} res = res.yzwx, res = res.yzwx;\n';636 break;637 case es3fShaderLoopTests.LoopCase.LOOPCASE_NO_ITERATIONS:638 iterCount = 0;639 numIters = 0;640 op += ' ${WHILE_LOOP} res = res.yzwx;\n';641 break;642 case es3fShaderLoopTests.LoopCase.LOOPCASE_SINGLE_ITERATION:643 iterCount = 1;644 numIters = 1;645 op += ' ${WHILE_LOOP} res = res.yzwx;\n';646 break;647 case es3fShaderLoopTests.LoopCase.LOOPCASE_SELECT_ITERATION_COUNT:648 op += ' ${COUNTER_PRECISION} int i = 0;\n' +649 ' while (i < (ub_true ? ${ITER_COUNT} : 0)) { res = res.yzwx; i++; }\n';650 break;651 case es3fShaderLoopTests.LoopCase.LOOPCASE_CONDITIONAL_CONTINUE:652 numIters = iterCount - 1;653 op += ' ${WHILE_LOOP} { if (i == ${TWO}) continue; res = res.yzwx; }\n';654 break;655 case es3fShaderLoopTests.LoopCase.LOOPCASE_UNCONDITIONAL_CONTINUE:656 op += ' ${WHILE_LOOP} { res = res.yzwx; continue; }\n';657 break;658 case es3fShaderLoopTests.LoopCase.LOOPCASE_ONLY_CONTINUE:659 numIters = 0;660 op += ' ${WHILE_LOOP} { continue; }\n';661 break;662 case es3fShaderLoopTests.LoopCase.LOOPCASE_DOUBLE_CONTINUE:663 numIters = iterCount - 1;664 op += ' ${WHILE_LOOP} { if (i == ${ONE}) continue; res = res.yzwx; continue; }\n';665 break;666 case es3fShaderLoopTests.LoopCase.LOOPCASE_CONDITIONAL_BREAK:667 numIters = 2;668 op += ' ${WHILE_LOOP} { if (i == ${THREE}) break; res = res.yzwx; }\n';669 break;670 case es3fShaderLoopTests.LoopCase.LOOPCASE_UNCONDITIONAL_BREAK:671 numIters = 1;672 op += ' ${WHILE_LOOP} { res = res.yzwx; break; }\n';673 break;674 case es3fShaderLoopTests.LoopCase.LOOPCASE_PRE_INCREMENT:675 numIters = iterCount - 1;676 op += ' ${COUNTER_PRECISION} int i = 0;\n' +677 ' while (++i < ${ITER_COUNT}) { res = res.yzwx; }\n';678 break;679 case es3fShaderLoopTests.LoopCase.LOOPCASE_POST_INCREMENT:680 op += ' ${COUNTER_PRECISION} int i = 0;\n' +681 ' while (i++ < ${ITER_COUNT}) { res = res.yzwx; }\n';682 break;683 case es3fShaderLoopTests.LoopCase.LOOPCASE_MIXED_BREAK_CONTINUE:684 numIters = 2;685 iterCount = 5;686 op += ' ${WHILE_LOOP} { if (i == 0) continue; else if (i == 3) break; res = res.yzwx; }\n';687 break;688 case es3fShaderLoopTests.LoopCase.LOOPCASE_VECTOR_COUNTER:689 op += ' ${COUNTER_PRECISION} ivec4 i = ivec4(0, 1, ${ITER_COUNT}, 0);\n' +690 ' while (i.x < i.z) { res = res.yzwx; i.x += i.y; }\n';691 break;692 case es3fShaderLoopTests.LoopCase.LOOPCASE_101_ITERATIONS:693 numIters = iterCount = 101;694 op += ' ${WHILE_LOOP} res = res.yzwx;\n';695 break;696 case es3fShaderLoopTests.LoopCase.LOOPCASE_SEQUENCE:697 iterCount = 6;698 numIters = iterCount - 1;699 op += ' ${COUNTER_PRECISION} int i = 0;\n' +700 ' while (i++ < ${TWO}) { res = res.yzwx; }\n' +701 ' while (i++ < ${ITER_COUNT}) { res = res.yzwx; }\n'; // \note skips one iteration702 break;703 case es3fShaderLoopTests.LoopCase.LOOPCASE_NESTED:704 numIters = 2 * iterCount;705 op += ' ${COUNTER_PRECISION} int i = 0;\n' +706 ' while (i++ < ${TWO})\n' +707 ' {\n' +708 ' ${COUNTER_PRECISION} int j = 0;\n' +709 ' while (j++ < ${ITER_COUNT})\n' +710 ' res = res.yzwx;\n' +711 ' }\n';712 break;713 case es3fShaderLoopTests.LoopCase.LOOPCASE_NESTED_SEQUENCE:714 numIters = 2 * iterCount;715 op += ' ${COUNTER_PRECISION} int i = 0;\n' +716 ' while (i++ < ${ITER_COUNT})\n' +717 ' {\n' +718 ' ${COUNTER_PRECISION} int j = 0;\n' +719 ' while (j++ < ${ONE})\n' +720 ' res = res.yzwx;\n' +721 ' while (j++ < ${THREE})\n' + // \note skips one iteration722 ' res = res.yzwx;\n' +723 ' }\n';724 break;725 case es3fShaderLoopTests.LoopCase.LOOPCASE_NESTED_TRICKY_DATAFLOW_1:726 numIters = 2;727 op += ' ${WHILE_LOOP}\n' +728 ' {\n' +729 ' res = coords; // ignore outer loop effect \n' +730 ' ${COUNTER_PRECISION} int j = 0;\n' +731 ' while (j++ < ${TWO})\n' +732 ' res = res.yzwx;\n' +733 ' }\n';734 break;735 case es3fShaderLoopTests.LoopCase.LOOPCASE_NESTED_TRICKY_DATAFLOW_2:736 numIters = iterCount;737 op += ' ${WHILE_LOOP}\n' +738 ' {\n' +739 ' res = coords.wxyz;\n' +740 ' ${COUNTER_PRECISION} int j = 0;\n' +741 ' while (j++ < ${TWO})\n' +742 ' res = res.yzwx;\n' +743 ' coords = res;\n' +744 ' }\n';745 break;746 default:747 throw new Error('Loop Case not supported: ' + loopCase);748 }749 if (loopCountType === es3fShaderLoopTests.LoopCountType.LOOPCOUNT_CONSTANT)750 whileLoopStr = '\t' + counterPrecisionStr + ' int i = 0;\n' + ' while(i++ < ' + iterCount + ')';751 else if (loopCountType === es3fShaderLoopTests.LoopCountType.LOOPCOUNT_UNIFORM)752 whileLoopStr = '\t' + counterPrecisionStr + ' int i = 0;\n' + ' while(i++ < ' + glsShaderRenderCase.getIntUniformName(iterCount) + ')';753 else if (loopCountType === es3fShaderLoopTests.LoopCountType.LOOPCOUNT_DYNAMIC)754 whileLoopStr = '\t' + counterPrecisionStr + ' int i = 0;\n' + ' while(i++ < one*' + glsShaderRenderCase.getIntUniformName(iterCount) + ')';755 else756 throw new Error('Loop Count Type not supported: ' + loopCountType);757 }758 else {759 assertMsgOptions(loopType === es3fShaderLoopTests.LoopType.LOOPTYPE_DO_WHILE, 'Expected LOOPTYPE_DO_WHILE', false, true);760 switch (loopCase) {761 case es3fShaderLoopTests.LoopCase.LOOPCASE_EMPTY_BODY:762 numIters = 0;763 op += ' ${DO_WHILE_PRE} {} ${DO_WHILE_POST}\n';764 break;765 case es3fShaderLoopTests.LoopCase.LOOPCASE_INFINITE_WITH_UNCONDITIONAL_BREAK_FIRST:766 numIters = 0;767 op += ' do { break; res = res.yzwx; } while (true);\n';768 break;769 case es3fShaderLoopTests.LoopCase.LOOPCASE_INFINITE_WITH_UNCONDITIONAL_BREAK_LAST:770 numIters = 1;771 op += ' do { res = res.yzwx; break; } while (true);\n';772 break;773 case es3fShaderLoopTests.LoopCase.LOOPCASE_INFINITE_WITH_CONDITIONAL_BREAK:774 numIters = 2;775 op += ' ${COUNTER_PRECISION} int i = 0;\n' +776 ' do { res = res.yzwx; if (i == ${ONE}) break; i++; } while (true);\n';777 break;778 case es3fShaderLoopTests.LoopCase.LOOPCASE_SINGLE_STATEMENT:779 op += ' ${DO_WHILE_PRE} res = res.yzwx; ${DO_WHILE_POST}\n';780 break;781 case es3fShaderLoopTests.LoopCase.LOOPCASE_COMPOUND_STATEMENT:782 iterCount = 2;783 numIters = 2 * iterCount;784 op += ' ${DO_WHILE_PRE} { res = res.yzwx; res = res.yzwx; } ${DO_WHILE_POST}\n';785 break;786 case es3fShaderLoopTests.LoopCase.LOOPCASE_SEQUENCE_STATEMENT:787 iterCount = 2;788 numIters = 2 * iterCount;789 op += ' ${DO_WHILE_PRE} res = res.yzwx, res = res.yzwx; ${DO_WHILE_POST}\n';790 break;791 case es3fShaderLoopTests.LoopCase.LOOPCASE_NO_ITERATIONS:792 //assertMsgOptions(false, 'LOOPCASE_NO_ITERATIONS', false, false);793 break;794 case es3fShaderLoopTests.LoopCase.LOOPCASE_SINGLE_ITERATION:795 iterCount = 1;796 numIters = 1;797 op += ' ${DO_WHILE_PRE} res = res.yzwx; ${DO_WHILE_POST}\n';798 break;799 case es3fShaderLoopTests.LoopCase.LOOPCASE_SELECT_ITERATION_COUNT:800 op += ' ${COUNTER_PRECISION} int i = 0;\n' +801 ' do { res = res.yzwx; } while (++i < (ub_true ? ${ITER_COUNT} : 0));\n';802 break;803 case es3fShaderLoopTests.LoopCase.LOOPCASE_CONDITIONAL_CONTINUE:804 numIters = iterCount - 1;805 op += ' ${DO_WHILE_PRE} { if (i == ${TWO}) continue; res = res.yzwx; } ${DO_WHILE_POST}\n';806 break;807 case es3fShaderLoopTests.LoopCase.LOOPCASE_UNCONDITIONAL_CONTINUE:808 op += ' ${DO_WHILE_PRE} { res = res.yzwx; continue; } ${DO_WHILE_POST}\n';809 break;810 case es3fShaderLoopTests.LoopCase.LOOPCASE_ONLY_CONTINUE:811 numIters = 0;812 op += ' ${DO_WHILE_PRE} { continue; } ${DO_WHILE_POST}\n';813 break;814 case es3fShaderLoopTests.LoopCase.LOOPCASE_DOUBLE_CONTINUE:815 numIters = iterCount - 1;816 op += ' ${DO_WHILE_PRE} { if (i == ${TWO}) continue; res = res.yzwx; continue; } ${DO_WHILE_POST}\n';817 break;818 case es3fShaderLoopTests.LoopCase.LOOPCASE_CONDITIONAL_BREAK:819 numIters = 2;820 op += ' ${DO_WHILE_PRE} { res = res.yzwx; if (i == ${ONE}) break; } ${DO_WHILE_POST}\n';821 break;822 case es3fShaderLoopTests.LoopCase.LOOPCASE_UNCONDITIONAL_BREAK:823 numIters = 1;824 op += ' ${DO_WHILE_PRE} { res = res.yzwx; break; } ${DO_WHILE_POST}\n';825 break;826 case es3fShaderLoopTests.LoopCase.LOOPCASE_PRE_INCREMENT:827 op += ' ${COUNTER_PRECISION} int i = 0;\n' +828 ' do { res = res.yzwx; } while (++i < ${ITER_COUNT});\n';829 break;830 case es3fShaderLoopTests.LoopCase.LOOPCASE_POST_INCREMENT:831 numIters = iterCount + 1;832 op += ' ${COUNTER_PRECISION} int i = 0;\n' +833 ' do { res = res.yzwx; } while (i++ < ${ITER_COUNT});\n';834 break;835 case es3fShaderLoopTests.LoopCase.LOOPCASE_MIXED_BREAK_CONTINUE:836 numIters = 2;837 iterCount = 5;838 op += ' ${DO_WHILE_PRE} { if (i == 0) continue; else if (i == 3) break; res = res.yzwx; } ${DO_WHILE_POST}\n';839 break;840 case es3fShaderLoopTests.LoopCase.LOOPCASE_VECTOR_COUNTER:841 op += ' ${COUNTER_PRECISION} ivec4 i = ivec4(0, 1, ${ITER_COUNT}, 0);\n' +842 ' do { res = res.yzwx; } while ((i.x += i.y) < i.z);\n';843 break;844 case es3fShaderLoopTests.LoopCase.LOOPCASE_101_ITERATIONS:845 numIters = iterCount = 101;846 op += ' ${DO_WHILE_PRE} res = res.yzwx; ${DO_WHILE_POST}\n';847 break;848 case es3fShaderLoopTests.LoopCase.LOOPCASE_SEQUENCE:849 iterCount = 5;850 numIters = 5;851 op += ' ${COUNTER_PRECISION} int i = 0;\n' +852 ' do { res = res.yzwx; } while (++i < ${TWO});\n' +853 ' do { res = res.yzwx; } while (++i < ${ITER_COUNT});\n';854 break;855 case es3fShaderLoopTests.LoopCase.LOOPCASE_NESTED:856 numIters = 2 * iterCount;857 op += ' ${COUNTER_PRECISION} int i = 0;\n' +858 ' do\n' +859 ' {\n' +860 ' ${COUNTER_PRECISION} int j = 0;\n' +861 ' do\n' +862 ' res = res.yzwx;\n' +863 ' while (++j < ${ITER_COUNT});\n' +864 ' } while (++i < ${TWO});\n';865 break;866 case es3fShaderLoopTests.LoopCase.LOOPCASE_NESTED_SEQUENCE:867 numIters = 3 * iterCount;868 op += ' ${COUNTER_PRECISION} int i = 0;\n' +869 ' do\n' +870 ' {\n' +871 ' ${COUNTER_PRECISION} int j = 0;\n' +872 ' do\n' +873 ' res = res.yzwx;\n' +874 ' while (++j < ${TWO});\n' +875 ' do\n' +876 ' res = res.yzwx;\n' +877 ' while (++j < ${THREE});\n' +878 ' } while (++i < ${ITER_COUNT});\n';879 break;880 case es3fShaderLoopTests.LoopCase.LOOPCASE_NESTED_TRICKY_DATAFLOW_1:881 numIters = 2;882 op += ' ${DO_WHILE_PRE}\n' +883 ' {\n' +884 ' res = coords; // ignore outer loop effect \n' +885 ' ${COUNTER_PRECISION} int j = 0;\n' +886 ' do\n' +887 ' res = res.yzwx;\n' +888 ' while (++j < ${TWO});\n' +889 ' } ${DO_WHILE_POST}\n';890 break;891 case es3fShaderLoopTests.LoopCase.LOOPCASE_NESTED_TRICKY_DATAFLOW_2:892 numIters = iterCount;893 op += ' ${DO_WHILE_PRE}\n' +894 ' {\n' +895 ' res = coords.wxyz;\n' +896 ' ${COUNTER_PRECISION} int j = 0;\n' +897 ' while (j++ < ${TWO})\n' +898 ' res = res.yzwx;\n' +899 ' coords = res;\n' +900 ' } ${DO_WHILE_POST}\n';901 break;902 default:903 throw new Error('Loop Case not supported: ' + loopCase);904 }905 doWhileLoopPreStr = '\t' + counterPrecisionStr + ' int i = 0;\n' + '\tdo ';906 if (loopCountType === es3fShaderLoopTests.LoopCountType.LOOPCOUNT_CONSTANT)907 doWhileLoopPostStr = ' while (++i < ' + iterCount + ');\n';908 else if (loopCountType === es3fShaderLoopTests.LoopCountType.LOOPCOUNT_UNIFORM)909 doWhileLoopPostStr = ' while (++i < ' + glsShaderRenderCase.getIntUniformName(iterCount) + ');\n';910 else if (loopCountType === es3fShaderLoopTests.LoopCountType.LOOPCOUNT_DYNAMIC)911 doWhileLoopPostStr = ' while (++i < one*' + glsShaderRenderCase.getIntUniformName(iterCount) + ');\n';912 else913 throw new Error('Loop Count Type not supported: ' + loopCountType);914 }915 vtx += isVertexCase ? op : '';916 frag += isVertexCase ? '' : op;917 op = '';918 // Shader footers.919 if (isVertexCase) {920 vtx += ' v_color = res.rgb;\n';921 frag += ' o_color = vec4(v_color.rgb, 1.0);\n';922 }923 else {924 vtx += ' v_coords = a_coords;\n';925 frag += ' o_color = vec4(res.rgb, 1.0);\n';926 if (loopCountType === es3fShaderLoopTests.LoopCountType.LOOPCOUNT_DYNAMIC)927 vtx += ' v_one = a_one;\n';928 }929 vtx += '}\n';930 frag += '}\n';931 // Constants.932 /** @type {string} */ var oneStr = '';933 /** @type {string} */ var twoStr = '';934 /** @type {string} */ var threeStr = '';935 /** @type {string} */ var iterCountStr = '';936 if (loopCountType === es3fShaderLoopTests.LoopCountType.LOOPCOUNT_CONSTANT) {937 oneStr = '1';938 twoStr = '2';939 threeStr = '3';940 iterCountStr = iterCount.toString();941 }942 else if (loopCountType === es3fShaderLoopTests.LoopCountType.LOOPCOUNT_UNIFORM) {943 oneStr = 'ui_one';944 twoStr = 'ui_two';945 threeStr = 'ui_three';946 iterCountStr = glsShaderRenderCase.getIntUniformName(iterCount);947 }948 else if (loopCountType === es3fShaderLoopTests.LoopCountType.LOOPCOUNT_DYNAMIC) {949 oneStr = 'one*ui_one';950 twoStr = 'one*ui_two';951 threeStr = 'one*ui_three';952 iterCountStr = 'one*' + glsShaderRenderCase.getIntUniformName(iterCount);953 }954 else throw new Error('Loop Count Type not supported: ' + loopCountType);955 // Fill in shader templates.956 /** @type {Object} */ var params = {};957 params["PRECISION"] = "mediump";958 params["ITER_COUNT"] = iterCountStr;959 params["COUNTER_PRECISION"] = counterPrecisionStr;960 params["FOR_LOOP"] = forLoopStr;961 params["WHILE_LOOP"] = whileLoopStr;962 params["DO_WHILE_PRE"] = doWhileLoopPreStr;963 params["DO_WHILE_POST"] = doWhileLoopPostStr;964 params["ONE"] = oneStr;965 params["TWO"] = twoStr;966 params["THREE"] = threeStr;967 /** @type {string} */ var vertexShaderSource = tcuStringTemplate.specialize(vtx, params);968 /** @type {string} */ var fragmentShaderSource = tcuStringTemplate.specialize(frag, params);969 // Create the case.970 /** @type {glsShaderRenderCase.ShaderEvalFunc} */971 var evalFunc = es3fShaderLoopTests.getLoopEvalFunc(numIters);972 return new es3fShaderLoopTests.ShaderLoopCase(caseName, description, isVertexCase, evalFunc, vertexShaderSource, fragmentShaderSource);973};974// ShaderLoopTests.975/**976 * @constructor977 * @extends {tcuTestCase.DeqpTest}978 */979es3fShaderLoopTests.ShaderLoopTests = function() {980 tcuTestCase.DeqpTest.call(this, 'loops', 'Loop Tests');981};982es3fShaderLoopTests.ShaderLoopTests.prototype = Object.create(tcuTestCase.DeqpTest.prototype);983es3fShaderLoopTests.ShaderLoopTests.prototype.constructor = es3fShaderLoopTests.ShaderLoopTests;984es3fShaderLoopTests.ShaderLoopTests.prototype.init = function() {985 var testGroup = tcuTestCase.runner.testCases;986 // Loop cases.987 /** @type {Array<gluShaderProgram.shaderType>} */ var s_shaderTypes = [988 gluShaderProgram.shaderType.VERTEX,989 gluShaderProgram.shaderType.FRAGMENT990 ];991 /** @type {Array<gluShaderUtil.DataType>} */ var s_countDataType = [992 gluShaderUtil.DataType.INT,993 gluShaderUtil.DataType.FLOAT994 ];995 /** @type {gluShaderProgram.shaderType} */ var shaderType;996 /** @type {string} */ var shaderTypeName;997 /** @type {boolean} */ var isVertexCase;998 /** @type {string} */ var name;999 /** @type {string} */ var desc;1000 for (var loopType in es3fShaderLoopTests.LoopType) {1001 /** @type {string} */ var loopTypeName = es3fShaderLoopTests.getLoopTypeName(es3fShaderLoopTests.LoopType[loopType]);1002 /** @type {tcuTestCase.DeqpTest} */ var loopTypeGroup = tcuTestCase.newTest(loopTypeName, 'Loop tests with ' + loopTypeName + ' loop type');1003 testGroup.addChild(loopTypeGroup);1004 for (var loopCountType in es3fShaderLoopTests.LoopCountType) {1005 /** @type {string} */ var loopCountName = es3fShaderLoopTests.getLoopCountTypeName(es3fShaderLoopTests.LoopCountType[loopCountType]);1006 /** @type {string} */ var groupName = loopCountName + '_iterations';1007 /** @type {string} */ var groupDesc = 'Loop tests with ' + loopCountName + ' loop counter.';1008 /** @type {tcuTestCase.DeqpTest} */ var group = tcuTestCase.newTest(groupName, groupDesc);1009 loopTypeGroup.addChild(group);1010 // Generic cases.1011 for (var precision in gluShaderUtil.precision) {1012 /** @type {string} */ var precisionName = gluShaderUtil.getPrecisionName(gluShaderUtil.precision[precision]);1013 for (var dataTypeNdx = 0; dataTypeNdx < s_countDataType.length; dataTypeNdx++) {1014 /** @type {gluShaderUtil.DataType} */ var loopDataType = s_countDataType[dataTypeNdx];1015 /** @type {string} */ var dataTypeName = gluShaderUtil.getDataTypeName(loopDataType);1016 for (var shaderTypeNdx = 0; shaderTypeNdx < s_shaderTypes.length; shaderTypeNdx++) {1017 shaderType = s_shaderTypes[shaderTypeNdx];1018 shaderTypeName = gluShaderProgram.getShaderTypeName(shaderType);1019 isVertexCase = (shaderType == gluShaderProgram.shaderType.VERTEX);1020 name = 'basic_' + precisionName + '_' + dataTypeName + '_' + shaderTypeName;1021 desc = loopTypeName + ' loop with ' + precisionName + dataTypeName + ' ' + loopCountName + ' iteration count in ' + shaderTypeName + ' shader.';1022 group.addChild(es3fShaderLoopTests.createGenericLoopCase(name, desc, isVertexCase, es3fShaderLoopTests.LoopType[loopType], es3fShaderLoopTests.LoopCountType[loopCountType], gluShaderUtil.precision[precision], loopDataType));1023 }1024 }1025 }1026 // Special cases.1027 for (var loopCase in es3fShaderLoopTests.LoopCase) {1028 /** @type {string} */ var loopCaseName = es3fShaderLoopTests.getLoopCaseName(es3fShaderLoopTests.LoopCase[loopCase]);1029 // no-iterations not possible with do-while.1030 if ((es3fShaderLoopTests.LoopCase[loopCase] == es3fShaderLoopTests.LoopCase.LOOPCASE_NO_ITERATIONS) && (es3fShaderLoopTests.LoopType[loopType] == es3fShaderLoopTests.LoopType.LOOPTYPE_DO_WHILE))1031 continue;1032 for (var shaderTypeNdx = 0; shaderTypeNdx < s_shaderTypes.length; shaderTypeNdx++) {1033 shaderType = s_shaderTypes[shaderTypeNdx];1034 shaderTypeName = gluShaderProgram.getShaderTypeName(shaderType);1035 isVertexCase = (shaderType == gluShaderProgram.shaderType.VERTEX);1036 name = loopCaseName + '_' + shaderTypeName;1037 desc = loopCaseName + ' loop with ' + loopTypeName + ' iteration count in ' + shaderTypeName + ' shader.';1038 group.addChild(es3fShaderLoopTests.createSpecialLoopCase(name, desc, isVertexCase, es3fShaderLoopTests.LoopCase[loopCase], es3fShaderLoopTests.LoopType[loopType], es3fShaderLoopTests.LoopCountType[loopCountType]));1039 }1040 }1041 }1042 }1043};1044/**1045* Run test1046* @param {WebGL2RenderingContext} context1047*/1048es3fShaderLoopTests.run = function(context, range) {1049 gl = context;1050 //Set up Test Root parameters1051 var state = tcuTestCase.runner;1052 state.setRoot(new es3fShaderLoopTests.ShaderLoopTests());1053 //Set up name and description of this test series.1054 setCurrentTestName(state.testCases.fullName());1055 description(state.testCases.getDescription());1056 try {1057 if (range)1058 state.setRange(range);1059 //Run test cases1060 tcuTestCase.runTestCases();1061 }1062 catch (err) {1063 testFailedOptions('Failed to es3fShaderLoopTests.run tests', false);1064 tcuTestCase.runner.terminate();1065 }1066};...
test_locks.py
Source:test_locks.py
...15 asyncio.set_event_loop_policy(None)16class LockTests(test_utils.TestCase):17 def setUp(self):18 super().setUp()19 self.loop = self.new_test_loop()20 def test_ctor_loop(self):21 loop = mock.Mock()22 with self.assertWarns(DeprecationWarning):23 lock = asyncio.Lock(loop=loop)24 self.assertIs(lock._loop, loop)25 with self.assertWarns(DeprecationWarning):26 lock = asyncio.Lock(loop=self.loop)27 self.assertIs(lock._loop, self.loop)28 def test_ctor_noloop(self):29 asyncio.set_event_loop(self.loop)30 lock = asyncio.Lock()31 self.assertIs(lock._loop, self.loop)32 def test_repr(self):33 with self.assertWarns(DeprecationWarning):34 lock = asyncio.Lock(loop=self.loop)35 self.assertTrue(repr(lock).endswith('[unlocked]>'))36 self.assertTrue(RGX_REPR.match(repr(lock)))37 with self.assertWarns(DeprecationWarning):38 @asyncio.coroutine39 def acquire_lock():40 with self.assertWarns(DeprecationWarning):41 yield from lock42 self.loop.run_until_complete(acquire_lock())43 self.assertTrue(repr(lock).endswith('[locked]>'))44 self.assertTrue(RGX_REPR.match(repr(lock)))45 def test_lock(self):46 with self.assertWarns(DeprecationWarning):47 lock = asyncio.Lock(loop=self.loop)48 @asyncio.coroutine49 def acquire_lock():50 with self.assertWarns(DeprecationWarning):51 return (yield from lock)52 res = self.loop.run_until_complete(acquire_lock())53 self.assertTrue(res)54 self.assertTrue(lock.locked())55 lock.release()56 self.assertFalse(lock.locked())57 def test_lock_by_with_statement(self):58 loop = asyncio.new_event_loop() # don't use TestLoop quirks59 self.set_event_loop(loop)60 with self.assertWarns(DeprecationWarning):61 primitives = [62 asyncio.Lock(loop=loop),63 asyncio.Condition(loop=loop),64 asyncio.Semaphore(loop=loop),65 asyncio.BoundedSemaphore(loop=loop),66 ]67 @asyncio.coroutine68 def test(lock):69 yield from asyncio.sleep(0.01)70 self.assertFalse(lock.locked())71 with self.assertWarns(DeprecationWarning):72 with (yield from lock) as _lock:73 self.assertIs(_lock, None)74 self.assertTrue(lock.locked())75 yield from asyncio.sleep(0.01)76 self.assertTrue(lock.locked())77 self.assertFalse(lock.locked())78 for primitive in primitives:79 loop.run_until_complete(test(primitive))80 self.assertFalse(primitive.locked())81 def test_acquire(self):82 with self.assertWarns(DeprecationWarning):83 lock = asyncio.Lock(loop=self.loop)84 result = []85 self.assertTrue(self.loop.run_until_complete(lock.acquire()))86 async def c1(result):87 if await lock.acquire():88 result.append(1)89 return True90 async def c2(result):91 if await lock.acquire():92 result.append(2)93 return True94 async def c3(result):95 if await lock.acquire():96 result.append(3)97 return True98 t1 = self.loop.create_task(c1(result))99 t2 = self.loop.create_task(c2(result))100 test_utils.run_briefly(self.loop)101 self.assertEqual([], result)102 lock.release()103 test_utils.run_briefly(self.loop)104 self.assertEqual([1], result)105 test_utils.run_briefly(self.loop)106 self.assertEqual([1], result)107 t3 = self.loop.create_task(c3(result))108 lock.release()109 test_utils.run_briefly(self.loop)110 self.assertEqual([1, 2], result)111 lock.release()112 test_utils.run_briefly(self.loop)113 self.assertEqual([1, 2, 3], result)114 self.assertTrue(t1.done())115 self.assertTrue(t1.result())116 self.assertTrue(t2.done())117 self.assertTrue(t2.result())118 self.assertTrue(t3.done())119 self.assertTrue(t3.result())120 def test_acquire_cancel(self):121 with self.assertWarns(DeprecationWarning):122 lock = asyncio.Lock(loop=self.loop)123 self.assertTrue(self.loop.run_until_complete(lock.acquire()))124 task = self.loop.create_task(lock.acquire())125 self.loop.call_soon(task.cancel)126 self.assertRaises(127 asyncio.CancelledError,128 self.loop.run_until_complete, task)129 self.assertFalse(lock._waiters)130 def test_cancel_race(self):131 # Several tasks:132 # - A acquires the lock133 # - B is blocked in acquire()134 # - C is blocked in acquire()135 #136 # Now, concurrently:137 # - B is cancelled138 # - A releases the lock139 #140 # If B's waiter is marked cancelled but not yet removed from141 # _waiters, A's release() call will crash when trying to set142 # B's waiter; instead, it should move on to C's waiter.143 # Setup: A has the lock, b and c are waiting.144 with self.assertWarns(DeprecationWarning):145 lock = asyncio.Lock(loop=self.loop)146 async def lockit(name, blocker):147 await lock.acquire()148 try:149 if blocker is not None:150 await blocker151 finally:152 lock.release()153 fa = self.loop.create_future()154 ta = self.loop.create_task(lockit('A', fa))155 test_utils.run_briefly(self.loop)156 self.assertTrue(lock.locked())157 tb = self.loop.create_task(lockit('B', None))158 test_utils.run_briefly(self.loop)159 self.assertEqual(len(lock._waiters), 1)160 tc = self.loop.create_task(lockit('C', None))161 test_utils.run_briefly(self.loop)162 self.assertEqual(len(lock._waiters), 2)163 # Create the race and check.164 # Without the fix this failed at the last assert.165 fa.set_result(None)166 tb.cancel()167 self.assertTrue(lock._waiters[0].cancelled())168 test_utils.run_briefly(self.loop)169 self.assertFalse(lock.locked())170 self.assertTrue(ta.done())171 self.assertTrue(tb.cancelled())172 self.assertTrue(tc.done())173 def test_cancel_release_race(self):174 # Issue 32734175 # Acquire 4 locks, cancel second, release first176 # and 2 locks are taken at once.177 with self.assertWarns(DeprecationWarning):178 lock = asyncio.Lock(loop=self.loop)179 lock_count = 0180 call_count = 0181 async def lockit():182 nonlocal lock_count183 nonlocal call_count184 call_count += 1185 await lock.acquire()186 lock_count += 1187 async def lockandtrigger():188 await lock.acquire()189 self.loop.call_soon(trigger)190 def trigger():191 t1.cancel()192 lock.release()193 t0 = self.loop.create_task(lockandtrigger())194 t1 = self.loop.create_task(lockit())195 t2 = self.loop.create_task(lockit())196 t3 = self.loop.create_task(lockit())197 # First loop acquires all198 test_utils.run_briefly(self.loop)199 self.assertTrue(t0.done())200 # Second loop calls trigger201 test_utils.run_briefly(self.loop)202 # Third loop calls cancellation203 test_utils.run_briefly(self.loop)204 # Make sure only one lock was taken205 self.assertEqual(lock_count, 1)206 # While 3 calls were made to lockit()207 self.assertEqual(call_count, 3)208 self.assertTrue(t1.cancelled() and t2.done())209 # Cleanup the task that is stuck on acquire.210 t3.cancel()211 test_utils.run_briefly(self.loop)212 self.assertTrue(t3.cancelled())213 def test_finished_waiter_cancelled(self):214 with self.assertWarns(DeprecationWarning):215 lock = asyncio.Lock(loop=self.loop)216 ta = self.loop.create_task(lock.acquire())217 test_utils.run_briefly(self.loop)218 self.assertTrue(lock.locked())219 tb = self.loop.create_task(lock.acquire())220 test_utils.run_briefly(self.loop)221 self.assertEqual(len(lock._waiters), 1)222 # Create a second waiter, wake up the first, and cancel it.223 # Without the fix, the second was not woken up.224 tc = self.loop.create_task(lock.acquire())225 lock.release()226 tb.cancel()227 test_utils.run_briefly(self.loop)228 self.assertTrue(lock.locked())229 self.assertTrue(ta.done())230 self.assertTrue(tb.cancelled())231 def test_release_not_acquired(self):232 with self.assertWarns(DeprecationWarning):233 lock = asyncio.Lock(loop=self.loop)234 self.assertRaises(RuntimeError, lock.release)235 def test_release_no_waiters(self):236 with self.assertWarns(DeprecationWarning):237 lock = asyncio.Lock(loop=self.loop)238 self.loop.run_until_complete(lock.acquire())239 self.assertTrue(lock.locked())240 lock.release()241 self.assertFalse(lock.locked())242 def test_context_manager(self):243 with self.assertWarns(DeprecationWarning):244 lock = asyncio.Lock(loop=self.loop)245 @asyncio.coroutine246 def acquire_lock():247 with self.assertWarns(DeprecationWarning):248 return (yield from lock)249 with self.loop.run_until_complete(acquire_lock()):250 self.assertTrue(lock.locked())251 self.assertFalse(lock.locked())252 def test_context_manager_cant_reuse(self):253 with self.assertWarns(DeprecationWarning):254 lock = asyncio.Lock(loop=self.loop)255 @asyncio.coroutine256 def acquire_lock():257 with self.assertWarns(DeprecationWarning):258 return (yield from lock)259 # This spells "yield from lock" outside a generator.260 cm = self.loop.run_until_complete(acquire_lock())261 with cm:262 self.assertTrue(lock.locked())263 self.assertFalse(lock.locked())264 with self.assertRaises(AttributeError):265 with cm:266 pass267 def test_context_manager_no_yield(self):268 with self.assertWarns(DeprecationWarning):269 lock = asyncio.Lock(loop=self.loop)270 try:271 with lock:272 self.fail('RuntimeError is not raised in with expression')273 except RuntimeError as err:274 self.assertEqual(275 str(err),276 '"yield from" should be used as context manager expression')277 self.assertFalse(lock.locked())278class EventTests(test_utils.TestCase):279 def setUp(self):280 super().setUp()281 self.loop = self.new_test_loop()282 def test_ctor_loop(self):283 loop = mock.Mock()284 with self.assertWarns(DeprecationWarning):285 ev = asyncio.Event(loop=loop)286 self.assertIs(ev._loop, loop)287 with self.assertWarns(DeprecationWarning):288 ev = asyncio.Event(loop=self.loop)289 self.assertIs(ev._loop, self.loop)290 def test_ctor_noloop(self):291 asyncio.set_event_loop(self.loop)292 ev = asyncio.Event()293 self.assertIs(ev._loop, self.loop)294 def test_repr(self):295 with self.assertWarns(DeprecationWarning):296 ev = asyncio.Event(loop=self.loop)297 self.assertTrue(repr(ev).endswith('[unset]>'))298 match = RGX_REPR.match(repr(ev))299 self.assertEqual(match.group('extras'), 'unset')300 ev.set()301 self.assertTrue(repr(ev).endswith('[set]>'))302 self.assertTrue(RGX_REPR.match(repr(ev)))303 ev._waiters.append(mock.Mock())304 self.assertTrue('waiters:1' in repr(ev))305 self.assertTrue(RGX_REPR.match(repr(ev)))306 def test_wait(self):307 with self.assertWarns(DeprecationWarning):308 ev = asyncio.Event(loop=self.loop)309 self.assertFalse(ev.is_set())310 result = []311 async def c1(result):312 if await ev.wait():313 result.append(1)314 async def c2(result):315 if await ev.wait():316 result.append(2)317 async def c3(result):318 if await ev.wait():319 result.append(3)320 t1 = self.loop.create_task(c1(result))321 t2 = self.loop.create_task(c2(result))322 test_utils.run_briefly(self.loop)323 self.assertEqual([], result)324 t3 = self.loop.create_task(c3(result))325 ev.set()326 test_utils.run_briefly(self.loop)327 self.assertEqual([3, 1, 2], result)328 self.assertTrue(t1.done())329 self.assertIsNone(t1.result())330 self.assertTrue(t2.done())331 self.assertIsNone(t2.result())332 self.assertTrue(t3.done())333 self.assertIsNone(t3.result())334 def test_wait_on_set(self):335 with self.assertWarns(DeprecationWarning):336 ev = asyncio.Event(loop=self.loop)337 ev.set()338 res = self.loop.run_until_complete(ev.wait())339 self.assertTrue(res)340 def test_wait_cancel(self):341 with self.assertWarns(DeprecationWarning):342 ev = asyncio.Event(loop=self.loop)343 wait = self.loop.create_task(ev.wait())344 self.loop.call_soon(wait.cancel)345 self.assertRaises(346 asyncio.CancelledError,347 self.loop.run_until_complete, wait)348 self.assertFalse(ev._waiters)349 def test_clear(self):350 with self.assertWarns(DeprecationWarning):351 ev = asyncio.Event(loop=self.loop)352 self.assertFalse(ev.is_set())353 ev.set()354 self.assertTrue(ev.is_set())355 ev.clear()356 self.assertFalse(ev.is_set())357 def test_clear_with_waiters(self):358 with self.assertWarns(DeprecationWarning):359 ev = asyncio.Event(loop=self.loop)360 result = []361 async def c1(result):362 if await ev.wait():363 result.append(1)364 return True365 t = self.loop.create_task(c1(result))366 test_utils.run_briefly(self.loop)367 self.assertEqual([], result)368 ev.set()369 ev.clear()370 self.assertFalse(ev.is_set())371 ev.set()372 ev.set()373 self.assertEqual(1, len(ev._waiters))374 test_utils.run_briefly(self.loop)375 self.assertEqual([1], result)376 self.assertEqual(0, len(ev._waiters))377 self.assertTrue(t.done())378 self.assertTrue(t.result())379class ConditionTests(test_utils.TestCase):380 def setUp(self):381 super().setUp()382 self.loop = self.new_test_loop()383 def test_ctor_loop(self):384 loop = mock.Mock()385 with self.assertWarns(DeprecationWarning):386 cond = asyncio.Condition(loop=loop)387 self.assertIs(cond._loop, loop)388 cond = asyncio.Condition(loop=self.loop)389 self.assertIs(cond._loop, self.loop)390 def test_ctor_noloop(self):391 asyncio.set_event_loop(self.loop)392 cond = asyncio.Condition()393 self.assertIs(cond._loop, self.loop)394 def test_wait(self):395 with self.assertWarns(DeprecationWarning):396 cond = asyncio.Condition(loop=self.loop)397 result = []398 async def c1(result):399 await cond.acquire()400 if await cond.wait():401 result.append(1)402 return True403 async def c2(result):404 await cond.acquire()405 if await cond.wait():406 result.append(2)407 return True408 async def c3(result):409 await cond.acquire()410 if await cond.wait():411 result.append(3)412 return True413 t1 = self.loop.create_task(c1(result))414 t2 = self.loop.create_task(c2(result))415 t3 = self.loop.create_task(c3(result))416 test_utils.run_briefly(self.loop)417 self.assertEqual([], result)418 self.assertFalse(cond.locked())419 self.assertTrue(self.loop.run_until_complete(cond.acquire()))420 cond.notify()421 test_utils.run_briefly(self.loop)422 self.assertEqual([], result)423 self.assertTrue(cond.locked())424 cond.release()425 test_utils.run_briefly(self.loop)426 self.assertEqual([1], result)427 self.assertTrue(cond.locked())428 cond.notify(2)429 test_utils.run_briefly(self.loop)430 self.assertEqual([1], result)431 self.assertTrue(cond.locked())432 cond.release()433 test_utils.run_briefly(self.loop)434 self.assertEqual([1, 2], result)435 self.assertTrue(cond.locked())436 cond.release()437 test_utils.run_briefly(self.loop)438 self.assertEqual([1, 2, 3], result)439 self.assertTrue(cond.locked())440 self.assertTrue(t1.done())441 self.assertTrue(t1.result())442 self.assertTrue(t2.done())443 self.assertTrue(t2.result())444 self.assertTrue(t3.done())445 self.assertTrue(t3.result())446 def test_wait_cancel(self):447 with self.assertWarns(DeprecationWarning):448 cond = asyncio.Condition(loop=self.loop)449 self.loop.run_until_complete(cond.acquire())450 wait = self.loop.create_task(cond.wait())451 self.loop.call_soon(wait.cancel)452 self.assertRaises(453 asyncio.CancelledError,454 self.loop.run_until_complete, wait)455 self.assertFalse(cond._waiters)456 self.assertTrue(cond.locked())457 def test_wait_cancel_contested(self):458 with self.assertWarns(DeprecationWarning):459 cond = asyncio.Condition(loop=self.loop)460 self.loop.run_until_complete(cond.acquire())461 self.assertTrue(cond.locked())462 wait_task = self.loop.create_task(cond.wait())463 test_utils.run_briefly(self.loop)464 self.assertFalse(cond.locked())465 # Notify, but contest the lock before cancelling466 self.loop.run_until_complete(cond.acquire())467 self.assertTrue(cond.locked())468 cond.notify()469 self.loop.call_soon(wait_task.cancel)470 self.loop.call_soon(cond.release)471 try:472 self.loop.run_until_complete(wait_task)473 except asyncio.CancelledError:474 # Should not happen, since no cancellation points475 pass476 self.assertTrue(cond.locked())477 def test_wait_cancel_after_notify(self):478 # See bpo-32841479 with self.assertWarns(DeprecationWarning):480 cond = asyncio.Condition(loop=self.loop)481 waited = False482 async def wait_on_cond():483 nonlocal waited484 async with cond:485 waited = True # Make sure this area was reached486 await cond.wait()487 waiter = asyncio.ensure_future(wait_on_cond(), loop=self.loop)488 test_utils.run_briefly(self.loop) # Start waiting489 self.loop.run_until_complete(cond.acquire())490 cond.notify()491 test_utils.run_briefly(self.loop) # Get to acquire()492 waiter.cancel()493 test_utils.run_briefly(self.loop) # Activate cancellation494 cond.release()495 test_utils.run_briefly(self.loop) # Cancellation should occur496 self.assertTrue(waiter.cancelled())497 self.assertTrue(waited)498 def test_wait_unacquired(self):499 with self.assertWarns(DeprecationWarning):500 cond = asyncio.Condition(loop=self.loop)501 self.assertRaises(502 RuntimeError,503 self.loop.run_until_complete, cond.wait())504 def test_wait_for(self):505 with self.assertWarns(DeprecationWarning):506 cond = asyncio.Condition(loop=self.loop)507 presult = False508 def predicate():509 return presult510 result = []511 async def c1(result):512 await cond.acquire()513 if await cond.wait_for(predicate):514 result.append(1)515 cond.release()516 return True517 t = self.loop.create_task(c1(result))518 test_utils.run_briefly(self.loop)519 self.assertEqual([], result)520 self.loop.run_until_complete(cond.acquire())521 cond.notify()522 cond.release()523 test_utils.run_briefly(self.loop)524 self.assertEqual([], result)525 presult = True526 self.loop.run_until_complete(cond.acquire())527 cond.notify()528 cond.release()529 test_utils.run_briefly(self.loop)530 self.assertEqual([1], result)531 self.assertTrue(t.done())532 self.assertTrue(t.result())533 def test_wait_for_unacquired(self):534 with self.assertWarns(DeprecationWarning):535 cond = asyncio.Condition(loop=self.loop)536 # predicate can return true immediately537 res = self.loop.run_until_complete(cond.wait_for(lambda: [1, 2, 3]))538 self.assertEqual([1, 2, 3], res)539 self.assertRaises(540 RuntimeError,541 self.loop.run_until_complete,542 cond.wait_for(lambda: False))543 def test_notify(self):544 with self.assertWarns(DeprecationWarning):545 cond = asyncio.Condition(loop=self.loop)546 result = []547 async def c1(result):548 await cond.acquire()549 if await cond.wait():550 result.append(1)551 cond.release()552 return True553 async def c2(result):554 await cond.acquire()555 if await cond.wait():556 result.append(2)557 cond.release()558 return True559 async def c3(result):560 await cond.acquire()561 if await cond.wait():562 result.append(3)563 cond.release()564 return True565 t1 = self.loop.create_task(c1(result))566 t2 = self.loop.create_task(c2(result))567 t3 = self.loop.create_task(c3(result))568 test_utils.run_briefly(self.loop)569 self.assertEqual([], result)570 self.loop.run_until_complete(cond.acquire())571 cond.notify(1)572 cond.release()573 test_utils.run_briefly(self.loop)574 self.assertEqual([1], result)575 self.loop.run_until_complete(cond.acquire())576 cond.notify(1)577 cond.notify(2048)578 cond.release()579 test_utils.run_briefly(self.loop)580 self.assertEqual([1, 2, 3], result)581 self.assertTrue(t1.done())582 self.assertTrue(t1.result())583 self.assertTrue(t2.done())584 self.assertTrue(t2.result())585 self.assertTrue(t3.done())586 self.assertTrue(t3.result())587 def test_notify_all(self):588 with self.assertWarns(DeprecationWarning):589 cond = asyncio.Condition(loop=self.loop)590 result = []591 async def c1(result):592 await cond.acquire()593 if await cond.wait():594 result.append(1)595 cond.release()596 return True597 async def c2(result):598 await cond.acquire()599 if await cond.wait():600 result.append(2)601 cond.release()602 return True603 t1 = self.loop.create_task(c1(result))604 t2 = self.loop.create_task(c2(result))605 test_utils.run_briefly(self.loop)606 self.assertEqual([], result)607 self.loop.run_until_complete(cond.acquire())608 cond.notify_all()609 cond.release()610 test_utils.run_briefly(self.loop)611 self.assertEqual([1, 2], result)612 self.assertTrue(t1.done())613 self.assertTrue(t1.result())614 self.assertTrue(t2.done())615 self.assertTrue(t2.result())616 def test_notify_unacquired(self):617 with self.assertWarns(DeprecationWarning):618 cond = asyncio.Condition(loop=self.loop)619 self.assertRaises(RuntimeError, cond.notify)620 def test_notify_all_unacquired(self):621 with self.assertWarns(DeprecationWarning):622 cond = asyncio.Condition(loop=self.loop)623 self.assertRaises(RuntimeError, cond.notify_all)624 def test_repr(self):625 with self.assertWarns(DeprecationWarning):626 cond = asyncio.Condition(loop=self.loop)627 self.assertTrue('unlocked' in repr(cond))628 self.assertTrue(RGX_REPR.match(repr(cond)))629 self.loop.run_until_complete(cond.acquire())630 self.assertTrue('locked' in repr(cond))631 cond._waiters.append(mock.Mock())632 self.assertTrue('waiters:1' in repr(cond))633 self.assertTrue(RGX_REPR.match(repr(cond)))634 cond._waiters.append(mock.Mock())635 self.assertTrue('waiters:2' in repr(cond))636 self.assertTrue(RGX_REPR.match(repr(cond)))637 def test_context_manager(self):638 with self.assertWarns(DeprecationWarning):639 cond = asyncio.Condition(loop=self.loop)640 with self.assertWarns(DeprecationWarning):641 @asyncio.coroutine642 def acquire_cond():643 with self.assertWarns(DeprecationWarning):644 return (yield from cond)645 with self.loop.run_until_complete(acquire_cond()):646 self.assertTrue(cond.locked())647 self.assertFalse(cond.locked())648 def test_context_manager_no_yield(self):649 with self.assertWarns(DeprecationWarning):650 cond = asyncio.Condition(loop=self.loop)651 try:652 with cond:653 self.fail('RuntimeError is not raised in with expression')654 except RuntimeError as err:655 self.assertEqual(656 str(err),657 '"yield from" should be used as context manager expression')658 self.assertFalse(cond.locked())659 def test_explicit_lock(self):660 with self.assertWarns(DeprecationWarning):661 lock = asyncio.Lock(loop=self.loop)662 cond = asyncio.Condition(lock, loop=self.loop)663 self.assertIs(cond._lock, lock)664 self.assertIs(cond._loop, lock._loop)665 def test_ambiguous_loops(self):666 loop = self.new_test_loop()667 self.addCleanup(loop.close)668 with self.assertWarns(DeprecationWarning):669 lock = asyncio.Lock(loop=self.loop)670 with self.assertRaises(ValueError):671 asyncio.Condition(lock, loop=loop)672 def test_timeout_in_block(self):673 loop = asyncio.new_event_loop()674 self.addCleanup(loop.close)675 async def task_timeout():676 condition = asyncio.Condition(loop=loop)677 async with condition:678 with self.assertRaises(asyncio.TimeoutError):679 await asyncio.wait_for(condition.wait(), timeout=0.5)680 with self.assertWarns(DeprecationWarning):681 loop.run_until_complete(task_timeout())682class SemaphoreTests(test_utils.TestCase):683 def setUp(self):684 super().setUp()685 self.loop = self.new_test_loop()686 def test_ctor_loop(self):687 loop = mock.Mock()688 with self.assertWarns(DeprecationWarning):689 sem = asyncio.Semaphore(loop=loop)690 self.assertIs(sem._loop, loop)691 with self.assertWarns(DeprecationWarning):692 sem = asyncio.Semaphore(loop=self.loop)693 self.assertIs(sem._loop, self.loop)694 def test_ctor_noloop(self):695 asyncio.set_event_loop(self.loop)696 sem = asyncio.Semaphore()697 self.assertIs(sem._loop, self.loop)698 def test_initial_value_zero(self):699 with self.assertWarns(DeprecationWarning):700 sem = asyncio.Semaphore(0, loop=self.loop)701 self.assertTrue(sem.locked())702 def test_repr(self):703 with self.assertWarns(DeprecationWarning):704 sem = asyncio.Semaphore(loop=self.loop)705 self.assertTrue(repr(sem).endswith('[unlocked, value:1]>'))706 self.assertTrue(RGX_REPR.match(repr(sem)))707 self.loop.run_until_complete(sem.acquire())708 self.assertTrue(repr(sem).endswith('[locked]>'))709 self.assertTrue('waiters' not in repr(sem))...
test_queues.py
Source:test_queues.py
...7 asyncio.set_event_loop_policy(None)8class _QueueTestBase(test_utils.TestCase):9 def setUp(self):10 super().setUp()11 self.loop = self.new_test_loop()12class QueueBasicTests(_QueueTestBase):13 def _test_repr_or_str(self, fn, expect_id):14 """Test Queue's repr or str.15 fn is repr or str. expect_id is True if we expect the Queue's id to16 appear in fn(Queue()).17 """18 def gen():19 when = yield20 self.assertAlmostEqual(0.1, when)21 when = yield 0.122 self.assertAlmostEqual(0.2, when)23 yield 0.124 loop = self.new_test_loop(gen)25 with self.assertWarns(DeprecationWarning):26 q = asyncio.Queue(loop=loop)27 self.assertTrue(fn(q).startswith('<Queue'), fn(q))28 id_is_present = hex(id(q)) in fn(q)29 self.assertEqual(expect_id, id_is_present)30 async def add_getter():31 q = asyncio.Queue(loop=loop)32 # Start a task that waits to get.33 loop.create_task(q.get())34 # Let it start waiting.35 await asyncio.sleep(0.1)36 self.assertTrue('_getters[1]' in fn(q))37 # resume q.get coroutine to finish generator38 q.put_nowait(0)39 with self.assertWarns(DeprecationWarning):40 loop.run_until_complete(add_getter())41 async def add_putter():42 q = asyncio.Queue(maxsize=1, loop=loop)43 q.put_nowait(1)44 # Start a task that waits to put.45 loop.create_task(q.put(2))46 # Let it start waiting.47 await asyncio.sleep(0.1)48 self.assertTrue('_putters[1]' in fn(q))49 # resume q.put coroutine to finish generator50 q.get_nowait()51 with self.assertWarns(DeprecationWarning):52 loop.run_until_complete(add_putter())53 q = asyncio.Queue(loop=loop)54 q.put_nowait(1)55 self.assertTrue('_queue=[1]' in fn(q))56 def test_ctor_loop(self):57 loop = mock.Mock()58 with self.assertWarns(DeprecationWarning):59 q = asyncio.Queue(loop=loop)60 self.assertIs(q._loop, loop)61 with self.assertWarns(DeprecationWarning):62 q = asyncio.Queue(loop=self.loop)63 self.assertIs(q._loop, self.loop)64 def test_ctor_noloop(self):65 asyncio.set_event_loop(self.loop)66 q = asyncio.Queue()67 self.assertIs(q._loop, self.loop)68 def test_repr(self):69 self._test_repr_or_str(repr, True)70 def test_str(self):71 self._test_repr_or_str(str, False)72 def test_empty(self):73 with self.assertWarns(DeprecationWarning):74 q = asyncio.Queue(loop=self.loop)75 self.assertTrue(q.empty())76 q.put_nowait(1)77 self.assertFalse(q.empty())78 self.assertEqual(1, q.get_nowait())79 self.assertTrue(q.empty())80 def test_full(self):81 with self.assertWarns(DeprecationWarning):82 q = asyncio.Queue(loop=self.loop)83 self.assertFalse(q.full())84 with self.assertWarns(DeprecationWarning):85 q = asyncio.Queue(maxsize=1, loop=self.loop)86 q.put_nowait(1)87 self.assertTrue(q.full())88 def test_order(self):89 with self.assertWarns(DeprecationWarning):90 q = asyncio.Queue(loop=self.loop)91 for i in [1, 3, 2]:92 q.put_nowait(i)93 items = [q.get_nowait() for _ in range(3)]94 self.assertEqual([1, 3, 2], items)95 def test_maxsize(self):96 def gen():97 when = yield98 self.assertAlmostEqual(0.01, when)99 when = yield 0.01100 self.assertAlmostEqual(0.02, when)101 yield 0.01102 loop = self.new_test_loop(gen)103 with self.assertWarns(DeprecationWarning):104 q = asyncio.Queue(maxsize=2, loop=loop)105 self.assertEqual(2, q.maxsize)106 have_been_put = []107 async def putter():108 for i in range(3):109 await q.put(i)110 have_been_put.append(i)111 return True112 async def test():113 t = loop.create_task(putter())114 await asyncio.sleep(0.01)115 # The putter is blocked after putting two items.116 self.assertEqual([0, 1], have_been_put)117 self.assertEqual(0, q.get_nowait())118 # Let the putter resume and put last item.119 await asyncio.sleep(0.01)120 self.assertEqual([0, 1, 2], have_been_put)121 self.assertEqual(1, q.get_nowait())122 self.assertEqual(2, q.get_nowait())123 self.assertTrue(t.done())124 self.assertTrue(t.result())125 loop.run_until_complete(test())126 self.assertAlmostEqual(0.02, loop.time())127class QueueGetTests(_QueueTestBase):128 def test_blocking_get(self):129 with self.assertWarns(DeprecationWarning):130 q = asyncio.Queue(loop=self.loop)131 q.put_nowait(1)132 async def queue_get():133 return await q.get()134 res = self.loop.run_until_complete(queue_get())135 self.assertEqual(1, res)136 def test_get_with_putters(self):137 with self.assertWarns(DeprecationWarning):138 q = asyncio.Queue(1, loop=self.loop)139 q.put_nowait(1)140 waiter = self.loop.create_future()141 q._putters.append(waiter)142 res = self.loop.run_until_complete(q.get())143 self.assertEqual(1, res)144 self.assertTrue(waiter.done())145 self.assertIsNone(waiter.result())146 def test_blocking_get_wait(self):147 def gen():148 when = yield149 self.assertAlmostEqual(0.01, when)150 yield 0.01151 loop = self.new_test_loop(gen)152 with self.assertWarns(DeprecationWarning):153 q = asyncio.Queue(loop=loop)154 started = asyncio.Event(loop=loop)155 finished = False156 async def queue_get():157 nonlocal finished158 started.set()159 res = await q.get()160 finished = True161 return res162 async def queue_put():163 loop.call_later(0.01, q.put_nowait, 1)164 queue_get_task = loop.create_task(queue_get())165 await started.wait()166 self.assertFalse(finished)167 res = await queue_get_task168 self.assertTrue(finished)169 return res170 res = loop.run_until_complete(queue_put())171 self.assertEqual(1, res)172 self.assertAlmostEqual(0.01, loop.time())173 def test_nonblocking_get(self):174 with self.assertWarns(DeprecationWarning):175 q = asyncio.Queue(loop=self.loop)176 q.put_nowait(1)177 self.assertEqual(1, q.get_nowait())178 def test_nonblocking_get_exception(self):179 with self.assertWarns(DeprecationWarning):180 q = asyncio.Queue(loop=self.loop)181 self.assertRaises(asyncio.QueueEmpty, q.get_nowait)182 def test_get_cancelled(self):183 def gen():184 when = yield185 self.assertAlmostEqual(0.01, when)186 when = yield 0.01187 self.assertAlmostEqual(0.061, when)188 yield 0.05189 loop = self.new_test_loop(gen)190 with self.assertWarns(DeprecationWarning):191 q = asyncio.Queue(loop=loop)192 async def queue_get():193 return await asyncio.wait_for(q.get(), 0.051)194 async def test():195 get_task = loop.create_task(queue_get())196 await asyncio.sleep(0.01) # let the task start197 q.put_nowait(1)198 return await get_task199 self.assertEqual(1, loop.run_until_complete(test()))200 self.assertAlmostEqual(0.06, loop.time())201 def test_get_cancelled_race(self):202 with self.assertWarns(DeprecationWarning):203 q = asyncio.Queue(loop=self.loop)204 t1 = self.loop.create_task(q.get())205 t2 = self.loop.create_task(q.get())206 test_utils.run_briefly(self.loop)207 t1.cancel()208 test_utils.run_briefly(self.loop)209 self.assertTrue(t1.done())210 q.put_nowait('a')211 test_utils.run_briefly(self.loop)212 self.assertEqual(t2.result(), 'a')213 def test_get_with_waiting_putters(self):214 with self.assertWarns(DeprecationWarning):215 q = asyncio.Queue(loop=self.loop, maxsize=1)216 self.loop.create_task(q.put('a'))217 self.loop.create_task(q.put('b'))218 test_utils.run_briefly(self.loop)219 self.assertEqual(self.loop.run_until_complete(q.get()), 'a')220 self.assertEqual(self.loop.run_until_complete(q.get()), 'b')221 def test_why_are_getters_waiting(self):222 # From issue #268.223 async def consumer(queue, num_expected):224 for _ in range(num_expected):225 await queue.get()226 async def producer(queue, num_items):227 for i in range(num_items):228 await queue.put(i)229 queue_size = 1230 producer_num_items = 5231 with self.assertWarns(DeprecationWarning):232 q = asyncio.Queue(queue_size, loop=self.loop)233 self.loop.run_until_complete(234 asyncio.gather(producer(q, producer_num_items),235 consumer(q, producer_num_items),236 loop=self.loop),237 )238 def test_cancelled_getters_not_being_held_in_self_getters(self):239 def a_generator():240 yield 0.1241 yield 0.2242 self.loop = self.new_test_loop(a_generator)243 async def consumer(queue):244 try:245 item = await asyncio.wait_for(queue.get(), 0.1)246 except asyncio.TimeoutError:247 pass248 with self.assertWarns(DeprecationWarning):249 queue = asyncio.Queue(loop=self.loop, maxsize=5)250 self.loop.run_until_complete(self.loop.create_task(consumer(queue)))251 self.assertEqual(len(queue._getters), 0)252class QueuePutTests(_QueueTestBase):253 def test_blocking_put(self):254 with self.assertWarns(DeprecationWarning):255 q = asyncio.Queue(loop=self.loop)256 async def queue_put():257 # No maxsize, won't block.258 await q.put(1)259 self.loop.run_until_complete(queue_put())260 def test_blocking_put_wait(self):261 def gen():262 when = yield263 self.assertAlmostEqual(0.01, when)264 yield 0.01265 loop = self.new_test_loop(gen)266 with self.assertWarns(DeprecationWarning):267 q = asyncio.Queue(maxsize=1, loop=loop)268 started = asyncio.Event(loop=loop)269 finished = False270 async def queue_put():271 nonlocal finished272 started.set()273 await q.put(1)274 await q.put(2)275 finished = True276 async def queue_get():277 loop.call_later(0.01, q.get_nowait)278 queue_put_task = loop.create_task(queue_put())279 await started.wait()280 self.assertFalse(finished)281 await queue_put_task282 self.assertTrue(finished)283 loop.run_until_complete(queue_get())284 self.assertAlmostEqual(0.01, loop.time())285 def test_nonblocking_put(self):286 with self.assertWarns(DeprecationWarning):287 q = asyncio.Queue(loop=self.loop)288 q.put_nowait(1)289 self.assertEqual(1, q.get_nowait())290 def test_get_cancel_drop_one_pending_reader(self):291 def gen():292 yield 0.01293 yield 0.1294 loop = self.new_test_loop(gen)295 with self.assertWarns(DeprecationWarning):296 q = asyncio.Queue(loop=loop)297 reader = loop.create_task(q.get())298 loop.run_until_complete(asyncio.sleep(0.01))299 q.put_nowait(1)300 q.put_nowait(2)301 reader.cancel()302 try:303 loop.run_until_complete(reader)304 except asyncio.CancelledError:305 # try again306 reader = loop.create_task(q.get())307 loop.run_until_complete(reader)308 result = reader.result()309 # if we get 2, it means 1 got dropped!310 self.assertEqual(1, result)311 def test_get_cancel_drop_many_pending_readers(self):312 def gen():313 yield 0.01314 yield 0.1315 loop = self.new_test_loop(gen)316 loop.set_debug(True)317 with self.assertWarns(DeprecationWarning):318 q = asyncio.Queue(loop=loop)319 reader1 = loop.create_task(q.get())320 reader2 = loop.create_task(q.get())321 reader3 = loop.create_task(q.get())322 loop.run_until_complete(asyncio.sleep(0.01))323 q.put_nowait(1)324 q.put_nowait(2)325 reader1.cancel()326 try:327 loop.run_until_complete(reader1)328 except asyncio.CancelledError:329 pass330 loop.run_until_complete(reader3)331 # It is undefined in which order concurrent readers receive results.332 self.assertEqual({reader2.result(), reader3.result()}, {1, 2})333 def test_put_cancel_drop(self):334 def gen():335 yield 0.01336 yield 0.1337 loop = self.new_test_loop(gen)338 with self.assertWarns(DeprecationWarning):339 q = asyncio.Queue(1, loop=loop)340 q.put_nowait(1)341 # putting a second item in the queue has to block (qsize=1)342 writer = loop.create_task(q.put(2))343 loop.run_until_complete(asyncio.sleep(0.01))344 value1 = q.get_nowait()345 self.assertEqual(value1, 1)346 writer.cancel()347 try:348 loop.run_until_complete(writer)349 except asyncio.CancelledError:350 # try again351 writer = loop.create_task(q.put(2))352 loop.run_until_complete(writer)353 value2 = q.get_nowait()354 self.assertEqual(value2, 2)355 self.assertEqual(q.qsize(), 0)356 def test_nonblocking_put_exception(self):357 with self.assertWarns(DeprecationWarning):358 q = asyncio.Queue(maxsize=1, loop=self.loop)359 q.put_nowait(1)360 self.assertRaises(asyncio.QueueFull, q.put_nowait, 2)361 def test_float_maxsize(self):362 with self.assertWarns(DeprecationWarning):363 q = asyncio.Queue(maxsize=1.3, loop=self.loop)364 q.put_nowait(1)365 q.put_nowait(2)366 self.assertTrue(q.full())367 self.assertRaises(asyncio.QueueFull, q.put_nowait, 3)368 with self.assertWarns(DeprecationWarning):369 q = asyncio.Queue(maxsize=1.3, loop=self.loop)370 async def queue_put():371 await q.put(1)372 await q.put(2)373 self.assertTrue(q.full())374 self.loop.run_until_complete(queue_put())375 def test_put_cancelled(self):376 with self.assertWarns(DeprecationWarning):377 q = asyncio.Queue(loop=self.loop)378 async def queue_put():379 await q.put(1)380 return True381 async def test():382 return await q.get()383 t = self.loop.create_task(queue_put())384 self.assertEqual(1, self.loop.run_until_complete(test()))385 self.assertTrue(t.done())386 self.assertTrue(t.result())387 def test_put_cancelled_race(self):388 with self.assertWarns(DeprecationWarning):389 q = asyncio.Queue(loop=self.loop, maxsize=1)390 put_a = self.loop.create_task(q.put('a'))391 put_b = self.loop.create_task(q.put('b'))392 put_c = self.loop.create_task(q.put('X'))393 test_utils.run_briefly(self.loop)394 self.assertTrue(put_a.done())395 self.assertFalse(put_b.done())396 put_c.cancel()397 test_utils.run_briefly(self.loop)398 self.assertTrue(put_c.done())399 self.assertEqual(q.get_nowait(), 'a')400 test_utils.run_briefly(self.loop)401 self.assertEqual(q.get_nowait(), 'b')402 self.loop.run_until_complete(put_b)403 def test_put_with_waiting_getters(self):404 with self.assertWarns(DeprecationWarning):405 q = asyncio.Queue(loop=self.loop)406 t = self.loop.create_task(q.get())407 test_utils.run_briefly(self.loop)408 self.loop.run_until_complete(q.put('a'))409 self.assertEqual(self.loop.run_until_complete(t), 'a')410 def test_why_are_putters_waiting(self):411 # From issue #265.412 with self.assertWarns(DeprecationWarning):413 queue = asyncio.Queue(2, loop=self.loop)414 async def putter(item):415 await queue.put(item)416 async def getter():417 await asyncio.sleep(0)418 num = queue.qsize()419 for _ in range(num):420 item = queue.get_nowait()421 t0 = putter(0)422 t1 = putter(1)423 t2 = putter(2)424 t3 = putter(3)425 self.loop.run_until_complete(426 asyncio.gather(getter(), t0, t1, t2, t3, loop=self.loop))427 def test_cancelled_puts_not_being_held_in_self_putters(self):428 def a_generator():429 yield 0.01430 yield 0.1431 loop = self.new_test_loop(a_generator)432 # Full queue.433 with self.assertWarns(DeprecationWarning):434 queue = asyncio.Queue(loop=loop, maxsize=1)435 queue.put_nowait(1)436 # Task waiting for space to put an item in the queue.437 put_task = loop.create_task(queue.put(1))438 loop.run_until_complete(asyncio.sleep(0.01))439 # Check that the putter is correctly removed from queue._putters when440 # the task is canceled.441 self.assertEqual(len(queue._putters), 1)442 put_task.cancel()443 with self.assertRaises(asyncio.CancelledError):444 loop.run_until_complete(put_task)445 self.assertEqual(len(queue._putters), 0)446 def test_cancelled_put_silence_value_error_exception(self):447 def gen():448 yield 0.01449 yield 0.1450 loop = self.new_test_loop(gen)451 # Full Queue.452 with self.assertWarns(DeprecationWarning):453 queue = asyncio.Queue(1, loop=loop)454 queue.put_nowait(1)455 # Task waiting for space to put a item in the queue.456 put_task = loop.create_task(queue.put(1))457 loop.run_until_complete(asyncio.sleep(0.01))458 # get_nowait() remove the future of put_task from queue._putters.459 queue.get_nowait()460 # When canceled, queue.put is going to remove its future from461 # self._putters but it was removed previously by queue.get_nowait().462 put_task.cancel()463 # The ValueError exception triggered by queue._putters.remove(putter)464 # inside queue.put should be silenced....
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!