Skip to content

Commit 887fcfd

Browse files
committed
More tests
1 parent 4ba6b9f commit 887fcfd

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed

Lib/test/test_asyncio/test_timeouts.py

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@ async def test_timeout_at_basic(self):
2626
loop = asyncio.get_running_loop()
2727

2828
with self.assertRaises(TimeoutError):
29-
async with asyncio.timeout_at(loop.time() + 0.01) as cm:
29+
deadline = loop.time() + 0.01
30+
async with asyncio.timeout_at(deadline) as cm:
3031
await asyncio.sleep(10)
3132
self.assertTrue(cm.expired())
33+
self.assertEqual(deadline, cm.deadline)
3234

3335
async def test_nested_timeouts(self):
3436
cancel = False
@@ -75,6 +77,7 @@ async def test_timeout_disabled(self):
7577
t1 = loop.time()
7678

7779
self.assertFalse(cm.expired())
80+
self.assertIsNone(cm.deadline)
7881
# finised fast. Very busy CI box requires high enough limit,
7982
# that's why 0.01 cannot be used
8083
self.assertLess(t1-t0, 2)
@@ -87,10 +90,53 @@ async def test_timeout_at_disabled(self):
8790
t1 = loop.time()
8891

8992
self.assertFalse(cm.expired())
93+
self.assertIsNone(cm.deadline)
9094
# finised fast. Very busy CI box requires high enough limit,
9195
# that's why 0.01 cannot be used
9296
self.assertLess(t1-t0, 2)
9397

98+
async def test_timeout_zero(self):
99+
loop = asyncio.get_running_loop()
100+
t0 = loop.time()
101+
with self.assertRaises(TimeoutError):
102+
async with asyncio.timeout(0) as cm:
103+
await asyncio.sleep(10)
104+
t1 = loop.time()
105+
self.assertTrue(cm.expired())
106+
# finised fast. Very busy CI box requires high enough limit,
107+
# that's why 0.01 cannot be used
108+
self.assertLess(t1-t0, 2)
109+
110+
async def test_foreign_exception_passed(self):
111+
with self.assertRaises(KeyError):
112+
async with asyncio.timeout(0.01) as cm:
113+
raise KeyError
114+
self.assertFalse(cm.expired())
115+
116+
async def test_foreign_cancel_doesnt_timeout_if_not_expired(self):
117+
with self.assertRaises(asyncio.CancelledError):
118+
async with asyncio.timeout(10) as cm:
119+
raise asyncio.CancelledError
120+
self.assertFalse(cm.expired())
121+
122+
async def test_outer_task_is_not_cancelled(self):
123+
124+
has_timeout = False
125+
126+
async def outer() -> None:
127+
nonlocal has_timeout
128+
try:
129+
async with asyncio.timeout(0.001):
130+
await asyncio.sleep(1)
131+
except asyncio.TimeoutError:
132+
has_timeout = True
133+
134+
task = asyncio.create_task(outer())
135+
await task
136+
assert has_timeout
137+
assert not task.cancelled()
138+
assert task.done()
139+
94140

95141
@unittest.skipUnless(hasattr(tasks, '_CTask'),
96142
'requires the C _asyncio module')

0 commit comments

Comments
 (0)