Skip to content

Commit

Permalink
tests - fix pytest-asyncio detected an unclosed event loop when teari…
Browse files Browse the repository at this point in the history
…ng down the event_loop

 - using a different policy
 - fixture event_loop in conftest.py - fixes pytest-dev/pytest-asyncio#257
  • Loading branch information
commonism committed Apr 5, 2023
1 parent b9ad43f commit cd90823
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 31 deletions.
9 changes: 2 additions & 7 deletions tests/apiv1_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,9 @@ def config(unused_tcp_port_factory):
return c


@pytest.fixture(scope="session")
def event_loop(request):
loop = asyncio.get_event_loop_policy().new_event_loop()
yield loop
loop.close()


@pytest_asyncio.fixture(scope="session")
async def server(event_loop, config):
policy = asyncio.get_event_loop_policy()
uvloop.install()
try:
sd = asyncio.Event()
Expand All @@ -38,6 +32,7 @@ async def server(event_loop, config):
finally:
sd.set()
await task
asyncio.set_event_loop_policy(policy)


@pytest_asyncio.fixture(scope="session")
Expand Down
9 changes: 2 additions & 7 deletions tests/apiv2_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,9 @@ def config(unused_tcp_port_factory):
return c


@pytest.fixture(scope="session")
def event_loop(request):
loop = asyncio.get_event_loop_policy().new_event_loop()
yield loop
loop.close()


@pytest_asyncio.fixture(scope="session")
async def server(event_loop, config):
policy = asyncio.get_event_loop_policy()
uvloop.install()
try:
sd = asyncio.Event()
Expand All @@ -52,6 +46,7 @@ async def server(event_loop, config):
finally:
sd.set()
await task
asyncio.set_event_loop_policy(policy)


@pytest.fixture(scope="session", params=[2])
Expand Down
8 changes: 8 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
import os
import dataclasses
import sys
Expand All @@ -16,6 +17,13 @@
URLBASE = "/"


@pytest.fixture(scope="session")
def event_loop(request):
loop = asyncio.get_event_loop_policy().new_event_loop()
yield loop
loop.close()


@pytest.fixture(autouse=True)
def skip_env(request):
if request.node.get_closest_marker("skip_env"):
Expand Down
11 changes: 3 additions & 8 deletions tests/forms_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,24 +269,19 @@ def config(unused_tcp_port_factory):
return c


@pytest.fixture(scope="session")
def event_loop(request):
loop = asyncio.get_event_loop_policy().new_event_loop()
yield loop
loop.close()


@pytest_asyncio.fixture(scope="session")
async def server(event_loop, config, app):
uvloop.install()
policy = asyncio.get_event_loop_policy()
try:
sd = asyncio.Event()
asgi = WsgiToAsgi(app)
task = event_loop.create_task(serve(asgi, config, shutdown_trigger=sd.wait))
yield config
finally:
sd.set()
del asgi
await task
asyncio.set_event_loop_policy(policy)


@pytest.fixture(scope="session", params=["application/x-www-form-urlencoded", "multipart/form-data"])
Expand Down
9 changes: 0 additions & 9 deletions tests/linode_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import asyncio

import aiopenapi3.plugin
from aiopenapi3 import OpenAPI
import pytest
Expand Down Expand Up @@ -70,13 +68,6 @@ def parsed(self, ctx):
return ctx


@pytest_asyncio.fixture(scope="session")
def event_loop(request):
loop = asyncio.get_event_loop_policy().new_event_loop()
yield loop
loop.close()


@pytest_asyncio.fixture(scope="session")
async def api():
from aiopenapi3.loader import NullLoader, YAMLCompatibilityLoader
Expand Down

0 comments on commit cd90823

Please sign in to comment.