- Notifications
You must be signed in to change notification settings - Fork 31.7k
/
Copy pathtest_waitfor.py
353 lines (262 loc) · 10.6 KB
/
test_waitfor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
importasyncio
importunittest
importtime
fromtestimportsupport
deftearDownModule():
asyncio.set_event_loop_policy(None)
# The following value can be used as a very small timeout:
# it passes check "timeout > 0", but has almost
# no effect on the test performance
_EPSILON=0.0001
classSlowTask:
""" Task will run for this defined time, ignoring cancel requests """
TASK_TIMEOUT=0.2
def__init__(self):
self.exited=False
asyncdefrun(self):
exitat=time.monotonic() +self.TASK_TIMEOUT
whileTrue:
tosleep=exitat-time.monotonic()
iftosleep<=0:
break
try:
awaitasyncio.sleep(tosleep)
exceptasyncio.CancelledError:
pass
self.exited=True
classAsyncioWaitForTest(unittest.IsolatedAsyncioTestCase):
asyncdeftest_asyncio_wait_for_cancelled(self):
t=SlowTask()
waitfortask=asyncio.create_task(
asyncio.wait_for(t.run(), t.TASK_TIMEOUT*2))
awaitasyncio.sleep(0)
waitfortask.cancel()
awaitasyncio.wait({waitfortask})
self.assertTrue(t.exited)
asyncdeftest_asyncio_wait_for_timeout(self):
t=SlowTask()
try:
awaitasyncio.wait_for(t.run(), t.TASK_TIMEOUT/2)
exceptasyncio.TimeoutError:
pass
self.assertTrue(t.exited)
asyncdeftest_wait_for_timeout_less_then_0_or_0_future_done(self):
loop=asyncio.get_running_loop()
fut=loop.create_future()
fut.set_result('done')
ret=awaitasyncio.wait_for(fut, 0)
self.assertEqual(ret, 'done')
self.assertTrue(fut.done())
asyncdeftest_wait_for_timeout_less_then_0_or_0_coroutine_do_not_started(self):
foo_started=False
asyncdeffoo():
nonlocalfoo_started
foo_started=True
withself.assertRaises(asyncio.TimeoutError):
awaitasyncio.wait_for(foo(), 0)
self.assertEqual(foo_started, False)
asyncdeftest_wait_for_timeout_less_then_0_or_0(self):
loop=asyncio.get_running_loop()
fortimeoutin [0, -1]:
withself.subTest(timeout=timeout):
foo_running=None
started=loop.create_future()
asyncdeffoo():
nonlocalfoo_running
foo_running=True
started.set_result(None)
try:
awaitasyncio.sleep(10)
finally:
foo_running=False
return'done'
fut=asyncio.create_task(foo())
awaitstarted
withself.assertRaises(asyncio.TimeoutError):
awaitasyncio.wait_for(fut, timeout)
self.assertTrue(fut.done())
# it should have been cancelled due to the timeout
self.assertTrue(fut.cancelled())
self.assertEqual(foo_running, False)
asyncdeftest_wait_for(self):
foo_running=None
asyncdeffoo():
nonlocalfoo_running
foo_running=True
try:
awaitasyncio.sleep(support.LONG_TIMEOUT)
finally:
foo_running=False
return'done'
fut=asyncio.create_task(foo())
withself.assertRaises(asyncio.TimeoutError):
awaitasyncio.wait_for(fut, 0.1)
self.assertTrue(fut.done())
# it should have been cancelled due to the timeout
self.assertTrue(fut.cancelled())
self.assertEqual(foo_running, False)
asyncdeftest_wait_for_blocking(self):
asyncdefcoro():
return'done'
res=awaitasyncio.wait_for(coro(), timeout=None)
self.assertEqual(res, 'done')
asyncdeftest_wait_for_race_condition(self):
loop=asyncio.get_running_loop()
fut=loop.create_future()
task=asyncio.wait_for(fut, timeout=0.2)
loop.call_soon(fut.set_result, "ok")
res=awaittask
self.assertEqual(res, "ok")
asyncdeftest_wait_for_cancellation_race_condition(self):
asyncdefinner():
withself.assertRaises(asyncio.CancelledError):
awaitasyncio.sleep(1)
return1
result=awaitasyncio.wait_for(inner(), timeout=.01)
self.assertEqual(result, 1)
asyncdeftest_wait_for_waits_for_task_cancellation(self):
task_done=False
asyncdefinner():
nonlocaltask_done
try:
awaitasyncio.sleep(10)
exceptasyncio.CancelledError:
awaitasyncio.sleep(_EPSILON)
raise
finally:
task_done=True
inner_task=asyncio.create_task(inner())
withself.assertRaises(asyncio.TimeoutError) ascm:
awaitasyncio.wait_for(inner_task, timeout=_EPSILON)
self.assertTrue(task_done)
chained=cm.exception.__context__
self.assertEqual(type(chained), asyncio.CancelledError)
asyncdeftest_wait_for_waits_for_task_cancellation_w_timeout_0(self):
task_done=False
asyncdeffoo():
asyncdefinner():
nonlocaltask_done
try:
awaitasyncio.sleep(10)
exceptasyncio.CancelledError:
awaitasyncio.sleep(_EPSILON)
raise
finally:
task_done=True
inner_task=asyncio.create_task(inner())
awaitasyncio.sleep(_EPSILON)
awaitasyncio.wait_for(inner_task, timeout=0)
withself.assertRaises(asyncio.TimeoutError) ascm:
awaitfoo()
self.assertTrue(task_done)
chained=cm.exception.__context__
self.assertEqual(type(chained), asyncio.CancelledError)
asyncdeftest_wait_for_reraises_exception_during_cancellation(self):
classFooException(Exception):
pass
asyncdeffoo():
asyncdefinner():
try:
awaitasyncio.sleep(0.2)
finally:
raiseFooException
inner_task=asyncio.create_task(inner())
awaitasyncio.wait_for(inner_task, timeout=_EPSILON)
withself.assertRaises(FooException):
awaitfoo()
asyncdef_test_cancel_wait_for(self, timeout):
loop=asyncio.get_running_loop()
asyncdefblocking_coroutine():
fut=loop.create_future()
# Block: fut result is never set
awaitfut
task=asyncio.create_task(blocking_coroutine())
wait=asyncio.create_task(asyncio.wait_for(task, timeout))
loop.call_soon(wait.cancel)
withself.assertRaises(asyncio.CancelledError):
awaitwait
# Python issue #23219: cancelling the wait must also cancel the task
self.assertTrue(task.cancelled())
asyncdeftest_cancel_blocking_wait_for(self):
awaitself._test_cancel_wait_for(None)
asyncdeftest_cancel_wait_for(self):
awaitself._test_cancel_wait_for(60.0)
asyncdeftest_wait_for_cancel_suppressed(self):
# GH-86296: Suppressing CancelledError is discouraged
# but if a task suppresses CancelledError and returns a value,
# `wait_for` should return the value instead of raising CancelledError.
# This is the same behavior as `asyncio.timeout`.
asyncdefreturn_42():
try:
awaitasyncio.sleep(10)
exceptasyncio.CancelledError:
return42
res=awaitasyncio.wait_for(return_42(), timeout=0.1)
self.assertEqual(res, 42)
asyncdeftest_wait_for_issue86296(self):
# GH-86296: The task should get cancelled and not run to completion.
# inner completes in one cycle of the event loop so it
# completes before the task is cancelled.
asyncdefinner():
return'done'
inner_task=asyncio.create_task(inner())
reached_end=False
asyncdefwait_for_coro():
awaitasyncio.wait_for(inner_task, timeout=100)
awaitasyncio.sleep(1)
nonlocalreached_end
reached_end=True
task=asyncio.create_task(wait_for_coro())
self.assertFalse(task.done())
# Run the task
awaitasyncio.sleep(0)
task.cancel()
withself.assertRaises(asyncio.CancelledError):
awaittask
self.assertTrue(inner_task.done())
self.assertEqual(awaitinner_task, 'done')
self.assertFalse(reached_end)
classWaitForShieldTests(unittest.IsolatedAsyncioTestCase):
asyncdeftest_zero_timeout(self):
# `asyncio.shield` creates a new task which wraps the passed in
# awaitable and shields it from cancellation so with timeout=0
# the task returned by `asyncio.shield` aka shielded_task gets
# cancelled immediately and the task wrapped by it is scheduled
# to run.
asyncdefcoro():
awaitasyncio.sleep(0.01)
return'done'
task=asyncio.create_task(coro())
withself.assertRaises(asyncio.TimeoutError):
shielded_task=asyncio.shield(task)
awaitasyncio.wait_for(shielded_task, timeout=0)
# Task is running in background
self.assertFalse(task.done())
self.assertFalse(task.cancelled())
self.assertTrue(shielded_task.cancelled())
# Wait for the task to complete
awaitasyncio.sleep(0.1)
self.assertTrue(task.done())
asyncdeftest_none_timeout(self):
# With timeout=None the timeout is disabled so it
# runs till completion.
asyncdefcoro():
awaitasyncio.sleep(0.1)
return'done'
task=asyncio.create_task(coro())
awaitasyncio.wait_for(asyncio.shield(task), timeout=None)
self.assertTrue(task.done())
self.assertEqual(awaittask, "done")
asyncdeftest_shielded_timeout(self):
# shield prevents the task from being cancelled.
asyncdefcoro():
awaitasyncio.sleep(0.1)
return'done'
task=asyncio.create_task(coro())
withself.assertRaises(asyncio.TimeoutError):
awaitasyncio.wait_for(asyncio.shield(task), timeout=0.01)
self.assertFalse(task.done())
self.assertFalse(task.cancelled())
self.assertEqual(awaittask, "done")
if__name__=='__main__':
unittest.main()