Skip to content

Commit f654350

Browse files
committed
Add timeouts to HTTP GETs
1 parent 3739295 commit f654350

File tree

2 files changed

+35
-21
lines changed

2 files changed

+35
-21
lines changed

reportportal_client/service.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ def __init__(self,
167167
verify_ssl=True,
168168
retries=None,
169169
max_pool_size=50,
170-
post_timeout=(10, 10),
170+
http_timeout=(10, 10),
171171
**kwargs):
172172
"""Init the service class.
173173
@@ -182,7 +182,7 @@ def __init__(self,
182182
verify_ssl: option to not verify ssl certificates
183183
max_pool_size: option to set the maximum number of
184184
connections to save in the pool.
185-
post_timeout: a float in seconds for the connect and read
185+
http_timeout: a float in seconds for the connect and read
186186
timeout. Use a Tuple to specific connect and
187187
read separately.
188188
"""
@@ -194,7 +194,7 @@ def __init__(self,
194194
self.is_skipped_an_issue = is_skipped_an_issue
195195
self.base_url_v1 = uri_join(self.endpoint, "api/v1", self.project)
196196
self.base_url_v2 = uri_join(self.endpoint, "api/v2", self.project)
197-
self.post_timeout = post_timeout
197+
self.http_timeout = http_timeout
198198

199199
self.session = requests.Session()
200200
if retries:
@@ -238,7 +238,7 @@ def start_launch(self,
238238
url=url,
239239
json=data,
240240
verify=self.verify_ssl,
241-
timeout=self.post_timeout
241+
timeout=self.http_timeout
242242
)
243243
self.launch_id = _get_id(r)
244244
logger.debug("start_launch - ID: %s", self.launch_id)
@@ -281,7 +281,11 @@ def get_launch_info(self, max_retries=5):
281281

282282
for _ in range(max_retries):
283283
logger.debug("get_launch_info - ID: %s", self.launch_id)
284-
resp = self.session.get(url=url, verify=self.verify_ssl)
284+
resp = self.session.request(
285+
method='GET',
286+
url=url,
287+
verify=self.verify_ssl,
288+
timeout=self.http_timeout)
285289

286290
if resp.status_code == 200:
287291
launch_info = _get_json(resp)
@@ -371,7 +375,7 @@ def start_test_item(self,
371375
url=url,
372376
json=data,
373377
verify=self.verify_ssl,
374-
timeout=self.post_timeout
378+
timeout=self.http_timeout
375379
)
376380

377381
item_id = _get_id(r)
@@ -440,8 +444,11 @@ def get_item_id_by_uuid(self, uuid):
440444
:return str: Test item id
441445
"""
442446
url = uri_join(self.base_url_v1, "item", "uuid", uuid)
443-
return _get_json(self.session.get(
444-
url=url, verify=self.verify_ssl))["id"]
447+
return _get_json(self.session.request(
448+
method='GET',
449+
url=url,
450+
verify=self.verify_ssl,
451+
timeout=self.http_timeout))["id"]
445452

446453
def get_project_settings(self):
447454
"""
@@ -450,7 +457,12 @@ def get_project_settings(self):
450457
:return: json body
451458
"""
452459
url = uri_join(self.base_url_v1, "settings")
453-
r = self.session.get(url=url, json={}, verify=self.verify_ssl)
460+
r = self.session.request(
461+
method='GET',
462+
url=url,
463+
json={},
464+
verify=self.verify_ssl,
465+
timeout=self.http_timeout)
454466
logger.debug("settings")
455467
return _get_json(r)
456468

@@ -531,7 +543,7 @@ def _log_batch(self, log_data, force=False):
531543
url=url,
532544
files=files,
533545
verify=self.verify_ssl,
534-
timeout=self.post_timeout
546+
timeout=self.http_timeout
535547
)
536548
logger.debug("log_batch response: %s", r.text)
537549
self._batch_logs = []

tests/test_service.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,17 @@ def test_get_launch_info(self, rp_service, monkeypatch):
109109
mock_resp = mock.Mock()
110110
mock_resp.status_code = 200
111111

112-
mock_get = mock.Mock(return_value=mock_resp)
113-
monkeypatch.setattr(rp_service.session, 'get', mock_get)
112+
mock_request = mock.Mock(return_value=mock_resp)
113+
monkeypatch.setattr(rp_service.session, 'request', mock_request)
114114
monkeypatch.setattr(rp_service, 'launch_id', '1234-cafe')
115115

116116
launch_id = rp_service.get_launch_info()
117-
mock_get.assert_called_once_with(
117+
mock_request.assert_called_once_with(
118+
method='GET',
118119
url='{0}/launch/uuid/{1}'.format(rp_service.base_url_v1,
119120
rp_service.launch_id),
120-
verify=rp_service.verify_ssl)
121+
verify=rp_service.verify_ssl,
122+
timeout=(10, 10))
121123
assert launch_id == {'id': 112}
122124

123125
def test_get_launch_info_launch_id_none(self, rp_service, monkeypatch):
@@ -145,12 +147,12 @@ def test_get_launch_info_wrong_launch_id(self, rp_service, monkeypatch):
145147
object with mocked session.
146148
:param monkeypatch: Pytest fixture to safely set/delete an attribute
147149
"""
148-
mock_get = mock.Mock()
149-
monkeypatch.setattr(rp_service.session, 'get', mock_get)
150+
mock_request = mock.Mock()
151+
monkeypatch.setattr(rp_service.session, 'request', mock_request)
150152
monkeypatch.setattr(rp_service, 'launch_id', '1234')
151153

152154
launch_info = rp_service.get_launch_info()
153-
expect(mock_get.call_count == 5)
155+
expect(mock_request.call_count == 5)
154156
expect(launch_info == {})
155157
assert_expectations()
156158

@@ -168,13 +170,13 @@ def test_get_launch_info_1st_failed(self, rp_service, monkeypatch):
168170
mock_resp1.status_code = 404
169171
mock_resp2 = mock.Mock()
170172
mock_resp2.status_code = 200
171-
mock_get = mock.Mock()
172-
mock_get.side_effect = [mock_resp1, mock_resp2]
173-
monkeypatch.setattr(rp_service.session, 'get', mock_get)
173+
mock_request = mock.Mock()
174+
mock_request.side_effect = [mock_resp1, mock_resp2]
175+
monkeypatch.setattr(rp_service.session, 'request', mock_request)
174176
monkeypatch.setattr(rp_service, 'launch_id', '1234')
175177

176178
launch_info = rp_service.get_launch_info()
177-
expect(mock_get.call_count == 2)
179+
expect(mock_request.call_count == 2)
178180
expect(launch_info == {'id': 112})
179181
assert_expectations()
180182

0 commit comments

Comments
 (0)