import asyncio import pytest from app.sync.scheduler import SyncScheduler class FakeSyncManager: def __init__(self, results=None) -> None: self.results = results if results is not None else [] self.calls = 0 async def sync_all_enabled(self): self.calls += 1 if self.results and isinstance(self.results[0], Exception): raise self.results[0] return list(self.results) @pytest.mark.asyncio async def test_scheduler_calls_sync_all_and_survives_failure() -> None: fake = FakeSyncManager(results=[RuntimeError("one user failed")]) scheduler = SyncScheduler(fake, interval_seconds=0.01) await scheduler.start() await asyncio.sleep(0.035) await scheduler.stop() assert fake.calls >= 2 assert scheduler.last_tick is not None @pytest.mark.asyncio async def test_scheduler_stop_cancels_the_loop() -> None: fake = FakeSyncManager() scheduler = SyncScheduler(fake, interval_seconds=0.01) await scheduler.start() await asyncio.sleep(0.035) await scheduler.stop() assert scheduler._task is None calls_after_stop = fake.calls await asyncio.sleep(0.05) assert fake.calls == calls_after_stop