|
| 1 | +"""Tests for asyncio/timeouts.py""" |
| 2 | + |
| 3 | +importunittest |
| 4 | +importtime |
| 5 | + |
| 6 | +importasyncio |
| 7 | +fromasyncioimporttasks |
| 8 | + |
| 9 | + |
| 10 | +deftearDownModule(): |
| 11 | +asyncio.set_event_loop_policy(None) |
| 12 | + |
| 13 | + |
| 14 | +classTimeoutTests(unittest.IsolatedAsyncioTestCase): |
| 15 | + |
| 16 | +asyncdeftest_timeout_basic(self): |
| 17 | +withself.assertRaises(TimeoutError): |
| 18 | +asyncwithasyncio.timeout(0.01) ascm: |
| 19 | +awaitasyncio.sleep(10) |
| 20 | +self.assertTrue(cm.expired()) |
| 21 | + |
| 22 | +asyncdeftest_timeout_at_basic(self): |
| 23 | +loop=asyncio.get_running_loop() |
| 24 | + |
| 25 | +withself.assertRaises(TimeoutError): |
| 26 | +deadline=loop.time() +0.01 |
| 27 | +asyncwithasyncio.timeout_at(deadline) ascm: |
| 28 | +awaitasyncio.sleep(10) |
| 29 | +self.assertTrue(cm.expired()) |
| 30 | +self.assertEqual(deadline, cm.when()) |
| 31 | + |
| 32 | +asyncdeftest_nested_timeouts(self): |
| 33 | +loop=asyncio.get_running_loop() |
| 34 | +cancelled=False |
| 35 | +withself.assertRaises(TimeoutError): |
| 36 | +deadline=loop.time() +0.01 |
| 37 | +asyncwithasyncio.timeout_at(deadline) ascm1: |
| 38 | +# Only the topmost context manager should raise TimeoutError |
| 39 | +try: |
| 40 | +asyncwithasyncio.timeout_at(deadline) ascm2: |
| 41 | +awaitasyncio.sleep(10) |
| 42 | +exceptasyncio.CancelledError: |
| 43 | +cancelled=True |
| 44 | +raise |
| 45 | +self.assertTrue(cancelled) |
| 46 | +self.assertTrue(cm1.expired()) |
| 47 | +self.assertTrue(cm2.expired()) |
| 48 | + |
| 49 | +asyncdeftest_waiter_cancelled(self): |
| 50 | +loop=asyncio.get_running_loop() |
| 51 | +cancelled=False |
| 52 | +withself.assertRaises(TimeoutError): |
| 53 | +asyncwithasyncio.timeout(0.01): |
| 54 | +try: |
| 55 | +awaitasyncio.sleep(10) |
| 56 | +exceptasyncio.CancelledError: |
| 57 | +cancelled=True |
| 58 | +raise |
| 59 | +self.assertTrue(cancelled) |
| 60 | + |
| 61 | +asyncdeftest_timeout_not_called(self): |
| 62 | +loop=asyncio.get_running_loop() |
| 63 | +t0=loop.time() |
| 64 | +asyncwithasyncio.timeout(10) ascm: |
| 65 | +awaitasyncio.sleep(0.01) |
| 66 | +t1=loop.time() |
| 67 | + |
| 68 | +self.assertFalse(cm.expired()) |
| 69 | +# 2 sec for slow CI boxes |
| 70 | +self.assertLess(t1-t0, 2) |
| 71 | +self.assertGreater(cm.when(), t1) |
| 72 | + |
| 73 | +asyncdeftest_timeout_disabled(self): |
| 74 | +loop=asyncio.get_running_loop() |
| 75 | +t0=loop.time() |
| 76 | +asyncwithasyncio.timeout(None) ascm: |
| 77 | +awaitasyncio.sleep(0.01) |
| 78 | +t1=loop.time() |
| 79 | + |
| 80 | +self.assertFalse(cm.expired()) |
| 81 | +self.assertIsNone(cm.when()) |
| 82 | +# 2 sec for slow CI boxes |
| 83 | +self.assertLess(t1-t0, 2) |
| 84 | + |
| 85 | +asyncdeftest_timeout_at_disabled(self): |
| 86 | +loop=asyncio.get_running_loop() |
| 87 | +t0=loop.time() |
| 88 | +asyncwithasyncio.timeout_at(None) ascm: |
| 89 | +awaitasyncio.sleep(0.01) |
| 90 | +t1=loop.time() |
| 91 | + |
| 92 | +self.assertFalse(cm.expired()) |
| 93 | +self.assertIsNone(cm.when()) |
| 94 | +# 2 sec for slow CI boxes |
| 95 | +self.assertLess(t1-t0, 2) |
| 96 | + |
| 97 | +asyncdeftest_timeout_zero(self): |
| 98 | +loop=asyncio.get_running_loop() |
| 99 | +t0=loop.time() |
| 100 | +withself.assertRaises(TimeoutError): |
| 101 | +asyncwithasyncio.timeout(0) ascm: |
| 102 | +awaitasyncio.sleep(10) |
| 103 | +t1=loop.time() |
| 104 | +self.assertTrue(cm.expired()) |
| 105 | +# 2 sec for slow CI boxes |
| 106 | +self.assertLess(t1-t0, 2) |
| 107 | +self.assertTrue(t0<=cm.when() <=t1) |
| 108 | + |
| 109 | +asyncdeftest_foreign_exception_passed(self): |
| 110 | +withself.assertRaises(KeyError): |
| 111 | +asyncwithasyncio.timeout(0.01) ascm: |
| 112 | +raiseKeyError |
| 113 | +self.assertFalse(cm.expired()) |
| 114 | + |
| 115 | +asyncdeftest_foreign_exception_on_timeout(self): |
| 116 | +asyncdefcrash(): |
| 117 | +try: |
| 118 | +awaitasyncio.sleep(1) |
| 119 | +finally: |
| 120 | +1/0 |
| 121 | +withself.assertRaises(ZeroDivisionError): |
| 122 | +asyncwithasyncio.timeout(0.01): |
| 123 | +awaitcrash() |
| 124 | + |
| 125 | +asyncdeftest_foreign_cancel_doesnt_timeout_if_not_expired(self): |
| 126 | +withself.assertRaises(asyncio.CancelledError): |
| 127 | +asyncwithasyncio.timeout(10) ascm: |
| 128 | +asyncio.current_task().cancel() |
| 129 | +awaitasyncio.sleep(10) |
| 130 | +self.assertFalse(cm.expired()) |
| 131 | + |
| 132 | +asyncdeftest_outer_task_is_not_cancelled(self): |
| 133 | +asyncdefouter() ->None: |
| 134 | +withself.assertRaises(TimeoutError): |
| 135 | +asyncwithasyncio.timeout(0.001): |
| 136 | +awaitasyncio.sleep(10) |
| 137 | + |
| 138 | +task=asyncio.create_task(outer()) |
| 139 | +awaittask |
| 140 | +self.assertFalse(task.cancelled()) |
| 141 | +self.assertTrue(task.done()) |
| 142 | + |
| 143 | +asyncdeftest_nested_timeouts_concurrent(self): |
| 144 | +withself.assertRaises(TimeoutError): |
| 145 | +asyncwithasyncio.timeout(0.002): |
| 146 | +withself.assertRaises(TimeoutError): |
| 147 | +asyncwithasyncio.timeout(0.1): |
| 148 | +# Pretend we crunch some numbers. |
| 149 | +time.sleep(0.01) |
| 150 | +awaitasyncio.sleep(1) |
| 151 | + |
| 152 | +asyncdeftest_nested_timeouts_loop_busy(self): |
| 153 | +# After the inner timeout is an expensive operation which should |
| 154 | +# be stopped by the outer timeout. |
| 155 | +loop=asyncio.get_running_loop() |
| 156 | +# Disable a message about long running task |
| 157 | +loop.slow_callback_duration=10 |
| 158 | +t0=loop.time() |
| 159 | +withself.assertRaises(TimeoutError): |
| 160 | +asyncwithasyncio.timeout(0.1): # (1) |
| 161 | +withself.assertRaises(TimeoutError): |
| 162 | +asyncwithasyncio.timeout(0.01): # (2) |
| 163 | +# Pretend the loop is busy for a while. |
| 164 | +time.sleep(0.1) |
| 165 | +awaitasyncio.sleep(1) |
| 166 | +# TimeoutError was cought by (2) |
| 167 | +awaitasyncio.sleep(10) # This sleep should be interrupted by (1) |
| 168 | +t1=loop.time() |
| 169 | +self.assertTrue(t0<=t1<=t0+1) |
| 170 | + |
| 171 | +asyncdeftest_reschedule(self): |
| 172 | +loop=asyncio.get_running_loop() |
| 173 | +fut=loop.create_future() |
| 174 | +deadline1=loop.time() +10 |
| 175 | +deadline2=deadline1+20 |
| 176 | + |
| 177 | +asyncdeff(): |
| 178 | +asyncwithasyncio.timeout_at(deadline1) ascm: |
| 179 | +fut.set_result(cm) |
| 180 | +awaitasyncio.sleep(50) |
| 181 | + |
| 182 | +task=asyncio.create_task(f()) |
| 183 | +cm=awaitfut |
| 184 | + |
| 185 | +self.assertEqual(cm.when(), deadline1) |
| 186 | +cm.reschedule(deadline2) |
| 187 | +self.assertEqual(cm.when(), deadline2) |
| 188 | +cm.reschedule(None) |
| 189 | +self.assertIsNone(cm.when()) |
| 190 | + |
| 191 | +task.cancel() |
| 192 | + |
| 193 | +withself.assertRaises(asyncio.CancelledError): |
| 194 | +awaittask |
| 195 | +self.assertFalse(cm.expired()) |
| 196 | + |
| 197 | +asyncdeftest_repr_active(self): |
| 198 | +asyncwithasyncio.timeout(10) ascm: |
| 199 | +self.assertRegex(repr(cm), r"<Timeout \[active\] when=\d+\.\d*>") |
| 200 | + |
| 201 | +asyncdeftest_repr_expired(self): |
| 202 | +withself.assertRaises(TimeoutError): |
| 203 | +asyncwithasyncio.timeout(0.01) ascm: |
| 204 | +awaitasyncio.sleep(10) |
| 205 | +self.assertEqual(repr(cm), "<Timeout [expired]>") |
| 206 | + |
| 207 | +asyncdeftest_repr_finished(self): |
| 208 | +asyncwithasyncio.timeout(10) ascm: |
| 209 | +awaitasyncio.sleep(0) |
| 210 | + |
| 211 | +self.assertEqual(repr(cm), "<Timeout [finished]>") |
| 212 | + |
| 213 | +asyncdeftest_repr_disabled(self): |
| 214 | +asyncwithasyncio.timeout(None) ascm: |
| 215 | +self.assertEqual(repr(cm), r"<Timeout [active] when=None>") |
| 216 | + |
| 217 | +asyncdeftest_nested_timeout_in_finally(self): |
| 218 | +withself.assertRaises(TimeoutError): |
| 219 | +asyncwithasyncio.timeout(0.01): |
| 220 | +try: |
| 221 | +awaitasyncio.sleep(1) |
| 222 | +finally: |
| 223 | +withself.assertRaises(TimeoutError): |
| 224 | +asyncwithasyncio.timeout(0.01): |
| 225 | +awaitasyncio.sleep(10) |
| 226 | + |
| 227 | + |
| 228 | +if__name__=='__main__': |
| 229 | +unittest.main() |
0 commit comments