diff --git a/tap_google_ads/streams.py b/tap_google_ads/streams.py index 53319cc..e9b1a0b 100644 --- a/tap_google_ads/streams.py +++ b/tap_google_ads/streams.py @@ -7,7 +7,7 @@ from singer import utils, metrics from google.protobuf.json_format import MessageToJson from google.ads.googleads.errors import GoogleAdsException -from google.api_core.exceptions import ServerError, TooManyRequests +from google.api_core.exceptions import ServerError, TooManyRequests, Unauthorized from requests.exceptions import ReadTimeout import backoff from . import report_definitions @@ -237,6 +237,7 @@ def on_giveup_func(err): giveup=should_give_up, on_giveup=on_giveup_func, logger=None) +@backoff.on_exception(backoff.expo, Unauthorized, max_tries=5, jitter=None,logger=None) def make_request(gas, query, customer_id, config=None): if config is None: config = {} diff --git a/tests/unittests/test_backoff.py b/tests/unittests/test_backoff.py index d3d1497..b6056fe 100644 --- a/tests/unittests/test_backoff.py +++ b/tests/unittests/test_backoff.py @@ -1,7 +1,7 @@ import unittest from unittest.mock import Mock, patch from tap_google_ads.streams import make_request -from google.api_core.exceptions import InternalServerError, BadGateway, MethodNotImplemented, ServiceUnavailable, GatewayTimeout, TooManyRequests +from google.api_core.exceptions import InternalServerError, BadGateway, MethodNotImplemented, ServiceUnavailable, GatewayTimeout, TooManyRequests, Unauthorized from requests.exceptions import ReadTimeout @patch('time.sleep') @@ -97,6 +97,21 @@ def test_429_too_may_request_error(self, mock_sleep): # Verify that tap backoff for 5 times self.assertEquals(mocked_google_ads_client.search.call_count, 5) + def test_401_uauthenticated(self, mock_sleep): + """ + Check whether the tap backoffs properly for 5 times in case of TooManyRequests error. + """ + mocked_google_ads_client = Mock() + mocked_google_ads_client.search.side_effect = Unauthorized("Resource has been exhausted") + + try: + make_request(mocked_google_ads_client, "", "") + except Unauthorized: + pass + + # Verify that tap backoff for 5 times + self.assertEquals(mocked_google_ads_client.search.call_count, 5) + def test_read_timeout_error(self, mock_sleep): """ Check whether the tap backoffs properly for 5 times in case of ReadTimeout error.