|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import httpx |
| 4 | +import pytest |
| 5 | + |
| 6 | +from kernel import Kernel, AsyncKernel, APIStatusError, APIConnectionError |
| 7 | + |
| 8 | + |
| 9 | +@pytest.mark.parametrize("failure", ["connection", 409, 429, 500]) |
| 10 | +def test_fill_does_not_retry(failure: str | int) -> None: |
| 11 | + calls = 0 |
| 12 | + |
| 13 | + def handle(request: httpx.Request) -> httpx.Response: |
| 14 | + nonlocal calls |
| 15 | + calls += 1 |
| 16 | + if isinstance(failure, str): |
| 17 | + raise httpx.ReadError("connection lost", request=request) |
| 18 | + return httpx.Response(failure, json={}) |
| 19 | + |
| 20 | + with Kernel( |
| 21 | + api_key="test", max_retries=1, http_client=httpx.Client(transport=httpx.MockTransport(handle)) |
| 22 | + ) as client: |
| 23 | + with pytest.raises((APIConnectionError, APIStatusError)): |
| 24 | + client.vaults.items.perform_operation( |
| 25 | + "login", |
| 26 | + id_or_name="vault", |
| 27 | + type="fill", |
| 28 | + browser_id="browser", |
| 29 | + fields=[{"field": "password", "selector": "#password"}], |
| 30 | + ) |
| 31 | + assert calls == 1 |
| 32 | + assert client.max_retries == 1 |
| 33 | + |
| 34 | + |
| 35 | +@pytest.mark.parametrize("failure", ["connection", 409, 429, 500]) |
| 36 | +async def test_async_fill_does_not_retry(failure: str | int) -> None: |
| 37 | + calls = 0 |
| 38 | + |
| 39 | + def handle(request: httpx.Request) -> httpx.Response: |
| 40 | + nonlocal calls |
| 41 | + calls += 1 |
| 42 | + if isinstance(failure, str): |
| 43 | + raise httpx.ReadError("connection lost", request=request) |
| 44 | + return httpx.Response(failure, json={}) |
| 45 | + |
| 46 | + async with AsyncKernel( |
| 47 | + api_key="test", max_retries=1, http_client=httpx.AsyncClient(transport=httpx.MockTransport(handle)) |
| 48 | + ) as client: |
| 49 | + with pytest.raises((APIConnectionError, APIStatusError)): |
| 50 | + await client.vaults.items.perform_operation( |
| 51 | + "login", |
| 52 | + id_or_name="vault", |
| 53 | + type="fill", |
| 54 | + browser_id="browser", |
| 55 | + fields=[{"field": "password", "selector": "#password"}], |
| 56 | + ) |
| 57 | + assert calls == 1 |
| 58 | + assert client.max_retries == 1 |
| 59 | + |
| 60 | + |
| 61 | +def test_reads_keep_client_retries() -> None: |
| 62 | + calls = 0 |
| 63 | + |
| 64 | + def handle(request: httpx.Request) -> httpx.Response: |
| 65 | + nonlocal calls |
| 66 | + calls += 1 |
| 67 | + raise httpx.ReadError("connection lost", request=request) |
| 68 | + |
| 69 | + with Kernel( |
| 70 | + api_key="test", max_retries=1, http_client=httpx.Client(transport=httpx.MockTransport(handle)) |
| 71 | + ) as client: |
| 72 | + with pytest.raises(APIConnectionError): |
| 73 | + client.vaults.items.retrieve("login", id_or_name="vault") |
| 74 | + assert calls == 2 |
0 commit comments