소유자가 2026-10-27 실제 적용과 정기 실행을 승인했다. 원본 90일·집계 365일· 집계 대상 7일 창은 2026-10-20 운영 dry-run과 같은 값으로 고정하고, 데이터를 지우는 작업이라 기본값은 꺼짐이다. DB 풀이 정상일 때만 예약하며 실패는 기록만 하고 다음 회차를 계속한다. NAS compose는 환경변수를 명시 전달하므로 설정 3개를 api 환경에 추가한다.
114 lines
4.3 KiB
Python
114 lines
4.3 KiB
Python
"""관리자 헬스 표본 보존 정책 일일 자동 실행 스케줄러 테스트.
|
|
|
|
실제 DB 접속이나 real-time sleep 없이 `_retention_loop` / `schedule_admin_health_retention` /
|
|
`stop_admin_health_retention`의 동작을 검증한다.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import asyncio
|
|
from unittest import IsolatedAsyncioTestCase
|
|
from unittest.mock import AsyncMock, patch
|
|
|
|
from .services import admin_health_maintenance as maintenance
|
|
|
|
|
|
def _result(
|
|
*, deleted_events: int = 0, deleted_rollups: int = 0, upserted_rollups: int = 0
|
|
) -> maintenance.AdminHealthMaintenanceResult:
|
|
return maintenance.AdminHealthMaintenanceResult(
|
|
applied=True,
|
|
rollup_days=7,
|
|
retention_days=90,
|
|
rollup_retention_days=365,
|
|
rollup_event_count=0,
|
|
rollup_bucket_count=0,
|
|
upserted_rollups=upserted_rollups,
|
|
prunable_event_count=0,
|
|
deleted_events=deleted_events,
|
|
prunable_rollup_count=0,
|
|
deleted_rollups=deleted_rollups,
|
|
)
|
|
|
|
|
|
class AdminHealthRetentionSchedulerTest(IsolatedAsyncioTestCase):
|
|
async def test_schedule_returns_none_when_disabled_by_default(self) -> None:
|
|
# 데이터를 지우는 작업이라 설정 기본값 자체가 꺼짐이어야 한다.
|
|
self.assertIs(
|
|
type(maintenance.settings).model_fields["admin_health_retention_enabled"].default,
|
|
False,
|
|
)
|
|
with patch.object(
|
|
maintenance.settings,
|
|
"admin_health_retention_enabled",
|
|
False,
|
|
):
|
|
self.assertIsNone(maintenance.schedule_admin_health_retention())
|
|
|
|
async def test_loop_applies_maintenance_with_fixed_retention_windows(self) -> None:
|
|
maintain = AsyncMock(return_value=_result())
|
|
with (
|
|
patch.object(maintenance.settings, "admin_health_retention_enabled", True),
|
|
patch.object(
|
|
maintenance.settings, "admin_health_retention_startup_delay_seconds", 0.0
|
|
),
|
|
patch.object(maintenance, "maintain_admin_health_events", maintain),
|
|
patch.object(
|
|
maintenance.asyncio,
|
|
"sleep",
|
|
AsyncMock(side_effect=asyncio.CancelledError()),
|
|
),
|
|
):
|
|
with self.assertRaises(asyncio.CancelledError):
|
|
await maintenance._retention_loop()
|
|
|
|
maintain.assert_awaited_once_with(
|
|
rollup_days=7,
|
|
retention_days=90,
|
|
rollup_retention_days=365,
|
|
apply=True,
|
|
)
|
|
|
|
async def test_loop_continues_after_maintenance_failure(self) -> None:
|
|
maintain = AsyncMock(side_effect=[RuntimeError("boom"), _result()])
|
|
sleep = AsyncMock(side_effect=[None, asyncio.CancelledError()])
|
|
with (
|
|
patch.object(maintenance.settings, "admin_health_retention_enabled", True),
|
|
patch.object(
|
|
maintenance.settings, "admin_health_retention_startup_delay_seconds", 0.0
|
|
),
|
|
patch.object(maintenance, "maintain_admin_health_events", maintain),
|
|
patch.object(maintenance.asyncio, "sleep", sleep),
|
|
):
|
|
with self.assertRaises(asyncio.CancelledError):
|
|
await maintenance._retention_loop()
|
|
|
|
self.assertEqual(maintain.await_count, 2)
|
|
self.assertEqual(sleep.await_count, 2)
|
|
|
|
async def test_stop_cancels_and_clears_scheduled_task(self) -> None:
|
|
maintain = AsyncMock(return_value=_result())
|
|
with (
|
|
patch.object(maintenance.settings, "admin_health_retention_enabled", True),
|
|
patch.object(
|
|
maintenance.settings, "admin_health_retention_startup_delay_seconds", 0.0
|
|
),
|
|
patch.object(
|
|
maintenance.settings, "admin_health_retention_interval_seconds", 0.01
|
|
),
|
|
patch.object(maintenance, "maintain_admin_health_events", maintain),
|
|
):
|
|
task = maintenance.schedule_admin_health_retention()
|
|
self.assertIsNotNone(task)
|
|
await asyncio.sleep(0.05)
|
|
await maintenance.stop_admin_health_retention()
|
|
|
|
self.assertTrue(task.cancelled() or task.done())
|
|
self.assertIsNone(maintenance._RETENTION_TASK)
|
|
maintain.assert_awaited()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
import unittest
|
|
|
|
unittest.main()
|