fix(km): keep backfill reconciler loop alive
This commit is contained in:
@@ -25,7 +25,9 @@ Feature Flag:
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
|
||||
import structlog
|
||||
|
||||
from src.core.config import settings
|
||||
|
||||
@@ -16,19 +16,20 @@ P1-1 C1 修復 2026-04-28 ogt + Claude Sonnet 4.6
|
||||
"""
|
||||
|
||||
import json
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
import src.jobs.km_backfill_reconciler_job as reconciler_job
|
||||
from src.jobs.km_backfill_reconciler_job import (
|
||||
run_km_backfill_reconciler,
|
||||
run_km_backfill_reconciler_loop,
|
||||
)
|
||||
from src.services.km_writer import (
|
||||
KM_BACKFILL_DLQ_KEY,
|
||||
_backfill_path_a_approval_safe,
|
||||
)
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Helper
|
||||
# =============================================================================
|
||||
@@ -136,6 +137,32 @@ async def test_reconciler_empty_dlq():
|
||||
assert result["failed"] == 0
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_reconciler_loop_can_sleep(monkeypatch: pytest.MonkeyPatch):
|
||||
"""loop 必須能 sleep;避免少 import asyncio 導致 background task 啟動即死亡。"""
|
||||
|
||||
class StopLoop(Exception):
|
||||
pass
|
||||
|
||||
calls = 0
|
||||
|
||||
async def fake_run_once():
|
||||
nonlocal calls
|
||||
calls += 1
|
||||
return {"processed": 0, "success": 0, "failed": 0}
|
||||
|
||||
async def fake_sleep(_seconds: int):
|
||||
raise StopLoop
|
||||
|
||||
monkeypatch.setattr(reconciler_job, "run_km_backfill_reconciler", fake_run_once)
|
||||
monkeypatch.setattr(reconciler_job.asyncio, "sleep", fake_sleep)
|
||||
|
||||
with pytest.raises(StopLoop):
|
||||
await run_km_backfill_reconciler_loop()
|
||||
|
||||
assert calls == 1
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# 5. ENABLE_KM_BACKFILL_RECONCILER=false → 跳過
|
||||
# =============================================================================
|
||||
|
||||
Reference in New Issue
Block a user