Files
AdrianoDev 9da2e12473
ci / ruff lint (push) Successful in 15s
ci / validate compose + Caddyfile (push) Successful in 2m6s
ci / mypy mcp_common (push) Successful in 30s
ci / pytest (push) Successful in 34s
ci / build & push to registry (push) Failing after 47s
lint: ruff clean services/ (autofix + manual + ignore E741)
- 24 autofix safe (SIM105 contextlib.suppress, F401 unused imports,
  I001 import order, B007 unused loop var, F811 redef, F841 unused).
- 15 unsafe-fix (UP038 X|Y in isinstance, SIM108 ternary, ecc.).
- Manual fix: SIM102 nested if in deribit term_structure, E402 imports
  in test_cot.py + sentiment server.py.
- Ignore E741 (variabili 'l' in list comprehensions deribit/client.py
  — stilistico, non bug).

Tests: 478/478 verdi.
2026-04-29 08:44:12 +02:00

73 lines
1.7 KiB
Python

from __future__ import annotations
import asyncio
import httpx
import pytest
from mcp_common.http import async_client, call_with_retry
def test_async_client_uses_retry_transport():
c = async_client(retries=5)
assert isinstance(c._transport, httpx.AsyncHTTPTransport)
# internal _retries on transport
assert c._transport._pool._retries == 5
@pytest.mark.asyncio
async def test_call_with_retry_succeeds_first_try():
calls = 0
async def fn():
nonlocal calls
calls += 1
return "ok"
result = await call_with_retry(fn)
assert result == "ok"
assert calls == 1
@pytest.mark.asyncio
async def test_call_with_retry_recovers_after_transient(monkeypatch):
monkeypatch.setattr(asyncio, "sleep", asyncio.coroutine(lambda *_: None) if False else _no_sleep)
calls = 0
async def fn():
nonlocal calls
calls += 1
if calls < 3:
raise httpx.ConnectError("boom")
return "ok"
result = await call_with_retry(fn, max_attempts=5, base_delay=0.0)
assert result == "ok"
assert calls == 3
async def _no_sleep(_):
return None
@pytest.mark.asyncio
async def test_call_with_retry_gives_up_after_max():
calls = 0
async def fn():
nonlocal calls
calls += 1
raise httpx.TimeoutException("slow")
with pytest.raises(httpx.TimeoutException):
await call_with_retry(fn, max_attempts=3, base_delay=0.0)
assert calls == 3
@pytest.mark.asyncio
async def test_call_with_retry_does_not_catch_unexpected():
async def fn():
raise ValueError("not transient")
with pytest.raises(ValueError):
await call_with_retry(fn, max_attempts=5, base_delay=0.0)