mirror of
https://github.com/fawney19/Aether.git
synced 2026-09-02 01:10:23 +08:00
feat: 添加启动任务开关,修复统计聚合内存泄漏
- 新增 CACHE_WARMUP_ENABLED 和 MAINTENANCE_STARTUP_TASKS_ENABLED 环境变量, 允许禁用缓存预热和维护调度器启动任务 - 在统计聚合批量处理循环中添加 db.expunge_all(),释放 Session identity map, 防止 ORM 对象累积导致内存暴涨 - 添加启动任务开关的单元测试
This commit is contained in:
86
tests/services/test_startup_toggles.py
Normal file
86
tests/services/test_startup_toggles.py
Normal file
@@ -0,0 +1,86 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import inspect
|
||||
from types import SimpleNamespace
|
||||
|
||||
import pytest
|
||||
|
||||
import src.services.system.cache_warmup as cache_warmup_module
|
||||
import src.services.system.maintenance_scheduler as maintenance_scheduler_module
|
||||
from src.config.settings import config
|
||||
from src.services.system.maintenance_scheduler import MaintenanceScheduler
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_start_cache_warmup_skips_task_when_disabled(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
monkeypatch.setattr(config, "cache_warmup_enabled", False)
|
||||
|
||||
created = False
|
||||
|
||||
def fake_create_task(coro): # type: ignore[no-untyped-def]
|
||||
nonlocal created
|
||||
created = True
|
||||
if inspect.iscoroutine(coro):
|
||||
coro.close()
|
||||
return object()
|
||||
|
||||
monkeypatch.setattr(cache_warmup_module.asyncio, "create_task", fake_create_task)
|
||||
|
||||
await cache_warmup_module.start_cache_warmup()
|
||||
|
||||
assert created is False
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_start_cache_warmup_creates_task_when_enabled(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
monkeypatch.setattr(config, "cache_warmup_enabled", True)
|
||||
|
||||
created = False
|
||||
|
||||
def fake_create_task(coro): # type: ignore[no-untyped-def]
|
||||
nonlocal created
|
||||
created = True
|
||||
if inspect.iscoroutine(coro):
|
||||
coro.close()
|
||||
return object()
|
||||
|
||||
monkeypatch.setattr(cache_warmup_module.asyncio, "create_task", fake_create_task)
|
||||
|
||||
await cache_warmup_module.start_cache_warmup()
|
||||
|
||||
assert created is True
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_maintenance_scheduler_start_skips_startup_task_when_disabled(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
monkeypatch.setattr(config, "maintenance_startup_tasks_enabled", False)
|
||||
|
||||
scheduler = MaintenanceScheduler()
|
||||
|
||||
created = False
|
||||
|
||||
def fake_create_task(coro): # type: ignore[no-untyped-def]
|
||||
nonlocal created
|
||||
created = True
|
||||
if inspect.iscoroutine(coro):
|
||||
coro.close()
|
||||
return object()
|
||||
|
||||
monkeypatch.setattr(maintenance_scheduler_module.asyncio, "create_task", fake_create_task)
|
||||
monkeypatch.setattr(scheduler, "_get_checkin_time", lambda: (1, 5))
|
||||
monkeypatch.setattr(
|
||||
maintenance_scheduler_module,
|
||||
"get_scheduler",
|
||||
lambda: SimpleNamespace(
|
||||
add_cron_job=lambda *args, **kwargs: None,
|
||||
add_interval_job=lambda *args, **kwargs: None,
|
||||
),
|
||||
)
|
||||
|
||||
await scheduler.start()
|
||||
|
||||
assert created is False
|
||||
Reference in New Issue
Block a user