Skip to content

Commit fd019a2

Browse files
committed
adds mock for bg
1 parent 59285a9 commit fd019a2

File tree

3 files changed

+43
-14
lines changed

3 files changed

+43
-14
lines changed

services/web/server/src/simcore_service_webserver/garbage_collector/plugin.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,12 @@
33
from aiohttp import web
44
from servicelib.aiohttp.application_setup import ModuleCategory, app_module_setup
55
from servicelib.logging_utils import set_parent_module_log_level
6-
from simcore_service_webserver.garbage_collector._tasks_trash import (
7-
create_background_task_to_prune_trash,
8-
)
96

107
from ..application_settings import get_application_settings
118
from ..login.plugin import setup_login_storage
129
from ..projects.db import setup_projects_db
1310
from ..socketio.plugin import setup_socketio
14-
from ._tasks_api_keys import create_background_task_to_prune_api_keys
15-
from ._tasks_core import run_background_task
16-
from ._tasks_users import create_background_task_for_trial_accounts
11+
from . import _tasks_api_keys, _tasks_core, _tasks_trash, _tasks_users
1712
from .settings import get_plugin_settings
1813

1914
_logger = logging.getLogger(__name__)
@@ -35,7 +30,7 @@ def setup_garbage_collector(app: web.Application) -> None:
3530

3631
settings = get_plugin_settings(app)
3732

38-
app.cleanup_ctx.append(run_background_task)
33+
app.cleanup_ctx.append(_tasks_core.run_background_task)
3934

4035
set_parent_module_log_level(
4136
_logger.name, min(logging.INFO, get_application_settings(app).log_level)
@@ -48,10 +43,17 @@ def setup_garbage_collector(app: web.Application) -> None:
4843
# If more tasks of this nature are needed, we should setup some sort of registration mechanism
4944
# with a interface such that plugins can pass tasks to the GC plugin to handle them
5045
interval_s = settings.GARBAGE_COLLECTOR_EXPIRED_USERS_CHECK_INTERVAL_S
51-
app.cleanup_ctx.append(create_background_task_for_trial_accounts(interval_s))
46+
app.cleanup_ctx.append(
47+
_tasks_users.create_background_task_for_trial_accounts(interval_s)
48+
)
5249

5350
# SEE https://github.com/ITISFoundation/osparc-issues/issues/705
5451
wait_period_s = settings.GARBAGE_COLLECTOR_PRUNE_APIKEYS_INTERVAL_S
55-
app.cleanup_ctx.append(create_background_task_to_prune_api_keys(wait_period_s))
52+
app.cleanup_ctx.append(
53+
_tasks_api_keys.create_background_task_to_prune_api_keys(wait_period_s)
54+
)
5655

57-
app.cleanup_ctx.append(create_background_task_to_prune_trash(wait_period_s))
56+
# SEE https://github.com/ITISFoundation/osparc-issues#468
57+
app.cleanup_ctx.append(
58+
_tasks_trash.create_background_task_to_prune_trash(wait_period_s)
59+
)

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,26 @@
66
# pylint: disable=unused-variable
77

88

9+
import logging
910
from collections.abc import AsyncIterable, Callable
1011
from pathlib import Path
1112

1213
import pytest
14+
from aiohttp import web
1315
from aiohttp.test_utils import TestClient
1416
from aioresponses import aioresponses
1517
from models_library.products import ProductName
18+
from pytest_mock import MockerFixture
19+
from pytest_simcore.helpers.logging_tools import log_context
1620
from pytest_simcore.helpers.monkeypatch_envs import setenvs_from_dict
1721
from pytest_simcore.helpers.typing_env import EnvVarsDict
1822
from pytest_simcore.helpers.webserver_login import NewUser, UserInfoDict
1923
from pytest_simcore.helpers.webserver_parametrizations import MockedStorageSubsystem
2024
from pytest_simcore.helpers.webserver_projects import NewProject
2125
from simcore_service_webserver.projects.models import ProjectDict
2226

27+
_logger = logging.getLogger(__name__)
28+
2329

2430
@pytest.fixture
2531
def app_environment(
@@ -79,3 +85,18 @@ def mocked_director_v2(director_v2_service_mock: aioresponses):
7985
@pytest.fixture
8086
def mocked_storage(storage_subsystem_mock: MockedStorageSubsystem):
8187
...
88+
89+
90+
@pytest.fixture
91+
def with_disabled_background_task_to_prune_trash(mocker: MockerFixture) -> None:
92+
async def _empty_lifespan(app: web.Application):
93+
with log_context(
94+
logging.INFO, "Fake background_task_to_prune_trash event", logger=_logger
95+
):
96+
yield
97+
98+
mocker.patch(
99+
"simcore_service_webserver.garbage_collector._tasks_trash.create_background_task_to_prune_trash",
100+
autospec=True,
101+
return_value=_empty_lifespan,
102+
)

services/web/server/tests/unit/with_dbs/03/trash/test_trash_service.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,21 @@
2020
from simcore_service_webserver.db.models import UserRole
2121
from simcore_service_webserver.projects import _trash_service
2222
from simcore_service_webserver.projects.models import ProjectDict
23-
from simcore_service_webserver.trash._service import delete_expired_trash
23+
from simcore_service_webserver.trash import trash_service
2424

2525

2626
@pytest.fixture
2727
def app_environment(
28-
app_environment: EnvVarsDict, monkeypatch: pytest.MonkeyPatch
28+
app_environment: EnvVarsDict,
29+
monkeypatch: pytest.MonkeyPatch,
30+
with_disabled_background_task_to_prune_trash: None,
2931
) -> EnvVarsDict:
3032
return app_environment | setenvs_from_dict(
31-
monkeypatch, {"TRASH_RETENTION_DAYS": "0"}
33+
monkeypatch,
34+
{
35+
"TRASH_RETENTION_DAYS": "0",
36+
"WEBSERVER_GARBAGE_COLLECTOR": "null",
37+
},
3238
)
3339

3440

@@ -104,7 +110,7 @@ async def test_trash_service__delete_expired_trash(
104110
assert ProjectGet.model_validate(data).trashed_by == logged_user["primary_gid"]
105111

106112
# UNDER TEST: Run delete_expired_trash
107-
await delete_expired_trash(client.app)
113+
await trash_service.delete_expired_trash(client.app)
108114

109115
# ASSERT: logged_user tries to get the project and expects 404
110116
resp = await client.get(f"/v0/projects/{user_project_id}")

0 commit comments

Comments
 (0)