Skip to content

Commit ec687d2

Browse files
authored
Merge pull request #26 from cloudblue/LITE-20615-align-timeouts
LITE-20615: Align timeouts and force autoretry if 5xx error
2 parents 403af83 + 7fe59cd commit ec687d2

File tree

3 files changed

+65
-7
lines changed

3 files changed

+65
-7
lines changed

connect/client/fluent.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ def __init__(
3030
default_limit=100,
3131
max_retries=0,
3232
logger=None,
33+
timeout=(180.0, 180.0),
3334
):
3435
"""
3536
Create a new instance of the ConnectClient.
@@ -63,6 +64,7 @@ def __init__(
6364
self.response = None
6465
self.logger = logger
6566
self._help_formatter = DefaultFormatter(self.specs)
67+
self.timeout = timeout
6668

6769
def __getattr__(self, name):
6870
"""
@@ -137,7 +139,8 @@ def _prepare_call_kwargs(self, kwargs):
137139
kwargs['headers'].update(get_headers(self.api_key))
138140
else:
139141
kwargs['headers'] = get_headers(self.api_key)
140-
142+
if 'timeout' not in kwargs:
143+
kwargs['timeout'] = self.timeout
141144
if self.default_headers:
142145
kwargs['headers'].update(self.default_headers)
143146
return kwargs

connect/client/mixins.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def _execute_http_call(self, method, url, kwargs):
8282
self.logger.log_response(self.response)
8383

8484
if ( # pragma: no branch
85-
self.response.status_code == 502
85+
self.response.status_code >= 500
8686
and retry_count < self.max_retries
8787
):
8888
retry_count += 1
@@ -159,7 +159,7 @@ async def _execute_http_call(self, method, url, kwargs):
159159
self.logger.log_response(self.response)
160160

161161
if ( # pragma: no branch
162-
self.response.status_code == 502
162+
self.response.status_code >= 500
163163
and retry_count < self.max_retries
164164
):
165165
retry_count += 1

tests/client/test_fluent.py

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -244,23 +244,27 @@ def test_execute_retries(mocked_responses):
244244
assert results == expected
245245

246246

247-
def test_execute_max_retries_exceeded(mocked_responses):
247+
@pytest.mark.parametrize(
248+
'code',
249+
[500, 501, 502],
250+
)
251+
def test_execute_max_retries_exceeded(code, mocked_responses):
248252
mocked_responses.add(
249253
responses.GET,
250254
'https://localhost/resources',
251-
status=502,
255+
status=code,
252256
)
253257

254258
mocked_responses.add(
255259
responses.GET,
256260
'https://localhost/resources',
257-
status=502,
261+
status=code,
258262
)
259263

260264
mocked_responses.add(
261265
responses.GET,
262266
'https://localhost/resources',
263-
status=502,
267+
status=code,
264268
)
265269

266270
c = ConnectClient(
@@ -323,6 +327,57 @@ def test_execute_with_kwargs(mocked_responses):
323327
assert 'X-Custom-Header' in headers and headers['X-Custom-Header'] == 'value'
324328

325329

330+
def test_execute_with_overwritten_timeout(mocked_responses, mocker):
331+
http_call = mocker.patch(
332+
'connect.client.mixins.SyncClientMixin._execute_http_call',
333+
side_effect=RequestException(),
334+
)
335+
336+
c = ConnectClient('API_KEY', endpoint='https://localhost', use_specs=False)
337+
kwargs = {
338+
'timeout': 500,
339+
}
340+
341+
with pytest.raises(ClientError):
342+
c.execute('post', 'resources', **kwargs)
343+
344+
http_call.assert_called_with(
345+
'post',
346+
'https://localhost/resources',
347+
{
348+
'timeout': 500,
349+
'headers': {
350+
'Authorization': 'API_KEY',
351+
'User-Agent': mocker.ANY,
352+
},
353+
},
354+
)
355+
356+
357+
def test_execute_with_default_timeout(mocked_responses, mocker):
358+
http_call = mocker.patch(
359+
'connect.client.mixins.SyncClientMixin._execute_http_call',
360+
side_effect=RequestException(),
361+
)
362+
363+
c = ConnectClient('API_KEY', endpoint='https://localhost', use_specs=False)
364+
365+
with pytest.raises(ClientError):
366+
c.execute('post', 'resources')
367+
368+
http_call.assert_called_with(
369+
'post',
370+
'https://localhost/resources',
371+
{
372+
'timeout': (180.0, 180.0),
373+
'headers': {
374+
'Authorization': 'API_KEY',
375+
'User-Agent': mocker.ANY,
376+
},
377+
},
378+
)
379+
380+
326381
def test_execute_connect_error(mocked_responses):
327382
connect_error = {
328383
'error_code': 'code',

0 commit comments

Comments
 (0)