|
| 1 | +# Copyright 2017 Google Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import grpc |
| 16 | +import mock |
| 17 | +import pytest |
| 18 | + |
| 19 | +from google.api.core import exceptions |
| 20 | +from google.api.core.helpers import grpc_helpers |
| 21 | + |
| 22 | + |
| 23 | +def test__patch_callable_name(): |
| 24 | + callable = mock.Mock(spec=['__class__']) |
| 25 | + callable.__class__ = mock.Mock(spec=['__name__']) |
| 26 | + callable.__class__.__name__ = 'TestCallable' |
| 27 | + |
| 28 | + grpc_helpers._patch_callable_name(callable) |
| 29 | + |
| 30 | + assert callable.__name__ == 'TestCallable' |
| 31 | + |
| 32 | + |
| 33 | +def test__patch_callable_name_no_op(): |
| 34 | + callable = mock.Mock(spec=['__name__']) |
| 35 | + callable.__name__ = 'test_callable' |
| 36 | + |
| 37 | + grpc_helpers._patch_callable_name(callable) |
| 38 | + |
| 39 | + assert callable.__name__ == 'test_callable' |
| 40 | + |
| 41 | + |
| 42 | +class RpcErrorImpl(grpc.RpcError, grpc.Call): |
| 43 | + def __init__(self, code): |
| 44 | + super(RpcErrorImpl, self).__init__() |
| 45 | + self._code = code |
| 46 | + |
| 47 | + def code(self): |
| 48 | + return self._code |
| 49 | + |
| 50 | + def details(self): |
| 51 | + return None |
| 52 | + |
| 53 | + |
| 54 | +def test_wrap_unary_errors(): |
| 55 | + grpc_error = RpcErrorImpl(grpc.StatusCode.INVALID_ARGUMENT) |
| 56 | + callable_ = mock.Mock(spec=['__call__'], side_effect=grpc_error) |
| 57 | + |
| 58 | + wrapped_callable = grpc_helpers._wrap_unary_errors(callable_) |
| 59 | + |
| 60 | + with pytest.raises(exceptions.InvalidArgument) as exc_info: |
| 61 | + wrapped_callable(1, 2, three='four') |
| 62 | + |
| 63 | + callable_.assert_called_once_with(1, 2, three='four') |
| 64 | + assert exc_info.value.response == grpc_error |
| 65 | + |
| 66 | + |
| 67 | +def test_wrap_stream_errors_invocation(): |
| 68 | + grpc_error = RpcErrorImpl(grpc.StatusCode.INVALID_ARGUMENT) |
| 69 | + callable_ = mock.Mock(spec=['__call__'], side_effect=grpc_error) |
| 70 | + |
| 71 | + wrapped_callable = grpc_helpers._wrap_stream_errors(callable_) |
| 72 | + |
| 73 | + with pytest.raises(exceptions.InvalidArgument) as exc_info: |
| 74 | + wrapped_callable(1, 2, three='four') |
| 75 | + |
| 76 | + callable_.assert_called_once_with(1, 2, three='four') |
| 77 | + assert exc_info.value.response == grpc_error |
| 78 | + |
| 79 | + |
| 80 | +class RpcResponseIteratorImpl(object): |
| 81 | + def __init__(self, exception): |
| 82 | + self._exception = exception |
| 83 | + |
| 84 | + # Note: This matches grpc._channel._Rendezvous._next which is what is |
| 85 | + # patched by _wrap_stream_errors. |
| 86 | + def _next(self): |
| 87 | + raise self._exception |
| 88 | + |
| 89 | + def __next__(self): # pragma: NO COVER |
| 90 | + return self._next() |
| 91 | + |
| 92 | + def next(self): # pragma: NO COVER |
| 93 | + return self._next() |
| 94 | + |
| 95 | + |
| 96 | +def test_wrap_stream_errors_iterator(): |
| 97 | + grpc_error = RpcErrorImpl(grpc.StatusCode.UNAVAILABLE) |
| 98 | + response_iter = RpcResponseIteratorImpl(grpc_error) |
| 99 | + callable_ = mock.Mock(spec=['__call__'], return_value=response_iter) |
| 100 | + |
| 101 | + wrapped_callable = grpc_helpers._wrap_stream_errors(callable_) |
| 102 | + |
| 103 | + got_iterator = wrapped_callable(1, 2, three='four') |
| 104 | + |
| 105 | + with pytest.raises(exceptions.ServiceUnavailable) as exc_info: |
| 106 | + next(got_iterator) |
| 107 | + |
| 108 | + assert got_iterator == response_iter |
| 109 | + callable_.assert_called_once_with(1, 2, three='four') |
| 110 | + assert exc_info.value.response == grpc_error |
| 111 | + |
| 112 | + |
| 113 | +@mock.patch('google.api.core.helpers.grpc_helpers._wrap_unary_errors') |
| 114 | +def test_wrap_errors_non_streaming(wrap_unary_errors): |
| 115 | + callable_ = mock.create_autospec(grpc.UnaryUnaryMultiCallable) |
| 116 | + |
| 117 | + result = grpc_helpers.wrap_errors(callable_) |
| 118 | + |
| 119 | + assert result == wrap_unary_errors.return_value |
| 120 | + wrap_unary_errors.assert_called_once_with(callable_) |
| 121 | + |
| 122 | + |
| 123 | +@mock.patch('google.api.core.helpers.grpc_helpers._wrap_stream_errors') |
| 124 | +def test_wrap_errors_streaming(wrap_stream_errors): |
| 125 | + callable_ = mock.create_autospec(grpc.UnaryStreamMultiCallable) |
| 126 | + |
| 127 | + result = grpc_helpers.wrap_errors(callable_) |
| 128 | + |
| 129 | + assert result == wrap_stream_errors.return_value |
| 130 | + wrap_stream_errors.assert_called_once_with(callable_) |
0 commit comments