Skip to content

Commit eb8d7ba

Browse files
committed
tests: Stop passing loop explicitly in tests
In 3.8 a lot of asyncio functions have their `loop` parameter deprecated. So we change the semantics of uvloop tests to never pass the loop explicitly (unless to Future objects, when necessary) That means that we now also need to set up asyncio/uvloop loop policies for tests in setUp hooks.
1 parent 65c1a04 commit eb8d7ba

14 files changed

+221
-343
lines changed

tests/test_base.py

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def test_close(self):
2727
self.loop.close()
2828

2929
# operation blocked when the loop is closed
30-
f = asyncio.Future(loop=self.loop)
30+
f = asyncio.Future()
3131
self.assertRaises(RuntimeError, self.loop.run_forever)
3232
self.assertRaises(RuntimeError, self.loop.run_until_complete, f)
3333

@@ -99,7 +99,7 @@ def cb():
9999

100100
meth(cb)
101101
self.assertIsNone(context)
102-
self.loop.run_until_complete(asyncio.sleep(0.05, loop=self.loop))
102+
self.loop.run_until_complete(asyncio.sleep(0.05))
103103

104104
self.assertIs(type(context['exception']), ZeroDivisionError)
105105
self.assertTrue(context['message'].startswith(
@@ -167,9 +167,9 @@ def test_call_later_2(self):
167167
# libuv cached time.
168168

169169
async def main():
170-
await asyncio.sleep(0.001, loop=self.loop)
170+
await asyncio.sleep(0.001)
171171
time.sleep(0.01)
172-
await asyncio.sleep(0.01, loop=self.loop)
172+
await asyncio.sleep(0.01)
173173

174174
started = time.monotonic()
175175
self.loop.run_until_complete(main())
@@ -331,7 +331,7 @@ def test_run_until_complete_type_error(self):
331331
TypeError, self.loop.run_until_complete, 'blah')
332332

333333
def test_run_until_complete_loop(self):
334-
task = asyncio.Future(loop=self.loop)
334+
task = asyncio.Future()
335335
other_loop = self.new_loop()
336336
self.addCleanup(other_loop.close)
337337
self.assertRaises(
@@ -351,7 +351,7 @@ class ShowStopper(BaseException):
351351
pass
352352

353353
async def foo(delay):
354-
await asyncio.sleep(delay, loop=self.loop)
354+
await asyncio.sleep(delay)
355355

356356
def throw():
357357
raise ShowStopper
@@ -373,7 +373,7 @@ def test_debug_slow_callbacks(self):
373373
self.loop.call_soon(lambda: time.sleep(0.3))
374374

375375
with mock.patch.object(logger, 'warning') as log:
376-
self.loop.run_until_complete(asyncio.sleep(0, loop=self.loop))
376+
self.loop.run_until_complete(asyncio.sleep(0))
377377

378378
self.assertEqual(log.call_count, 1)
379379
# format message
@@ -389,7 +389,7 @@ def test_debug_slow_timer_callbacks(self):
389389
self.loop.call_later(0.01, lambda: time.sleep(0.3))
390390

391391
with mock.patch.object(logger, 'warning') as log:
392-
self.loop.run_until_complete(asyncio.sleep(0.02, loop=self.loop))
392+
self.loop.run_until_complete(asyncio.sleep(0.02))
393393

394394
self.assertEqual(log.call_count, 1)
395395
# format message
@@ -429,7 +429,7 @@ def zero_error(fut):
429429

430430
# Test call_soon (events.Handle)
431431
with mock.patch.object(logger, 'error') as log:
432-
fut = asyncio.Future(loop=self.loop)
432+
fut = asyncio.Future()
433433
self.loop.call_soon(zero_error, fut)
434434
fut.add_done_callback(lambda fut: self.loop.stop())
435435
self.loop.run_forever()
@@ -439,7 +439,7 @@ def zero_error(fut):
439439

440440
# Test call_later (events.TimerHandle)
441441
with mock.patch.object(logger, 'error') as log:
442-
fut = asyncio.Future(loop=self.loop)
442+
fut = asyncio.Future()
443443
self.loop.call_later(0.01, zero_error, fut)
444444
fut.add_done_callback(lambda fut: self.loop.stop())
445445
self.loop.run_forever()
@@ -562,31 +562,34 @@ def test_shutdown_asyncgens_01(self):
562562
raise unittest.SkipTest()
563563

564564
waiter = self._compile_agen(
565-
'''async def waiter(timeout, finalized, loop):
565+
'''async def waiter(timeout, finalized):
566566
try:
567-
await asyncio.sleep(timeout, loop=loop)
567+
await asyncio.sleep(timeout)
568568
yield 1
569569
finally:
570-
await asyncio.sleep(0, loop=loop)
570+
await asyncio.sleep(0)
571571
finalized.append(1)
572572
''')
573573

574574
async def wait():
575-
async for _ in waiter(1, finalized, self.loop):
575+
async for _ in waiter(1, finalized):
576576
pass
577577

578578
t1 = self.loop.create_task(wait())
579579
t2 = self.loop.create_task(wait())
580580

581-
self.loop.run_until_complete(asyncio.sleep(0.1, loop=self.loop))
581+
self.loop.run_until_complete(asyncio.sleep(0.1))
582582

583+
t1.cancel()
584+
t2.cancel()
583585
self.loop.run_until_complete(self.loop.shutdown_asyncgens())
584586
self.assertEqual(finalized, [1, 1])
585587

586-
# Silence warnings
587-
t1.cancel()
588-
t2.cancel()
589-
self.loop.run_until_complete(asyncio.sleep(0.1, loop=self.loop))
588+
for t in {t1, t2}:
589+
try:
590+
self.loop.run_until_complete(t)
591+
except asyncio.CancelledError:
592+
pass
590593

591594
def test_shutdown_asyncgens_02(self):
592595
if not hasattr(self.loop, 'shutdown_asyncgens'):
@@ -601,20 +604,20 @@ def logger(loop, context):
601604
if expected in context['message']:
602605
logged += 1
603606

604-
waiter = self._compile_agen('''async def waiter(timeout, loop):
607+
waiter = self._compile_agen('''async def waiter(timeout):
605608
try:
606-
await asyncio.sleep(timeout, loop=loop)
609+
await asyncio.sleep(timeout)
607610
yield 1
608611
finally:
609612
1 / 0
610613
''')
611614

612615
async def wait():
613-
async for _ in waiter(1, self.loop):
616+
async for _ in waiter(1):
614617
pass
615618

616619
t = self.loop.create_task(wait())
617-
self.loop.run_until_complete(asyncio.sleep(0.1, loop=self.loop))
620+
self.loop.run_until_complete(asyncio.sleep(0.1))
618621

619622
self.loop.set_exception_handler(logger)
620623
self.loop.run_until_complete(self.loop.shutdown_asyncgens())
@@ -623,7 +626,7 @@ async def wait():
623626

624627
# Silence warnings
625628
t.cancel()
626-
self.loop.run_until_complete(asyncio.sleep(0.1, loop=self.loop))
629+
self.loop.run_until_complete(asyncio.sleep(0.1))
627630

628631
def test_shutdown_asyncgens_03(self):
629632
if not hasattr(self.loop, 'shutdown_asyncgens'):
@@ -640,14 +643,14 @@ async def foo():
640643
await waiter().asend(None)
641644

642645
self.loop.run_until_complete(foo())
643-
self.loop.run_until_complete(asyncio.sleep(0.01, loop=self.loop))
646+
self.loop.run_until_complete(asyncio.sleep(0.01))
644647

645648
def test_inf_wait_for(self):
646649
async def foo():
647-
await asyncio.sleep(0.1, loop=self.loop)
650+
await asyncio.sleep(0.1)
648651
return 123
649652
res = self.loop.run_until_complete(
650-
asyncio.wait_for(foo(), timeout=float('inf'), loop=self.loop))
653+
asyncio.wait_for(foo(), timeout=float('inf')))
651654
self.assertEqual(res, 123)
652655

653656

tests/test_context.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,13 @@ async def fractions(t, precision, x, y):
1919
with decimal.localcontext() as ctx:
2020
ctx.prec = precision
2121
a = decimal.Decimal(x) / decimal.Decimal(y)
22-
await asyncio.sleep(t, loop=self.loop)
22+
await asyncio.sleep(t)
2323
b = decimal.Decimal(x) / decimal.Decimal(y ** 2)
2424
return a, b
2525

2626
async def main():
2727
r1, r2 = await asyncio.gather(
28-
fractions(0.1, 3, 1, 3), fractions(0.2, 6, 1, 3),
29-
loop=self.loop)
28+
fractions(0.1, 3, 1, 3), fractions(0.2, 6, 1, 3))
3029

3130
return r1, r2
3231

@@ -44,7 +43,7 @@ def test_task_context_1(self):
4443
cvar = contextvars.ContextVar('cvar', default='nope')
4544

4645
async def sub():
47-
await asyncio.sleep(0.01, loop=self.loop)
46+
await asyncio.sleep(0.01)
4847
self.assertEqual(cvar.get(), 'nope')
4948
cvar.set('something else')
5049

@@ -83,7 +82,7 @@ def fut_on_done(fut):
8382
for i in range(3):
8483
# Test that task passed its context to add_done_callback:
8584
cvar.set('yes{}-{}'.format(i, j))
86-
await asyncio.sleep(0.001, loop=self.loop)
85+
await asyncio.sleep(0.001)
8786
self.assertEqual(cvar.get(), 'yes{}-{}'.format(i, j))
8887

8988
task = self.loop.create_task(main())
@@ -101,8 +100,7 @@ def test_task_context_3(self):
101100
async def sub(num):
102101
for i in range(10):
103102
cvar.set(num + i)
104-
await asyncio.sleep(
105-
random.uniform(0.001, 0.05), loop=self.loop)
103+
await asyncio.sleep(random.uniform(0.001, 0.05))
106104
self.assertEqual(cvar.get(), num + i)
107105

108106
async def main():
@@ -111,8 +109,7 @@ async def main():
111109
task = self.loop.create_task(sub(random.randint(0, 10)))
112110
tasks.append(task)
113111

114-
await asyncio.gather(
115-
*tasks, loop=self.loop, return_exceptions=True)
112+
await asyncio.gather(*tasks, return_exceptions=True)
116113

117114
self.loop.run_until_complete(main())
118115

@@ -134,7 +131,7 @@ async def sub():
134131

135132
async def main():
136133
await self.loop.create_task(sub())
137-
await asyncio.sleep(0.01, loop=self.loop)
134+
await asyncio.sleep(0.01)
138135

139136
task = self.loop.create_task(main())
140137
self.loop.run_until_complete(task)

tests/test_cython.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def test_cython_coro_is_coroutine(self):
1616
self.assertEqual(_test_coroutine_1.__qualname__, '_test_coroutine_1')
1717
self.assertEqual(_test_coroutine_1.__name__, '_test_coroutine_1')
1818
self.assertTrue(asyncio.iscoroutine(coro))
19-
fut = asyncio.ensure_future(coro, loop=self.loop)
19+
fut = asyncio.ensure_future(coro)
2020
self.assertTrue(isinstance(fut, asyncio.Future))
2121
self.assertTrue(isinstance(fut, asyncio.Task))
2222
fut.cancel()

tests/test_dealloc.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ def main():
5050
proc = await asyncio.create_subprocess_exec(
5151
cmd, b'-W', b'ignore', b'-c', prog,
5252
stdout=subprocess.PIPE,
53-
stderr=subprocess.PIPE,
54-
loop=self.loop)
53+
stderr=subprocess.PIPE)
5554

5655
await proc.wait()
5756
out = await proc.stdout.read()

tests/test_dns.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ def test_getaddrinfo_close_loop(self):
168168
async def run():
169169
fut = self.loop.create_task(
170170
self.loop.getaddrinfo('example.com', 80))
171-
await asyncio.sleep(0, loop=self.loop)
171+
await asyncio.sleep(0)
172172
fut.cancel()
173173
self.loop.stop()
174174

tests/test_executors.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ async def run():
1919
coros = []
2020
for i in range(0, 10):
2121
coros.append(self.loop.run_in_executor(pool, fib, i))
22-
res = await asyncio.gather(*coros, loop=self.loop)
22+
res = await asyncio.gather(*coros)
2323
self.assertEqual(res, fib10)
24-
await asyncio.sleep(0.01, loop=self.loop)
24+
await asyncio.sleep(0.01)
2525

2626
fib10 = [fib(i) for i in range(10)]
2727
self.loop.run_until_complete(run())

0 commit comments

Comments
 (0)