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.
This commit is contained in:
Yury Selivanov 2016-11-07 19:27:37 -05:00
parent bf8b6e6bb9
commit 57a3abe515
5 changed files with 28 additions and 5 deletions

View File

@ -6,6 +6,7 @@ except ImportError:
else:
skip_tests = False
import asyncio
import unittest
from uvloop import _testbase as tb
@ -29,6 +30,8 @@ class _TestAioHTTP:
response.write(PAYLOAD)
await response.write_eof()
asyncio.set_event_loop(self.loop)
f = self.loop.create_server(
lambda: HttpRequestHandler(keepalive_timeout=1),
'0.0.0.0', '0')

View File

@ -149,9 +149,9 @@ class _TestBase:
# libuv cached time.
async def main():
await asyncio.sleep(0.001)
await asyncio.sleep(0.001, loop=self.loop)
time.sleep(0.01)
await asyncio.sleep(0.01)
await asyncio.sleep(0.01, loop=self.loop)
started = time.monotonic()
self.loop.run_until_complete(main())

View File

@ -21,7 +21,7 @@ class _TestExecutors:
coros.append(self.loop.run_in_executor(pool, fib, i))
res = await asyncio.gather(*coros, loop=self.loop)
self.assertEqual(res, fib10)
await asyncio.sleep(0.01)
await asyncio.sleep(0.01, loop=self.loop)
fib10 = [fib(i) for i in range(10)]
self.loop.run_until_complete(run())

View File

@ -708,7 +708,7 @@ class Test_UV_TCP(_TestTCP, tb.UVTestCase):
self.loop.create_task(run())
self.loop.run_until_complete(srv.wait_closed())
gc.collect()
self.loop.run_until_complete(asyncio.sleep(0.1))
self.loop.run_until_complete(asyncio.sleep(0.1, loop=self.loop))
# Since one TCPTransport handle wasn't closed correctly,
# we need to disable this check:

View File

@ -2,6 +2,7 @@
import asyncio
import asyncio.events
import collections
import contextlib
import gc
@ -68,12 +69,20 @@ class BaseTestCase(unittest.TestCase, metaclass=BaseTestCaseMeta):
def setUp(self):
self.loop = self.new_loop()
asyncio.set_event_loop(self.loop)
asyncio.set_event_loop(None)
self._check_unclosed_resources_in_debug = True
if hasattr(asyncio, '_get_running_loop'):
# Disable `_get_running_loop`.
self._get_running_loop = asyncio.events._get_running_loop
asyncio.events._get_running_loop = lambda: None
def tearDown(self):
self.loop.close()
if hasattr(asyncio, '_get_running_loop'):
asyncio.events._get_running_loop = self._get_running_loop
if not self._check_unclosed_resources_in_debug:
return
@ -199,6 +208,17 @@ class UVTestCase(BaseTestCase):
class AIOTestCase(BaseTestCase):
def setUp(self):
super().setUp()
watcher = asyncio.SafeChildWatcher()
watcher.attach_loop(self.loop)
asyncio.set_child_watcher(watcher)
def tearDown(self):
asyncio.set_child_watcher(None)
super().tearDown()
def new_loop(self):
return asyncio.new_event_loop()