|
| 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 | +Set it in your current shell session with `export UNS_API_KEY=<api_key>`""") |
| 16 | + return api_key |
| 17 | + |
| 18 | +# this test requires UNS_API_KEY be set in your shell session. Ex: `export UNS_API_KEY=<api_key>` |
| 19 | +def test_backoff_strategy(): |
| 20 | + filename = "README.md" |
| 21 | + backoff_strategy = BackoffStrategy( |
| 22 | + initial_interval=100, max_interval=1000, exponent=1.5, max_elapsed_time=3000 |
| 23 | + ) |
| 24 | + retries = RetryConfig( |
| 25 | + strategy="backoff", backoff=backoff_strategy, retry_connection_errors=True |
| 26 | + ) |
| 27 | + |
| 28 | + with requests_mock.Mocker() as mock: |
| 29 | + # mock a 500 status code for POST requests to the api |
| 30 | + mock.post("https://api.unstructured.io/general/v0/general", status_code=500) |
| 31 | + session = UnstructuredClient(api_key_auth=get_api_key()) |
| 32 | + |
| 33 | + with open(filename, "rb") as f: |
| 34 | + files=shared.Files( |
| 35 | + content=f.read(), |
| 36 | + file_name=filename, |
| 37 | + ) |
| 38 | + |
| 39 | + req = shared.PartitionParameters(files=files) |
| 40 | + |
| 41 | + with pytest.raises(Exception) as excinfo: |
| 42 | + resp = session.general.partition(req, retries=retries) |
| 43 | + assert resp.status_code == 500 |
| 44 | + assert "API error occurred" in str(excinfo.value) |
| 45 | + |
| 46 | + # the number of retries varies |
| 47 | + assert len(mock.request_history) > 1 |
0 commit comments