@@ -2260,192 +2260,5 @@ def coro():
22602260 self .assertEqual (result , 11 )
22612261
22622262
2263- class TimeoutTests (test_utils .TestCase ):
2264- def setUp (self ):
2265- self .loop = asyncio .new_event_loop ()
2266- asyncio .set_event_loop (None )
2267-
2268- def tearDown (self ):
2269- self .loop .close ()
2270- self .loop = None
2271-
2272- def test_timeout (self ):
2273- canceled_raised = [False ]
2274-
2275- @asyncio .coroutine
2276- def long_running_task ():
2277- try :
2278- yield from asyncio .sleep (10 , loop = self .loop )
2279- except asyncio .CancelledError :
2280- canceled_raised [0 ] = True
2281- raise
2282-
2283- @asyncio .coroutine
2284- def go ():
2285- with self .assertRaises (asyncio .TimeoutError ):
2286- with asyncio .timeout (0.01 , loop = self .loop ) as t :
2287- yield from long_running_task ()
2288- self .assertIs (t ._loop , self .loop )
2289-
2290- self .loop .run_until_complete (go ())
2291- self .assertTrue (canceled_raised [0 ], 'CancelledError was not raised' )
2292-
2293- def test_timeout_finish_in_time (self ):
2294- @asyncio .coroutine
2295- def long_running_task ():
2296- yield from asyncio .sleep (0.01 , loop = self .loop )
2297- return 'done'
2298-
2299- @asyncio .coroutine
2300- def go ():
2301- with asyncio .timeout (0.1 , loop = self .loop ):
2302- resp = yield from long_running_task ()
2303- self .assertEqual (resp , 'done' )
2304-
2305- self .loop .run_until_complete (go ())
2306-
2307- def test_timeout_gloabal_loop (self ):
2308- asyncio .set_event_loop (self .loop )
2309-
2310- @asyncio .coroutine
2311- def run ():
2312- with asyncio .timeout (0.1 ) as t :
2313- yield from asyncio .sleep (0.01 )
2314- self .assertIs (t ._loop , self .loop )
2315-
2316- self .loop .run_until_complete (run ())
2317-
2318- def test_timeout_not_relevant_exception (self ):
2319- @asyncio .coroutine
2320- def go ():
2321- yield from asyncio .sleep (0 , loop = self .loop )
2322- with self .assertRaises (KeyError ):
2323- with asyncio .timeout (0.1 , loop = self .loop ):
2324- raise KeyError
2325-
2326- self .loop .run_until_complete (go ())
2327-
2328- def test_timeout_canceled_error_is_converted_to_timeout (self ):
2329- @asyncio .coroutine
2330- def go ():
2331- yield from asyncio .sleep (0 , loop = self .loop )
2332- with self .assertRaises (asyncio .CancelledError ):
2333- with asyncio .timeout (0.001 , loop = self .loop ):
2334- raise asyncio .CancelledError
2335-
2336- self .loop .run_until_complete (go ())
2337-
2338- def test_timeout_blocking_loop (self ):
2339- @asyncio .coroutine
2340- def long_running_task ():
2341- time .sleep (0.05 )
2342- return 'done'
2343-
2344- @asyncio .coroutine
2345- def go ():
2346- with asyncio .timeout (0.01 , loop = self .loop ):
2347- result = yield from long_running_task ()
2348- self .assertEqual (result , 'done' )
2349-
2350- self .loop .run_until_complete (go ())
2351-
2352- def test_for_race_conditions (self ):
2353- fut = asyncio .Future (loop = self .loop )
2354- self .loop .call_later (0.1 , fut .set_result ('done' ))
2355-
2356- @asyncio .coroutine
2357- def go ():
2358- with asyncio .timeout (0.2 , loop = self .loop ):
2359- resp = yield from fut
2360- self .assertEqual (resp , 'done' )
2361-
2362- self .loop .run_until_complete (go ())
2363-
2364- def test_timeout_time (self ):
2365- @asyncio .coroutine
2366- def go ():
2367- foo_running = None
2368-
2369- start = self .loop .time ()
2370- with self .assertRaises (asyncio .TimeoutError ):
2371- with asyncio .timeout (0.1 , loop = self .loop ):
2372- foo_running = True
2373- try :
2374- yield from asyncio .sleep (0.2 , loop = self .loop )
2375- finally :
2376- foo_running = False
2377-
2378- dt = self .loop .time () - start
2379- # tolerate a small delta for slow delta or unstable clocks
2380- self .assertTrue (0.09 < dt < 0.12 , dt )
2381- self .assertFalse (foo_running )
2382-
2383- self .loop .run_until_complete (go ())
2384-
2385- def test_timeout_disable (self ):
2386- @asyncio .coroutine
2387- def long_running_task ():
2388- yield from asyncio .sleep (0.1 , loop = self .loop )
2389- return 'done'
2390-
2391- @asyncio .coroutine
2392- def go ():
2393- t0 = self .loop .time ()
2394- with asyncio .timeout (None , loop = self .loop ):
2395- resp = yield from long_running_task ()
2396- self .assertEqual (resp , 'done' )
2397- dt = self .loop .time () - t0
2398- # tolerate a time delta for clocks with bad resolution
2399- # and slow buildbots
2400- self .assertTrue (0.09 < dt < 0.15 , dt )
2401- self .loop .run_until_complete (go ())
2402-
2403- def test_raise_runtimeerror_if_no_task (self ):
2404- with self .assertRaises (RuntimeError ):
2405- with asyncio .timeout (0.1 , loop = self .loop ):
2406- pass
2407-
2408- def test_outer_coro_is_not_cancelled (self ):
2409-
2410- has_timeout = [False ]
2411-
2412- @asyncio .coroutine
2413- def outer ():
2414- try :
2415- with asyncio .timeout (0.001 , loop = self .loop ):
2416- yield from asyncio .sleep (1 , loop = self .loop )
2417- except asyncio .TimeoutError :
2418- has_timeout [0 ] = True
2419-
2420- @asyncio .coroutine
2421- def go ():
2422- task = asyncio .ensure_future (outer (), loop = self .loop )
2423- yield from task
2424- self .assertTrue (has_timeout [0 ])
2425- self .assertFalse (task .cancelled ())
2426- self .assertTrue (task .done ())
2427-
2428- self .loop .run_until_complete (go ())
2429-
2430- def test_cancel_outer_coro (self ):
2431- fut = asyncio .Future (loop = self .loop )
2432-
2433- @asyncio .coroutine
2434- def outer ():
2435- fut .set_result (None )
2436- yield from asyncio .sleep (1 , loop = self .loop )
2437-
2438- @asyncio .coroutine
2439- def go ():
2440- task = asyncio .ensure_future (outer (), loop = self .loop )
2441- yield from fut
2442- task .cancel ()
2443- with self .assertRaises (asyncio .CancelledError ):
2444- yield from task
2445- self .assertTrue (task .cancelled ())
2446- self .assertTrue (task .done ())
2447-
2448- self .loop .run_until_complete (go ())
2449-
24502263if __name__ == '__main__' :
24512264 unittest .main ()
0 commit comments