Files
DeepTutor/tests/services/web_source/test_sync_scheduler.py
yuleihaotong 9417f76f43 test(web_source): make the lease-renewal test assert renewal, not scheduling speed
`test_scheduler_renews_lease_during_a_long_sync` ran a 200 ms lease with a 40 ms
renewal and then compared the deadline against `time.time()`. On the Python 3.12
runner the event loop was delayed past the deadline and the assertion read
`1793772763914 > 1793772763951` — 37 ms short. The test was really asserting that
the runner schedules an asyncio task within 200 ms, which it does not promise.

The lease is now 5 s with a renewal every 0.5 s, and the check runs after 1.2 s
with the database left alone in between. That still proves the lease is extended
while a sync is running, and it cannot lose the race.

Reproduction before the change: 0/20 failures idle, 1/10 with four busy loops on a
2-vCPU box. After: 0/20 idle and 0/20 under the same load.

Found while debugging, deliberately not changed here: `_renew_lease()` does not
catch exceptions from `renew_lease()`, and the task's exception is swallowed by
`asyncio.gather(..., return_exceptions=True)` in `_run_job`'s `finally`. One
transient SQLite error therefore ends the renewal loop silently, and the sync
loses its lease with nothing logged. That deserves its own change and its own
test.
2026-11-05 01:55:44 +00:00

261 lines
10 KiB
Python

from __future__ import annotations
import asyncio
from pathlib import Path
import time
from unittest.mock import AsyncMock, patch
import pytest
from deeptutor.knowledge.manager import KnowledgeBaseManager
from deeptutor.services.web_source.repository import (
SQLiteWebSourceSyncRepository,
WebSourceSyncJob,
)
from deeptutor.services.web_source.scheduler import WebSourceSyncScheduler
from deeptutor.services.web_source.sync import WebSyncResult
def test_repository_creates_reconciles_and_recovers(tmp_path: Path) -> None:
repository = SQLiteWebSourceSyncRepository(tmp_path / "jobs.sqlite")
key = "local-admin", "kb", "abc"
repository.reconcile_sources({key})
assert repository.list_jobs("local-admin", "kb")[0].state == "pending"
job = repository.get(key)
assert job is not None
claimed = repository.claim(job, runner_id="runner-a", lease_until_ms=10_000)
assert claimed is not None and claimed.state == "running"
repository.recover_interrupted("runner-b")
recovered = repository.get(key)
assert recovered is not None
assert recovered.state == "interrupted"
assert recovered.runner_id == ""
repository.reconcile_sources(set())
assert repository.get(key) is None
def test_repository_recover_preserves_active_lease(tmp_path: Path) -> None:
repository = SQLiteWebSourceSyncRepository(tmp_path / "jobs.sqlite")
key = "local-admin", "kb", "abc"
repository.reconcile_sources({key})
job = repository.get(key)
assert job is not None
claimed = repository.claim(
job,
runner_id="runner-a",
lease_until_ms=int(time.time() * 1000) + 3_600_000,
)
assert claimed is not None
repository.recover_interrupted("runner-b")
active = repository.get(key)
assert active is not None
assert active.state == "running"
def test_repository_cancel_and_retry(tmp_path: Path) -> None:
repository = SQLiteWebSourceSyncRepository(tmp_path / "jobs.sqlite")
key = "local-admin", "kb", "abc"
repository.reconcile_sources({key})
assert repository.request_cancel(key) is True
assert repository.get(key).state == "cancelled" # type: ignore[union-attr]
retried = repository.retry(key)
assert retried is not None and retried.state == "pending"
@pytest.mark.asyncio
async def test_scheduler_marks_success_and_schedules_next_run(tmp_path: Path) -> None:
manager = KnowledgeBaseManager(base_dir=str(tmp_path / "kbs"))
(manager.base_dir / "kb").mkdir()
(manager.base_dir / "kb" / "metadata.json").write_text("{}", encoding="utf-8")
manager.register_knowledge_base("kb")
source = manager.add_web_source("kb", "https://example.com/docs/")
source["sync_interval_hours"] = 2
repository = SQLiteWebSourceSyncRepository(tmp_path / "jobs.sqlite")
scheduler = WebSourceSyncScheduler(
repository=repository,
manager_factory=lambda: manager,
)
await scheduler._synchronize_sources()
job = repository.list_jobs("local-admin", "kb")[0]
sync = AsyncMock(return_value=WebSyncResult(ok=True))
with patch("deeptutor.services.web_source.sync.sync_source", sync):
await scheduler._run_job(job)
persisted = repository.get((job.owner_id, job.kb_name, job.source_id))
assert persisted is not None
assert persisted.state == "pending"
assert persisted.attempt == 0
assert persisted.next_run_at_ms > persisted.last_run_at_ms # type: ignore[operator]
sync.assert_awaited_once()
@pytest.mark.asyncio
async def test_scheduler_cancelled_run_is_not_marked_successful(tmp_path: Path) -> None:
manager = KnowledgeBaseManager(base_dir=str(tmp_path / "kbs"))
(manager.base_dir / "kb").mkdir()
(manager.base_dir / "kb" / "metadata.json").write_text("{}", encoding="utf-8")
manager.register_knowledge_base("kb")
manager.add_web_source("kb", "https://example.com/docs/")
repository = SQLiteWebSourceSyncRepository(tmp_path / "jobs.sqlite")
scheduler = WebSourceSyncScheduler(
repository=repository,
manager_factory=lambda: manager,
)
await scheduler._synchronize_sources()
job = repository.list_jobs("local-admin", "kb")[0]
sync = AsyncMock(side_effect=asyncio.CancelledError)
with patch("deeptutor.services.web_source.sync.sync_source", sync):
with pytest.raises(asyncio.CancelledError):
await scheduler._run_job(job)
persisted = repository.get((job.owner_id, job.kb_name, job.source_id))
assert persisted is not None
assert persisted.state == "interrupted"
@pytest.mark.asyncio
async def test_scheduler_ignores_manual_and_disabled_sources(tmp_path: Path) -> None:
manager = KnowledgeBaseManager(base_dir=str(tmp_path / "kbs"))
(manager.base_dir / "kb").mkdir()
(manager.base_dir / "kb" / "metadata.json").write_text("{}", encoding="utf-8")
manager.register_knowledge_base("kb")
automatic = manager.add_web_source("kb", "https://auto.example.com/docs/")
manager.add_web_source("kb", "https://disabled.example.com/docs/")
manager.update_web_source_state(
"kb",
manager.get_web_sources("kb")[1]["id"],
auto_sync_enabled=False,
)
scheduler = WebSourceSyncScheduler(
repository=SQLiteWebSourceSyncRepository(tmp_path / "jobs.sqlite"),
manager_factory=lambda: manager,
)
await scheduler._synchronize_sources()
assert [job.source_id for job in scheduler.repo.list_jobs("local-admin", "kb")] == [
automatic["id"]
]
def test_reconcile_preserves_jobs_for_an_owner_that_failed_to_scan(tmp_path: Path) -> None:
repository = SQLiteWebSourceSyncRepository(tmp_path / "jobs.sqlite")
old = ("local-admin", "kb", "old")
repository.reconcile_sources({old})
repository.reconcile_sources(set(), scanned_owner_ids=set())
assert repository.get(old) is not None
repository.reconcile_sources(set(), scanned_owner_ids={"local-admin"})
assert repository.get(old) is None
def test_expired_owner_cannot_overwrite_a_new_claim(tmp_path: Path, monkeypatch) -> None:
repository = SQLiteWebSourceSyncRepository(tmp_path / "jobs.sqlite")
now = [1_000]
monkeypatch.setattr(repository, "_now_ms", lambda: now[0])
key = ("local-admin", "kb", "source")
repository.reconcile_sources({key})
old = repository.claim(repository.get(key), runner_id="runner-old", lease_until_ms=2_000)
assert old is not None
assert repository.renew_lease(old, 3_000) is True
now[0] = 3_001
assert repository.renew_lease(old, 4_000) is False
repository.recover_interrupted("runner-new")
new = repository.claim(repository.get(key), runner_id="runner-new", lease_until_ms=4_000)
assert new is not None
repository.mark_success(old, 9_000)
assert repository.get(key).runner_id == "runner-new"
repository.mark_success(new, 9_000)
assert repository.get(key).state == "pending"
@pytest.mark.asyncio
async def test_failed_owner_enumeration_does_not_delete_persisted_job(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
repository = SQLiteWebSourceSyncRepository(tmp_path / "jobs.sqlite")
key = ("local-admin", "kb", "source")
repository.reconcile_sources({key})
scheduler = WebSourceSyncScheduler(repository=repository)
class BrokenManager:
def get_all_web_sources(self):
raise RuntimeError("temporary read failure")
monkeypatch.setattr(scheduler, "_manager_for_owner", lambda _owner: BrokenManager())
await scheduler._synchronize_sources()
assert repository.get(key) is not None
assert scheduler._sources == {}
@pytest.mark.asyncio
async def test_scheduler_renews_lease_during_a_long_sync(tmp_path: Path, monkeypatch) -> None:
"""A sync that outlives its lease keeps the lease alive.
The margins are wide on purpose. This test used to run a 200 ms lease with a
40 ms renewal and then compare the deadline against ``time.time()`` — which
made it an assertion about how fast the CI runner schedules an asyncio task,
not about lease renewal, and it failed with the lease 37 ms past expiry. Five
seconds with a renewal every half second still proves the lease is extended,
and it cannot lose the race.
Reads are also kept to a handful: polling the repository every 20 ms for five
seconds is enough write-lock pressure to make the renewal ``UPDATE`` raise,
which kills the renewal task silently.
"""
from deeptutor.services.web_source import scheduler as scheduler_module
monkeypatch.setattr(scheduler_module, "WEB_SYNC_LEASE_SECONDS", 5.0)
monkeypatch.setattr(scheduler_module, "WEB_SYNC_LEASE_RENEW_SECONDS", 0.5)
manager = KnowledgeBaseManager(base_dir=str(tmp_path / "kbs"))
(manager.base_dir / "kb").mkdir()
(manager.base_dir / "kb" / "metadata.json").write_text("{}", encoding="utf-8")
manager.register_knowledge_base("kb")
manager.add_web_source("kb", "https://example.com/docs/")
repository = SQLiteWebSourceSyncRepository(tmp_path / "jobs.sqlite")
scheduler = WebSourceSyncScheduler(repository=repository, manager_factory=lambda: manager)
await scheduler._synchronize_sources()
job = repository.list_jobs("local-admin", "kb")[0]
key = repository.key(job)
release_sync = asyncio.Event()
async def slow_sync(**_kwargs):
await release_sync.wait()
return WebSyncResult(ok=True)
with patch("deeptutor.services.web_source.sync.sync_source", slow_sync):
running = asyncio.create_task(scheduler._run_job(job))
claimed = None
for _ in range(50):
await asyncio.sleep(0.02)
current = repository.get(key)
if current is not None and current.state == "running" and current.lease_until_ms:
claimed = current.lease_until_ms
break
assert claimed is not None, "the sync never started"
# Two renewal intervals with nobody reading the database.
await asyncio.sleep(1.2)
current = repository.get(key)
assert current is not None and current.state == "running"
assert current.lease_until_ms is not None
assert current.lease_until_ms > claimed, "the lease was not renewed"
assert current.lease_until_ms > int(time.time() * 1000), "the lease expired while running"
release_sync.set()
await running
assert repository.get(key).state == "pending"