13
13
FAKE_KEY = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
14
14
15
15
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 ):
17
18
caplog .set_level (logging .INFO )
18
19
filename = "README.md"
19
20
backoff_strategy = BackoffStrategy (
@@ -24,50 +25,54 @@ def test_unit_retry_with_backoff_does_retry(caplog):
24
25
)
25
26
26
27
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 )
29
30
session = UnstructuredClient (api_key_auth = FAKE_KEY )
30
31
31
32
with open (filename , "rb" ) as f :
32
33
files = shared .Files (content = f .read (), file_name = filename )
33
34
34
35
req = shared .PartitionParameters (files = files )
36
+ with pytest .raises (Exception ):
37
+ session .general .partition (req , retries = retries )
35
38
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 ))
43
42
44
43
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 ):
48
54
filename = "README.md"
49
55
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
51
57
)
52
58
retries = RetryConfig (
53
59
strategy = "backoff" , backoff = backoff_strategy , retry_connection_errors = True
54
60
)
55
61
56
62
with requests_mock .Mocker () as mock :
57
- # mock a 500/503 status code for POST requests to the api
58
63
mock .post ("https://api.unstructuredapp.io/general/v0/general" , status_code = status_code )
59
64
session = UnstructuredClient (api_key_auth = FAKE_KEY )
60
65
61
66
with open (filename , "rb" ) as f :
62
67
files = shared .Files (content = f .read (), file_name = filename )
63
68
64
69
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 } " ):
66
71
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
71
76
72
77
73
78
def test_unit_backoff_strategy_logs_retries_connection_error (caplog ):
0 commit comments