Skip to content

Commit 98ced40

Browse files
committed
trying to fix pytest-asyncio
1 parent fbec68c commit 98ced40

22 files changed

+80
-145
lines changed

services/web/server/setup.cfg

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@ commit_args = --no-verify
1212
[tool:pytest]
1313
addopts = --strict-markers
1414
asyncio_mode = auto
15-
markers =
15+
markers =
1616
slow: marks tests as slow (deselect with '-m "not slow"')
1717
acceptance_test: "marks tests as 'acceptance tests' i.e. does the system do what the user expects? Typically those are workflows."
1818
testit: "marks test to run during development"
1919
heavy_load: "mark tests that require large amount of data"
20+
asyncio_default_fixture_loop_scope = function
2021

2122
[mypy]
22-
plugins =
23+
plugins =
2324
pydantic.mypy
2425
sqlalchemy.ext.mypy.plugin

services/web/server/tests/integration/01/test_exporter_requests_handlers.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# pylint: disable=unused-argument
22
# pylint: disable=redefined-outer-name
33

4-
import asyncio
54
import logging
65
import sys
76
from collections.abc import AsyncIterable, Callable, Iterable
@@ -145,9 +144,8 @@ def product_name() -> str:
145144

146145

147146
@pytest.fixture
148-
def client(
147+
async def client(
149148
docker_swarm: None,
150-
event_loop: asyncio.AbstractEventLoop,
151149
aiohttp_client: Callable,
152150
app_config: dict,
153151
monkeypatch_setenv_from_app_config: Callable,
@@ -185,11 +183,9 @@ def client(
185183
setup_socketio(app)
186184
setup_resource_manager(app)
187185

188-
return event_loop.run_until_complete(
189-
aiohttp_client(
190-
app,
191-
server_kwargs={"port": cfg["main"]["port"], "host": cfg["main"]["host"]},
192-
)
186+
return await aiohttp_client(
187+
app,
188+
server_kwargs={"port": cfg["main"]["port"], "host": cfg["main"]["host"]},
193189
)
194190

195191

services/web/server/tests/integration/01/test_garbage_collection.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,7 @@ async def director_v2_service_mock(
139139

140140

141141
@pytest.fixture
142-
def client(
143-
event_loop: asyncio.AbstractEventLoop,
142+
async def client(
144143
aiohttp_client: Callable[..., Awaitable[TestClient]],
145144
app_config: dict[str, Any],
146145
postgres_with_template_db: sa.engine.Engine,
@@ -181,11 +180,9 @@ def client(
181180
assert setup_resource_manager(app)
182181
setup_garbage_collector(app)
183182

184-
return event_loop.run_until_complete(
185-
aiohttp_client(
186-
app,
187-
server_kwargs={"port": cfg["main"]["port"], "host": cfg["main"]["host"]},
188-
)
183+
return await aiohttp_client(
184+
app,
185+
server_kwargs={"port": cfg["main"]["port"], "host": cfg["main"]["host"]},
189186
)
190187

191188

services/web/server/tests/integration/02/notifications/test_rabbitmq_consumers.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
# pylint: disable=unused-variable
44
# pylint: disable=too-many-arguments
55

6-
import asyncio
76
from collections.abc import Awaitable, Callable
87
from random import choice
98
from typing import Any
@@ -130,9 +129,8 @@ async def _assert_handler_called_with_json(
130129

131130

132131
@pytest.fixture
133-
def client(
132+
async def client(
134133
mock_redis_socket_timeout: None,
135-
event_loop: asyncio.AbstractEventLoop,
136134
aiohttp_client: Callable,
137135
app_config: dict[str, Any],
138136
rabbit_service: RabbitSettings,
@@ -159,14 +157,12 @@ def client(
159157
setup_socketio(app)
160158
setup_resource_manager(app)
161159

162-
return event_loop.run_until_complete(
163-
aiohttp_client(
164-
app,
165-
server_kwargs={
166-
"port": app_config["main"]["port"],
167-
"host": app_config["main"]["host"],
168-
},
169-
)
160+
return await aiohttp_client(
161+
app,
162+
server_kwargs={
163+
"port": app_config["main"]["port"],
164+
"host": app_config["main"]["host"],
165+
},
170166
)
171167

172168

services/web/server/tests/integration/02/test_computation.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,7 @@ def user_role_response():
113113

114114

115115
@pytest.fixture
116-
def client(
117-
event_loop: asyncio.AbstractEventLoop,
116+
async def client(
118117
postgres_db: sa.engine.Engine,
119118
rabbit_service: RabbitSettings,
120119
redis_settings: RedisSettings,
@@ -151,14 +150,12 @@ def client(
151150
setup_db_listener(app)
152151
# no garbage collector
153152

154-
return event_loop.run_until_complete(
155-
aiohttp_client(
156-
app,
157-
server_kwargs={
158-
"port": app_config["main"]["port"],
159-
"host": app_config["main"]["host"],
160-
},
161-
)
153+
return await aiohttp_client(
154+
app,
155+
server_kwargs={
156+
"port": app_config["main"]["port"],
157+
"host": app_config["main"]["host"],
158+
},
162159
)
163160

164161

@@ -253,7 +250,7 @@ def _get_project_workbench_from_db(
253250
project_in_db
254251
), f"missing pipeline in the database under comp_pipeline {project_id}"
255252
print(
256-
f"<-- found following workbench: {json_dumps( project_in_db.workbench, indent=2)}"
253+
f"<-- found following workbench: {json_dumps(project_in_db.workbench, indent=2)}"
257254
)
258255
return project_in_db.workbench
259256

services/web/server/tests/unit/isolated/scicrunch/test_scicrunch_service_client.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
# pylint:disable=redefined-outer-name
44

55
"""
6-
Use this test to emulate situations with scicrunch service API
6+
Use this test to emulate situations with scicrunch service API
77
88
"""
9-
import asyncio
9+
1010
import json
1111
import os
1212
from collections.abc import Callable
@@ -100,18 +100,16 @@ async def mock_scicrunch_service_resolver(
100100

101101

102102
@pytest.fixture
103-
def app(
103+
async def app(
104104
mock_env_devel_environment: EnvVarsDict,
105-
event_loop: asyncio.AbstractEventLoop,
106105
aiohttp_server: Callable,
107106
) -> web.Application:
108-
109107
app_ = create_safe_application()
110108

111109
setup_settings(app_)
112110
setup_scicrunch(app_)
113111

114-
server = event_loop.run_until_complete(aiohttp_server(app_))
112+
server = await aiohttp_server(app_)
115113
assert server.app == app_
116114
return server.app
117115

services/web/server/tests/unit/isolated/test__redirections.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
# pylint:disable=redefined-outer-name
44

55

6-
import asyncio
76
import textwrap
87
from collections.abc import Awaitable, Callable
98
from pathlib import Path
@@ -36,12 +35,10 @@ def index_static_path(tmpdir):
3635

3736

3837
@pytest.fixture
39-
def client(
40-
event_loop: asyncio.AbstractEventLoop,
38+
async def client(
4139
aiohttp_client: Callable[..., Awaitable[TestClient]],
4240
index_static_path,
4341
):
44-
4542
routes = web.RouteTableDef()
4643

4744
@routes.get("/")
@@ -71,7 +68,7 @@ async def get_redirect_to_root(_request):
7168

7269
app = web.Application()
7370
app.add_routes(routes)
74-
return event_loop.run_until_complete(aiohttp_client(app))
71+
return await aiohttp_client(app)
7572

7673

7774
@pytest.mark.parametrize("test_path", ["/", "/other"])

services/web/server/tests/unit/isolated/test_activity.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
# pylint:disable=unused-argument
33
# pylint:disable=redefined-outer-name
44

5-
import asyncio
65
import os
76
from collections.abc import Awaitable, Callable
87
from typing import Any
@@ -62,7 +61,6 @@ def mocked_monitoring_down(mocker: MockerFixture) -> None:
6261
def app_environment(
6362
mock_env_devel_environment: EnvVarsDict, monkeypatch: pytest.MonkeyPatch
6463
) -> EnvVarsDict:
65-
6664
envs = mock_env_devel_environment | setenvs_from_dict(
6765
monkeypatch,
6866
{
@@ -99,8 +97,7 @@ def app_environment(
9997

10098

10199
@pytest.fixture
102-
def client(
103-
event_loop: asyncio.AbstractEventLoop,
100+
async def client(
104101
aiohttp_client: Callable[..., Awaitable[TestClient]],
105102
mock_orphaned_services: MagicMock,
106103
app_environment: EnvVarsDict,
@@ -119,7 +116,7 @@ def client(
119116
setup_rest(app)
120117
assert setup_activity(app)
121118

122-
return event_loop.run_until_complete(aiohttp_client(app))
119+
return await aiohttp_client(app)
123120

124121

125122
async def test_has_login_required(client: TestClient):

services/web/server/tests/unit/isolated/test_catalog_setup.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
# pylint: disable=unused-argument
33
# pylint: disable=unused-variable
44

5-
import asyncio
65
from collections.abc import Awaitable, Callable
76

87
import pytest
@@ -15,15 +14,14 @@
1514

1615

1716
@pytest.fixture
18-
def client(
19-
event_loop: asyncio.AbstractEventLoop,
17+
async def client(
2018
aiohttp_client: Callable[..., Awaitable[TestClient]],
2119
):
2220
app = create_safe_application()
2321

2422
setup_catalog.__wrapped__(app)
2523

26-
return event_loop.run_until_complete(aiohttp_client(app))
24+
return await aiohttp_client(app)
2725

2826

2927
def test_url_translation():

services/web/server/tests/unit/isolated/test_diagnostics_healthcheck.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,12 @@ def mock_environment(
103103

104104

105105
@pytest.fixture
106-
def client(
107-
event_loop: asyncio.AbstractEventLoop,
106+
async def client(
108107
unused_tcp_port_factory: Callable,
109108
aiohttp_client: Callable[..., Awaitable[TestClient]],
110109
api_version_prefix: str,
111110
mock_environment: EnvVarsDict,
112111
) -> TestClient:
113-
114112
routes = web.RouteTableDef()
115113

116114
@routes.get("/error")
@@ -167,8 +165,8 @@ async def delay_response(request: web.Request):
167165

168166
app.router.add_routes(routes)
169167

170-
return event_loop.run_until_complete(
171-
aiohttp_client(app, server_kwargs={key: main[key] for key in ("host", "port")})
168+
return await aiohttp_client(
169+
app, server_kwargs={key: main[key] for key in ("host", "port")}
172170
)
173171

174172

@@ -228,7 +226,7 @@ async def test_diagnose_on_response_delays(client: TestClient):
228226
settings: DiagnosticsSettings = client.app[APP_SETTINGS_KEY].WEBSERVER_DIAGNOSTICS
229227

230228
tmax = settings.DIAGNOSTICS_MAX_AVG_LATENCY
231-
coros = [client.get(f"/delay/{1.1*tmax}") for _ in range(10)]
229+
coros = [client.get(f"/delay/{1.1 * tmax}") for _ in range(10)]
232230
resps = await asyncio.gather(*coros)
233231

234232
for resp in resps:

0 commit comments

Comments
 (0)