deer-flow/backend/tests/test_scheduler_shutdown_timeout.py
Xinmin Zeng df544f126a
test(backend): prune 32 zero-unique redundant suites and rewrite 9 source-inspection tests (#8362)
* test(backend): prune 32 zero-unique redundant suites and rewrite 9 source-inspection tests

- Remove 32 superseded test files proven lossless across statement/branch coverage (per-test dynamic contexts), hotspot mutation testing (mutmut), and 20 historical bug replays.
- Replace all inspect.getsource and brittle source-string assertions across 9 test files with behavioral runtime assertions.

* test(backend): consolidate hidden-answer and compaction-hash guards and assert protocol defaults

- Fold hidden human-input turn-boundary regressions into tests/test_channels.py while keeping test_channel_hidden_human_input.py removed.
- Fold multimodal source content hashing and unobserved-skip regressions into tests/test_compaction_event_identity.py.
- Assert contract-specific default return values/types (False for bool registrations, empty sequence for middleware contributions, explicit return None for void hooks) in test_extension_api_contracts.py and add stub rejection tests.
2026-11-18 21:13:19 +08:00

85 lines
3.3 KiB
Python

from __future__ import annotations
import asyncio
import logging
from contextlib import ExitStack, asynccontextmanager
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from fastapi import FastAPI
from app.gateway import app as gateway_app
@pytest.mark.asyncio
async def test_scheduled_task_service_shutdown_is_bounded(monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture) -> None:
async def hang() -> None:
await asyncio.sleep(3600)
app = FastAPI()
service = MagicMock()
service.stop = AsyncMock(side_effect=hang)
app.state.scheduled_task_service = service
monkeypatch.setattr(gateway_app, "_SHUTDOWN_HOOK_TIMEOUT_SECONDS", 0.01)
started = asyncio.get_running_loop().time()
with caplog.at_level(logging.WARNING, logger="app.gateway.app"):
await gateway_app._shutdown_scheduled_task_service(app)
elapsed = asyncio.get_running_loop().time() - started
assert elapsed < 0.5
service.stop.assert_awaited_once()
assert "Scheduled task service shutdown exceeded" in caplog.text
@pytest.mark.asyncio
async def test_lifespan_bounds_hung_scheduled_task_shutdown(monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture) -> None:
async def hang() -> None:
await asyncio.sleep(3600)
service = MagicMock()
service.stop = AsyncMock(side_effect=hang)
@asynccontextmanager
async def _runtime_with_hung_scheduler(app: FastAPI, _startup_config: object):
app.state.scheduled_task_service = service
yield
app = FastAPI()
startup_config = MagicMock()
startup_config.log_level = "INFO"
startup_config.memory.enabled = False
startup_config.memory.shutdown_flush_timeout_seconds = 5.0
startup_config.scheduler.enabled = False
monkeypatch.setattr(gateway_app, "_SHUTDOWN_HOOK_TIMEOUT_SECONDS", 0.01)
channel_service = MagicMock()
channel_service.get_status.return_value = {}
patches = [
patch("app.gateway.app.get_app_config", return_value=startup_config),
patch("app.gateway.app.get_gateway_config", return_value=MagicMock(host="x", port=0)),
patch("app.gateway.app.langgraph_runtime", _runtime_with_hung_scheduler),
patch("app.gateway.app.auth.close_oidc_service", AsyncMock()),
patch("app.channels.service.start_channel_service", AsyncMock(return_value=channel_service)),
patch("app.channels.service.stop_channel_service", AsyncMock()),
patch("deerflow.skills.projection.ensure_public_skill_projection"),
patch("deerflow.agents.memory.get_memory_manager", return_value=MagicMock()),
patch("deerflow.mcp.session_pool.get_session_pool", return_value=MagicMock(close_all=AsyncMock())),
patch(
"deerflow.community.browser_automation.get_browser_session_manager",
return_value=MagicMock(close_all_sessions=AsyncMock(return_value=0)),
),
]
with ExitStack() as stack:
for patcher in patches:
stack.enter_context(patcher)
with caplog.at_level(logging.WARNING, logger="app.gateway.app"):
async with gateway_app.lifespan(app):
started = asyncio.get_running_loop().time()
elapsed = asyncio.get_running_loop().time() - started
assert elapsed < 0.5
service.stop.assert_awaited_once()
assert "Scheduled task service shutdown exceeded" in caplog.text