feat: schedule periodic user synchronization

This commit is contained in:
Bastian Wagner
2026-08-15 16:14:25 +02:00
parent 1d414d6298
commit fd50bbbab7
4 changed files with 151 additions and 1 deletions

View File

@@ -0,0 +1,42 @@
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