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

Fix bigquery retry #3238

Merged
merged 20 commits into from
May 3, 2023
6 changes: 3 additions & 3 deletions luigi/contrib/bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def is_error_5xx(err):
wait=wait_exponential(multiplier=1, min=1, max=10),
stop=stop_after_attempt(3),
reraise=True,
after=lambda x: x.args[0].__initialise_client()
after=lambda x: x.args[0]._initialise_client()
)


Expand Down Expand Up @@ -152,9 +152,9 @@ def __init__(self, oauth_credentials=None, descriptor='', http_=None):
self.descriptor = descriptor
self.http_ = http_

self.__initialise_client()
self._initialise_client()

def __initialise_client(self):
def _initialise_client(self):
authenticate_kwargs = gcp.get_authenticate_kwargs(self.oauth_credentials, self.http_)

if self.descriptor:
Expand Down
54 changes: 54 additions & 0 deletions test/contrib/bigquery_client_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# -*- coding: utf-8 -*-
#
# Copyright 2019 Spotify AB
sonjaer marked this conversation as resolved.
Show resolved Hide resolved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

"""
These are the unit tests for the BigQueryClient class.
"""

import unittest

from mock.mock import MagicMock

from luigi.contrib import bigquery
try:
from googleapiclient import errors
except ImportError:
raise unittest.SkipTest('Unable to load googleapiclient module')


class BigQueryClientTest(unittest.TestCase):

def test_retry_succeeds_on_second_attempt(self):
client = MagicMock(spec=bigquery.BigQueryClient)
attempts = 0

@bigquery.bq_retry
def fail_once(bq_client):
nonlocal attempts
attempts += 1
if attempts == 1:
raise errors.HttpError(
resp=MagicMock(status=500),
content=b'{"error": {"message": "stub"}',
)
else:
return MagicMock(status=200)

response = fail_once(client)
client._initialise_client.assert_called_once()
self.assertEqual(attempts, 2)
self.assertEqual(response.status, 200)