Skip to content

Commit 57a3abe

Browse files
committed
tests: Do set_event_loop(None) in unittests
uvloop should consistently pass around the loop reference in its internal APIs. Even though explicit passing of event loop objects is discouraged in asyncio programs, uvloop itself is too low-level to rely on get_event_loop.
1 parent bf8b6e6 commit 57a3abe

File tree

5 files changed

+28
-5
lines changed

5 files changed

+28
-5
lines changed

tests/test_aiohttp.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
else:
77
skip_tests = False
88

9+
import asyncio
910
import unittest
1011

1112
from uvloop import _testbase as tb
@@ -29,6 +30,8 @@ async def handle_request(self, message, payload):
2930
response.write(PAYLOAD)
3031
await response.write_eof()
3132

33+
asyncio.set_event_loop(self.loop)
34+
3235
f = self.loop.create_server(
3336
lambda: HttpRequestHandler(keepalive_timeout=1),
3437
'0.0.0.0', '0')

tests/test_base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,9 @@ def test_call_later_2(self):
149149
# libuv cached time.
150150

151151
async def main():
152-
await asyncio.sleep(0.001)
152+
await asyncio.sleep(0.001, loop=self.loop)
153153
time.sleep(0.01)
154-
await asyncio.sleep(0.01)
154+
await asyncio.sleep(0.01, loop=self.loop)
155155

156156
started = time.monotonic()
157157
self.loop.run_until_complete(main())

tests/test_executors.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ async def run():
2121
coros.append(self.loop.run_in_executor(pool, fib, i))
2222
res = await asyncio.gather(*coros, loop=self.loop)
2323
self.assertEqual(res, fib10)
24-
await asyncio.sleep(0.01)
24+
await asyncio.sleep(0.01, loop=self.loop)
2525

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

tests/test_tcp.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,7 @@ async def run():
708708
self.loop.create_task(run())
709709
self.loop.run_until_complete(srv.wait_closed())
710710
gc.collect()
711-
self.loop.run_until_complete(asyncio.sleep(0.1))
711+
self.loop.run_until_complete(asyncio.sleep(0.1, loop=self.loop))
712712

713713
# Since one TCPTransport handle wasn't closed correctly,
714714
# we need to disable this check:

uvloop/_testbase.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33

44
import asyncio
5+
import asyncio.events
56
import collections
67
import contextlib
78
import gc
@@ -68,12 +69,20 @@ def is_asyncio_loop(self):
6869

6970
def setUp(self):
7071
self.loop = self.new_loop()
71-
asyncio.set_event_loop(self.loop)
72+
asyncio.set_event_loop(None)
7273
self._check_unclosed_resources_in_debug = True
7374

75+
if hasattr(asyncio, '_get_running_loop'):
76+
# Disable `_get_running_loop`.
77+
self._get_running_loop = asyncio.events._get_running_loop
78+
asyncio.events._get_running_loop = lambda: None
79+
7480
def tearDown(self):
7581
self.loop.close()
7682

83+
if hasattr(asyncio, '_get_running_loop'):
84+
asyncio.events._get_running_loop = self._get_running_loop
85+
7786
if not self._check_unclosed_resources_in_debug:
7887
return
7988

@@ -199,6 +208,17 @@ def new_loop(self):
199208

200209
class AIOTestCase(BaseTestCase):
201210

211+
def setUp(self):
212+
super().setUp()
213+
214+
watcher = asyncio.SafeChildWatcher()
215+
watcher.attach_loop(self.loop)
216+
asyncio.set_child_watcher(watcher)
217+
218+
def tearDown(self):
219+
asyncio.set_child_watcher(None)
220+
super().tearDown()
221+
202222
def new_loop(self):
203223
return asyncio.new_event_loop()
204224

0 commit comments

Comments
 (0)