<s> from __future__ import absolute_import , division , print_function , with_statement <EOL> import contextlib <EOL> import datetime <EOL> import functools <EOL> import sys <EOL> import textwrap <EOL> import time <EOL> import weakref <EOL> from tornado . concurrent import return_future , Future <EOL> from tornado . escape import url_escape <EOL> from tornado . httpclient import AsyncHTTPClient <EOL> from tornado . ioloop import IOLoop <EOL> from tornado . log import app_log <EOL> from tornado import stack_context <EOL> from tornado . testing import AsyncHTTPTestCase , AsyncTestCase , ExpectLog , gen_test <EOL> from tornado . test . util import unittest , skipOnTravis , skipBefore33 , skipBefore35 , skipNotCPython , exec_test <EOL> from tornado . web import Application , RequestHandler , asynchronous , HTTPError <EOL> from tornado import gen <EOL> try : <EOL> from concurrent import futures <EOL> except ImportError : <EOL> futures = None <EOL> class GenEngineTest ( AsyncTestCase ) : <EOL> def setUp ( self ) : <EOL> super ( GenEngineTest , self ) . setUp ( ) <EOL> self . named_contexts = [ ] <EOL> def named_context ( self , name ) : <EOL> @ contextlib . contextmanager <EOL> def context ( ) : <EOL> self . named_contexts . append ( name ) <EOL> try : <EOL> yield <EOL> finally : <EOL> self . assertEqual ( self . named_contexts . pop ( ) , name ) <EOL> return context <EOL> def run_gen ( self , f ) : <EOL> f ( ) <EOL> return self . wait ( ) <EOL> def delay_callback ( self , iterations , callback , arg ) : <EOL> """<STR_LIT>""" <EOL> if iterations == <NUM_LIT:0> : <EOL> callback ( arg ) <EOL> else : <EOL> self . io_loop . add_callback ( functools . partial ( <EOL> self . delay_callback , iterations - <NUM_LIT:1> , callback , arg ) ) <EOL> @ return_future <EOL> def async_future ( self , result , callback ) : <EOL> self . io_loop . add_callback ( callback , result ) <EOL> @ gen . coroutine <EOL> def async_exception ( self , e ) : <EOL> yield gen . moment <EOL> raise e <EOL> def test_no_yield ( self ) : <EOL> @ gen . engine <EOL> def f ( ) : <EOL> self . stop ( ) <EOL> self . run_gen ( f ) <EOL> def test_inline_cb ( self ) : <EOL> @ gen . engine <EOL> def f ( ) : <EOL> ( yield gen . Callback ( "<STR_LIT>" ) ) ( ) <EOL> res = yield gen . Wait ( "<STR_LIT>" ) <EOL> self . assertTrue ( res is None ) <EOL> self . stop ( ) <EOL> self . run_gen ( f ) <EOL> def test_ioloop_cb ( self ) : <EOL> @ gen . engine <EOL> def f ( ) : <EOL> self . io_loop . add_callback ( ( yield gen . Callback ( "<STR_LIT>" ) ) ) <EOL> yield gen . Wait ( "<STR_LIT>" ) <EOL> self . stop ( ) <EOL> self . run_gen ( f ) <EOL> def test_exception_phase1 ( self ) : <EOL> @ gen . engine <EOL> def f ( ) : <EOL> <NUM_LIT:1> / <NUM_LIT:0> <EOL> self . assertRaises ( ZeroDivisionError , self . run_gen , f ) <EOL> def test_exception_phase2 ( self ) : <EOL> @ gen . engine <EOL> def f ( ) : <EOL> self . io_loop . add_callback ( ( yield gen . Callback ( "<STR_LIT>" ) ) ) <EOL> yield gen . Wait ( "<STR_LIT>" ) <EOL> <NUM_LIT:1> / <NUM_LIT:0> <EOL> self . assertRaises ( ZeroDivisionError , self . run_gen , f ) <EOL> def test_exception_in_task_phase1 ( self ) : <EOL> def fail_task ( callback ) : <EOL> <NUM_LIT:1> / <NUM_LIT:0> <EOL> @ gen . engine <EOL> def f ( ) : <EOL> try : <EOL> yield gen . Task ( fail_task ) <EOL> raise Exception ( "<STR_LIT>" ) <EOL> except ZeroDivisionError : <EOL> self . stop ( ) <EOL> self . run_gen ( f ) <EOL> def test_exception_in_task_phase2 ( self ) : <EOL> def fail_task ( callback ) : <EOL> self . io_loop . add_callback ( lambda : <NUM_LIT:1> / <NUM_LIT:0> ) <EOL> @ gen . engine <EOL> def f ( ) : <EOL> try : <EOL> yield gen . Task ( fail_task ) <EOL> raise Exception ( "<STR_LIT>" ) <EOL> except ZeroDivisionError : <EOL> self . stop ( ) <EOL> self . run_gen ( f ) <EOL> def test_with_arg ( self ) : <EOL> @ gen . engine <EOL> def f ( ) : <EOL> ( yield gen . Callback ( "<STR_LIT>" ) ) ( <NUM_LIT> ) <EOL> res = yield gen . Wait ( "<STR_LIT>" ) <EOL> self . assertEqual ( <NUM_LIT> , res ) <EOL> self . stop ( ) <EOL> self . run_gen ( f ) <EOL> def test_with_arg_tuple ( self ) : <EOL> @ gen . engine <EOL> def f ( ) : <EOL> ( yield gen . Callback ( ( <NUM_LIT:1> , <NUM_LIT:2> ) ) ) ( ( <NUM_LIT:3> , <NUM_LIT:4> ) ) <EOL> res = yield gen . Wait ( ( <NUM_LIT:1> , <NUM_LIT:2> ) ) <EOL> self . assertEqual ( ( <NUM_LIT:3> , <NUM_LIT:4> ) , res ) <EOL> self . stop ( ) <EOL> self . run_gen ( f ) <EOL> def test_key_reuse ( self ) : <EOL> @ gen . engine <EOL> def f ( ) : <EOL> yield gen . Callback ( "<STR_LIT>" ) <EOL> yield gen . Callback ( "<STR_LIT>" ) <EOL> self . stop ( ) <EOL> self . assertRaises ( gen . KeyReuseError , self . run_gen , f ) <EOL> def test_key_reuse_tuple ( self ) : <EOL> @ gen . engine <EOL> def f ( ) : <EOL> yield gen . Callback ( ( <NUM_LIT:1> , <NUM_LIT:2> ) ) <EOL> yield gen . Callback ( ( <NUM_LIT:1> , <NUM_LIT:2> ) ) <EOL> self . stop ( ) <EOL> self . assertRaises ( gen . KeyReuseError , self . run_gen , f ) <EOL> def test_key_mismatch ( self ) : <EOL> @ gen . engine <EOL> def f ( ) : <EOL> yield gen . Callback ( "<STR_LIT>" ) <EOL> yield gen . Wait ( "<STR_LIT>" ) <EOL> self . stop ( ) <EOL> self . assertRaises ( gen . UnknownKeyError , self . run_gen , f ) <EOL> def test_key_mismatch_tuple ( self ) : <EOL> @ gen . engine <EOL> def f ( ) : <EOL> yield gen . Callback ( ( <NUM_LIT:1> , <NUM_LIT:2> ) ) <EOL> yield gen . Wait ( ( <NUM_LIT:2> , <NUM_LIT:3> ) ) <EOL> self . stop ( ) <EOL> self . assertRaises ( gen . UnknownKeyError , self . run_gen , f ) <EOL> def test_leaked_callback ( self ) : <EOL> @ gen . engine <EOL> def f ( ) : <EOL> yield gen . Callback ( "<STR_LIT>" ) <EOL> self . stop ( ) <EOL> self . assertRaises ( gen . LeakedCallbackError , self . run_gen , f ) <EOL> def test_leaked_callback_tuple ( self ) : <EOL> @ gen . engine <EOL> def f ( ) : <EOL> yield gen . Callback ( ( <NUM_LIT:1> , <NUM_LIT:2> ) ) <EOL> self . stop ( ) <EOL> self . assertRaises ( gen . LeakedCallbackError , self . run_gen , f ) <EOL> def test_parallel_callback ( self ) : <EOL> @ gen . engine <EOL> def f ( ) : <EOL> for k in range ( <NUM_LIT:3> ) : <EOL> self . io_loop . add_callback ( ( yield gen . Callback ( k ) ) ) <EOL> yield gen . Wait ( <NUM_LIT:1> ) <EOL> self . io_loop . add_callback ( ( yield gen . Callback ( <NUM_LIT:3> ) ) ) <EOL> yield gen . Wait ( <NUM_LIT:0> ) <EOL> yield gen . Wait ( <NUM_LIT:3> ) <EOL> yield gen . Wait ( <NUM_LIT:2> ) <EOL> self . stop ( ) <EOL> self . run_gen ( f ) <EOL> def test_bogus_yield ( self ) : <EOL> @ gen . engine <EOL> def f ( ) : <EOL> yield <NUM_LIT> <EOL> self . assertRaises ( gen . BadYieldError , self . run_gen , f ) <EOL> def test_bogus_yield_tuple ( self ) : <EOL> @ gen . engine <EOL> def f ( ) : <EOL> yield ( <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> self . assertRaises ( gen . BadYieldError , self . run_gen , f ) <EOL> def test_reuse ( self ) : <EOL> @ gen . engine <EOL> def f ( ) : <EOL> self . io_loop . add_callback ( ( yield gen . Callback ( <NUM_LIT:0> ) ) ) <EOL> yield gen . Wait ( <NUM_LIT:0> ) <EOL> self . stop ( ) <EOL> self . run_gen ( f ) <EOL> self . run_gen ( f ) <EOL> def test_task ( self ) : <EOL> @ gen . engine <EOL> def f ( ) : <EOL> yield gen . Task ( self . io_loop . add_callback ) <EOL> self . stop ( ) <EOL> self . run_gen ( f ) <EOL> def test_wait_all ( self ) : <EOL> @ gen . engine <EOL> def f ( ) : <EOL> ( yield gen . Callback ( "<STR_LIT>" ) ) ( "<STR_LIT>" ) <EOL> ( yield gen . Callback ( "<STR_LIT>" ) ) ( "<STR_LIT>" ) <EOL> results = yield gen . WaitAll ( [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> self . assertEqual ( results , [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> self . stop ( ) <EOL> self . run_gen ( f ) <EOL> def test_exception_in_yield ( self ) : <EOL> @ gen . engine <EOL> def f ( ) : <EOL> try : <EOL> yield gen . Wait ( "<STR_LIT>" ) <EOL> raise Exception ( "<STR_LIT>" ) <EOL> except gen . UnknownKeyError : <EOL> pass <EOL> self . stop ( ) <EOL> self . run_gen ( f ) <EOL> def test_resume_after_exception_in_yield ( self ) : <EOL> @ gen . engine <EOL> def f ( ) : <EOL> try : <EOL> yield gen . Wait ( "<STR_LIT>" ) <EOL> raise Exception ( "<STR_LIT>" ) <EOL> except gen . UnknownKeyError : <EOL> pass <EOL> ( yield gen . Callback ( "<STR_LIT>" ) ) ( "<STR_LIT>" ) <EOL> self . assertEqual ( ( yield gen . Wait ( "<STR_LIT>" ) ) , "<STR_LIT>" ) <EOL> self . stop ( ) <EOL> self . run_gen ( f ) <EOL> def test_orphaned_callback ( self ) : <EOL> @ gen . engine <EOL> def f ( ) : <EOL> self . orphaned_callback = yield gen . Callback ( <NUM_LIT:1> ) <EOL> try : <EOL> self . run_gen ( f ) <EOL> raise Exception ( "<STR_LIT>" ) <EOL> except gen . LeakedCallbackError : <EOL> pass <EOL> self . orphaned_callback ( ) <EOL> def test_multi ( self ) : <EOL> @ gen . engine <EOL> def f ( ) : <EOL> ( yield gen . Callback ( "<STR_LIT>" ) ) ( "<STR_LIT>" ) <EOL> ( yield gen . Callback ( "<STR_LIT>" ) ) ( "<STR_LIT>" ) <EOL> results = yield [ gen . Wait ( "<STR_LIT>" ) , gen . Wait ( "<STR_LIT>" ) ] <EOL> self . assertEqual ( results , [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> self . stop ( ) <EOL> self . run_gen ( f ) <EOL> def test_multi_dict ( self ) : <EOL> @ gen . engine <EOL> def f ( ) : <EOL> ( yield gen . Callback ( "<STR_LIT>" ) ) ( "<STR_LIT>" ) <EOL> ( yield gen . Callback ( "<STR_LIT>" ) ) ( "<STR_LIT>" ) <EOL> results = yield dict ( foo = gen . Wait ( "<STR_LIT>" ) , bar = gen . Wait ( "<STR_LIT>" ) ) <EOL> self . assertEqual ( results , dict ( foo = "<STR_LIT>" , bar = "<STR_LIT>" ) ) <EOL> self . stop ( ) <EOL> self . run_gen ( f ) <EOL> def test_multi_yieldpoint_delayed ( self ) : <EOL> @ gen . engine <EOL> def f ( ) : <EOL> responses = yield gen . Multi ( [ <EOL> gen . Task ( self . delay_callback , <NUM_LIT:3> , arg = "<STR_LIT>" ) , <EOL> gen . Task ( self . delay_callback , <NUM_LIT:1> , arg = "<STR_LIT>" ) , <EOL> ] ) <EOL> self . assertEqual ( responses , [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> self . stop ( ) <EOL> self . run_gen ( f ) <EOL> def test_multi_yieldpoint_dict_delayed ( self ) : <EOL> @ gen . engine <EOL> def f ( ) : <EOL> responses = yield gen . Multi ( dict ( <EOL> foo = gen . Task ( self . delay_callback , <NUM_LIT:3> , arg = "<STR_LIT>" ) , <EOL> bar = gen . Task ( self . delay_callback , <NUM_LIT:1> , arg = "<STR_LIT>" ) , <EOL> ) ) <EOL> self . assertEqual ( responses , dict ( foo = "<STR_LIT>" , bar = "<STR_LIT>" ) ) <EOL> self . stop ( ) <EOL> self . run_gen ( f ) <EOL> def test_multi_future_delayed ( self ) : <EOL> @ gen . engine <EOL> def f ( ) : <EOL> responses = yield gen . multi_future ( [ <EOL> gen . Task ( self . delay_callback , <NUM_LIT:3> , arg = "<STR_LIT>" ) , <EOL> gen . Task ( self . delay_callback , <NUM_LIT:1> , arg = "<STR_LIT>" ) , <EOL> ] ) <EOL> self . assertEqual ( responses , [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> self . stop ( ) <EOL> self . run_gen ( f ) <EOL> def test_multi_future_dict_delayed ( self ) : <EOL> @ gen . engine <EOL> def f ( ) : <EOL> responses = yield gen . multi_future ( dict ( <EOL> foo = gen . Task ( self . delay_callback , <NUM_LIT:3> , arg = "<STR_LIT>" ) , <EOL> bar = gen . Task ( self . delay_callback , <NUM_LIT:1> , arg = "<STR_LIT>" ) , <EOL> ) ) <EOL> self . assertEqual ( responses , dict ( foo = "<STR_LIT>" , bar = "<STR_LIT>" ) ) <EOL> self . stop ( ) <EOL> self . run_gen ( f ) <EOL> @ skipOnTravis <EOL> @ gen_test <EOL> def test_multi_performance ( self ) : <EOL> start = time . time ( ) <EOL> yield [ gen . Task ( self . io_loop . add_callback ) for i in range ( <NUM_LIT> ) ] <EOL> end = time . time ( ) <EOL> self . assertLess ( end - start , <NUM_LIT:1.0> ) <EOL> @ gen_test <EOL> def test_multi_empty ( self ) : <EOL> x = yield [ ] <EOL> self . assertTrue ( isinstance ( x , list ) ) <EOL> y = yield { } <EOL> self . assertTrue ( isinstance ( y , dict ) ) <EOL> @ gen_test <EOL> def test_multi_mixed_types ( self ) : <EOL> ( yield gen . Callback ( "<STR_LIT>" ) ) ( "<STR_LIT>" ) <EOL> responses = yield [ gen . Wait ( "<STR_LIT>" ) , <EOL> gen . Task ( self . delay_callback , <NUM_LIT:3> , arg = "<STR_LIT>" ) ] <EOL> self . assertEqual ( responses , [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> @ gen_test <EOL> def test_future ( self ) : <EOL> result = yield self . async_future ( <NUM_LIT:1> ) <EOL> self . assertEqual ( result , <NUM_LIT:1> ) <EOL> @ gen_test <EOL> def test_multi_future ( self ) : <EOL> results = yield [ self . async_future ( <NUM_LIT:1> ) , self . async_future ( <NUM_LIT:2> ) ] <EOL> self . assertEqual ( results , [ <NUM_LIT:1> , <NUM_LIT:2> ] ) <EOL> @ gen_test <EOL> def test_multi_future_duplicate ( self ) : <EOL> f = self . async_future ( <NUM_LIT:2> ) <EOL> results = yield [ self . async_future ( <NUM_LIT:1> ) , f , self . async_future ( <NUM_LIT:3> ) , f ] <EOL> self . assertEqual ( results , [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:2> ] ) <EOL> @ gen_test <EOL> def test_multi_dict_future ( self ) : <EOL> results = yield dict ( foo = self . async_future ( <NUM_LIT:1> ) , bar = self . async_future ( <NUM_LIT:2> ) ) <EOL> self . assertEqual ( results , dict ( foo = <NUM_LIT:1> , bar = <NUM_LIT:2> ) ) <EOL> @ gen_test <EOL> def test_multi_exceptions ( self ) : <EOL> with ExpectLog ( app_log , "<STR_LIT>" ) : <EOL> with self . assertRaises ( RuntimeError ) as cm : <EOL> yield gen . Multi ( [ self . async_exception ( RuntimeError ( "<STR_LIT>" ) ) , <EOL> self . async_exception ( RuntimeError ( "<STR_LIT>" ) ) ] ) <EOL> self . assertEqual ( str ( cm . exception ) , "<STR_LIT>" ) <EOL> with self . assertRaises ( RuntimeError ) : <EOL> yield gen . Multi ( [ self . async_exception ( RuntimeError ( "<STR_LIT>" ) ) , <EOL> self . async_future ( <NUM_LIT:2> ) ] ) <EOL> with self . assertRaises ( RuntimeError ) : <EOL> yield gen . Multi ( [ self . async_exception ( RuntimeError ( "<STR_LIT>" ) ) , <EOL> self . async_exception ( RuntimeError ( "<STR_LIT>" ) ) ] , <EOL> quiet_exceptions = RuntimeError ) <EOL> @ gen_test <EOL> def test_multi_future_exceptions ( self ) : <EOL> with ExpectLog ( app_log , "<STR_LIT>" ) : <EOL> with self . assertRaises ( RuntimeError ) as cm : <EOL> yield [ self . async_exception ( RuntimeError ( "<STR_LIT>" ) ) , <EOL> self . async_exception ( RuntimeError ( "<STR_LIT>" ) ) ] <EOL> self . assertEqual ( str ( cm . exception ) , "<STR_LIT>" ) <EOL> with self . assertRaises ( RuntimeError ) : <EOL> yield [ self . async_exception ( RuntimeError ( "<STR_LIT>" ) ) , <EOL> self . async_future ( <NUM_LIT:2> ) ] <EOL> with self . assertRaises ( RuntimeError ) : <EOL> yield gen . multi_future ( <EOL> [ self . async_exception ( RuntimeError ( "<STR_LIT>" ) ) , <EOL> self . async_exception ( RuntimeError ( "<STR_LIT>" ) ) ] , <EOL> quiet_exceptions = RuntimeError ) <EOL> def test_arguments ( self ) : <EOL> @ gen . engine <EOL> def f ( ) : <EOL> ( yield gen . Callback ( "<STR_LIT>" ) ) ( ) <EOL> self . assertEqual ( ( yield gen . Wait ( "<STR_LIT>" ) ) , None ) <EOL> ( yield gen . Callback ( "<STR_LIT>" ) ) ( <NUM_LIT> ) <EOL> self . assertEqual ( ( yield gen . Wait ( "<STR_LIT>" ) ) , <NUM_LIT> ) <EOL> ( yield gen . Callback ( "<STR_LIT>" ) ) ( value = <NUM_LIT> ) <EOL> result = yield gen . Wait ( "<STR_LIT>" ) <EOL> self . assertTrue ( isinstance ( result , gen . Arguments ) ) <EOL> self . assertEqual ( ( ( ) , dict ( value = <NUM_LIT> ) ) , result ) <EOL> self . assertEqual ( dict ( value = <NUM_LIT> ) , result . kwargs ) <EOL> ( yield gen . Callback ( "<STR_LIT>" ) ) ( <NUM_LIT> , <NUM_LIT> ) <EOL> result = yield gen . Wait ( "<STR_LIT>" ) <EOL> self . assertTrue ( isinstance ( result , gen . Arguments ) ) <EOL> self . assertEqual ( ( ( <NUM_LIT> , <NUM_LIT> ) , { } ) , result ) <EOL> self . assertEqual ( ( <NUM_LIT> , <NUM_LIT> ) , result . args ) <EOL> def task_func ( callback ) : <EOL> callback ( None , error = "<STR_LIT:foo>" ) <EOL> result = yield gen . Task ( task_func ) <EOL> self . assertTrue ( isinstance ( result , gen . Arguments ) ) <EOL> self . assertEqual ( ( ( None , ) , dict ( error = "<STR_LIT:foo>" ) ) , result ) <EOL> self . stop ( ) <EOL> self . run_gen ( f ) <EOL> def test_stack_context_leak ( self ) : <EOL> def _stack_depth ( ) : <EOL> head = stack_context . _state . contexts [ <NUM_LIT:1> ] <EOL> length = <NUM_LIT:0> <EOL> while head is not None : <EOL> length += <NUM_LIT:1> <EOL> head = head . old_contexts [ <NUM_LIT:1> ] <EOL> return length <EOL> @ gen . engine <EOL> def inner ( callback ) : <EOL> yield gen . Task ( self . io_loop . add_callback ) <EOL> callback ( ) <EOL> @ gen . engine <EOL> def outer ( ) : <EOL> for i in range ( <NUM_LIT:10> ) : <EOL> yield gen . Task ( inner ) <EOL> stack_increase = _stack_depth ( ) - initial_stack_depth <EOL> self . assertTrue ( stack_increase <= <NUM_LIT:2> ) <EOL> self . stop ( ) <EOL> initial_stack_depth = _stack_depth ( ) <EOL> self . run_gen ( outer ) <EOL> def test_stack_context_leak_exception ( self ) : <EOL> @ gen . engine <EOL> def inner ( callback ) : <EOL> yield gen . Task ( self . io_loop . add_callback ) <EOL> <NUM_LIT:1> / <NUM_LIT:0> <EOL> @ gen . engine <EOL> def outer ( ) : <EOL> for i in range ( <NUM_LIT:10> ) : <EOL> try : <EOL> yield gen . Task ( inner ) <EOL> except ZeroDivisionError : <EOL> pass <EOL> stack_increase = len ( stack_context . _state . contexts ) - initial_stack_depth <EOL> self . assertTrue ( stack_increase <= <NUM_LIT:2> ) <EOL> self . stop ( ) <EOL> initial_stack_depth = len ( stack_context . _state . contexts ) <EOL> self . run_gen ( outer ) <EOL> def function_with_stack_context ( self , callback ) : <EOL> def step2 ( ) : <EOL> self . assertEqual ( self . named_contexts , [ '<STR_LIT:a>' ] ) <EOL> self . io_loop . add_callback ( callback ) <EOL> with stack_context . StackContext ( self . named_context ( '<STR_LIT:a>' ) ) : <EOL> self . io_loop . add_callback ( step2 ) <EOL> @ gen_test <EOL> def test_wait_transfer_stack_context ( self ) : <EOL> cb = yield gen . Callback ( '<STR_LIT>' ) <EOL> self . function_with_stack_context ( cb ) <EOL> self . assertEqual ( self . named_contexts , [ ] ) <EOL> yield gen . Wait ( '<STR_LIT>' ) <EOL> self . assertEqual ( self . named_contexts , [ ] ) <EOL> @ gen_test <EOL> def test_task_transfer_stack_context ( self ) : <EOL> yield gen . Task ( self . function_with_stack_context ) <EOL> self . assertEqual ( self . named_contexts , [ ] ) <EOL> def test_raise_after_stop ( self ) : <EOL> @ gen . engine <EOL> def f ( ) : <EOL> self . stop ( ) <EOL> <NUM_LIT:1> / <NUM_LIT:0> <EOL> with self . assertRaises ( ZeroDivisionError ) : <EOL> self . run_gen ( f ) <EOL> def test_sync_raise_return ( self ) : <EOL> @ gen . engine <EOL> def f ( ) : <EOL> self . stop ( <NUM_LIT> ) <EOL> raise gen . Return ( ) <EOL> result = self . run_gen ( f ) <EOL> self . assertEqual ( result , <NUM_LIT> ) <EOL> def test_async_raise_return ( self ) : <EOL> @ gen . engine <EOL> def f ( ) : <EOL> yield gen . Task ( self . io_loop . add_callback ) <EOL> self . stop ( <NUM_LIT> ) <EOL> raise gen . Return ( ) <EOL> result = self . run_gen ( f ) <EOL> self . assertEqual ( result , <NUM_LIT> ) <EOL> def test_sync_raise_return_value ( self ) : <EOL> @ gen . engine <EOL> def f ( ) : <EOL> raise gen . Return ( <NUM_LIT> ) <EOL> with self . assertRaises ( gen . ReturnValueIgnoredError ) : <EOL> self . run_gen ( f ) <EOL> def test_sync_raise_return_value_tuple ( self ) : <EOL> @ gen . engine <EOL> def f ( ) : <EOL> raise gen . Return ( ( <NUM_LIT:1> , <NUM_LIT:2> ) ) <EOL> with self . assertRaises ( gen . ReturnValueIgnoredError ) : <EOL> self . run_gen ( f ) <EOL> def test_async_raise_return_value ( self ) : <EOL> @ gen . engine <EOL> def f ( ) : <EOL> yield gen . Task ( self . io_loop . add_callback ) <EOL> raise gen . Return ( <NUM_LIT> ) <EOL> with self . assertRaises ( gen . ReturnValueIgnoredError ) : <EOL> self . run_gen ( f ) <EOL> def test_async_raise_return_value_tuple ( self ) : <EOL> @ gen . engine <EOL> def f ( ) : <EOL> yield gen . Task ( self . io_loop . add_callback ) <EOL> raise gen . Return ( ( <NUM_LIT:1> , <NUM_LIT:2> ) ) <EOL> with self . assertRaises ( gen . ReturnValueIgnoredError ) : <EOL> self . run_gen ( f ) <EOL> def test_return_value ( self ) : <EOL> @ gen . engine <EOL> def f ( ) : <EOL> return <NUM_LIT> <EOL> with self . assertRaises ( gen . ReturnValueIgnoredError ) : <EOL> self . run_gen ( f ) <EOL> def test_return_value_tuple ( self ) : <EOL> @ gen . engine <EOL> def f ( ) : <EOL> return ( <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> with self . assertRaises ( gen . ReturnValueIgnoredError ) : <EOL> self . run_gen ( f ) <EOL> @ skipNotCPython <EOL> def test_task_refcounting ( self ) : <EOL> @ gen . engine <EOL> def f ( ) : <EOL> class Foo ( object ) : <EOL> pass <EOL> arg = Foo ( ) <EOL> self . arg_ref = weakref . ref ( arg ) <EOL> task = gen . Task ( self . io_loop . add_callback , arg = arg ) <EOL> self . task_ref = weakref . ref ( task ) <EOL> yield task <EOL> self . stop ( ) <EOL> self . run_gen ( f ) <EOL> self . assertIs ( self . arg_ref ( ) , None ) <EOL> self . assertIs ( self . task_ref ( ) , None ) <EOL> class GenCoroutineTest ( AsyncTestCase ) : <EOL> def setUp ( self ) : <EOL> self . finished = False <EOL> super ( GenCoroutineTest , self ) . setUp ( ) <EOL> def tearDown ( self ) : <EOL> super ( GenCoroutineTest , self ) . tearDown ( ) <EOL> assert self . finished <EOL> @ gen_test <EOL> def test_sync_gen_return ( self ) : <EOL> @ gen . coroutine <EOL> def f ( ) : <EOL> raise gen . Return ( <NUM_LIT> ) <EOL> result = yield f ( ) <EOL> self . assertEqual ( result , <NUM_LIT> ) <EOL> self . finished = True <EOL> @ gen_test <EOL> def test_async_gen_return ( self ) : <EOL> @ gen . coroutine <EOL> def f ( ) : <EOL> yield gen . Task ( self . io_loop . add_callback ) <EOL> raise gen . Return ( <NUM_LIT> ) <EOL> result = yield f ( ) <EOL> self . assertEqual ( result , <NUM_LIT> ) <EOL> self . finished = True <EOL> @ gen_test <EOL> def test_sync_return ( self ) : <EOL> @ gen . coroutine <EOL> def f ( ) : <EOL> return <NUM_LIT> <EOL> result = yield f ( ) <EOL> self . assertEqual ( result , <NUM_LIT> ) <EOL> self . finished = True <EOL> @ skipBefore33 <EOL> @ gen_test <EOL> def test_async_return ( self ) : <EOL> namespace = exec_test ( globals ( ) , locals ( ) , """<STR_LIT>""" ) <EOL> result = yield namespace [ '<STR_LIT:f>' ] ( ) <EOL> self . assertEqual ( result , <NUM_LIT> ) <EOL> self . finished = True <EOL> @ skipBefore33 <EOL> @ gen_test <EOL> def test_async_early_return ( self ) : <EOL> namespace = exec_test ( globals ( ) , locals ( ) , """<STR_LIT>""" ) <EOL> result = yield namespace [ '<STR_LIT:f>' ] ( ) <EOL> self . assertEqual ( result , <NUM_LIT> ) <EOL> self . finished = True <EOL> @ skipBefore35 <EOL> @ gen_test <EOL> def test_async_await ( self ) : <EOL> namespace = exec_test ( globals ( ) , locals ( ) , """<STR_LIT>""" ) <EOL> result = yield namespace [ '<STR_LIT:f>' ] ( ) <EOL> self . assertEqual ( result , <NUM_LIT> ) <EOL> self . finished = True <EOL> @ skipBefore35 <EOL> @ gen_test <EOL> def test_async_await_mixed_multi_native_future ( self ) : <EOL> namespace = exec_test ( globals ( ) , locals ( ) , """<STR_LIT>""" ) <EOL> @ gen . coroutine <EOL> def f2 ( ) : <EOL> yield gen . Task ( self . io_loop . add_callback ) <EOL> raise gen . Return ( <NUM_LIT> ) <EOL> results = yield [ namespace [ '<STR_LIT>' ] ( ) , f2 ( ) ] <EOL> self . assertEqual ( results , [ <NUM_LIT> , <NUM_LIT> ] ) <EOL> self . finished = True <EOL> @ skipBefore35 <EOL> @ gen_test <EOL> def test_async_await_mixed_multi_native_yieldpoint ( self ) : <EOL> namespace = exec_test ( globals ( ) , locals ( ) , """<STR_LIT>""" ) <EOL> @ gen . coroutine <EOL> def f2 ( ) : <EOL> yield gen . Task ( self . io_loop . add_callback ) <EOL> raise gen . Return ( <NUM_LIT> ) <EOL> f2 ( callback = ( yield gen . Callback ( '<STR_LIT>' ) ) ) <EOL> results = yield [ namespace [ '<STR_LIT>' ] ( ) , gen . Wait ( '<STR_LIT>' ) ] <EOL> self . assertEqual ( results , [ <NUM_LIT> , <NUM_LIT> ] ) <EOL> self . finished = True <EOL> @ gen_test <EOL> def test_sync_return_no_value ( self ) : <EOL> @ gen . coroutine <EOL> def f ( ) : <EOL> return <EOL> result = yield f ( ) <EOL> self . assertEqual ( result , None ) <EOL> self . finished = True <EOL> @ gen_test <EOL> def test_async_return_no_value ( self ) : <EOL> @ gen . coroutine <EOL> def f ( ) : <EOL> yield gen . Task ( self . io_loop . add_callback ) <EOL> return <EOL> result = yield f ( ) <EOL> self . assertEqual ( result , None ) <EOL> self . finished = True <EOL> @ gen_test <EOL> def test_sync_raise ( self ) : <EOL> @ gen . coroutine <EOL> def f ( ) : <EOL> <NUM_LIT:1> / <NUM_LIT:0> <EOL> future = f ( ) <EOL> with self . assertRaises ( ZeroDivisionError ) : <EOL> yield future <EOL> self . finished = True <EOL> @ gen_test <EOL> def test_async_raise ( self ) : <EOL> @ gen . coroutine <EOL> def f ( ) : <EOL> yield gen . Task ( self . io_loop . add_callback ) <EOL> <NUM_LIT:1> / <NUM_LIT:0> <EOL> future = f ( ) <EOL> with self . assertRaises ( ZeroDivisionError ) : <EOL> yield future <EOL> self . finished = True <EOL> @ gen_test <EOL> def test_pass_callback ( self ) : <EOL> @ gen . coroutine <EOL> def f ( ) : <EOL> raise gen . Return ( <NUM_LIT> ) <EOL> result = yield gen . Task ( f ) <EOL> self . assertEqual ( result , <NUM_LIT> ) <EOL> self . finished = True <EOL> @ gen_test <EOL> def test_replace_yieldpoint_exception ( self ) : <EOL> @ gen . coroutine <EOL> def f1 ( ) : <EOL> <NUM_LIT:1> / <NUM_LIT:0> <EOL> @ gen . coroutine <EOL> def f2 ( ) : <EOL> try : <EOL> yield f1 ( ) <EOL> except ZeroDivisionError : <EOL> raise KeyError ( ) <EOL> future = f2 ( ) <EOL> with self . assertRaises ( KeyError ) : <EOL> yield future <EOL> self . finished = True <EOL> @ gen_test <EOL> def test_swallow_yieldpoint_exception ( self ) : <EOL> @ gen . coroutine <EOL> def f1 ( ) : <EOL> <NUM_LIT:1> / <NUM_LIT:0> <EOL> @ gen . coroutine <EOL> def f2 ( ) : <EOL> try : <EOL> yield f1 ( ) <EOL> except ZeroDivisionError : <EOL> raise gen . Return ( <NUM_LIT> ) <EOL> result = yield f2 ( ) <EOL> self . assertEqual ( result , <NUM_LIT> ) <EOL> self . finished = True <EOL> @ gen_test <EOL> def test_replace_context_exception ( self ) : <EOL> @ gen . coroutine <EOL> def f2 ( ) : <EOL> ( yield gen . Callback ( <NUM_LIT:1> ) ) ( ) <EOL> yield gen . Wait ( <NUM_LIT:1> ) <EOL> self . io_loop . add_callback ( lambda : <NUM_LIT:1> / <NUM_LIT:0> ) <EOL> try : <EOL> yield gen . Task ( self . io_loop . add_timeout , <EOL> self . io_loop . time ( ) + <NUM_LIT:10> ) <EOL> except ZeroDivisionError : <EOL> raise KeyError ( ) <EOL> future = f2 ( ) <EOL> with self . assertRaises ( KeyError ) : <EOL> yield future <EOL> self . finished = True <EOL> @ gen_test <EOL> def test_swallow_context_exception ( self ) : <EOL> @ gen . coroutine <EOL> def f2 ( ) : <EOL> ( yield gen . Callback ( <NUM_LIT:1> ) ) ( ) <EOL> yield gen . Wait ( <NUM_LIT:1> ) <EOL> self . io_loop . add_callback ( lambda : <NUM_LIT:1> / <NUM_LIT:0> ) <EOL> try : <EOL> yield gen . Task ( self . io_loop . add_timeout , <EOL> self . io_loop . time ( ) + <NUM_LIT:10> ) <EOL> except ZeroDivisionError : <EOL> raise gen . Return ( <NUM_LIT> ) <EOL> result = yield f2 ( ) <EOL> self . assertEqual ( result , <NUM_LIT> ) <EOL> self . finished = True <EOL> @ gen_test <EOL> def test_moment ( self ) : <EOL> calls = [ ] <EOL> @ gen . coroutine <EOL> def f ( name , yieldable ) : <EOL> for i in range ( <NUM_LIT:5> ) : <EOL> calls . append ( name ) <EOL> yield yieldable <EOL> immediate = Future ( ) <EOL> immediate . set_result ( None ) <EOL> yield [ f ( '<STR_LIT:a>' , immediate ) , f ( '<STR_LIT:b>' , immediate ) ] <EOL> self . assertEqual ( '<STR_LIT>' . join ( calls ) , '<STR_LIT>' ) <EOL> calls = [ ] <EOL> yield [ f ( '<STR_LIT:a>' , gen . moment ) , f ( '<STR_LIT:b>' , gen . moment ) ] <EOL> self . assertEqual ( '<STR_LIT>' . join ( calls ) , '<STR_LIT>' ) <EOL> self . finished = True <EOL> calls = [ ] <EOL> yield [ f ( '<STR_LIT:a>' , gen . moment ) , f ( '<STR_LIT:b>' , immediate ) ] <EOL> self . assertEqual ( '<STR_LIT>' . join ( calls ) , '<STR_LIT>' ) <EOL> @ gen_test <EOL> def test_sleep ( self ) : <EOL> yield gen . sleep ( <NUM_LIT> ) <EOL> self . finished = True <EOL> @ skipBefore33 <EOL> @ gen_test <EOL> def test_py3_leak_exception_context ( self ) : <EOL> class LeakedException ( Exception ) : <EOL> pass <EOL> @ gen . coroutine <EOL> def inner ( iteration ) : <EOL> raise LeakedException ( iteration ) <EOL> try : <EOL> yield inner ( <NUM_LIT:1> ) <EOL> except LeakedException as e : <EOL> self . assertEqual ( str ( e ) , "<STR_LIT:1>" ) <EOL> self . assertIsNone ( e . __context__ ) <EOL> try : <EOL> yield inner ( <NUM_LIT:2> ) <EOL> except LeakedException as e : <EOL> self . assertEqual ( str ( e ) , "<STR_LIT:2>" ) <EOL> self . assertIsNone ( e . __context__ ) <EOL> self . finished = True <EOL> class GenSequenceHandler ( RequestHandler ) : <EOL> @ asynchronous <EOL> @ gen . engine <EOL> def get ( self ) : <EOL> self . io_loop = self . request . connection . stream . io_loop <EOL> self . io_loop . add_callback ( ( yield gen . Callback ( "<STR_LIT>" ) ) ) <EOL> yield gen . Wait ( "<STR_LIT>" ) <EOL> self . write ( "<STR_LIT:1>" ) <EOL> self . io_loop . add_callback ( ( yield gen . Callback ( "<STR_LIT>" ) ) ) <EOL> yield gen . Wait ( "<STR_LIT>" ) <EOL> self . write ( "<STR_LIT:2>" ) <EOL> self . io_loop . add_callback ( ( yield gen . Callback ( "<STR_LIT>" ) ) ) <EOL> yield gen . Wait ( "<STR_LIT>" ) <EOL> self . finish ( "<STR_LIT:3>" ) <EOL> class GenCoroutineSequenceHandler ( RequestHandler ) : <EOL> @ gen . coroutine <EOL> def get ( self ) : <EOL> self . io_loop = self . request . connection . stream . io_loop <EOL> self . io_loop . add_callback ( ( yield gen . Callback ( "<STR_LIT>" ) ) ) <EOL> yield gen . Wait ( "<STR_LIT>" ) <EOL> self . write ( "<STR_LIT:1>" ) <EOL> self . io_loop . add_callback ( ( yield gen . Callback ( "<STR_LIT>" ) ) ) <EOL> yield gen . Wait ( "<STR_LIT>" ) <EOL> self . write ( "<STR_LIT:2>" ) <EOL> self . io_loop . add_callback ( ( yield gen . Callback ( "<STR_LIT>" ) ) ) <EOL> yield gen . Wait ( "<STR_LIT>" ) <EOL> self . finish ( "<STR_LIT:3>" ) <EOL> class GenCoroutineUnfinishedSequenceHandler ( RequestHandler ) : <EOL> @ asynchronous <EOL> @ gen . coroutine <EOL> def get ( self ) : <EOL> self . io_loop = self . request . connection . stream . io_loop <EOL> self . io_loop . add_callback ( ( yield gen . Callback ( "<STR_LIT>" ) ) ) <EOL> yield gen . Wait ( "<STR_LIT>" ) <EOL> self . write ( "<STR_LIT:1>" ) <EOL> self . io_loop . add_callback ( ( yield gen . Callback ( "<STR_LIT>" ) ) ) <EOL> yield gen . Wait ( "<STR_LIT>" ) <EOL> self . write ( "<STR_LIT:2>" ) <EOL> self . io_loop . add_callback ( ( yield gen . Callback ( "<STR_LIT>" ) ) ) <EOL> yield gen . Wait ( "<STR_LIT>" ) <EOL> self . write ( "<STR_LIT:3>" ) <EOL> class GenTaskHandler ( RequestHandler ) : <EOL> @ asynchronous <EOL> @ gen . engine <EOL> def get ( self ) : <EOL> io_loop = self . request . connection . stream . io_loop <EOL> client = AsyncHTTPClient ( io_loop = io_loop ) <EOL> response = yield gen . Task ( client . fetch , self . get_argument ( '<STR_LIT:url>' ) ) <EOL> response . rethrow ( ) <EOL> self . finish ( b"<STR_LIT>" + response . body ) <EOL> class GenExceptionHandler ( RequestHandler ) : <EOL> @ asynchronous <EOL> @ gen . engine <EOL> def get ( self ) : <EOL> io_loop = self . request . connection . stream . io_loop <EOL> yield gen . Task ( io_loop . add_callback ) <EOL> raise Exception ( "<STR_LIT>" ) <EOL> class GenCoroutineExceptionHandler ( RequestHandler ) : <EOL> @ gen . coroutine <EOL> def get ( self ) : <EOL> io_loop = self . request . connection . stream . io_loop <EOL> yield gen . Task ( io_loop . add_callback ) <EOL> raise Exception ( "<STR_LIT>" ) <EOL> class GenYieldExceptionHandler ( RequestHandler ) : <EOL> @ asynchronous <EOL> @ gen . engine <EOL> def get ( self ) : <EOL> io_loop = self . request . connection . stream . io_loop <EOL> def fail_task ( callback ) : <EOL> io_loop . add_callback ( lambda : <NUM_LIT:1> / <NUM_LIT:0> ) <EOL> try : <EOL> yield gen . Task ( fail_task ) <EOL> raise Exception ( "<STR_LIT>" ) <EOL> except ZeroDivisionError : <EOL> self . finish ( '<STR_LIT>' ) <EOL> class UndecoratedCoroutinesHandler ( RequestHandler ) : <EOL> @ gen . coroutine <EOL> def prepare ( self ) : <EOL> self . chunks = [ ] <EOL> yield gen . Task ( IOLoop . current ( ) . add_callback ) <EOL> self . chunks . append ( '<STR_LIT:1>' ) <EOL> @ gen . coroutine <EOL> def get ( self ) : <EOL> self . chunks . append ( '<STR_LIT:2>' ) <EOL> yield gen . Task ( IOLoop . current ( ) . add_callback ) <EOL> self . chunks . append ( '<STR_LIT:3>' ) <EOL> yield gen . Task ( IOLoop . current ( ) . add_callback ) <EOL> self . write ( '<STR_LIT>' . join ( self . chunks ) ) <EOL> class AsyncPrepareErrorHandler ( RequestHandler ) : <EOL> @ gen . coroutine <EOL> def prepare ( self ) : <EOL> yield gen . Task ( IOLoop . current ( ) . add_callback ) <EOL> raise HTTPError ( <NUM_LIT> ) <EOL> def get ( self ) : <EOL> self . finish ( '<STR_LIT>' ) <EOL> class NativeCoroutineHandler ( RequestHandler ) : <EOL> if sys . version_info > ( <NUM_LIT:3> , <NUM_LIT:5> ) : <EOL> exec ( textwrap . dedent ( """<STR_LIT>""" ) ) <EOL> class GenWebTest ( AsyncHTTPTestCase ) : <EOL> def get_app ( self ) : <EOL> return Application ( [ <EOL> ( '<STR_LIT>' , GenSequenceHandler ) , <EOL> ( '<STR_LIT>' , GenCoroutineSequenceHandler ) , <EOL> ( '<STR_LIT>' , <EOL> GenCoroutineUnfinishedSequenceHandler ) , <EOL> ( '<STR_LIT>' , GenTaskHandler ) , <EOL> ( '<STR_LIT>' , GenExceptionHandler ) , <EOL> ( '<STR_LIT>' , GenCoroutineExceptionHandler ) , <EOL> ( '<STR_LIT>' , GenYieldExceptionHandler ) , <EOL> ( '<STR_LIT>' , UndecoratedCoroutinesHandler ) , <EOL> ( '<STR_LIT>' , AsyncPrepareErrorHandler ) , <EOL> ( '<STR_LIT>' , NativeCoroutineHandler ) , <EOL> ] ) <EOL> def test_sequence_handler ( self ) : <EOL> response = self . fetch ( '<STR_LIT>' ) <EOL> self . assertEqual ( response . body , b"<STR_LIT>" ) <EOL> def test_coroutine_sequence_handler ( self ) : <EOL> response = self . fetch ( '<STR_LIT>' ) <EOL> self . assertEqual ( response . body , b"<STR_LIT>" ) <EOL> def test_coroutine_unfinished_sequence_handler ( self ) : <EOL> response = self . fetch ( '<STR_LIT>' ) <EOL> self . assertEqual ( response . body , b"<STR_LIT>" ) <EOL> def test_task_handler ( self ) : <EOL> response = self . fetch ( '<STR_LIT>' % url_escape ( self . get_url ( '<STR_LIT>' ) ) ) <EOL> self . assertEqual ( response . body , b"<STR_LIT>" ) <EOL> def test_exception_handler ( self ) : <EOL> with ExpectLog ( app_log , "<STR_LIT>" ) : <EOL> response = self . fetch ( '<STR_LIT>' ) <EOL> self . assertEqual ( <NUM_LIT> , response . code ) <EOL> def test_coroutine_exception_handler ( self ) : <EOL> with ExpectLog ( app_log , "<STR_LIT>" ) : <EOL> response = self . fetch ( '<STR_LIT>' ) <EOL> self . assertEqual ( <NUM_LIT> , response . code ) <EOL> def test_yield_exception_handler ( self ) : <EOL> response = self . fetch ( '<STR_LIT>' ) <EOL> self . assertEqual ( response . body , b'<STR_LIT>' ) <EOL> def test_undecorated_coroutines ( self ) : <EOL> response = self . fetch ( '<STR_LIT>' ) <EOL> self . assertEqual ( response . body , b'<STR_LIT>' ) <EOL> def test_async_prepare_error_handler ( self ) : <EOL> response = self . fetch ( '<STR_LIT>' ) <EOL> self . assertEqual ( response . code , <NUM_LIT> ) <EOL> @ skipBefore35 <EOL> def test_native_coroutine_handler ( self ) : <EOL> response = self . fetch ( '<STR_LIT>' ) <EOL> self . assertEqual ( response . code , <NUM_LIT:200> ) <EOL> self . assertEqual ( response . body , b'<STR_LIT>' ) <EOL> class WithTimeoutTest ( AsyncTestCase ) : <EOL> @ gen_test <EOL> def test_timeout ( self ) : <EOL> with self . assertRaises ( gen . TimeoutError ) : <EOL> yield gen . with_timeout ( datetime . timedelta ( seconds = <NUM_LIT:0.1> ) , <EOL> Future ( ) ) <EOL> @ gen_test <EOL> def test_completes_before_timeout ( self ) : <EOL> future = Future ( ) <EOL> self . io_loop . add_timeout ( datetime . timedelta ( seconds = <NUM_LIT:0.1> ) , <EOL> lambda : future . set_result ( '<STR_LIT>' ) ) <EOL> result = yield gen . with_timeout ( datetime . timedelta ( seconds = <NUM_LIT> ) , <EOL> future , io_loop = self . io_loop ) <EOL> self . assertEqual ( result , '<STR_LIT>' ) <EOL> @ gen_test <EOL> def test_fails_before_timeout ( self ) : <EOL> future = Future ( ) <EOL> self . io_loop . add_timeout ( <EOL> datetime . timedelta ( seconds = <NUM_LIT:0.1> ) , <EOL> lambda : future . set_exception ( ZeroDivisionError ( ) ) ) <EOL> with self . assertRaises ( ZeroDivisionError ) : <EOL> yield gen . with_timeout ( datetime . timedelta ( seconds = <NUM_LIT> ) , <EOL> future , io_loop = self . io_loop ) <EOL> @ gen_test <EOL> def test_already_resolved ( self ) : <EOL> future = Future ( ) <EOL> future . set_result ( '<STR_LIT>' ) <EOL> result = yield gen . with_timeout ( datetime . timedelta ( seconds = <NUM_LIT> ) , <EOL> future , io_loop = self . io_loop ) <EOL> self . assertEqual ( result , '<STR_LIT>' ) <EOL> @ unittest . skipIf ( futures is None , '<STR_LIT>' ) <EOL> @ gen_test <EOL> def test_timeout_concurrent_future ( self ) : <EOL> with futures . ThreadPoolExecutor ( <NUM_LIT:1> ) as executor : <EOL> with self . assertRaises ( gen . TimeoutError ) : <EOL> yield gen . with_timeout ( self . io_loop . time ( ) , <EOL> executor . submit ( time . sleep , <NUM_LIT:0.1> ) ) <EOL> @ unittest . skipIf ( futures is None , '<STR_LIT>' ) <EOL> @ gen_test <EOL> def test_completed_concurrent_future ( self ) : <EOL> with futures . ThreadPoolExecutor ( <NUM_LIT:1> ) as executor : <EOL> yield gen . with_timeout ( datetime . timedelta ( seconds = <NUM_LIT> ) , <EOL> executor . submit ( lambda : None ) ) <EOL> class WaitIteratorTest ( AsyncTestCase ) : <EOL> @ gen_test <EOL> def test_empty_iterator ( self ) : <EOL> g = gen . WaitIterator ( ) <EOL> self . assertTrue ( g . done ( ) , '<STR_LIT>' ) <EOL> with self . assertRaises ( ValueError ) : <EOL> g = gen . WaitIterator ( False , bar = False ) <EOL> self . assertEqual ( g . current_index , None , "<STR_LIT>" ) <EOL> self . assertEqual ( g . current_future , None , "<STR_LIT>" ) <EOL> @ gen_test <EOL> def test_already_done ( self ) : <EOL> f1 = Future ( ) <EOL> f2 = Future ( ) <EOL> f3 = Future ( ) <EOL> f1 . set_result ( <NUM_LIT> ) <EOL> f2 . set_result ( <NUM_LIT> ) <EOL> f3 . set_result ( <NUM_LIT> ) <EOL> g = gen . WaitIterator ( f1 , f2 , f3 ) <EOL> i = <NUM_LIT:0> <EOL> while not g . done ( ) : <EOL> r = yield g . next ( ) <EOL> if i == <NUM_LIT:0> : <EOL> self . assertEqual ( g . current_index , <NUM_LIT:0> ) <EOL> self . assertIs ( g . current_future , f1 ) <EOL> self . assertEqual ( r , <NUM_LIT> ) <EOL> elif i == <NUM_LIT:1> : <EOL> self . assertEqual ( g . current_index , <NUM_LIT:1> ) <EOL> self . assertIs ( g . current_future , f2 ) <EOL> self . assertEqual ( r , <NUM_LIT> ) <EOL> elif i == <NUM_LIT:2> : <EOL> self . assertEqual ( g . current_index , <NUM_LIT:2> ) <EOL> self . assertIs ( g . current_future , f3 ) <EOL> self . assertEqual ( r , <NUM_LIT> ) <EOL> i += <NUM_LIT:1> <EOL> self . assertEqual ( g . current_index , None , "<STR_LIT>" ) <EOL> self . assertEqual ( g . current_future , None , "<STR_LIT>" ) <EOL> dg = gen . WaitIterator ( f1 = f1 , f2 = f2 ) <EOL> while not dg . done ( ) : <EOL> dr = yield dg . next ( ) <EOL> if dg . current_index == "<STR_LIT>" : <EOL> self . assertTrue ( dg . current_future == f1 and dr == <NUM_LIT> , <EOL> "<STR_LIT>" ) <EOL> elif dg . current_index == "<STR_LIT>" : <EOL> self . assertTrue ( dg . current_future == f2 and dr == <NUM_LIT> , <EOL> "<STR_LIT>" ) <EOL> else : <EOL> self . fail ( "<STR_LIT>" . format ( <EOL> dg . current_index ) ) <EOL> i += <NUM_LIT:1> <EOL> self . assertEqual ( dg . current_index , None , "<STR_LIT>" ) <EOL> self . assertEqual ( dg . current_future , None , "<STR_LIT>" ) <EOL> def finish_coroutines ( self , iteration , futures ) : <EOL> if iteration == <NUM_LIT:3> : <EOL> futures [ <NUM_LIT:2> ] . set_result ( <NUM_LIT> ) <EOL> elif iteration == <NUM_LIT:5> : <EOL> futures [ <NUM_LIT:0> ] . set_exception ( ZeroDivisionError ( ) ) <EOL> elif iteration == <NUM_LIT:8> : <EOL> futures [ <NUM_LIT:1> ] . set_result ( <NUM_LIT> ) <EOL> futures [ <NUM_LIT:3> ] . set_result ( <NUM_LIT> ) <EOL> if iteration < <NUM_LIT:8> : <EOL> self . io_loop . add_callback ( self . finish_coroutines , iteration + <NUM_LIT:1> , futures ) <EOL> @ gen_test <EOL> def test_iterator ( self ) : <EOL> futures = [ Future ( ) , Future ( ) , Future ( ) , Future ( ) ] <EOL> self . finish_coroutines ( <NUM_LIT:0> , futures ) <EOL> g = gen . WaitIterator ( * futures ) <EOL> i = <NUM_LIT:0> <EOL> while not g . done ( ) : <EOL> try : <EOL> r = yield g . next ( ) <EOL> except ZeroDivisionError : <EOL> self . assertIs ( g . current_future , futures [ <NUM_LIT:0> ] , <EOL> '<STR_LIT>' ) <EOL> else : <EOL> if i == <NUM_LIT:0> : <EOL> self . assertEqual ( r , <NUM_LIT> , '<STR_LIT>' ) <EOL> self . assertEqual ( g . current_index , <NUM_LIT:2> , '<STR_LIT>' ) <EOL> elif i == <NUM_LIT:2> : <EOL> self . assertEqual ( r , <NUM_LIT> , '<STR_LIT>' ) <EOL> self . assertEqual ( g . current_index , <NUM_LIT:1> , '<STR_LIT>' ) <EOL> elif i == <NUM_LIT:3> : <EOL> self . assertEqual ( r , <NUM_LIT> , '<STR_LIT>' ) <EOL> self . assertEqual ( g . current_index , <NUM_LIT:3> , '<STR_LIT>' ) <EOL> i += <NUM_LIT:1> <EOL> @ skipBefore35 <EOL> @ gen_test <EOL> def test_iterator_async_await ( self ) : <EOL> futures = [ Future ( ) , Future ( ) , Future ( ) , Future ( ) ] <EOL> self . finish_coroutines ( <NUM_LIT:0> , futures ) <EOL> self . finished = False <EOL> namespace = exec_test ( globals ( ) , locals ( ) , """<STR_LIT>""" ) <EOL> yield namespace [ '<STR_LIT:f>' ] ( ) <EOL> self . assertTrue ( self . finished ) <EOL> @ gen_test <EOL> def test_no_ref ( self ) : <EOL> yield gen . with_timeout ( datetime . timedelta ( seconds = <NUM_LIT:0.1> ) , <EOL> gen . WaitIterator ( gen . sleep ( <NUM_LIT:0> ) ) . next ( ) ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
