-
Notifications
You must be signed in to change notification settings - Fork 25
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
base: master
Are you sure you want to change the base?
Changes from 7 commits
fa74413
c6eaa29
c556d4c
21a1865
57f8a46
e1b2739
8795a49
0e2790e
5141f96
6e62306
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,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): | ||
|
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. 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.
Author
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. Updated |
||
| 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) | ||
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.
Is there any specific reason to import ConnectTimeout?
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.
It was not used anywhere as ConnectTimeout inherits Timeout, hence removed it