|
| 1 | +# Copyright 2025 Google LLC |
| 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 | +# try/except added for compatibility with python < 3.8 |
| 16 | +try: |
| 17 | + from unittest import mock |
| 18 | +except ImportError: # pragma: NO COVER |
| 19 | + import mock # type: ignore |
| 20 | + |
| 21 | +import pytest |
| 22 | +from grpc import ChannelConnectivity |
| 23 | + |
| 24 | +from google.cloud.bigtable.data._cross_sync import CrossSync |
| 25 | + |
| 26 | +if CrossSync.is_async: |
| 27 | + from google.cloud.bigtable.data._async._swappable_channel import ( |
| 28 | + AsyncSwappableChannel as TargetType, |
| 29 | + ) |
| 30 | +else: |
| 31 | + from google.cloud.bigtable.data._sync_autogen._swappable_channel import ( |
| 32 | + SwappableChannel as TargetType, |
| 33 | + ) |
| 34 | + |
| 35 | + |
| 36 | +__CROSS_SYNC_OUTPUT__ = "tests.unit.data._sync_autogen.test__swappable_channel" |
| 37 | + |
| 38 | + |
| 39 | +@CrossSync.convert_class(sync_name="TestSwappableChannel") |
| 40 | +class TestAsyncSwappableChannel: |
| 41 | + @staticmethod |
| 42 | + @CrossSync.convert |
| 43 | + def _get_target_class(): |
| 44 | + return TargetType |
| 45 | + |
| 46 | + def _make_one(self, *args, **kwargs): |
| 47 | + return self._get_target_class()(*args, **kwargs) |
| 48 | + |
| 49 | + def test_ctor(self): |
| 50 | + channel_fn = mock.Mock() |
| 51 | + instance = self._make_one(channel_fn) |
| 52 | + assert instance._channel_fn == channel_fn |
| 53 | + channel_fn.assert_called_once_with() |
| 54 | + assert instance._channel == channel_fn.return_value |
| 55 | + |
| 56 | + def test_swap_channel(self): |
| 57 | + channel_fn = mock.Mock() |
| 58 | + instance = self._make_one(channel_fn) |
| 59 | + old_channel = instance._channel |
| 60 | + new_channel = object() |
| 61 | + result = instance.swap_channel(new_channel) |
| 62 | + assert result == old_channel |
| 63 | + assert instance._channel == new_channel |
| 64 | + |
| 65 | + def test_create_channel(self): |
| 66 | + channel_fn = mock.Mock() |
| 67 | + instance = self._make_one(channel_fn) |
| 68 | + # reset mock from ctor call |
| 69 | + channel_fn.reset_mock() |
| 70 | + new_channel = instance.create_channel() |
| 71 | + channel_fn.assert_called_once_with() |
| 72 | + assert new_channel == channel_fn.return_value |
| 73 | + |
| 74 | + @CrossSync.drop |
| 75 | + def test_create_channel_async_interceptors_copied(self): |
| 76 | + channel_fn = mock.Mock() |
| 77 | + instance = self._make_one(channel_fn) |
| 78 | + # reset mock from ctor call |
| 79 | + channel_fn.reset_mock() |
| 80 | + # mock out interceptors on original channel |
| 81 | + instance._channel._unary_unary_interceptors = ["unary_unary"] |
| 82 | + instance._channel._unary_stream_interceptors = ["unary_stream"] |
| 83 | + instance._channel._stream_unary_interceptors = ["stream_unary"] |
| 84 | + instance._channel._stream_stream_interceptors = ["stream_stream"] |
| 85 | + |
| 86 | + new_channel = instance.create_channel() |
| 87 | + channel_fn.assert_called_once_with() |
| 88 | + assert new_channel == channel_fn.return_value |
| 89 | + assert new_channel._unary_unary_interceptors == ["unary_unary"] |
| 90 | + assert new_channel._unary_stream_interceptors == ["unary_stream"] |
| 91 | + assert new_channel._stream_unary_interceptors == ["stream_unary"] |
| 92 | + assert new_channel._stream_stream_interceptors == ["stream_stream"] |
| 93 | + |
| 94 | + @pytest.mark.parametrize( |
| 95 | + "method_name,args,kwargs", |
| 96 | + [ |
| 97 | + ("unary_unary", (1,), {"kw": 2}), |
| 98 | + ("unary_stream", (3,), {"kw": 4}), |
| 99 | + ("stream_unary", (5,), {"kw": 6}), |
| 100 | + ("stream_stream", (7,), {"kw": 8}), |
| 101 | + ("get_state", (), {"try_to_connect": True}), |
| 102 | + ], |
| 103 | + ) |
| 104 | + def test_forwarded_methods(self, method_name, args, kwargs): |
| 105 | + channel_fn = mock.Mock() |
| 106 | + instance = self._make_one(channel_fn) |
| 107 | + method = getattr(instance, method_name) |
| 108 | + result = method(*args, **kwargs) |
| 109 | + mock_method = getattr(channel_fn.return_value, method_name) |
| 110 | + mock_method.assert_called_once_with(*args, **kwargs) |
| 111 | + assert result == mock_method.return_value |
| 112 | + |
| 113 | + @pytest.mark.parametrize( |
| 114 | + "method_name,args,kwargs", |
| 115 | + [ |
| 116 | + ("channel_ready", (), {}), |
| 117 | + ("wait_for_state_change", (ChannelConnectivity.READY,), {}), |
| 118 | + ], |
| 119 | + ) |
| 120 | + @CrossSync.pytest |
| 121 | + async def test_forwarded_async_methods(self, method_name, args, kwargs): |
| 122 | + async def dummy_coro(*a, **k): |
| 123 | + return mock.sentinel.result |
| 124 | + |
| 125 | + channel = mock.Mock() |
| 126 | + mock_method = getattr(channel, method_name) |
| 127 | + mock_method.side_effect = dummy_coro |
| 128 | + |
| 129 | + channel_fn = mock.Mock(return_value=channel) |
| 130 | + instance = self._make_one(channel_fn) |
| 131 | + method = getattr(instance, method_name) |
| 132 | + result = await method(*args, **kwargs) |
| 133 | + |
| 134 | + mock_method.assert_called_once_with(*args, **kwargs) |
| 135 | + assert result == mock.sentinel.result |
0 commit comments