Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a timeout to HTTP POST connects and reads #172

Merged
merged 5 commits into from
Apr 6, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions reportportal_client/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ def __init__(self,
verify_ssl=True,
retries=None,
max_pool_size=50,
post_timeout=(10, 10),
**kwargs):
"""Init the service class.

Expand All @@ -181,6 +182,9 @@ def __init__(self,
verify_ssl: option to not verify ssl certificates
max_pool_size: option to set the maximum number of
connections to save in the pool.
post_timeout: a float in seconds for the connect and read
timeout. Use a Tuple to specific connect and
read separately.
"""
self._batch_logs = []
self.endpoint = endpoint
Expand All @@ -190,6 +194,7 @@ def __init__(self,
self.is_skipped_an_issue = is_skipped_an_issue
self.base_url_v1 = uri_join(self.endpoint, "api/v1", self.project)
self.base_url_v2 = uri_join(self.endpoint, "api/v2", self.project)
self.post_timeout = post_timeout

self.session = requests.Session()
if retries:
Expand Down Expand Up @@ -228,7 +233,13 @@ def start_launch(self,
"rerunOf": rerunOf
}
url = uri_join(self.base_url_v2, "launch")
r = self.session.post(url=url, json=data, verify=self.verify_ssl)
r = self.session.request(
method='POST',
url=url,
json=data,
verify=self.verify_ssl,
timeout=self.post_timeout
)
self.launch_id = _get_id(r)
logger.debug("start_launch - ID: %s", self.launch_id)
return self.launch_id
Expand Down Expand Up @@ -355,7 +366,13 @@ def start_test_item(self,
url = uri_join(self.base_url_v2, "item", parent_item_id)
else:
url = uri_join(self.base_url_v2, "item")
r = self.session.post(url=url, json=data, verify=self.verify_ssl)
r = self.session.request(
method='POST',
url=url,
json=data,
verify=self.verify_ssl,
timeout=self.post_timeout
)

item_id = _get_id(r)
logger.debug("start_test_item - ID: %s", item_id)
Expand Down Expand Up @@ -509,10 +526,12 @@ def _log_batch(self, log_data, force=False):
files.extend(attachments)
for i in range(POST_LOGBATCH_RETRY_COUNT):
try:
r = self.session.post(
r = self.session.request(
method='POST',
url=url,
files=files,
verify=self.verify_ssl
verify=self.verify_ssl,
timeout=self.post_timeout
)
logger.debug("log_batch response: %s", r.text)
self._batch_logs = []
Expand Down
8 changes: 6 additions & 2 deletions tests/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,11 @@ def test_start_item(self, rp_service):
'testCaseId': None,
'retry': False},
url='http://endpoint/api/v2/project/item',
method='POST',
timeout=(10, 10),
verify=True)

rp_service.session.post.assert_called_with(**expected_result)
rp_service.session.request.assert_called_with(**expected_result)
assert rp_start == 123

start_item_optional = [
Expand Down Expand Up @@ -301,6 +303,8 @@ def test_start_item_code_optional_params(self, rp_service, field_name,
'testCaseId': None,
'retry': False},
url='http://endpoint/api/v2/project/item',
method='POST',
timeout=(10, 10),
verify=True)
expected_result['json'][expected_name] = expected_value
rp_service.session.post.assert_called_with(**expected_result)
rp_service.session.request.assert_called_with(**expected_result)