Skip to content

Commit 7fba4a3

Browse files
committed
Even loop lifecycle ownership fix
1 parent 751d829 commit 7fba4a3

File tree

7 files changed

+85
-60
lines changed

7 files changed

+85
-60
lines changed

tests/integration/contrib/aiohttp/conftest.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import asyncio
22
import pathlib
3+
from collections.abc import AsyncGenerator
34
from typing import Any
45
from unittest import mock
56

67
import pytest
8+
import pytest_asyncio
79
from aiohttp import web
810
from aiohttp.test_utils import TestClient
911

@@ -114,6 +116,10 @@ def app(router):
114116
return app
115117

116118

117-
@pytest.fixture
118-
async def client(app, aiohttp_client) -> TestClient:
119-
return await aiohttp_client(app)
119+
@pytest_asyncio.fixture
120+
async def client(app, aiohttp_client) -> AsyncGenerator[TestClient, None]:
121+
test_client = await aiohttp_client(app)
122+
try:
123+
yield test_client
124+
finally:
125+
await test_client.close()

tests/integration/contrib/aiohttp/data/v3.0/aiohttpproject/__main__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
]
77

88

9-
def get_app(loop=None):
10-
app = web.Application(loop=loop)
9+
def get_app():
10+
app = web.Application()
1111
app.add_routes(routes)
1212
return app
1313

tests/integration/contrib/aiohttp/test_aiohttp_project.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import os
22
import sys
33
from base64 import b64encode
4+
from collections.abc import AsyncGenerator
45
from io import BytesIO
56

67
import pytest
8+
import pytest_asyncio
79

810

911
@pytest.fixture(autouse=True, scope="session")
@@ -22,9 +24,13 @@ def app(project_setup):
2224
return get_app()
2325

2426

25-
@pytest.fixture
26-
async def client(app, aiohttp_client):
27-
return await aiohttp_client(app)
27+
@pytest_asyncio.fixture
28+
async def client(app, aiohttp_client) -> AsyncGenerator:
29+
test_client = await aiohttp_client(app)
30+
try:
31+
yield test_client
32+
finally:
33+
await test_client.close()
2834

2935

3036
class BaseTestPetstore:

tests/integration/contrib/falcon/test_falcon_asgi_middleware.py

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from falcon.asgi import App
1010
from falcon.asgi import Response
1111
from falcon.constants import MEDIA_JSON
12-
from falcon.testing import TestClient
12+
from falcon.testing import ASGIConductor
1313
from jsonschema_path import SchemaPath
1414

1515
from openapi_core.contrib.falcon.middlewares import FalconASGIOpenAPIMiddleware
@@ -75,18 +75,19 @@ async def __anext__(self):
7575
return chunk
7676

7777

78-
def test_dual_mode_sync_middleware_works_with_asgi_app(spec):
78+
@pytest.mark.asyncio
79+
async def test_dual_mode_sync_middleware_works_with_asgi_app(spec):
7980
middleware = FalconOpenAPIMiddleware.from_spec(spec)
8081
app = App(middleware=[middleware])
8182
app.add_route("/v1/pets", PetListResource())
82-
client = TestClient(app)
8383

84-
with pytest.warns(DeprecationWarning):
85-
response = client.simulate_get(
86-
"/v1/pets",
87-
host="petstore.swagger.io",
88-
query_string="limit=12",
89-
)
84+
async with ASGIConductor(app) as conductor:
85+
with pytest.warns(DeprecationWarning):
86+
response = await conductor.simulate_get(
87+
"/v1/pets",
88+
host="petstore.swagger.io",
89+
query_string="limit=12",
90+
)
9091

9192
assert response.status_code == 200
9293
assert response.json == {
@@ -102,17 +103,18 @@ def test_dual_mode_sync_middleware_works_with_asgi_app(spec):
102103
}
103104

104105

105-
def test_explicit_asgi_middleware_handles_request_validation(spec):
106+
@pytest.mark.asyncio
107+
async def test_explicit_asgi_middleware_handles_request_validation(spec):
106108
middleware = FalconASGIOpenAPIMiddleware.from_spec(spec)
107109
app = App(middleware=[middleware])
108110
app.add_route("/v1/pets", PetListResource())
109-
client = TestClient(app)
110111

111-
with pytest.warns(DeprecationWarning):
112-
response = client.simulate_get(
113-
"/v1/pets",
114-
host="petstore.swagger.io",
115-
)
112+
async with ASGIConductor(app) as conductor:
113+
with pytest.warns(DeprecationWarning):
114+
response = await conductor.simulate_get(
115+
"/v1/pets",
116+
host="petstore.swagger.io",
117+
)
116118

117119
assert response.status_code == 400
118120
assert response.json == {
@@ -129,18 +131,19 @@ def test_explicit_asgi_middleware_handles_request_validation(spec):
129131
}
130132

131133

132-
def test_explicit_asgi_middleware_validates_response(spec):
134+
@pytest.mark.asyncio
135+
async def test_explicit_asgi_middleware_validates_response(spec):
133136
middleware = FalconASGIOpenAPIMiddleware.from_spec(spec)
134137
app = App(middleware=[middleware])
135138
app.add_route("/v1/pets", InvalidPetListResource())
136-
client = TestClient(app)
137139

138-
with pytest.warns(DeprecationWarning):
139-
response = client.simulate_get(
140-
"/v1/pets",
141-
host="petstore.swagger.io",
142-
query_string="limit=12",
143-
)
140+
async with ASGIConductor(app) as conductor:
141+
with pytest.warns(DeprecationWarning):
142+
response = await conductor.simulate_get(
143+
"/v1/pets",
144+
host="petstore.swagger.io",
145+
query_string="limit=12",
146+
)
144147

145148
assert response.status_code == 400
146149
assert "errors" in response.json

tests/integration/contrib/fastapi/test_fastapi_project.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ def app():
2424

2525
@pytest.fixture
2626
def client(app):
27-
return TestClient(app, base_url="http://petstore.swagger.io")
27+
with TestClient(app, base_url="http://petstore.swagger.io") as test_client:
28+
yield test_client
2829

2930

3031
class BaseTestPetstore:

tests/integration/contrib/starlette/test_starlette_project.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ def app(self):
2626

2727
@pytest.fixture
2828
def client(self, app):
29-
return TestClient(app, base_url="http://petstore.swagger.io")
29+
with TestClient(
30+
app,
31+
base_url="http://petstore.swagger.io",
32+
) as test_client:
33+
yield test_client
3034

3135
@property
3236
def api_key_encoded(self):
@@ -45,7 +49,11 @@ def app(self):
4549

4650
@pytest.fixture
4751
def client(self, app):
48-
return TestClient(app, base_url="http://petstore.swagger.io")
52+
with TestClient(
53+
app,
54+
base_url="http://petstore.swagger.io",
55+
) as test_client:
56+
yield test_client
4957

5058

5159
class TestPetListEndpoint(BaseTestPetstore):

tests/integration/contrib/starlette/test_starlette_validation.py

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ async def test_route(scope, receive, send):
4343

4444
@pytest.fixture
4545
def client(self, app):
46-
return TestClient(app, base_url="http://localhost")
46+
with TestClient(app, base_url="http://localhost") as test_client:
47+
yield test_client
4748

4849
def test_request_validator_path_pattern(self, client, schema_path):
4950
response_data = {"data": "data"}
@@ -65,18 +66,18 @@ async def test_route(request):
6566
Route("/browse/12/", test_route, methods=["POST"]),
6667
],
6768
)
68-
client = TestClient(app, base_url="http://localhost")
69-
query_string = {
70-
"q": "string",
71-
}
72-
headers = {"content-type": "application/json"}
73-
data = {"param1": 1}
74-
response = client.post(
75-
"/browse/12/",
76-
params=query_string,
77-
json=data,
78-
headers=headers,
79-
)
69+
with TestClient(app, base_url="http://localhost") as client:
70+
query_string = {
71+
"q": "string",
72+
}
73+
headers = {"content-type": "application/json"}
74+
data = {"param1": 1}
75+
response = client.post(
76+
"/browse/12/",
77+
params=query_string,
78+
json=data,
79+
headers=headers,
80+
)
8081

8182
assert response.status_code == 200
8283
assert response.json() == response_data
@@ -104,18 +105,18 @@ def test_route(request):
104105
Route("/browse/12/", test_route, methods=["POST"]),
105106
],
106107
)
107-
client = TestClient(app, base_url="http://localhost")
108-
query_string = {
109-
"q": "string",
110-
}
111-
headers = {"content-type": "application/json"}
112-
data = {"param1": 1}
113-
response = client.post(
114-
"/browse/12/",
115-
params=query_string,
116-
json=data,
117-
headers=headers,
118-
)
108+
with TestClient(app, base_url="http://localhost") as client:
109+
query_string = {
110+
"q": "string",
111+
}
112+
headers = {"content-type": "application/json"}
113+
data = {"param1": 1}
114+
response = client.post(
115+
"/browse/12/",
116+
params=query_string,
117+
json=data,
118+
headers=headers,
119+
)
119120

120121
assert response.status_code == 200
121122
assert response.json() == response_data

0 commit comments

Comments
 (0)