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: don't rely on duck typing for _retry.is_transient_error #425

Merged
merged 2 commits into from
May 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions google/cloud/ndb/_retry.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,10 @@ def is_transient_error(error):
if core_retry.if_transient_error(error):
return True

method = getattr(error, "code", None)
if method is not None:
code = method()
return code in TRANSIENT_CODES
if isinstance(error, grpc.Call):
method = getattr(error, "code", None)
if callable(method):
code = method()
return code in TRANSIENT_CODES

return False
32 changes: 27 additions & 5 deletions tests/unit/test__retry.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,17 +142,34 @@ def test_core_says_yes(core_retry):

@staticmethod
@mock.patch("google.cloud.ndb._retry.core_retry")
def test_core_says_no_we_say_no(core_retry):
def test_not_a_grpc_call(core_retry):
error = object()
core_retry.if_transient_error.return_value = False
assert _retry.is_transient_error(error) is False
core_retry.if_transient_error.assert_called_once_with(error)

@staticmethod
@mock.patch("google.cloud.ndb._retry.core_retry")
def test_code_is_not_callable(core_retry):
error = mock.Mock(spec=grpc.Call, code=404)
core_retry.if_transient_error.return_value = False
assert _retry.is_transient_error(error) is False
core_retry.if_transient_error.assert_called_once_with(error)

@staticmethod
@mock.patch("google.cloud.ndb._retry.core_retry")
def test_code_is_not_transient(core_retry):
error = mock.Mock(spec=grpc.Call, code=mock.Mock(return_value=42))
core_retry.if_transient_error.return_value = False
assert _retry.is_transient_error(error) is False
core_retry.if_transient_error.assert_called_once_with(error)

@staticmethod
@mock.patch("google.cloud.ndb._retry.core_retry")
def test_unavailable(core_retry):
error = mock.Mock(
code=mock.Mock(return_value=grpc.StatusCode.UNAVAILABLE)
spec=grpc.Call,
code=mock.Mock(return_value=grpc.StatusCode.UNAVAILABLE),
)
core_retry.if_transient_error.return_value = False
assert _retry.is_transient_error(error) is True
Expand All @@ -162,7 +179,8 @@ def test_unavailable(core_retry):
@mock.patch("google.cloud.ndb._retry.core_retry")
def test_internal(core_retry):
error = mock.Mock(
code=mock.Mock(return_value=grpc.StatusCode.INTERNAL)
spec=grpc.Call,
code=mock.Mock(return_value=grpc.StatusCode.INTERNAL),
)
core_retry.if_transient_error.return_value = False
assert _retry.is_transient_error(error) is True
Expand All @@ -172,7 +190,8 @@ def test_internal(core_retry):
@mock.patch("google.cloud.ndb._retry.core_retry")
def test_unauthenticated(core_retry):
error = mock.Mock(
code=mock.Mock(return_value=grpc.StatusCode.UNAUTHENTICATED)
spec=grpc.Call,
code=mock.Mock(return_value=grpc.StatusCode.UNAUTHENTICATED),
)
core_retry.if_transient_error.return_value = False
assert _retry.is_transient_error(error) is False
Expand All @@ -181,7 +200,10 @@ def test_unauthenticated(core_retry):
@staticmethod
@mock.patch("google.cloud.ndb._retry.core_retry")
def test_aborted(core_retry):
error = mock.Mock(code=mock.Mock(return_value=grpc.StatusCode.ABORTED))
error = mock.Mock(
spec=grpc.Call,
code=mock.Mock(return_value=grpc.StatusCode.ABORTED),
)
core_retry.if_transient_error.return_value = False
assert _retry.is_transient_error(error) is True
core_retry.if_transient_error.assert_called_once_with(error)