MpakoImageAIbot/tests/test_retry.py

46 lines
989 B
Python

from __future__ import annotations
import pytest
from telegram.error import TimedOut
from bot import MpakoBot
from config import Settings
def make_settings() -> Settings:
return Settings(
bot_token="",
telegram_retry_attempts=3,
telegram_retry_max_delay=0.01,
telegram_health_interval=3600,
)
async def test_retry_succeeds_after_timeouts() -> None:
bot = MpakoBot(make_settings())
calls = 0
async def flaky() -> str:
nonlocal calls
calls += 1
if calls < 3:
raise TimedOut("proxy timeout")
return "ok"
assert await bot._retry(flaky) == "ok"
assert calls == 3
async def test_retry_raises_after_limit() -> None:
bot = MpakoBot(make_settings())
calls = 0
async def broken() -> str:
nonlocal calls
calls += 1
raise TimedOut("proxy timeout")
with pytest.raises(TimedOut):
await bot._retry(broken, attempts=2)
assert calls == 2