-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_exponential_backoff.py
65 lines (50 loc) · 1.63 KB
/
test_exponential_backoff.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"""
Example of implementing exponential backoff policy where the client
should increase the delay exponentially for each try.
"""
import time
import httpx
import pytest
import respx
from httpx_retry import AsyncRetryTransport, RetryPolicy, RetryTransport
@respx.mock()
def test_exponential_backoff(respx_mock: respx.MockRouter):
route = respx_mock.get("https://example.com")
route.side_effect = [
httpx.Response(500),
httpx.Response(500),
httpx.Response(200),
]
exponential_retry = (
RetryPolicy().with_attempts(3).with_min_delay(0.1).with_multiplier(2)
)
start = time.monotonic()
with httpx.Client(transport=RetryTransport(policy=exponential_retry)) as client:
res = client.get("https://example.com")
assert res.status_code == 200
end = time.monotonic()
elapsed = end - start
assert elapsed >= 0.3
assert route.call_count == 3
@pytest.mark.asyncio()
@respx.mock()
async def test_async_exponential_backoff(respx_mock: respx.MockRouter):
route = respx_mock.get("https://example.com")
route.side_effect = [
httpx.Response(500),
httpx.Response(500),
httpx.Response(200),
]
exponential_retry = (
RetryPolicy().with_attempts(3).with_min_delay(0.1).with_multiplier(2)
)
start = time.monotonic()
async with httpx.AsyncClient(
transport=AsyncRetryTransport(policy=exponential_retry)
) as client:
res = await client.get("https://example.com")
assert res.status_code == 200
end = time.monotonic()
elapsed = end - start
assert elapsed >= 0.3
assert route.call_count == 3