summaryrefslogtreecommitdiff
path: root/dev-python/async_timeout/files/async_timeout-3.0.1-fix-py3.10.patch
blob: 5bfdcf2e6daba9121c9be6c7130205aa67f5451f (plain)
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
diff --git a/tests/test_py35.py b/tests/test_py35.py
index 00bb7f0..7d88d99 100644
--- a/tests/test_py35.py
+++ b/tests/test_py35.py
@@ -4,30 +4,32 @@ import pytest
 
 from async_timeout import timeout
 
-pytestmark = pytest.mark.asyncio
 
-
-async def test_async_timeout(loop):
+@pytest.mark.asyncio
+async def test_async_timeout():
     with pytest.raises(asyncio.TimeoutError):
-        async with timeout(0.01, loop=loop) as cm:
-            await asyncio.sleep(10, loop=loop)
+        async with timeout(0.01) as cm:
+            await asyncio.sleep(10)
     assert cm.expired
 
 
-async def test_async_no_timeout(loop):
-    async with timeout(1, loop=loop) as cm:
-        await asyncio.sleep(0, loop=loop)
+@pytest.mark.asyncio
+async def test_async_no_timeout():
+    async with timeout(1) as cm:
+        await asyncio.sleep(0)
     assert not cm.expired
 
 
-async def test_async_zero(loop):
+@pytest.mark.asyncio
+async def test_async_zero():
     with pytest.raises(asyncio.TimeoutError):
-        async with timeout(0, loop=loop) as cm:
-            await asyncio.sleep(10, loop=loop)
+        async with timeout(0) as cm:
+            await asyncio.sleep(10)
     assert cm.expired
 
 
-async def test_async_zero_coro_not_started(loop):
+@pytest.mark.asyncio
+async def test_async_zero_coro_not_started():
     coro_started = False
 
     async def coro():
@@ -35,8 +37,8 @@ async def test_async_zero_coro_not_started(loop):
         coro_started = True
 
     with pytest.raises(asyncio.TimeoutError):
-        async with timeout(0, loop=loop) as cm:
-            await asyncio.sleep(0, loop=loop)
+        async with timeout(0) as cm:
+            await asyncio.sleep(0)
             await coro()
 
     assert cm.expired
diff --git a/tests/test_timeout.py b/tests/test_timeout.py
index 8915546..b1cb3c7 100644
--- a/tests/test_timeout.py
+++ b/tests/test_timeout.py
@@ -6,89 +6,69 @@ import pytest
 
 from async_timeout import timeout
 
-from asyncio import ensure_future
-
-
-def create_future(loop):
-    """Compatibility wrapper for the loop.create_future() call introduced in
-    3.5.2."""
-    if hasattr(loop, 'create_future'):
-        return loop.create_future()
-    else:
-        return asyncio.Future(loop=loop)
-
 
 @pytest.mark.asyncio
-async def test_timeout(loop):
+async def test_timeout():
     canceled_raised = False
 
     async def long_running_task():
         try:
-            await asyncio.sleep(10, loop=loop)
+            await asyncio.sleep(10)
         except asyncio.CancelledError:
             nonlocal canceled_raised
             canceled_raised = True
             raise
 
     with pytest.raises(asyncio.TimeoutError):
-        with timeout(0.01, loop=loop) as t:
+        with timeout(0.01) as t:
             await long_running_task()
-            assert t._loop is loop
+            assert t._loop is asyncio.get_event_loop()
     assert canceled_raised, 'CancelledError was not raised'
 
 
 @pytest.mark.asyncio
-async def test_timeout_finish_in_time(loop):
+async def test_timeout_finish_in_time():
     async def long_running_task():
-        await asyncio.sleep(0.01, loop=loop)
+        await asyncio.sleep(0.01)
         return 'done'
 
-    with timeout(0.1, loop=loop):
+    with timeout(0.1):
         resp = await long_running_task()
     assert resp == 'done'
 
 
-def test_timeout_global_loop(loop):
-    asyncio.set_event_loop(loop)
-
-    async def run():
-        with timeout(10) as t:
-            await asyncio.sleep(0.01)
-            assert t._loop is loop
-
-    loop.run_until_complete(run())
-
-
 @pytest.mark.asyncio
-async def test_timeout_disable(loop):
+async def test_timeout_disable():
     async def long_running_task():
-        await asyncio.sleep(0.1, loop=loop)
+        await asyncio.sleep(0.1)
         return 'done'
 
+    loop = asyncio.get_event_loop()
     t0 = loop.time()
-    with timeout(None, loop=loop):
+    with timeout(None):
         resp = await long_running_task()
     assert resp == 'done'
     dt = loop.time() - t0
     assert 0.09 < dt < 0.13, dt
 
 
-def test_timeout_is_none_no_task(loop):
+def test_timeout_is_none_no_task():
+    loop = asyncio.get_event_loop()
     with timeout(None, loop=loop) as cm:
         assert cm._task is None
 
 
 @pytest.mark.asyncio
-async def test_timeout_enable_zero(loop):
+async def test_timeout_enable_zero():
     with pytest.raises(asyncio.TimeoutError):
-        with timeout(0, loop=loop) as cm:
-            await asyncio.sleep(0.1, loop=loop)
+        with timeout(0) as cm:
+            await asyncio.sleep(0.1)
 
     assert cm.expired
 
 
 @pytest.mark.asyncio
-async def test_timeout_enable_zero_coro_not_started(loop):
+async def test_timeout_enable_zero_coro_not_started():
     coro_started = False
 
     async def coro():
@@ -96,8 +76,8 @@ async def test_timeout_enable_zero_coro_not_started(loop):
         coro_started = True
 
     with pytest.raises(asyncio.TimeoutError):
-        with timeout(0, loop=loop) as cm:
-            await asyncio.sleep(0, loop=loop)
+        with timeout(0) as cm:
+            await asyncio.sleep(0)
             await coro()
 
     assert cm.expired
@@ -105,51 +85,52 @@ async def test_timeout_enable_zero_coro_not_started(loop):
 
 
 @pytest.mark.asyncio
-async def test_timeout_not_relevant_exception(loop):
-    await asyncio.sleep(0, loop=loop)
+async def test_timeout_not_relevant_exception():
+    await asyncio.sleep(0)
     with pytest.raises(KeyError):
-        with timeout(0.1, loop=loop):
+        with timeout(0.1):
             raise KeyError
 
 
 @pytest.mark.asyncio
-async def test_timeout_canceled_error_is_not_converted_to_timeout(loop):
-    await asyncio.sleep(0, loop=loop)
+async def test_timeout_canceled_error_is_not_converted_to_timeout():
+    await asyncio.sleep(0)
     with pytest.raises(asyncio.CancelledError):
-        with timeout(0.001, loop=loop):
+        with timeout(0.001):
             raise asyncio.CancelledError
 
 
 @pytest.mark.asyncio
-async def test_timeout_blocking_loop(loop):
+async def test_timeout_blocking_loop():
     async def long_running_task():
         time.sleep(0.1)
         return 'done'
 
-    with timeout(0.01, loop=loop):
+    with timeout(0.01):
         result = await long_running_task()
     assert result == 'done'
 
 
 @pytest.mark.asyncio
-async def test_for_race_conditions(loop):
-    fut = create_future(loop)
+async def test_for_race_conditions():
+    loop = asyncio.get_event_loop()
+    fut = loop.create_future()
     loop.call_later(0.1, fut.set_result('done'))
-    with timeout(0.2, loop=loop):
+    with timeout(0.2):
         resp = await fut
     assert resp == 'done'
 
 
 @pytest.mark.asyncio
-async def test_timeout_time(loop):
+async def test_timeout_time():
     foo_running = None
-
+    loop = asyncio.get_event_loop()
     start = loop.time()
     with pytest.raises(asyncio.TimeoutError):
-        with timeout(0.1, loop=loop):
+        with timeout(0.1):
             foo_running = True
             try:
-                await asyncio.sleep(0.2, loop=loop)
+                await asyncio.sleep(0.2)
             finally:
                 foo_running = False
 
@@ -160,26 +141,26 @@ async def test_timeout_time(loop):
     assert not foo_running
 
 
-def test_raise_runtimeerror_if_no_task(loop):
+def test_raise_runtimeerror_if_no_task():
     with pytest.raises(RuntimeError):
-        with timeout(0.1, loop=loop):
+        with timeout(0.1):
             pass
 
 
 @pytest.mark.asyncio
-async def test_outer_coro_is_not_cancelled(loop):
+async def test_outer_coro_is_not_cancelled():
 
     has_timeout = False
 
     async def outer():
         nonlocal has_timeout
         try:
-            with timeout(0.001, loop=loop):
-                await asyncio.sleep(1, loop=loop)
+            with timeout(0.001):
+                await asyncio.sleep(1)
         except asyncio.TimeoutError:
             has_timeout = True
 
-    task = ensure_future(outer(), loop=loop)
+    task = asyncio.ensure_future(outer())
     await task
     assert has_timeout
     assert not task.cancelled()
@@ -187,14 +168,15 @@ async def test_outer_coro_is_not_cancelled(loop):
 
 
 @pytest.mark.asyncio
-async def test_cancel_outer_coro(loop):
-    fut = create_future(loop)
+async def test_cancel_outer_coro():
+    loop = asyncio.get_event_loop()
+    fut = loop.create_future()
 
     async def outer():
         fut.set_result(None)
-        await asyncio.sleep(1, loop=loop)
+        await asyncio.sleep(1)
 
-    task = ensure_future(outer(), loop=loop)
+    task = asyncio.ensure_future(outer())
     await fut
     task.cancel()
     with pytest.raises(asyncio.CancelledError):
@@ -204,57 +186,64 @@ async def test_cancel_outer_coro(loop):
 
 
 @pytest.mark.asyncio
-async def test_timeout_suppress_exception_chain(loop):
+async def test_timeout_suppress_exception_chain():
     with pytest.raises(asyncio.TimeoutError) as ctx:
-        with timeout(0.01, loop=loop):
-            await asyncio.sleep(10, loop=loop)
+        with timeout(0.01):
+            await asyncio.sleep(10)
     assert not ctx.value.__suppress_context__
 
 
 @pytest.mark.asyncio
-async def test_timeout_expired(loop):
+async def test_timeout_expired():
     with pytest.raises(asyncio.TimeoutError):
-        with timeout(0.01, loop=loop) as cm:
-            await asyncio.sleep(10, loop=loop)
+        with timeout(0.01) as cm:
+            await asyncio.sleep(10)
     assert cm.expired
 
 
 @pytest.mark.asyncio
-async def test_timeout_inner_timeout_error(loop):
+async def test_timeout_inner_timeout_error():
     with pytest.raises(asyncio.TimeoutError):
-        with timeout(0.01, loop=loop) as cm:
+        with timeout(0.01) as cm:
             raise asyncio.TimeoutError
     assert not cm.expired
 
 
 @pytest.mark.asyncio
-async def test_timeout_inner_other_error(loop):
+async def test_timeout_inner_other_error():
     with pytest.raises(RuntimeError):
-        with timeout(0.01, loop=loop) as cm:
+        with timeout(0.01) as cm:
             raise RuntimeError
     assert not cm.expired
 
 
 @pytest.mark.asyncio
-async def test_timeout_remaining(loop):
-    with timeout(None, loop=loop) as cm:
+async def test_timeout_remaining():
+    with timeout(None) as cm:
         assert cm.remaining is None
+    assert cm.remaining is None
+
+    t = timeout(None)
+    assert t.remaining is None
 
-    t = timeout(1.0, loop=loop)
+    t = timeout(1.0)
     assert t.remaining is None
 
-    with timeout(1.0, loop=loop) as cm:
-        await asyncio.sleep(0.1, loop=loop)
+    with timeout(1.0) as cm:
+        await asyncio.sleep(0.1)
         assert cm.remaining < 1.0
+    r = cm.remaining
+    time.sleep(0.1)
+    assert abs(r - cm.remaining) < 1.0
 
     with pytest.raises(asyncio.TimeoutError):
-        with timeout(0.1, loop=loop) as cm:
-            await asyncio.sleep(0.5, loop=loop)
+        with timeout(0.1) as cm:
+            await asyncio.sleep(0.5)
 
     assert cm.remaining == 0.0
 
 
-def test_cancel_without_starting(loop):
-    tm = timeout(1, loop=loop)
+def test_cancel_without_starting():
+    tm = timeout(1)
     tm._cancel_task()
     tm._cancel_task()  # double call should success