fix(km): keep backfill reconciler loop alive
All checks were successful
Code Review / ai-code-review (push) Successful in 11s
CD Pipeline / tests (push) Successful in 1m12s
CD Pipeline / build-and-deploy (push) Successful in 4m2s
CD Pipeline / post-deploy-checks (push) Successful in 1m18s

This commit is contained in:
Your Name
2026-05-06 17:03:22 +08:00
parent 73d7e332a4
commit c1ac157aaf
3 changed files with 61 additions and 2 deletions

View File

@@ -25,7 +25,9 @@ Feature Flag
from __future__ import annotations
import asyncio
import json
import structlog
from src.core.config import settings

View File

@@ -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 → 跳過
# =============================================================================