|
6 | 6 | import os
|
7 | 7 | import re
|
8 | 8 | import sys
|
| 9 | +import time |
9 | 10 | import types
|
10 | 11 | import unittest
|
11 | 12 | import weakref
|
@@ -2235,5 +2236,173 @@ def coro():
|
2235 | 2236 | self.assertEqual(result, 11)
|
2236 | 2237 |
|
2237 | 2238 |
|
| 2239 | +class TimeoutTests(test_utils.TestCase): |
| 2240 | + def setUp(self): |
| 2241 | + self.loop = asyncio.new_event_loop() |
| 2242 | + asyncio.set_event_loop(None) |
| 2243 | + |
| 2244 | + def tearDown(self): |
| 2245 | + self.loop.close() |
| 2246 | + self.loop = None |
| 2247 | + |
| 2248 | + def test_timeout(self): |
| 2249 | + canceled_raised = [False] |
| 2250 | + |
| 2251 | + @asyncio.coroutine |
| 2252 | + def long_running_task(): |
| 2253 | + try: |
| 2254 | + yield from asyncio.sleep(10, loop=self.loop) |
| 2255 | + except asyncio.CancelledError: |
| 2256 | + canceled_raised[0] = True |
| 2257 | + raise |
| 2258 | + |
| 2259 | + @asyncio.coroutine |
| 2260 | + def go(): |
| 2261 | + with self.assertRaises(asyncio.TimeoutError): |
| 2262 | + with asyncio.timeout(0.01, loop=self.loop) as t: |
| 2263 | + yield from long_running_task() |
| 2264 | + self.assertIs(t._loop, self.loop) |
| 2265 | + |
| 2266 | + self.loop.run_until_complete(go()) |
| 2267 | + self.assertTrue(canceled_raised[0], 'CancelledError was not raised') |
| 2268 | + |
| 2269 | + def test_timeout_finish_in_time(self): |
| 2270 | + @asyncio.coroutine |
| 2271 | + def long_running_task(): |
| 2272 | + yield from asyncio.sleep(0.01, loop=self.loop) |
| 2273 | + return 'done' |
| 2274 | + |
| 2275 | + @asyncio.coroutine |
| 2276 | + def go(): |
| 2277 | + with asyncio.timeout(0.1, loop=self.loop): |
| 2278 | + resp = yield from long_running_task() |
| 2279 | + self.assertEqual(resp, 'done') |
| 2280 | + |
| 2281 | + self.loop.run_until_complete(go()) |
| 2282 | + |
| 2283 | + def test_timeout_gloabal_loop(self): |
| 2284 | + asyncio.set_event_loop(self.loop) |
| 2285 | + |
| 2286 | + @asyncio.coroutine |
| 2287 | + def run(): |
| 2288 | + with asyncio.timeout(0.1) as t: |
| 2289 | + yield from asyncio.sleep(0.01) |
| 2290 | + self.assertIs(t._loop, self.loop) |
| 2291 | + |
| 2292 | + self.loop.run_until_complete(run()) |
| 2293 | + |
| 2294 | + def test_timeout_not_relevant_exception(self): |
| 2295 | + @asyncio.coroutine |
| 2296 | + def go(): |
| 2297 | + yield from asyncio.sleep(0, loop=self.loop) |
| 2298 | + with self.assertRaises(KeyError): |
| 2299 | + with asyncio.timeout(0.1, loop=self.loop): |
| 2300 | + raise KeyError |
| 2301 | + |
| 2302 | + self.loop.run_until_complete(go()) |
| 2303 | + |
| 2304 | + def test_timeout_canceled_error_is_converted_to_timeout(self): |
| 2305 | + @asyncio.coroutine |
| 2306 | + def go(): |
| 2307 | + yield from asyncio.sleep(0, loop=self.loop) |
| 2308 | + with self.assertRaises(asyncio.CancelledError): |
| 2309 | + with asyncio.timeout(0.001, loop=self.loop): |
| 2310 | + raise asyncio.CancelledError |
| 2311 | + |
| 2312 | + self.loop.run_until_complete(go()) |
| 2313 | + |
| 2314 | + def test_timeout_blocking_loop(self): |
| 2315 | + @asyncio.coroutine |
| 2316 | + def long_running_task(): |
| 2317 | + time.sleep(0.05) |
| 2318 | + return 'done' |
| 2319 | + |
| 2320 | + @asyncio.coroutine |
| 2321 | + def go(): |
| 2322 | + with asyncio.timeout(0.01, loop=self.loop): |
| 2323 | + result = yield from long_running_task() |
| 2324 | + self.assertEqual(result, 'done') |
| 2325 | + |
| 2326 | + self.loop.run_until_complete(go()) |
| 2327 | + |
| 2328 | + def test_for_race_conditions(self): |
| 2329 | + fut = asyncio.Future(loop=self.loop) |
| 2330 | + self.loop.call_later(0.1, fut.set_result('done')) |
| 2331 | + |
| 2332 | + @asyncio.coroutine |
| 2333 | + def go(): |
| 2334 | + with asyncio.timeout(0.2, loop=self.loop): |
| 2335 | + resp = yield from fut |
| 2336 | + self.assertEqual(resp, 'done') |
| 2337 | + |
| 2338 | + self.loop.run_until_complete(go()) |
| 2339 | + |
| 2340 | + def test_timeout_time(self): |
| 2341 | + @asyncio.coroutine |
| 2342 | + def go(): |
| 2343 | + foo_running = None |
| 2344 | + |
| 2345 | + start = self.loop.time() |
| 2346 | + with self.assertRaises(asyncio.TimeoutError): |
| 2347 | + with asyncio.timeout(0.1, loop=self.loop): |
| 2348 | + foo_running = True |
| 2349 | + try: |
| 2350 | + yield from asyncio.sleep(0.2, loop=self.loop) |
| 2351 | + finally: |
| 2352 | + foo_running = False |
| 2353 | + |
| 2354 | + dt = self.loop.time() - start |
| 2355 | + self.assertTrue(0.09 < dt < 0.11, dt) |
| 2356 | + self.assertFalse(foo_running) |
| 2357 | + |
| 2358 | + self.loop.run_until_complete(go()) |
| 2359 | + |
| 2360 | + def test_raise_runtimeerror_if_no_task(self): |
| 2361 | + with self.assertRaises(RuntimeError): |
| 2362 | + with asyncio.timeout(0.1, loop=self.loop): |
| 2363 | + pass |
| 2364 | + |
| 2365 | + def test_outer_coro_is_not_cancelled(self): |
| 2366 | + |
| 2367 | + has_timeout = [False] |
| 2368 | + |
| 2369 | + @asyncio.coroutine |
| 2370 | + def outer(): |
| 2371 | + try: |
| 2372 | + with asyncio.timeout(0.001, loop=self.loop): |
| 2373 | + yield from asyncio.sleep(1, loop=self.loop) |
| 2374 | + except asyncio.TimeoutError: |
| 2375 | + has_timeout[0] = True |
| 2376 | + |
| 2377 | + @asyncio.coroutine |
| 2378 | + def go(): |
| 2379 | + task = asyncio.ensure_future(outer(), loop=self.loop) |
| 2380 | + yield from task |
| 2381 | + self.assertTrue(has_timeout[0]) |
| 2382 | + self.assertFalse(task.cancelled()) |
| 2383 | + self.assertTrue(task.done()) |
| 2384 | + |
| 2385 | + self.loop.run_until_complete(go()) |
| 2386 | + |
| 2387 | + def test_cancel_outer_coro(self): |
| 2388 | + fut = asyncio.Future(loop=self.loop) |
| 2389 | + |
| 2390 | + @asyncio.coroutine |
| 2391 | + def outer(): |
| 2392 | + fut.set_result(None) |
| 2393 | + yield from asyncio.sleep(1, loop=self.loop) |
| 2394 | + |
| 2395 | + @asyncio.coroutine |
| 2396 | + def go(): |
| 2397 | + task = asyncio.ensure_future(outer(), loop=self.loop) |
| 2398 | + yield from fut |
| 2399 | + task.cancel() |
| 2400 | + with self.assertRaises(asyncio.CancelledError): |
| 2401 | + yield from task |
| 2402 | + self.assertTrue(task.cancelled()) |
| 2403 | + self.assertTrue(task.done()) |
| 2404 | + |
| 2405 | + self.loop.run_until_complete(go()) |
| 2406 | + |
2238 | 2407 | if __name__ == '__main__':
|
2239 | 2408 | unittest.main()
|
0 commit comments