-
Notifications
You must be signed in to change notification settings - Fork 27
TDL-16314 added request timeout #96
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
Open
namrata270998
wants to merge
10
commits into
master
Choose a base branch
from
TDL-16314-implement-request-timeout
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
fa74413
addd request timeout
namrata270998 c6eaa29
fixed trailing spaces pylint errors
namrata270998 c556d4c
fixed pylint errors
namrata270998 21a1865
added coverage in cci
namrata270998 57f8a46
updated request_timeout
namrata270998 e1b2739
updated comments
namrata270998 8795a49
added jitter
namrata270998 0e2790e
removed connecttimeout
namrata270998 5141f96
resolved comments
namrata270998 6e62306
Merge branch 'master' of https://github.com/singer-io/tap-xero into T…
namrata270998 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,191 @@ | ||
from tap_xero.client import XeroClient | ||
import unittest | ||
from unittest import mock | ||
from unittest.case import TestCase | ||
from requests.exceptions import Timeout, ConnectTimeout | ||
import datetime | ||
|
||
class TestBackoffError(unittest.TestCase): | ||
''' | ||
Test that backoff logic works properly. | ||
''' | ||
@mock.patch('tap_xero.client.requests.Request') | ||
@mock.patch('tap_xero.client.requests.Session.send') | ||
@mock.patch('tap_xero.client.requests.Session.post') | ||
def test_backoff_check_platform_access_timeout_error(self, mock_post, mock_send, mock_request): | ||
""" | ||
Check whether the request backoffs properly for 60 seconds in case of Timeout error. | ||
""" | ||
mock_send.side_effect = Timeout | ||
mock_post.side_effect = Timeout | ||
before_time = datetime.datetime.now() | ||
with self.assertRaises(Timeout): | ||
config = {"start_date": "dummy_st", "client_id": "dummy_ci", "client_secret": "dummy_cs", "tenant_id": "dummy_ti", "refresh_token": "dummy_rt"} | ||
client = XeroClient(config) | ||
client.check_platform_access(config, "dummy_path") | ||
after_time = datetime.datetime.now() | ||
time_difference = (after_time - before_time).total_seconds() | ||
self.assertGreaterEqual(time_difference, 120) | ||
|
||
@mock.patch('tap_xero.client.requests.Request') | ||
@mock.patch('tap_xero.client.requests.Session.send') | ||
@mock.patch('tap_xero.client.requests.Session.post') | ||
def test_backoff_check_platform_access_connect_timeout_error(self, mock_post, mock_send, mock_request): | ||
""" | ||
Check whether the request backoffs properly for 60 seconds in case of ConnectTimeout error. | ||
""" | ||
mock_send.side_effect = ConnectTimeout | ||
mock_post.side_effect = ConnectTimeout | ||
before_time = datetime.datetime.now() | ||
with self.assertRaises(Timeout): | ||
config = {"start_date": "dummy_st", "client_id": "dummy_ci", "client_secret": "dummy_cs", "tenant_id": "dummy_ti", "refresh_token": "dummy_rt"} | ||
client = XeroClient(config) | ||
client.check_platform_access(config, "dummy_path") | ||
after_time = datetime.datetime.now() | ||
time_difference = (after_time - before_time).total_seconds() | ||
self.assertGreaterEqual(time_difference, 120) | ||
|
||
@mock.patch('tap_xero.client.requests.Session.post') | ||
def test_backoff_refresh_credentials_timeout_error(self, mock_post): | ||
""" | ||
Check whether the request backoffs properly for 60 seconds in case of Timeout error. | ||
""" | ||
mock_post.side_effect = Timeout | ||
before_time = datetime.datetime.now() | ||
with self.assertRaises(Timeout): | ||
config = {"start_date": "dummy_st", "client_id": "dummy_ci", "client_secret": "dummy_cs", "tenant_id": "dummy_ti", "refresh_token": "dummy_rt"} | ||
client = XeroClient(config) | ||
client.refresh_credentials(config, "dummy_path") | ||
after_time = datetime.datetime.now() | ||
time_difference = (after_time - before_time).total_seconds() | ||
self.assertGreaterEqual(time_difference, 60) | ||
|
||
@mock.patch('tap_xero.client.requests.Session.post') | ||
def test_backoff_refresh_credentials_connect_timeout_error(self, mock_post): | ||
""" | ||
Check whether the request backoffs properly for 60 seconds in case of ConnectTimeout error. | ||
""" | ||
mock_post.side_effect = ConnectTimeout | ||
before_time = datetime.datetime.now() | ||
with self.assertRaises(Timeout): | ||
config = {"start_date": "dummy_st", "client_id": "dummy_ci", "client_secret": "dummy_cs", "tenant_id": "dummy_ti", "refresh_token": "dummy_rt"} | ||
client = XeroClient(config) | ||
client.refresh_credentials(config, "dummy_path") | ||
after_time = datetime.datetime.now() | ||
time_difference = (after_time - before_time).total_seconds() | ||
self.assertGreaterEqual(time_difference, 60) | ||
|
||
@mock.patch('tap_xero.client.requests.Session.send') | ||
@mock.patch('tap_xero.client.requests.Request') | ||
def test_backoff_filter_timeout_error(self, mock_request, mock_send): | ||
""" | ||
Check whether the request backoffs properly for 5 times in case of Timeout error. | ||
""" | ||
mock_send.side_effect = Timeout | ||
before_time = datetime.datetime.now() | ||
with self.assertRaises(Timeout): | ||
config = {"start_date": "dummy_st", "client_id": "dummy_ci", "client_secret": "dummy_cs", "tenant_id": "dummy_ti", "refresh_token": "dummy_rt"} | ||
client = XeroClient(config) | ||
client.access_token = "dummy_token" | ||
client.filter(tap_stream_id='dummy_stream') | ||
after_time = datetime.datetime.now() | ||
time_difference = (after_time - before_time).total_seconds() | ||
self.assertGreaterEqual(time_difference, 60) | ||
|
||
class MockResponse(): | ||
''' | ||
Mock response object for the requests call | ||
''' | ||
def __init__(self, resp, status_code, content=[""], headers=None, raise_error=False, text={}): | ||
self.json_data = resp | ||
self.status_code = status_code | ||
self.content = content | ||
self.headers = headers | ||
self.raise_error = raise_error | ||
self.text = text | ||
self.reason = "error" | ||
|
||
def prepare(self): | ||
return (self.json_data, self.status_code, self.content, self.headers, self.raise_error) | ||
|
||
def json(self): | ||
return self.text | ||
|
||
class MockRequest(): | ||
''' | ||
Mock Request object for mocking the Requests() | ||
''' | ||
def __init__(self): | ||
pass | ||
|
||
def prepare(self): | ||
pass | ||
|
||
class TestRequestTimeoutValue(unittest.TestCase): | ||
''' | ||
Test that request timeout parameter works properly in various cases | ||
''' | ||
@mock.patch('tap_xero.client.requests.Session.send', return_value = MockResponse("", status_code=200)) | ||
@mock.patch('tap_xero.client.requests.Request', return_value = MockRequest()) | ||
@mock.patch('tap_xero.client.XeroClient.refresh_credentials') | ||
def test_config_provided_request_timeout(self, mock_refresh_creds, mock_request, mock_send): | ||
""" | ||
Unit tests to ensure that request timeout is set based on config value | ||
""" | ||
config = {"start_date": "dummy_st", "client_id": "dummy_ci", "client_secret": "dummy_cs", "tenant_id": "dummy_ti", "refresh_token": "dummy_rt", "request_timeout": 100} | ||
client = XeroClient(config) | ||
client.access_token = "dummy_access_token" | ||
client.check_platform_access("GET", "dummy_path") | ||
mock_send.assert_called_with(MockRequest().prepare(), timeout=100.0) | ||
|
||
@mock.patch('tap_xero.client.requests.Session.send', return_value = MockResponse("", status_code=200)) | ||
@mock.patch('tap_xero.client.requests.Request', return_value = MockRequest()) | ||
@mock.patch('tap_xero.client.XeroClient.refresh_credentials') | ||
def test_default_value_request_timeout(self, mock_refresh_creds, mock_request, mock_send): | ||
""" | ||
Unit tests to ensure that request timeout is set based default value | ||
""" | ||
config = {"start_date": "dummy_st", "client_id": "dummy_ci", "client_secret": "dummy_cs", "tenant_id": "dummy_ti", "refresh_token": "dummy_rt"} | ||
client = XeroClient(config) | ||
client.access_token = "dummy_access_token" | ||
client.check_platform_access("GET", "dummy_path") | ||
mock_send.assert_called_with(MockRequest().prepare(), timeout=300.0) | ||
|
||
@mock.patch('tap_xero.client.requests.Session.send', return_value = MockResponse("", status_code=200)) | ||
@mock.patch('tap_xero.client.requests.Request', return_value = MockRequest()) | ||
@mock.patch('tap_xero.client.XeroClient.refresh_credentials') | ||
def test_config_provided_empty_request_timeout(self, mock_refresh_creds, mock_request, mock_send): | ||
""" | ||
Unit tests to ensure that request timeout is set based on default value if empty value is given in config | ||
""" | ||
config = {"start_date": "dummy_st", "client_id": "dummy_ci", "client_secret": "dummy_cs", "tenant_id": "dummy_ti", "refresh_token": "dummy_rt", "request_timeout": ""} | ||
client = XeroClient(config) | ||
client.access_token = "dummy_access_token" | ||
client.check_platform_access("GET", "dummy_path") | ||
mock_send.assert_called_with(MockRequest().prepare(), timeout=300.0) | ||
|
||
@mock.patch('tap_xero.client.requests.Session.send', return_value = MockResponse("", status_code=200)) | ||
@mock.patch('tap_xero.client.requests.Request', return_value = MockRequest()) | ||
@mock.patch('tap_xero.client.XeroClient.refresh_credentials') | ||
def test_config_provided_string_request_timeout(self, mock_refresh_creds, mock_request, mock_send): | ||
""" | ||
Unit tests to ensure that request timeout is set based on config string value | ||
""" | ||
config = {"start_date": "dummy_st", "client_id": "dummy_ci", "client_secret": "dummy_cs", "tenant_id": "dummy_ti", "refresh_token": "dummy_rt", "request_timeout": "100"} | ||
client = XeroClient(config) | ||
client.access_token = "dummy_access_token" | ||
client.check_platform_access("GET", "dummy_path") | ||
mock_send.assert_called_with(MockRequest().prepare(), timeout=100.0) | ||
|
||
@mock.patch('tap_xero.client.requests.Session.send', return_value = MockResponse("", status_code=200)) | ||
@mock.patch('tap_xero.client.requests.Request', return_value = MockRequest()) | ||
@mock.patch('tap_xero.client.XeroClient.refresh_credentials') | ||
def test_config_provided_float_request_timeout(self, mock_refresh_creds, mock_request, mock_send): | ||
""" | ||
Unit tests to ensure that request timeout is set based on config float value | ||
""" | ||
config = {"start_date": "dummy_st", "client_id": "dummy_ci", "client_secret": "dummy_cs", "tenant_id": "dummy_ti", "refresh_token": "dummy_rt", "request_timeout": 100.8} | ||
client = XeroClient(config) | ||
client.access_token = "dummy_access_token" | ||
client.check_platform_access("GET", "dummy_path") | ||
mock_send.assert_called_with(MockRequest().prepare(), timeout=100.8) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I think these with assertRaises contexts should contain only the method that raises the exception, if the XeroClient init had a request in it and that timed out unexpectedly, this would never get to the check_platform_access step. But it would still pass. While I think it is performing functionally the way it's intended, it is difficult to see exactly what is being tested.
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