| 1 |
import asyncio |
| 2 |
import unittest |
| 3 |
import time |
| 4 |
from test import support |
| 5 |
|
| 6 |
|
| 7 |
def tearDownModule(): |
| 8 |
asyncio.events._set_event_loop_policy(None) |
| 9 |
|
| 10 |
|
| 11 |
# The following value can be used as a very small timeout: |
| 12 |
# it passes check "timeout > 0", but has almost |
| 13 |
# no effect on the test performance |
| 14 |
_EPSILON = 0.0001 |
| 15 |
|
| 16 |
|
| 17 |
class SlowTask: |
| 18 |
""" Task will run for this defined time, ignoring cancel requests """ |
| 19 |
TASK_TIMEOUT = 0.2 |
| 20 |
|
| 21 |
def __init__(self): |
| 22 |
self.exited = False |
| 23 |
|
| 24 |
async def run(self): |
| 25 |
exitat = time.monotonic() + self.TASK_TIMEOUT |
| 26 |
|
| 27 |
while True: |
| 28 |
tosleep = exitat - time.monotonic() |
| 29 |
if tosleep <= 0: |
| 30 |
break |
| 31 |
|
| 32 |
try: |
| 33 |
await asyncio.sleep(tosleep) |
| 34 |
except asyncio.CancelledError: |
| 35 |
pass |
| 36 |
|
| 37 |
self.exited = True |
| 38 |
|
| 39 |
|
| 40 |
class AsyncioWaitForTest(unittest.IsolatedAsyncioTestCase): |
| 41 |
|
| 42 |
async def test_asyncio_wait_for_cancelled(self): |
| 43 |
t = SlowTask() |
| 44 |
|
| 45 |
waitfortask = asyncio.create_task( |
| 46 |
asyncio.wait_for(t.run(), t.TASK_TIMEOUT * 2)) |
| 47 |
await asyncio.sleep(0) |
| 48 |
waitfortask.cancel() |
| 49 |
await asyncio.wait({waitfortask}) |
| 50 |
|
| 51 |
self.assertTrue(t.exited) |
| 52 |
|
| 53 |
async def test_asyncio_wait_for_timeout(self): |
| 54 |
t = SlowTask() |
| 55 |
|
| 56 |
try: |
| 57 |
await asyncio.wait_for(t.run(), t.TASK_TIMEOUT / 2) |
| 58 |
except asyncio.TimeoutError: |
| 59 |
pass |
| 60 |
|
| 61 |
self.assertTrue(t.exited) |
| 62 |
|
| 63 |
async def test_wait_for_timeout_less_then_0_or_0_future_done(self): |
| 64 |
loop = asyncio.get_running_loop() |
| 65 |
|
| 66 |
fut = loop.create_future() |
| 67 |
fut.set_result('done') |
| 68 |
|
| 69 |
ret = await asyncio.wait_for(fut, 0) |
| 70 |
|
| 71 |
self.assertEqual(ret, 'done') |
| 72 |
self.assertTrue(fut.done()) |
| 73 |
|
| 74 |
async def test_wait_for_timeout_less_then_0_or_0_coroutine_do_not_started(self): |
| 75 |
foo_started = False |
| 76 |
|
| 77 |
async def foo(): |
| 78 |
nonlocal foo_started |
| 79 |
foo_started = True |
| 80 |
|
| 81 |
with self.assertRaises(asyncio.TimeoutError): |
| 82 |
await asyncio.wait_for(foo(), 0) |
| 83 |
|
| 84 |
self.assertEqual(foo_started, False) |
| 85 |
|
| 86 |
async def test_wait_for_timeout_less_then_0_or_0(self): |
| 87 |
loop = asyncio.get_running_loop() |
| 88 |
|
| 89 |
for timeout in [0, -1]: |
| 90 |
with self.subTest(timeout=timeout): |
| 91 |
foo_running = None |
| 92 |
started = loop.create_future() |
| 93 |
|
| 94 |
async def foo(): |
| 95 |
nonlocal foo_running |
| 96 |
foo_running = True |
| 97 |
started.set_result(None) |
| 98 |
try: |
| 99 |
await asyncio.sleep(10) |
| 100 |
finally: |
| 101 |
foo_running = False |
| 102 |
return 'done' |
| 103 |
|
| 104 |
fut = asyncio.create_task(foo()) |
| 105 |
await started |
| 106 |
|
| 107 |
with self.assertRaises(asyncio.TimeoutError): |
| 108 |
await asyncio.wait_for(fut, timeout) |
| 109 |
|
| 110 |
self.assertTrue(fut.done()) |
| 111 |
# it should have been cancelled due to the timeout |
| 112 |
self.assertTrue(fut.cancelled()) |
| 113 |
self.assertEqual(foo_running, False) |
| 114 |
|
| 115 |
async def test_wait_for(self): |
| 116 |
foo_running = None |
| 117 |
|
| 118 |
async def foo(): |
| 119 |
nonlocal foo_running |
| 120 |
foo_running = True |
| 121 |
try: |
| 122 |
await asyncio.sleep(support.LONG_TIMEOUT) |
| 123 |
finally: |
| 124 |
foo_running = False |
| 125 |
return 'done' |
| 126 |
|
| 127 |
fut = asyncio.create_task(foo()) |
| 128 |
|
| 129 |
with self.assertRaises(asyncio.TimeoutError): |
| 130 |
await asyncio.wait_for(fut, 0.1) |
| 131 |
self.assertTrue(fut.done()) |
| 132 |
# it should have been cancelled due to the timeout |
| 133 |
self.assertTrue(fut.cancelled()) |
| 134 |
self.assertEqual(foo_running, False) |
| 135 |
|
| 136 |
async def test_wait_for_blocking(self): |
| 137 |
async def coro(): |
| 138 |
return 'done' |
| 139 |
|
| 140 |
res = await asyncio.wait_for(coro(), timeout=None) |
| 141 |
self.assertEqual(res, 'done') |
| 142 |
|
| 143 |
async def test_wait_for_race_condition(self): |
| 144 |
loop = asyncio.get_running_loop() |
| 145 |
|
| 146 |
fut = loop.create_future() |
| 147 |
task = asyncio.wait_for(fut, timeout=0.2) |
| 148 |
loop.call_soon(fut.set_result, "ok") |
| 149 |
res = await task |
| 150 |
self.assertEqual(res, "ok") |
| 151 |
|
| 152 |
async def test_wait_for_cancellation_race_condition(self): |
| 153 |
async def inner(): |
| 154 |
with self.assertRaises(asyncio.CancelledError): |
| 155 |
await asyncio.sleep(1) |
| 156 |
return 1 |
| 157 |
|
| 158 |
result = await asyncio.wait_for(inner(), timeout=.01) |
| 159 |
self.assertEqual(result, 1) |
| 160 |
|
| 161 |
async def test_wait_for_waits_for_task_cancellation(self): |
| 162 |
task_done = False |
| 163 |
|
| 164 |
async def inner(): |
| 165 |
nonlocal task_done |
| 166 |
try: |
| 167 |
await asyncio.sleep(10) |
| 168 |
except asyncio.CancelledError: |
| 169 |
await asyncio.sleep(_EPSILON) |
| 170 |
raise |
| 171 |
finally: |
| 172 |
task_done = True |
| 173 |
|
| 174 |
inner_task = asyncio.create_task(inner()) |
| 175 |
|
| 176 |
with self.assertRaises(asyncio.TimeoutError) as cm: |
| 177 |
await asyncio.wait_for(inner_task, timeout=_EPSILON) |
| 178 |
|
| 179 |
self.assertTrue(task_done) |
| 180 |
chained = cm.exception.__context__ |
| 181 |
self.assertEqual(type(chained), asyncio.CancelledError) |
| 182 |
|
| 183 |
async def test_wait_for_waits_for_task_cancellation_w_timeout_0(self): |
| 184 |
task_done = False |
| 185 |
|
| 186 |
async def foo(): |
| 187 |
async def inner(): |
| 188 |
nonlocal task_done |
| 189 |
try: |
| 190 |
await asyncio.sleep(10) |
| 191 |
except asyncio.CancelledError: |
| 192 |
await asyncio.sleep(_EPSILON) |
| 193 |
raise |
| 194 |
finally: |
| 195 |
task_done = True |
| 196 |
|
| 197 |
inner_task = asyncio.create_task(inner()) |
| 198 |
await asyncio.sleep(_EPSILON) |
| 199 |
await asyncio.wait_for(inner_task, timeout=0) |
| 200 |
|
| 201 |
with self.assertRaises(asyncio.TimeoutError) as cm: |
| 202 |
await foo() |
| 203 |
|
| 204 |
self.assertTrue(task_done) |
| 205 |
chained = cm.exception.__context__ |
| 206 |
self.assertEqual(type(chained), asyncio.CancelledError) |
| 207 |
|
| 208 |
async def test_wait_for_reraises_exception_during_cancellation(self): |
| 209 |
class FooException(Exception): |
| 210 |
pass |
| 211 |
|
| 212 |
async def foo(): |
| 213 |
async def inner(): |
| 214 |
try: |
| 215 |
await asyncio.sleep(0.2) |
| 216 |
finally: |
| 217 |
raise FooException |
| 218 |
|
| 219 |
inner_task = asyncio.create_task(inner()) |
| 220 |
|
| 221 |
await asyncio.wait_for(inner_task, timeout=_EPSILON) |
| 222 |
|
| 223 |
with self.assertRaises(FooException): |
| 224 |
await foo() |
| 225 |
|
| 226 |
async def _test_cancel_wait_for(self, timeout): |
| 227 |
loop = asyncio.get_running_loop() |
| 228 |
|
| 229 |
async def blocking_coroutine(): |
| 230 |
fut = loop.create_future() |
| 231 |
# Block: fut result is never set |
| 232 |
await fut |
| 233 |
|
| 234 |
task = asyncio.create_task(blocking_coroutine()) |
| 235 |
|
| 236 |
wait = asyncio.create_task(asyncio.wait_for(task, timeout)) |
| 237 |
loop.call_soon(wait.cancel) |
| 238 |
|
| 239 |
with self.assertRaises(asyncio.CancelledError): |
| 240 |
await wait |
| 241 |
|
| 242 |
# Python issue #23219: cancelling the wait must also cancel the task |
| 243 |
self.assertTrue(task.cancelled()) |
| 244 |
|
| 245 |
async def test_cancel_blocking_wait_for(self): |
| 246 |
await self._test_cancel_wait_for(None) |
| 247 |
|
| 248 |
async def test_cancel_wait_for(self): |
| 249 |
await self._test_cancel_wait_for(60.0) |
| 250 |
|
| 251 |
async def test_wait_for_cancel_suppressed(self): |
| 252 |
# GH-86296: Suppressing CancelledError is discouraged |
| 253 |
# but if a task suppresses CancelledError and returns a value, |
| 254 |
# `wait_for` should return the value instead of raising CancelledError. |
| 255 |
# This is the same behavior as `asyncio.timeout`. |
| 256 |
|
| 257 |
async def return_42(): |
| 258 |
try: |
| 259 |
await asyncio.sleep(10) |
| 260 |
except asyncio.CancelledError: |
| 261 |
return 42 |
| 262 |
|
| 263 |
res = await asyncio.wait_for(return_42(), timeout=0.1) |
| 264 |
self.assertEqual(res, 42) |
| 265 |
|
| 266 |
|
| 267 |
async def test_wait_for_issue86296(self): |
| 268 |
# GH-86296: The task should get cancelled and not run to completion. |
| 269 |
# inner completes in one cycle of the event loop so it |
| 270 |
# completes before the task is cancelled. |
| 271 |
|
| 272 |
async def inner(): |
| 273 |
return 'done' |
| 274 |
|
| 275 |
inner_task = asyncio.create_task(inner()) |
| 276 |
reached_end = False |
| 277 |
|
| 278 |
async def wait_for_coro(): |
| 279 |
await asyncio.wait_for(inner_task, timeout=100) |
| 280 |
await asyncio.sleep(1) |
| 281 |
nonlocal reached_end |
| 282 |
reached_end = True |
| 283 |
|
| 284 |
task = asyncio.create_task(wait_for_coro()) |
| 285 |
self.assertFalse(task.done()) |
| 286 |
# Run the task |
| 287 |
await asyncio.sleep(0) |
| 288 |
task.cancel() |
| 289 |
with self.assertRaises(asyncio.CancelledError): |
| 290 |
await task |
| 291 |
self.assertTrue(inner_task.done()) |
| 292 |
self.assertEqual(await inner_task, 'done') |
| 293 |
self.assertFalse(reached_end) |
| 294 |
|
| 295 |
|
| 296 |
class WaitForShieldTests(unittest.IsolatedAsyncioTestCase): |
| 297 |
|
| 298 |
async def test_zero_timeout(self): |
| 299 |
# `asyncio.shield` creates a new task which wraps the passed in |
| 300 |
# awaitable and shields it from cancellation so with timeout=0 |
| 301 |
# the task returned by `asyncio.shield` aka shielded_task gets |
| 302 |
# cancelled immediately and the task wrapped by it is scheduled |
| 303 |
# to run. |
| 304 |
|
| 305 |
async def coro(): |
| 306 |
await asyncio.sleep(0.01) |
| 307 |
return 'done' |
| 308 |
|
| 309 |
task = asyncio.create_task(coro()) |
| 310 |
with self.assertRaises(asyncio.TimeoutError): |
| 311 |
shielded_task = asyncio.shield(task) |
| 312 |
await asyncio.wait_for(shielded_task, timeout=0) |
| 313 |
|
| 314 |
# Task is running in background |
| 315 |
self.assertFalse(task.done()) |
| 316 |
self.assertFalse(task.cancelled()) |
| 317 |
self.assertTrue(shielded_task.cancelled()) |
| 318 |
|
| 319 |
# Wait for the task to complete |
| 320 |
await asyncio.sleep(0.1) |
| 321 |
self.assertTrue(task.done()) |
| 322 |
|
| 323 |
|
| 324 |
async def test_none_timeout(self): |
| 325 |
# With timeout=None the timeout is disabled so it |
| 326 |
# runs till completion. |
| 327 |
async def coro(): |
| 328 |
await asyncio.sleep(0.1) |
| 329 |
return 'done' |
| 330 |
|
| 331 |
task = asyncio.create_task(coro()) |
| 332 |
await asyncio.wait_for(asyncio.shield(task), timeout=None) |
| 333 |
|
| 334 |
self.assertTrue(task.done()) |
| 335 |
self.assertEqual(await task, "done") |
| 336 |
|
| 337 |
async def test_shielded_timeout(self): |
| 338 |
# shield prevents the task from being cancelled. |
| 339 |
async def coro(): |
| 340 |
await asyncio.sleep(0.1) |
| 341 |
return 'done' |
| 342 |
|
| 343 |
task = asyncio.create_task(coro()) |
| 344 |
with self.assertRaises(asyncio.TimeoutError): |
| 345 |
await asyncio.wait_for(asyncio.shield(task), timeout=0.01) |
| 346 |
|
| 347 |
self.assertFalse(task.done()) |
| 348 |
self.assertFalse(task.cancelled()) |
| 349 |
self.assertEqual(await task, "done") |
| 350 |
|
| 351 |
|
| 352 |
if __name__ == '__main__': |
| 353 |
unittest.main() |