Skip to content
Permalink
Browse files
fix: add ConnectionError to default retry (#445)
This adds python built-in [ConnectionError](https://docs.python.org/3/library/exceptions.html#ConnectionError) to default retryable types. 
ConnectionError was recently added in the [BigQuery library](googleapis/python-bigquery#571) to allow retries. 

Fixes #426 🦕
  • Loading branch information
cojenco committed May 20, 2021
1 parent 179de2a commit 8344253a1969b9d04b81f87a6d7bddd3ddb55006
Showing with 30 additions and 1 deletion.
  1. +9 −1 google/cloud/storage/retry.py
  2. +21 −0 tests/unit/test_retry.py
@@ -21,7 +21,14 @@
import json


_RETRYABLE_TYPES = (
# ConnectionError is a built-in exception only in Python3 and not in Python2.
try:
_RETRYABLE_STDLIB_TYPES = (ConnectionError,)
except NameError:
_RETRYABLE_STDLIB_TYPES = ()


_RETRYABLE_TYPES = _RETRYABLE_STDLIB_TYPES + (
api_exceptions.TooManyRequests, # 429
api_exceptions.InternalServerError, # 500
api_exceptions.BadGateway, # 502
@@ -30,6 +37,7 @@
requests.ConnectionError,
)


# Some retriable errors don't have their own custom exception in api_core.
_ADDITIONAL_RETRYABLE_STATUS_CODES = (408,)

@@ -19,6 +19,14 @@
import mock


try:
ConnectionError
except NameError:
_HAS_STDLIB_CONNECTION_ERROR = False
else:
_HAS_STDLIB_CONNECTION_ERROR = True


class Test_should_retry(unittest.TestCase):
def _call_fut(self, exc):
from google.cloud.storage import retry
@@ -56,9 +64,22 @@ def test_w_google_api_call_error_miss(self):
self.assertFalse(self._call_fut(exc))

def test_w_requests_connection_error(self):
import requests

exc = requests.ConnectionError()
self.assertTrue(self._call_fut(exc))

def test_miss_w_stdlib_error(self):
exc = ValueError("testing")
self.assertFalse(self._call_fut(exc))

@unittest.skipUnless(
_HAS_STDLIB_CONNECTION_ERROR, "No builtin 'ConnectionError' in Python 2",
)
def test_w_stdlib_connection_error(self):
exc = ConnectionError()
self.assertTrue(self._call_fut(exc))


class TestConditionalRetryPolicy(unittest.TestCase):
def _make_one(self, retry_policy, conditional_predicate, required_kwargs):

0 comments on commit 8344253

Please sign in to comment.