Refactor periodic tasks to use injected scheduler resources
This commit is contained in:
@@ -280,16 +280,21 @@ class TestApplySchedule:
|
||||
_, kwargs = scheduler.add_job.call_args
|
||||
assert kwargs["id"] == JOB_ID
|
||||
|
||||
def test_apply_schedule_passes_app_in_kwargs(self) -> None:
|
||||
"""The scheduled job must receive ``app`` as a kwarg for state access."""
|
||||
def test_apply_schedule_passes_resources_in_kwargs(self) -> None:
|
||||
"""The scheduled job must receive explicit resources instead of app."""
|
||||
scheduler = _make_scheduler()
|
||||
app = self._make_app_with_scheduler(scheduler)
|
||||
app.state.settings = MagicMock(database_path="/tmp/fake.db")
|
||||
app.state.http_session = MagicMock()
|
||||
config = ScheduleConfig(frequency=ScheduleFrequency.daily)
|
||||
|
||||
_apply_schedule(app, config)
|
||||
|
||||
_, kwargs = scheduler.add_job.call_args
|
||||
assert kwargs["kwargs"] == {"app": app}
|
||||
assert kwargs["kwargs"] == {
|
||||
"settings": app.state.settings,
|
||||
"http_session": app.state.http_session,
|
||||
}
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
@@ -140,11 +140,11 @@ class TestRegister:
|
||||
_, kwargs = app.state.scheduler.add_job.call_args
|
||||
assert kwargs["replace_existing"] is True
|
||||
|
||||
def test_register_passes_app_in_kwargs(self) -> None:
|
||||
"""The scheduled job must receive ``app`` as a kwarg for state access."""
|
||||
def test_register_passes_settings_in_kwargs(self) -> None:
|
||||
"""The scheduled job must receive settings as a kwarg instead of app."""
|
||||
app = _make_app()
|
||||
|
||||
register(app)
|
||||
|
||||
_, kwargs = app.state.scheduler.add_job.call_args
|
||||
assert kwargs["kwargs"] == {"app": app}
|
||||
assert kwargs["kwargs"] == {"settings": app.state.settings}
|
||||
|
||||
@@ -16,6 +16,7 @@ import pytest
|
||||
from app.models.config import PendingRecovery
|
||||
from app.models.server import ServerStatus
|
||||
from app.tasks.health_check import HEALTH_CHECK_INTERVAL, _run_probe, register
|
||||
from app.utils.runtime_state import ApplicationState, RuntimeState
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Helpers
|
||||
@@ -31,12 +32,15 @@ def _make_app(prev_online: bool = False) -> MagicMock:
|
||||
Returns:
|
||||
A :class:`unittest.mock.MagicMock` that mimics ``fastapi.FastAPI``.
|
||||
"""
|
||||
runtime_state = RuntimeState(
|
||||
server_status=ServerStatus(online=prev_online),
|
||||
pending_recovery=None,
|
||||
last_activation=None,
|
||||
)
|
||||
app = MagicMock()
|
||||
app.state.settings.fail2ban_socket = "/var/run/fail2ban/fail2ban.sock"
|
||||
app.state.server_status = ServerStatus(online=prev_online)
|
||||
app.state = ApplicationState(runtime_state)
|
||||
app.state.settings = MagicMock(fail2ban_socket="/var/run/fail2ban/fail2ban.sock")
|
||||
app.state.scheduler = MagicMock()
|
||||
app.state.last_activation = None
|
||||
app.state.pending_recovery = None
|
||||
return app
|
||||
|
||||
|
||||
@@ -232,14 +236,17 @@ class TestRegister:
|
||||
_, kwargs = app.state.scheduler.add_job.call_args
|
||||
assert kwargs["replace_existing"] is True
|
||||
|
||||
def test_register_passes_app_in_kwargs(self) -> None:
|
||||
"""The scheduled job must receive ``app`` as a kwarg for state access."""
|
||||
def test_register_passes_resources_in_kwargs(self) -> None:
|
||||
"""The scheduled job must receive explicit resources instead of app."""
|
||||
app = _make_app()
|
||||
|
||||
register(app)
|
||||
|
||||
_, kwargs = app.state.scheduler.add_job.call_args
|
||||
assert kwargs["kwargs"] == {"app": app}
|
||||
assert kwargs["kwargs"] == {
|
||||
"settings": app.state.settings,
|
||||
"runtime_state": app.state.runtime_state,
|
||||
}
|
||||
|
||||
def test_register_initialises_last_activation_none(self) -> None:
|
||||
"""``register`` must set ``app.state.last_activation = None``."""
|
||||
|
||||
@@ -26,7 +26,7 @@ class TestHistorySyncTask:
|
||||
fake_scheduler.add_job.assert_called_once()
|
||||
called_args, called_kwargs = fake_scheduler.add_job.call_args
|
||||
assert called_kwargs["id"] == history_sync.JOB_ID
|
||||
assert called_kwargs["kwargs"]["app"] == app
|
||||
assert called_kwargs["kwargs"]["settings"] is app.state.settings
|
||||
|
||||
async def test_backfill_window_is_7_5_days(self) -> None:
|
||||
assert history_sync.BACKFILL_WINDOW == 648000
|
||||
|
||||
Reference in New Issue
Block a user