Skip to content

Commit cd90823

Browse files
committed
tests - fix pytest-asyncio detected an unclosed event loop when tearing down the event_loop
- using a different policy - fixture event_loop in conftest.py - fixes pytest-dev/pytest-asyncio#257
1 parent b9ad43f commit cd90823

File tree

5 files changed

+15
-31
lines changed

5 files changed

+15
-31
lines changed

tests/apiv1_test.py

+2-7
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,9 @@ def config(unused_tcp_port_factory):
2121
return c
2222

2323

24-
@pytest.fixture(scope="session")
25-
def event_loop(request):
26-
loop = asyncio.get_event_loop_policy().new_event_loop()
27-
yield loop
28-
loop.close()
29-
30-
3124
@pytest_asyncio.fixture(scope="session")
3225
async def server(event_loop, config):
26+
policy = asyncio.get_event_loop_policy()
3327
uvloop.install()
3428
try:
3529
sd = asyncio.Event()
@@ -38,6 +32,7 @@ async def server(event_loop, config):
3832
finally:
3933
sd.set()
4034
await task
35+
asyncio.set_event_loop_policy(policy)
4136

4237

4338
@pytest_asyncio.fixture(scope="session")

tests/apiv2_test.py

+2-7
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,9 @@ def config(unused_tcp_port_factory):
3535
return c
3636

3737

38-
@pytest.fixture(scope="session")
39-
def event_loop(request):
40-
loop = asyncio.get_event_loop_policy().new_event_loop()
41-
yield loop
42-
loop.close()
43-
44-
4538
@pytest_asyncio.fixture(scope="session")
4639
async def server(event_loop, config):
40+
policy = asyncio.get_event_loop_policy()
4741
uvloop.install()
4842
try:
4943
sd = asyncio.Event()
@@ -52,6 +46,7 @@ async def server(event_loop, config):
5246
finally:
5347
sd.set()
5448
await task
49+
asyncio.set_event_loop_policy(policy)
5550

5651

5752
@pytest.fixture(scope="session", params=[2])

tests/conftest.py

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import asyncio
12
import os
23
import dataclasses
34
import sys
@@ -16,6 +17,13 @@
1617
URLBASE = "/"
1718

1819

20+
@pytest.fixture(scope="session")
21+
def event_loop(request):
22+
loop = asyncio.get_event_loop_policy().new_event_loop()
23+
yield loop
24+
loop.close()
25+
26+
1927
@pytest.fixture(autouse=True)
2028
def skip_env(request):
2129
if request.node.get_closest_marker("skip_env"):

tests/forms_test.py

+3-8
Original file line numberDiff line numberDiff line change
@@ -269,24 +269,19 @@ def config(unused_tcp_port_factory):
269269
return c
270270

271271

272-
@pytest.fixture(scope="session")
273-
def event_loop(request):
274-
loop = asyncio.get_event_loop_policy().new_event_loop()
275-
yield loop
276-
loop.close()
277-
278-
279272
@pytest_asyncio.fixture(scope="session")
280273
async def server(event_loop, config, app):
281-
uvloop.install()
274+
policy = asyncio.get_event_loop_policy()
282275
try:
283276
sd = asyncio.Event()
284277
asgi = WsgiToAsgi(app)
285278
task = event_loop.create_task(serve(asgi, config, shutdown_trigger=sd.wait))
286279
yield config
287280
finally:
288281
sd.set()
282+
del asgi
289283
await task
284+
asyncio.set_event_loop_policy(policy)
290285

291286

292287
@pytest.fixture(scope="session", params=["application/x-www-form-urlencoded", "multipart/form-data"])

tests/linode_test.py

-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import asyncio
2-
31
import aiopenapi3.plugin
42
from aiopenapi3 import OpenAPI
53
import pytest
@@ -70,13 +68,6 @@ def parsed(self, ctx):
7068
return ctx
7169

7270

73-
@pytest_asyncio.fixture(scope="session")
74-
def event_loop(request):
75-
loop = asyncio.get_event_loop_policy().new_event_loop()
76-
yield loop
77-
loop.close()
78-
79-
8071
@pytest_asyncio.fixture(scope="session")
8172
async def api():
8273
from aiopenapi3.loader import NullLoader, YAMLCompatibilityLoader

0 commit comments

Comments
 (0)