Skip to content

Commit 5f99db3

Browse files
authored
test: retry on 5xx codes (#147)
## Description Test retrying on 5xx codes
1 parent 7397411 commit 5f99db3

File tree

1 file changed

+25
-20
lines changed

1 file changed

+25
-20
lines changed

_test_unstructured_client/unit/test_custom_hooks.py

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
FAKE_KEY = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
1414

1515

16-
def test_unit_retry_with_backoff_does_retry(caplog):
16+
@pytest.mark.parametrize("status_code", [500, 503])
17+
def test_unit_backoff_strategy_logs_retries_5XX(status_code: int, caplog):
1718
caplog.set_level(logging.INFO)
1819
filename = "README.md"
1920
backoff_strategy = BackoffStrategy(
@@ -24,50 +25,54 @@ def test_unit_retry_with_backoff_does_retry(caplog):
2425
)
2526

2627
with requests_mock.Mocker() as mock:
27-
# mock a 502 status code for POST requests to the api
28-
mock.post("https://api.unstructuredapp.io/general/v0/general", status_code=502)
28+
# mock a 500/503 status code for POST requests to the api
29+
mock.post("https://api.unstructuredapp.io/general/v0/general", status_code=status_code)
2930
session = UnstructuredClient(api_key_auth=FAKE_KEY)
3031

3132
with open(filename, "rb") as f:
3233
files = shared.Files(content=f.read(), file_name=filename)
3334

3435
req = shared.PartitionParameters(files=files)
36+
with pytest.raises(Exception):
37+
session.general.partition(req, retries=retries)
3538

36-
with pytest.raises(Exception) as excinfo:
37-
resp = session.general.partition(req, retries=retries)
38-
assert resp.status_code == 502
39-
assert "API error occurred" in str(excinfo.value)
40-
41-
# the number of retries varies
42-
assert len(mock.request_history) > 1
39+
pattern = re.compile(f"Failed to process a request due to API server error with status code {status_code}. "
40+
"Attempting retry number 1 after sleep.")
41+
assert bool(pattern.search(caplog.text))
4342

4443

45-
@pytest.mark.parametrize("status_code", [500, 503])
46-
def test_unit_backoff_strategy_logs_retries_5XX(status_code: int, caplog):
47-
caplog.set_level(logging.INFO)
44+
@pytest.mark.parametrize(
45+
("status_code", "expect_retry"),
46+
[
47+
[500, False],
48+
[502, True],
49+
[503, True],
50+
[504, True],
51+
]
52+
)
53+
def test_unit_number_of_retries_in_5xx(status_code: int, expect_retry: bool):
4854
filename = "README.md"
4955
backoff_strategy = BackoffStrategy(
50-
initial_interval=10, max_interval=100, exponent=1.5, max_elapsed_time=300
56+
initial_interval=1, max_interval=10, exponent=1.5, max_elapsed_time=300
5157
)
5258
retries = RetryConfig(
5359
strategy="backoff", backoff=backoff_strategy, retry_connection_errors=True
5460
)
5561

5662
with requests_mock.Mocker() as mock:
57-
# mock a 500/503 status code for POST requests to the api
5863
mock.post("https://api.unstructuredapp.io/general/v0/general", status_code=status_code)
5964
session = UnstructuredClient(api_key_auth=FAKE_KEY)
6065

6166
with open(filename, "rb") as f:
6267
files = shared.Files(content=f.read(), file_name=filename)
6368

6469
req = shared.PartitionParameters(files=files)
65-
with pytest.raises(Exception):
70+
with pytest.raises(Exception, match=f"unknown content-type received: None: Status {status_code}"):
6671
session.general.partition(req, retries=retries)
67-
68-
pattern = re.compile(f"Failed to process a request due to API server error with status code {status_code}. "
69-
"Attempting retry number 1 after sleep.")
70-
assert bool(pattern.search(caplog.text))
72+
if expect_retry:
73+
assert len(mock.request_history) > 1
74+
else:
75+
assert len(mock.request_history) == 1
7176

7277

7378
def test_unit_backoff_strategy_logs_retries_connection_error(caplog):

0 commit comments

Comments
 (0)