| 1 |
"""Tests for asyncio/timeouts.py""" |
| 2 |
|
| 3 |
import unittest |
| 4 |
import time |
| 5 |
|
| 6 |
import asyncio |
| 7 |
|
| 8 |
from test.test_asyncio.utils import await_without_task |
| 9 |
|
| 10 |
|
| 11 |
def tearDownModule(): |
| 12 |
asyncio.events._set_event_loop_policy(None) |
| 13 |
|
| 14 |
class TimeoutTests(unittest.IsolatedAsyncioTestCase): |
| 15 |
|
| 16 |
async def test_timeout_basic(self): |
| 17 |
with self.assertRaises(TimeoutError): |
| 18 |
async with asyncio.timeout(0.01) as cm: |
| 19 |
await asyncio.sleep(10) |
| 20 |
self.assertTrue(cm.expired()) |
| 21 |
|
| 22 |
async def test_timeout_at_basic(self): |
| 23 |
loop = asyncio.get_running_loop() |
| 24 |
|
| 25 |
with self.assertRaises(TimeoutError): |
| 26 |
deadline = loop.time() + 0.01 |
| 27 |
async with asyncio.timeout_at(deadline) as cm: |
| 28 |
await asyncio.sleep(10) |
| 29 |
self.assertTrue(cm.expired()) |
| 30 |
self.assertEqual(deadline, cm.when()) |
| 31 |
|
| 32 |
async def test_nested_timeouts(self): |
| 33 |
loop = asyncio.get_running_loop() |
| 34 |
cancelled = False |
| 35 |
with self.assertRaises(TimeoutError): |
| 36 |
deadline = loop.time() + 0.01 |
| 37 |
async with asyncio.timeout_at(deadline) as cm1: |
| 38 |
# Only the topmost context manager should raise TimeoutError |
| 39 |
try: |
| 40 |
async with asyncio.timeout_at(deadline) as cm2: |
| 41 |
await asyncio.sleep(10) |
| 42 |
except asyncio.CancelledError: |
| 43 |
cancelled = True |
| 44 |
raise |
| 45 |
self.assertTrue(cancelled) |
| 46 |
self.assertTrue(cm1.expired()) |
| 47 |
self.assertTrue(cm2.expired()) |
| 48 |
|
| 49 |
async def test_waiter_cancelled(self): |
| 50 |
cancelled = False |
| 51 |
with self.assertRaises(TimeoutError): |
| 52 |
async with asyncio.timeout(0.01): |
| 53 |
try: |
| 54 |
await asyncio.sleep(10) |
| 55 |
except asyncio.CancelledError: |
| 56 |
cancelled = True |
| 57 |
raise |
| 58 |
self.assertTrue(cancelled) |
| 59 |
|
| 60 |
async def test_timeout_not_called(self): |
| 61 |
loop = asyncio.get_running_loop() |
| 62 |
async with asyncio.timeout(10) as cm: |
| 63 |
await asyncio.sleep(0.01) |
| 64 |
t1 = loop.time() |
| 65 |
|
| 66 |
self.assertFalse(cm.expired()) |
| 67 |
self.assertGreater(cm.when(), t1) |
| 68 |
|
| 69 |
async def test_timeout_disabled(self): |
| 70 |
async with asyncio.timeout(None) as cm: |
| 71 |
await asyncio.sleep(0.01) |
| 72 |
|
| 73 |
self.assertFalse(cm.expired()) |
| 74 |
self.assertIsNone(cm.when()) |
| 75 |
|
| 76 |
async def test_timeout_at_disabled(self): |
| 77 |
async with asyncio.timeout_at(None) as cm: |
| 78 |
await asyncio.sleep(0.01) |
| 79 |
|
| 80 |
self.assertFalse(cm.expired()) |
| 81 |
self.assertIsNone(cm.when()) |
| 82 |
|
| 83 |
async def test_timeout_zero(self): |
| 84 |
loop = asyncio.get_running_loop() |
| 85 |
t0 = loop.time() |
| 86 |
with self.assertRaises(TimeoutError): |
| 87 |
async with asyncio.timeout(0) as cm: |
| 88 |
await asyncio.sleep(10) |
| 89 |
t1 = loop.time() |
| 90 |
self.assertTrue(cm.expired()) |
| 91 |
self.assertTrue(t0 <= cm.when() <= t1) |
| 92 |
|
| 93 |
async def test_timeout_zero_sleep_zero(self): |
| 94 |
loop = asyncio.get_running_loop() |
| 95 |
t0 = loop.time() |
| 96 |
with self.assertRaises(TimeoutError): |
| 97 |
async with asyncio.timeout(0) as cm: |
| 98 |
await asyncio.sleep(0) |
| 99 |
t1 = loop.time() |
| 100 |
self.assertTrue(cm.expired()) |
| 101 |
self.assertTrue(t0 <= cm.when() <= t1) |
| 102 |
|
| 103 |
async def test_timeout_in_the_past_sleep_zero(self): |
| 104 |
loop = asyncio.get_running_loop() |
| 105 |
t0 = loop.time() |
| 106 |
with self.assertRaises(TimeoutError): |
| 107 |
async with asyncio.timeout(-11) as cm: |
| 108 |
await asyncio.sleep(0) |
| 109 |
t1 = loop.time() |
| 110 |
self.assertTrue(cm.expired()) |
| 111 |
self.assertTrue(t0 >= cm.when() <= t1) |
| 112 |
|
| 113 |
async def test_foreign_exception_passed(self): |
| 114 |
with self.assertRaises(KeyError): |
| 115 |
async with asyncio.timeout(0.01) as cm: |
| 116 |
raise KeyError |
| 117 |
self.assertFalse(cm.expired()) |
| 118 |
|
| 119 |
async def test_timeout_exception_context(self): |
| 120 |
with self.assertRaises(TimeoutError) as cm: |
| 121 |
async with asyncio.timeout(0.01): |
| 122 |
try: |
| 123 |
1/0 |
| 124 |
finally: |
| 125 |
await asyncio.sleep(1) |
| 126 |
e = cm.exception |
| 127 |
# Expect TimeoutError caused by CancelledError raised during handling |
| 128 |
# of ZeroDivisionError. |
| 129 |
e2 = e.__cause__ |
| 130 |
self.assertIsInstance(e2, asyncio.CancelledError) |
| 131 |
self.assertIs(e.__context__, e2) |
| 132 |
self.assertIsNone(e2.__cause__) |
| 133 |
self.assertIsInstance(e2.__context__, ZeroDivisionError) |
| 134 |
|
| 135 |
async def test_foreign_exception_on_timeout(self): |
| 136 |
async def crash(): |
| 137 |
try: |
| 138 |
await asyncio.sleep(1) |
| 139 |
finally: |
| 140 |
1/0 |
| 141 |
with self.assertRaises(ZeroDivisionError) as cm: |
| 142 |
async with asyncio.timeout(0.01): |
| 143 |
await crash() |
| 144 |
e = cm.exception |
| 145 |
# Expect ZeroDivisionError raised during handling of TimeoutError |
| 146 |
# caused by CancelledError. |
| 147 |
self.assertIsNone(e.__cause__) |
| 148 |
e2 = e.__context__ |
| 149 |
self.assertIsInstance(e2, TimeoutError) |
| 150 |
e3 = e2.__cause__ |
| 151 |
self.assertIsInstance(e3, asyncio.CancelledError) |
| 152 |
self.assertIs(e2.__context__, e3) |
| 153 |
|
| 154 |
async def test_foreign_exception_on_timeout_2(self): |
| 155 |
with self.assertRaises(ZeroDivisionError) as cm: |
| 156 |
async with asyncio.timeout(0.01): |
| 157 |
try: |
| 158 |
try: |
| 159 |
raise ValueError |
| 160 |
finally: |
| 161 |
await asyncio.sleep(1) |
| 162 |
finally: |
| 163 |
try: |
| 164 |
raise KeyError |
| 165 |
finally: |
| 166 |
1/0 |
| 167 |
e = cm.exception |
| 168 |
# Expect ZeroDivisionError raised during handling of KeyError |
| 169 |
# raised during handling of TimeoutError caused by CancelledError. |
| 170 |
self.assertIsNone(e.__cause__) |
| 171 |
e2 = e.__context__ |
| 172 |
self.assertIsInstance(e2, KeyError) |
| 173 |
self.assertIsNone(e2.__cause__) |
| 174 |
e3 = e2.__context__ |
| 175 |
self.assertIsInstance(e3, TimeoutError) |
| 176 |
e4 = e3.__cause__ |
| 177 |
self.assertIsInstance(e4, asyncio.CancelledError) |
| 178 |
self.assertIsNone(e4.__cause__) |
| 179 |
self.assertIsInstance(e4.__context__, ValueError) |
| 180 |
self.assertIs(e3.__context__, e4) |
| 181 |
|
| 182 |
async def test_foreign_cancel_doesnt_timeout_if_not_expired(self): |
| 183 |
with self.assertRaises(asyncio.CancelledError): |
| 184 |
async with asyncio.timeout(10) as cm: |
| 185 |
asyncio.current_task().cancel() |
| 186 |
await asyncio.sleep(10) |
| 187 |
self.assertFalse(cm.expired()) |
| 188 |
|
| 189 |
async def test_outer_task_is_not_cancelled(self): |
| 190 |
async def outer() -> None: |
| 191 |
with self.assertRaises(TimeoutError): |
| 192 |
async with asyncio.timeout(0.001): |
| 193 |
await asyncio.sleep(10) |
| 194 |
|
| 195 |
task = asyncio.create_task(outer()) |
| 196 |
await task |
| 197 |
self.assertFalse(task.cancelled()) |
| 198 |
self.assertTrue(task.done()) |
| 199 |
|
| 200 |
async def test_nested_timeouts_concurrent(self): |
| 201 |
with self.assertRaises(TimeoutError): |
| 202 |
async with asyncio.timeout(0.002): |
| 203 |
with self.assertRaises(TimeoutError): |
| 204 |
async with asyncio.timeout(0.1): |
| 205 |
# Pretend we crunch some numbers. |
| 206 |
time.sleep(0.01) |
| 207 |
await asyncio.sleep(1) |
| 208 |
|
| 209 |
async def test_nested_timeouts_loop_busy(self): |
| 210 |
# After the inner timeout is an expensive operation which should |
| 211 |
# be stopped by the outer timeout. |
| 212 |
loop = asyncio.get_running_loop() |
| 213 |
# Disable a message about long running task |
| 214 |
loop.slow_callback_duration = 10 |
| 215 |
t0 = loop.time() |
| 216 |
with self.assertRaises(TimeoutError): |
| 217 |
async with asyncio.timeout(0.1): # (1) |
| 218 |
with self.assertRaises(TimeoutError): |
| 219 |
async with asyncio.timeout(0.01): # (2) |
| 220 |
# Pretend the loop is busy for a while. |
| 221 |
time.sleep(0.1) |
| 222 |
await asyncio.sleep(1) |
| 223 |
# TimeoutError was caught by (2) |
| 224 |
await asyncio.sleep(10) # This sleep should be interrupted by (1) |
| 225 |
t1 = loop.time() |
| 226 |
self.assertTrue(t0 <= t1 <= t0 + 1) |
| 227 |
|
| 228 |
async def test_reschedule(self): |
| 229 |
loop = asyncio.get_running_loop() |
| 230 |
fut = loop.create_future() |
| 231 |
deadline1 = loop.time() + 10 |
| 232 |
deadline2 = deadline1 + 20 |
| 233 |
|
| 234 |
async def f(): |
| 235 |
async with asyncio.timeout_at(deadline1) as cm: |
| 236 |
fut.set_result(cm) |
| 237 |
await asyncio.sleep(50) |
| 238 |
|
| 239 |
task = asyncio.create_task(f()) |
| 240 |
cm = await fut |
| 241 |
|
| 242 |
self.assertEqual(cm.when(), deadline1) |
| 243 |
cm.reschedule(deadline2) |
| 244 |
self.assertEqual(cm.when(), deadline2) |
| 245 |
cm.reschedule(None) |
| 246 |
self.assertIsNone(cm.when()) |
| 247 |
|
| 248 |
task.cancel() |
| 249 |
|
| 250 |
with self.assertRaises(asyncio.CancelledError): |
| 251 |
await task |
| 252 |
self.assertFalse(cm.expired()) |
| 253 |
|
| 254 |
async def test_repr_active(self): |
| 255 |
async with asyncio.timeout(10) as cm: |
| 256 |
self.assertRegex(repr(cm), r"<Timeout \[active\] when=\d+\.\d*>") |
| 257 |
|
| 258 |
async def test_repr_expired(self): |
| 259 |
with self.assertRaises(TimeoutError): |
| 260 |
async with asyncio.timeout(0.01) as cm: |
| 261 |
await asyncio.sleep(10) |
| 262 |
self.assertEqual(repr(cm), "<Timeout [expired]>") |
| 263 |
|
| 264 |
async def test_repr_finished(self): |
| 265 |
async with asyncio.timeout(10) as cm: |
| 266 |
await asyncio.sleep(0) |
| 267 |
|
| 268 |
self.assertEqual(repr(cm), "<Timeout [finished]>") |
| 269 |
|
| 270 |
async def test_repr_disabled(self): |
| 271 |
async with asyncio.timeout(None) as cm: |
| 272 |
self.assertEqual(repr(cm), r"<Timeout [active] when=None>") |
| 273 |
|
| 274 |
async def test_nested_timeout_in_finally(self): |
| 275 |
with self.assertRaises(TimeoutError) as cm1: |
| 276 |
async with asyncio.timeout(0.01): |
| 277 |
try: |
| 278 |
await asyncio.sleep(1) |
| 279 |
finally: |
| 280 |
with self.assertRaises(TimeoutError) as cm2: |
| 281 |
async with asyncio.timeout(0.01): |
| 282 |
await asyncio.sleep(10) |
| 283 |
e1 = cm1.exception |
| 284 |
# Expect TimeoutError caused by CancelledError. |
| 285 |
e12 = e1.__cause__ |
| 286 |
self.assertIsInstance(e12, asyncio.CancelledError) |
| 287 |
self.assertIsNone(e12.__cause__) |
| 288 |
self.assertIsNone(e12.__context__) |
| 289 |
self.assertIs(e1.__context__, e12) |
| 290 |
e2 = cm2.exception |
| 291 |
# Expect TimeoutError caused by CancelledError raised during |
| 292 |
# handling of other CancelledError (which is the same as in |
| 293 |
# the above chain). |
| 294 |
e22 = e2.__cause__ |
| 295 |
self.assertIsInstance(e22, asyncio.CancelledError) |
| 296 |
self.assertIsNone(e22.__cause__) |
| 297 |
self.assertIs(e22.__context__, e12) |
| 298 |
self.assertIs(e2.__context__, e22) |
| 299 |
|
| 300 |
async def test_timeout_after_cancellation(self): |
| 301 |
try: |
| 302 |
asyncio.current_task().cancel() |
| 303 |
await asyncio.sleep(1) # work which will be cancelled |
| 304 |
except asyncio.CancelledError: |
| 305 |
pass |
| 306 |
finally: |
| 307 |
with self.assertRaises(TimeoutError) as cm: |
| 308 |
async with asyncio.timeout(0.0): |
| 309 |
await asyncio.sleep(1) # some cleanup |
| 310 |
|
| 311 |
async def test_cancel_in_timeout_after_cancellation(self): |
| 312 |
try: |
| 313 |
asyncio.current_task().cancel() |
| 314 |
await asyncio.sleep(1) # work which will be cancelled |
| 315 |
except asyncio.CancelledError: |
| 316 |
pass |
| 317 |
finally: |
| 318 |
with self.assertRaises(asyncio.CancelledError): |
| 319 |
async with asyncio.timeout(1.0): |
| 320 |
asyncio.current_task().cancel() |
| 321 |
await asyncio.sleep(2) # some cleanup |
| 322 |
|
| 323 |
async def test_timeout_already_entered(self): |
| 324 |
async with asyncio.timeout(0.01) as cm: |
| 325 |
with self.assertRaisesRegex(RuntimeError, "has already been entered"): |
| 326 |
async with cm: |
| 327 |
pass |
| 328 |
|
| 329 |
async def test_timeout_double_enter(self): |
| 330 |
async with asyncio.timeout(0.01) as cm: |
| 331 |
pass |
| 332 |
with self.assertRaisesRegex(RuntimeError, "has already been entered"): |
| 333 |
async with cm: |
| 334 |
pass |
| 335 |
|
| 336 |
async def test_timeout_finished(self): |
| 337 |
async with asyncio.timeout(0.01) as cm: |
| 338 |
pass |
| 339 |
with self.assertRaisesRegex(RuntimeError, "finished"): |
| 340 |
cm.reschedule(0.02) |
| 341 |
|
| 342 |
async def test_timeout_expired(self): |
| 343 |
with self.assertRaises(TimeoutError): |
| 344 |
async with asyncio.timeout(0.01) as cm: |
| 345 |
await asyncio.sleep(1) |
| 346 |
with self.assertRaisesRegex(RuntimeError, "expired"): |
| 347 |
cm.reschedule(0.02) |
| 348 |
|
| 349 |
async def test_timeout_expiring(self): |
| 350 |
async with asyncio.timeout(0.01) as cm: |
| 351 |
with self.assertRaises(asyncio.CancelledError): |
| 352 |
await asyncio.sleep(1) |
| 353 |
with self.assertRaisesRegex(RuntimeError, "expiring"): |
| 354 |
cm.reschedule(0.02) |
| 355 |
|
| 356 |
async def test_timeout_not_entered(self): |
| 357 |
cm = asyncio.timeout(0.01) |
| 358 |
with self.assertRaisesRegex(RuntimeError, "has not been entered"): |
| 359 |
cm.reschedule(0.02) |
| 360 |
|
| 361 |
async def test_timeout_without_task(self): |
| 362 |
cm = asyncio.timeout(0.01) |
| 363 |
with self.assertRaisesRegex(RuntimeError, "task"): |
| 364 |
await await_without_task(cm.__aenter__()) |
| 365 |
with self.assertRaisesRegex(RuntimeError, "has not been entered"): |
| 366 |
cm.reschedule(0.02) |
| 367 |
|
| 368 |
async def test_timeout_taskgroup(self): |
| 369 |
async def task(): |
| 370 |
try: |
| 371 |
await asyncio.sleep(2) # Will be interrupted after 0.01 second |
| 372 |
finally: |
| 373 |
1/0 # Crash in cleanup |
| 374 |
|
| 375 |
with self.assertRaises(ExceptionGroup) as cm: |
| 376 |
async with asyncio.timeout(0.01): |
| 377 |
async with asyncio.TaskGroup() as tg: |
| 378 |
tg.create_task(task()) |
| 379 |
try: |
| 380 |
raise ValueError |
| 381 |
finally: |
| 382 |
await asyncio.sleep(1) |
| 383 |
eg = cm.exception |
| 384 |
# Expect ExceptionGroup raised during handling of TimeoutError caused |
| 385 |
# by CancelledError raised during handling of ValueError. |
| 386 |
self.assertIsNone(eg.__cause__) |
| 387 |
e_1 = eg.__context__ |
| 388 |
self.assertIsInstance(e_1, TimeoutError) |
| 389 |
e_2 = e_1.__cause__ |
| 390 |
self.assertIsInstance(e_2, asyncio.CancelledError) |
| 391 |
self.assertIsNone(e_2.__cause__) |
| 392 |
self.assertIsInstance(e_2.__context__, ValueError) |
| 393 |
self.assertIs(e_1.__context__, e_2) |
| 394 |
|
| 395 |
self.assertEqual(len(eg.exceptions), 1, eg) |
| 396 |
e1 = eg.exceptions[0] |
| 397 |
# Expect ZeroDivisionError raised during handling of TimeoutError |
| 398 |
# caused by CancelledError (it is a different CancelledError). |
| 399 |
self.assertIsInstance(e1, ZeroDivisionError) |
| 400 |
self.assertIsNone(e1.__cause__) |
| 401 |
e2 = e1.__context__ |
| 402 |
self.assertIsInstance(e2, TimeoutError) |
| 403 |
e3 = e2.__cause__ |
| 404 |
self.assertIsInstance(e3, asyncio.CancelledError) |
| 405 |
self.assertIsNone(e3.__context__) |
| 406 |
self.assertIsNone(e3.__cause__) |
| 407 |
self.assertIs(e2.__context__, e3) |
| 408 |
|
| 409 |
|
| 410 |
if __name__ == '__main__': |
| 411 |
unittest.main() |