@@ -26,9 +26,11 @@ async def test_timeout_at_basic(self):
26
26
loop = asyncio .get_running_loop ()
27
27
28
28
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 :
30
31
await asyncio .sleep (10 )
31
32
self .assertTrue (cm .expired ())
33
+ self .assertEqual (deadline , cm .deadline )
32
34
33
35
async def test_nested_timeouts (self ):
34
36
cancel = False
@@ -75,6 +77,7 @@ async def test_timeout_disabled(self):
75
77
t1 = loop .time ()
76
78
77
79
self .assertFalse (cm .expired ())
80
+ self .assertIsNone (cm .deadline )
78
81
# finised fast. Very busy CI box requires high enough limit,
79
82
# that's why 0.01 cannot be used
80
83
self .assertLess (t1 - t0 , 2 )
@@ -87,10 +90,53 @@ async def test_timeout_at_disabled(self):
87
90
t1 = loop .time ()
88
91
89
92
self .assertFalse (cm .expired ())
93
+ self .assertIsNone (cm .deadline )
90
94
# finised fast. Very busy CI box requires high enough limit,
91
95
# that's why 0.01 cannot be used
92
96
self .assertLess (t1 - t0 , 2 )
93
97
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
+
94
140
95
141
@unittest .skipUnless (hasattr (tasks , '_CTask' ),
96
142
'requires the C _asyncio module' )
0 commit comments