-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_immediate_retry.py
49 lines (37 loc) · 1.31 KB
/
test_immediate_retry.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"""
Example of implementing immediate retry policy (spam policy) where the client
should retry immediately without a delay.
"""
import httpx
import pytest
import respx
from httpx_retry import AsyncRetryTransport, RetryPolicy, RetryTransport
@respx.mock()
def test_immediate_retry(respx_mock: respx.MockRouter):
route = respx_mock.get("https://example.com")
route.side_effect = [
httpx.Response(500),
httpx.Response(500),
httpx.Response(200),
]
immediate_retry = RetryPolicy().with_attempts(3).with_delay(0)
with httpx.Client(transport=RetryTransport(policy=immediate_retry)) as client:
res = client.get("https://example.com")
assert res.status_code == 200
assert route.call_count == 3
@pytest.mark.asyncio()
@respx.mock()
async def test_async_immediate_retry(respx_mock: respx.MockRouter):
route = respx_mock.get("https://example.com")
route.side_effect = [
httpx.Response(500),
httpx.Response(500),
httpx.Response(200),
]
immediate_retry = RetryPolicy().with_attempts(3).with_delay(0)
async with httpx.AsyncClient(
transport=AsyncRetryTransport(policy=immediate_retry)
) as client:
res = await client.get("https://example.com")
assert res.status_code == 200
assert route.call_count == 3