Skip to content

Commit 5217133

Browse files
committed
ensure aiothtp_server gets the right loop + hack for pytest-asyncio
1 parent f83103a commit 5217133

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

services/web/server/tests/unit/with_dbs/01/storage/test_storage.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,18 @@
7979
]
8080

8181

82+
@pytest.mark.xfail(
83+
reason="This is really weird: A first test that fails is necessary to make this test module work with pytest-asyncio>=0.24.0. "
84+
"Maybe because of module/session event loops?"
85+
)
86+
async def test_pytest_asyncio_failure(
87+
client: TestClient,
88+
):
89+
url = "/v0/storage/locations"
90+
assert url.startswith(PREFIX)
91+
await client.get(url)
92+
93+
8294
@pytest.mark.parametrize(
8395
"user_role,expected",
8496
[
@@ -95,7 +107,6 @@ async def test_list_storage_locations(
95107
):
96108
url = "/v0/storage/locations"
97109
assert url.startswith(PREFIX)
98-
99110
resp = await client.get(url)
100111
data, error = await assert_status(resp, expected)
101112

services/web/server/tests/unit/with_dbs/01/storage/test_storage_handlers.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,20 @@ def _resolve(*args, **kwargs) -> AnyUrl:
8989
DOUBLE_ENCODE_SLASH_IN_FILE_ID = "ef944bbe-14c7-11ee-a195-02420a0f07ab%252F46ac4913-92dc-432c-98e3-2dea21d3f0ed%252Fa_text_file.txt"
9090
SINGLE_ENCODE_SLASH_IN_FILE_ID = "ef944bbe-14c7-11ee-a195-02420a0f07ab%2F46ac4913-92dc-432c-98e3-2dea21d3f0ed%2Fa_text_file.txt"
9191

92+
PREFIX = "/" + "v0" + "/storage"
93+
94+
95+
@pytest.mark.xfail(
96+
reason="This is really weird: A first test that fails is necessary to make this test module work with pytest-asyncio>=0.24.0. "
97+
"Maybe because of module/session event loops?"
98+
)
99+
async def test_pytest_asyncio_failure(
100+
client: TestClient,
101+
):
102+
url = "/v0/storage/locations"
103+
assert url.startswith(PREFIX)
104+
await client.get(url)
105+
92106

93107
@pytest.mark.parametrize("user_role", [UserRole.USER])
94108
@pytest.mark.parametrize(

services/web/server/tests/unit/with_dbs/conftest.py

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

6+
import asyncio
67
import random
78
import sys
89
import textwrap
@@ -24,6 +25,7 @@
2425

2526
import aiopg.sa
2627
import pytest
28+
import pytest_asyncio
2729
import redis
2830
import redis.asyncio as aioredis
2931
import simcore_postgres_database.cli as pg_cli
@@ -204,7 +206,13 @@ async def _print_mail_to_stdout(
204206
)
205207

206208

207-
@pytest.fixture
209+
@pytest_asyncio.fixture(loop_scope="function")
210+
async def loop() -> asyncio.AbstractEventLoop:
211+
# NOTE: This is a hack to ensure the loop is set in the aiohttp_server
212+
return asyncio.get_running_loop()
213+
214+
215+
@pytest_asyncio.fixture(loop_scope="function")
208216
async def web_server(
209217
app_environment: EnvVarsDict,
210218
postgres_db: sa.engine.Engine,

0 commit comments

Comments
 (0)