Skip to content

Commit 86e3895

Browse files
committed
create initial test for retry logic
1 parent 7142e6a commit 86e3895

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

tests/test_utils_retries.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import os
2+
import pytest
3+
4+
import requests_mock
5+
6+
from unstructured_client import UnstructuredClient
7+
from unstructured_client.models import shared
8+
from unstructured_client.utils.retries import BackoffStrategy, RetryConfig
9+
10+
11+
def get_api_key():
12+
api_key = os.getenv("UNS_API_KEY")
13+
if api_key is None:
14+
raise ValueError("UNS_API_KEY environment variable not set")
15+
return api_key
16+
17+
def test_backoff_strategy():
18+
filename = "README.md"
19+
backoff_strategy = BackoffStrategy(
20+
initial_interval=100, max_interval=1000, exponent=1.5, max_elapsed_time=3000
21+
)
22+
retries = RetryConfig(
23+
strategy="backoff", backoff=backoff_strategy, retry_connection_errors=True
24+
)
25+
26+
with requests_mock.Mocker() as mock:
27+
# mock 500 status code for POST requests to the api
28+
mock.post("https://api.unstructured.io/general/v0/general", status_code=500)
29+
session = UnstructuredClient(api_key_auth=get_api_key())
30+
31+
with open(filename, "rb") as f:
32+
files=shared.Files(
33+
content=f.read(),
34+
file_name=filename,
35+
)
36+
37+
req = shared.PartitionParameters(files=files)
38+
39+
with pytest.raises(Exception) as excinfo:
40+
resp = session.general.partition(req, retries=retries)
41+
assert resp.status_code == 500
42+
assert "API error occurred" in str(excinfo.value)
43+
44+
# the number of retries varies
45+
assert len(mock.request_history) > 1

0 commit comments

Comments
 (0)