-
Notifications
You must be signed in to change notification settings - Fork 12
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
TDL-16355 Implement request timeouts #43
Changes from all commits
ea484c2
f2a8be2
3e6da74
290b86a
2f15788
5f55bf1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
from tap_mailchimp.client import MailchimpClient | ||
import unittest | ||
from unittest.mock import patch | ||
import requests | ||
|
||
REQUEST_TIMEOUT_INT = 300 | ||
REQUEST_TIMEOUT_STR = "300" | ||
REQUEST_TIMEOUT_FLOAT = 300.0 | ||
|
||
# Mock response object | ||
def get_mock_http_response(*args, **kwargs): | ||
contents = '{"access_token": "test", "expires_in":100, "accounts":[{"id": 12}]}' | ||
response = requests.Response() | ||
response.status_code = 200 | ||
response._content = contents.encode() | ||
return response | ||
|
||
@patch("time.sleep") | ||
@patch("requests.Session.request", side_effect=requests.exceptions.Timeout) | ||
class TestRequestTimeoutsBackoff(unittest.TestCase): | ||
|
||
def test_request_timeout_backoff(self, mocked_request, mock_sleep): | ||
""" | ||
Verify request function is backoff for 5 times on Timeout exceeption | ||
""" | ||
# Initialize MailchimpClient object | ||
client = MailchimpClient({'access_token': 'as'}) | ||
try: | ||
client.request('GET', "http://test", "base_url") | ||
except requests.exceptions.Timeout: | ||
pass | ||
|
||
# Verify that requests.Session.request is called 5 times | ||
self.assertEqual(mocked_request.call_count, 5) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @prijendev the test case name and the assertion that you are making is different. Can you please verify it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, updated test case name. |
||
|
||
@patch("requests.Session.request", side_effect=get_mock_http_response) | ||
class TestRequestTimeoutsValue(unittest.TestCase): | ||
|
||
def test_no_request_timeout_in_config(self, mocked_request): | ||
""" | ||
Verify that if request_timeout is not provided in config then default value(300) is used | ||
""" | ||
# Initialize MailchimpClient object | ||
client = MailchimpClient({'access_token': 'as'}) | ||
|
||
# Call request method which call requests.Session.request with timeout | ||
client.request('GET', "http://test", "base_url") | ||
|
||
# Verify requests.Session.request is called with expected timeout | ||
args, kwargs = mocked_request.call_args | ||
self.assertEqual(kwargs.get('timeout'), REQUEST_TIMEOUT_INT) # Verify timeout argument | ||
|
||
def test_integer_request_timeout_in_config(self, mocked_request): | ||
""" | ||
Verify that if request_timeout is provided in config(integer value) then it should be use | ||
""" | ||
# Initialize MailchimpClient object | ||
client = MailchimpClient({'access_token': 'as', "request_timeout": REQUEST_TIMEOUT_INT}) # integer timeout in config | ||
|
||
# Call request method which call requests.Session.request with timeout | ||
client.request('GET', "http://test", "base_url") | ||
|
||
# Verify requests.Session.request is called with expected timeout. | ||
# If none zero positive integer or string value passed in the config then it converted to float value. So, here we are verifying the same. | ||
args, kwargs = mocked_request.call_args | ||
self.assertEqual(kwargs.get('timeout'), REQUEST_TIMEOUT_FLOAT) | ||
|
||
def test_float_request_timeout_in_config(self, mocked_request): | ||
""" | ||
Verify that if request_timeout is provided in config(float value) then it should be use | ||
""" | ||
# Initialize MailchimpClient object | ||
client = MailchimpClient({'access_token': 'as', "request_timeout": REQUEST_TIMEOUT_FLOAT}) # float timeout in config | ||
|
||
# Call request method which call requests.Session.request with timeout | ||
client.request('GET', "http://test", "base_url") | ||
|
||
# Verify requests.Session.request is called with expected timeout | ||
args, kwargs = mocked_request.call_args | ||
self.assertEqual(kwargs.get('timeout'), REQUEST_TIMEOUT_FLOAT) # Verify timeout argument | ||
|
||
def test_string_request_timeout_in_config(self, mocked_request): | ||
""" | ||
Verify that if request_timeout is provided in config(string value) then it should be use | ||
""" | ||
# Initialize MailchimpClient object | ||
client = MailchimpClient({'access_token': 'as', "request_timeout": REQUEST_TIMEOUT_STR}) # string timeout in config | ||
|
||
# Call request method which call requests.Session.request with timeout | ||
client.request('GET', "http://test", "base_url") | ||
|
||
# Verify requests.Session.request is called with expected timeout | ||
# If none zero positive integer or string value passed in the config then it converted to float value. So, here we are verifying the same. | ||
args, kwargs = mocked_request.call_args | ||
self.assertEqual(kwargs.get('timeout'), REQUEST_TIMEOUT_FLOAT) # Verify timeout argument | ||
|
||
def test_empty_string_request_timeout_in_config(self, mocked_request): | ||
""" | ||
Verify that if request_timeout is provided in config with empty string then default value(300) is used | ||
""" | ||
# Initialize MailchimpClient object | ||
client = MailchimpClient({'access_token': 'as', "request_timeout": ""}) # empty string timeout in config | ||
|
||
# Call request method which call requests.Session.request with timeout | ||
client.request('GET', "http://test", "base_url") | ||
|
||
# Verify requests.Session.request is called with expected timeout | ||
args, kwargs = mocked_request.call_args | ||
self.assertEqual(kwargs.get('timeout'), REQUEST_TIMEOUT_INT) # Verify timeout argument | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @prijendev Why int value is expected in this test case? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the request_timeout value is not passed in the config then the default value that is int value(300) is used. |
||
|
||
def test_zero_int_request_timeout_in_config(self, mocked_request): | ||
""" | ||
Verify that if request_timeout is provided in config with int zero value then default value(300) is used | ||
""" | ||
# Initialize MailchimpClient object | ||
client = MailchimpClient({'access_token': 'as', "request_timeout": 0}) # int zero value in config | ||
|
||
# Call request method which call requests.Session.request with timeout | ||
client.request('GET', "http://test", "base_url") | ||
|
||
# Verify requests.Session.request is called with expected timeout | ||
args, kwargs = mocked_request.call_args | ||
self.assertEqual(kwargs.get('timeout'), REQUEST_TIMEOUT_INT) # Verify timeout argument | ||
|
||
def test_zero_string_request_timeout_in_config(self, mocked_request): | ||
""" | ||
Verify that if request_timeout is provided in config with string zero in string format then default value(300) is used | ||
""" | ||
client = MailchimpClient({'access_token': 'as', "request_timeout": "0"}) # string zero value in config | ||
|
||
# Call request method which call requests.Session.request with timeout | ||
client.request('GET', "http://test", "base_url") | ||
|
||
# Verify requests.Session.request is called with expected timeout | ||
args, kwargs = mocked_request.call_args | ||
self.assertEqual(kwargs.get('timeout'), REQUEST_TIMEOUT_INT) # Verify timeout argument |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use latest tap-tester image.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated tap-tester image