Skip to content

Commit d049740

Browse files
🐛 Use tenacity in flaky api keys tests (#7304)
1 parent 3250473 commit d049740

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

services/web/server/tests/unit/with_dbs/01/test_api_keys.py

Lines changed: 15 additions & 5 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 AsyncIterable
87
from datetime import timedelta
98
from http import HTTPStatus
@@ -29,7 +28,12 @@
2928
)
3029
from simcore_service_webserver.application_settings import GarbageCollectorSettings
3130
from simcore_service_webserver.db.models import UserRole
32-
from tenacity import retry_if_exception_type, stop_after_attempt, wait_fixed
31+
from tenacity import (
32+
retry_if_exception_type,
33+
stop_after_attempt,
34+
stop_after_delay,
35+
wait_fixed,
36+
)
3337

3438

3539
@pytest.fixture
@@ -172,9 +176,15 @@ async def test_create_api_key_with_expiration(
172176
assert [d["displayName"] for d in data] == ["foo"]
173177

174178
# wait for api-key for it to expire and force-run scheduled task
175-
await asyncio.sleep(expiration_interval.seconds)
176-
deleted = await prune_expired_api_keys(client.app)
177-
assert deleted == ["foo"]
179+
async for attempt in tenacity.AsyncRetrying(
180+
wait=wait_fixed(1),
181+
retry=retry_if_exception_type(AssertionError),
182+
stop=stop_after_delay(5 * expiration_interval.seconds),
183+
reraise=True,
184+
):
185+
with attempt:
186+
deleted = await prune_expired_api_keys(client.app)
187+
assert deleted == ["foo"]
178188

179189
resp = await client.get("/v0/auth/api-keys")
180190
data, _ = await assert_status(resp, expected)

services/web/server/tests/unit/with_dbs/02/test_projects_repository.py

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

6-
from datetime import timedelta
6+
from datetime import datetime, timedelta
77
from uuid import UUID
88

99
import arrow
@@ -55,7 +55,9 @@ async def test_patch_project(
5555
assert client.app
5656

5757
# This will change after in patched_project
58-
assert user_project["creationDate"] == user_project["lastChangeDate"]
58+
creation_date = datetime.fromisoformat(user_project["creationDate"])
59+
last_change_date = datetime.fromisoformat(user_project["lastChangeDate"])
60+
assert abs(creation_date - last_change_date) < timedelta(seconds=1)
5961

6062
# Patch valid project
6163
patch_data = {"name": "Updated Project Name"}

0 commit comments

Comments
 (0)