|
5 | 5 |
|
6 | 6 | import asyncio
|
7 | 7 |
|
| 8 | +from test.test_asyncio.utils import await_without_task |
| 9 | + |
8 | 10 |
|
9 | 11 | def tearDownModule():
|
10 | 12 | asyncio.set_event_loop_policy(None)
|
11 | 13 |
|
12 |
| - |
13 | 14 | class TimeoutTests(unittest.IsolatedAsyncioTestCase):
|
14 | 15 |
|
15 | 16 | async def test_timeout_basic(self):
|
@@ -257,6 +258,51 @@ async def test_timeout_exception_cause (self):
|
257 | 258 | cause = exc.exception.__cause__
|
258 | 259 | assert isinstance(cause, asyncio.CancelledError)
|
259 | 260 |
|
| 261 | + async def test_timeout_already_entered(self): |
| 262 | + async with asyncio.timeout(0.01) as cm: |
| 263 | + with self.assertRaisesRegex(RuntimeError, "has already been entered"): |
| 264 | + async with cm: |
| 265 | + pass |
| 266 | + |
| 267 | + async def test_timeout_double_enter(self): |
| 268 | + async with asyncio.timeout(0.01) as cm: |
| 269 | + pass |
| 270 | + with self.assertRaisesRegex(RuntimeError, "has already been entered"): |
| 271 | + async with cm: |
| 272 | + pass |
| 273 | + |
| 274 | + async def test_timeout_finished(self): |
| 275 | + async with asyncio.timeout(0.01) as cm: |
| 276 | + pass |
| 277 | + with self.assertRaisesRegex(RuntimeError, "finished"): |
| 278 | + cm.reschedule(0.02) |
| 279 | + |
| 280 | + async def test_timeout_expired(self): |
| 281 | + with self.assertRaises(TimeoutError): |
| 282 | + async with asyncio.timeout(0.01) as cm: |
| 283 | + await asyncio.sleep(1) |
| 284 | + with self.assertRaisesRegex(RuntimeError, "expired"): |
| 285 | + cm.reschedule(0.02) |
| 286 | + |
| 287 | + async def test_timeout_expiring(self): |
| 288 | + async with asyncio.timeout(0.01) as cm: |
| 289 | + with self.assertRaises(asyncio.CancelledError): |
| 290 | + await asyncio.sleep(1) |
| 291 | + with self.assertRaisesRegex(RuntimeError, "expiring"): |
| 292 | + cm.reschedule(0.02) |
| 293 | + |
| 294 | + async def test_timeout_not_entered(self): |
| 295 | + cm = asyncio.timeout(0.01) |
| 296 | + with self.assertRaisesRegex(RuntimeError, "has not been entered"): |
| 297 | + cm.reschedule(0.02) |
| 298 | + |
| 299 | + async def test_timeout_without_task(self): |
| 300 | + cm = asyncio.timeout(0.01) |
| 301 | + with self.assertRaisesRegex(RuntimeError, "task"): |
| 302 | + await await_without_task(cm.__aenter__()) |
| 303 | + with self.assertRaisesRegex(RuntimeError, "has not been entered"): |
| 304 | + cm.reschedule(0.02) |
| 305 | + |
260 | 306 |
|
261 | 307 | if __name__ == '__main__':
|
262 | 308 | unittest.main()
|
0 commit comments