From fd9148f963af6e95584c2b0c51790253f59afcf8 Mon Sep 17 00:00:00 2001 From: Aza Tulepbergenov Date: Tue, 7 Sep 2021 10:58:28 -0700 Subject: [PATCH 01/14] feat: add support for context manager in client. --- .../%sub/services/%service/client.py.j2 | 11 ++++++ .../services/%service/transports/base.py.j2 | 5 +++ .../services/%service/transports/grpc.py.j2 | 9 +++++ .../services/%service/transports/rest.py.j2 | 8 +++++ tests/system/conftest.py | 1 + tests/system/test_client_context_manager.py | 34 +++++++++++++++++++ 6 files changed, 68 insertions(+) create mode 100644 tests/system/test_client_context_manager.py diff --git a/gapic/templates/%namespace/%name_%version/%sub/services/%service/client.py.j2 b/gapic/templates/%namespace/%name_%version/%sub/services/%service/client.py.j2 index f7a513220e..3237ad5a6e 100644 --- a/gapic/templates/%namespace/%name_%version/%sub/services/%service/client.py.j2 +++ b/gapic/templates/%namespace/%name_%version/%sub/services/%service/client.py.j2 @@ -480,6 +480,17 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta): {{ "\n" }} {% endfor %} + def __enter__(self): + self.transport.__enter__() + return self + + def __exit__(self, type, value, traceback): + """Releases underlying transport's resources. + + WARNING: This makes the transport unusable. + """ + return self.transport.__exit__(type, value, traceback) + {% if opts.add_iam_methods %} def set_iam_policy( self, diff --git a/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/base.py.j2 b/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/base.py.j2 index b38a3535b0..3367214378 100644 --- a/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/base.py.j2 +++ b/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/base.py.j2 @@ -183,6 +183,11 @@ class {{ service.name }}Transport(abc.ABC): {% endfor %} {# precomputed wrappers loop #} } + def __enter__(self): + raise NotImplementedError() + + def __exit__(self, type, value, traceback): + raise NotImplementedError() {% if service.has_lro %} diff --git a/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/grpc.py.j2 b/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/grpc.py.j2 index f2f0da6ea3..c3400fbd66 100644 --- a/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/grpc.py.j2 +++ b/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/grpc.py.j2 @@ -224,6 +224,15 @@ class {{ service.name }}GrpcTransport({{ service.name }}Transport): """Return the channel designed to connect to this service. """ return self._grpc_channel + + def __enter__(self): + return self + + def __exit__(self, type, value, traceback): + """Closes underlying gRPC channel. + """ + self.grpc_channel.close() + {% if service.has_lro %} @property diff --git a/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/rest.py.j2 b/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/rest.py.j2 index 8c677847da..3f16d62535 100644 --- a/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/rest.py.j2 +++ b/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/rest.py.j2 @@ -133,6 +133,14 @@ class {{ service.name }}RestTransport({{ service.name }}Transport): {% for method in service.methods.values() %} {% if method.http_opt %} + def __enter__(self): + return self + + def __exit__(self, type, value, traceback): + """Closes underlying HTTP session. + """ + self._session.close() + def {{ method.name|snake_case }}(self, request: {{ method.input.ident }}, *, retry: retries.Retry = gapic_v1.method.DEFAULT, diff --git a/tests/system/conftest.py b/tests/system/conftest.py index e01f596b2d..3b09346d53 100644 --- a/tests/system/conftest.py +++ b/tests/system/conftest.py @@ -21,6 +21,7 @@ import google.api_core.client_options as ClientOptions from google.auth import credentials + from google.showcase import EchoClient from google.showcase import IdentityClient from google.showcase import MessagingClient diff --git a/tests/system/test_client_context_manager.py b/tests/system/test_client_context_manager.py new file mode 100644 index 0000000000..08d597acb8 --- /dev/null +++ b/tests/system/test_client_context_manager.py @@ -0,0 +1,34 @@ +# Copyright 2021 Google LLC +# +# 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 +# +# https://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. + + +import pytest + +from conftest import construct_client, EchoClient + + +def test_client(): + with construct_client(EchoClient, False) as c: + resp = c.echo({ + 'content': 'hello' + }) + assert resp.content == 'hello' + + +def test_client_destroyed(echo): + echo.__exit__(None, None, None) + with pytest.raises(ValueError): + echo.echo({ + 'content': 'hello' + }) From b67710878f621080787ae8c56537fafc14c56804 Mon Sep 17 00:00:00 2001 From: Aza Tulepbergenov Date: Tue, 7 Sep 2021 11:01:22 -0700 Subject: [PATCH 02/14] chore: remove extra whitespace. --- tests/system/conftest.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/system/conftest.py b/tests/system/conftest.py index 3b09346d53..e01f596b2d 100644 --- a/tests/system/conftest.py +++ b/tests/system/conftest.py @@ -21,7 +21,6 @@ import google.api_core.client_options as ClientOptions from google.auth import credentials - from google.showcase import EchoClient from google.showcase import IdentityClient from google.showcase import MessagingClient From 15b25337ab05a1480cf487d9f478f3c8d9c4339e Mon Sep 17 00:00:00 2001 From: Aza Tulepbergenov Date: Wed, 8 Sep 2021 15:44:18 -0700 Subject: [PATCH 03/14] chore: adds autogenerated unit tests. --- .../%name_%version/%sub/test_%service.py.j2 | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 b/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 index 61618036d4..c0e3bb5758 100644 --- a/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 +++ b/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 @@ -1508,6 +1508,11 @@ def test_{{ service.name|snake_case }}_base_transport(): with pytest.raises(NotImplementedError): getattr(transport, method)(request=object()) + with pytest.raises(NotImplementedError): + transport.__enter__() + with pytest.raises(NotImplementedError): + transport.__exit__(None, None, None) + {% if service.has_lro %} # Additionally, the LRO client (a property) should # also raise NotImplementedError @@ -2459,4 +2464,33 @@ async def test_test_iam_permissions_from_dict_async(): {% endif %} +def test_client_enter_exit(): + client = {{ service.client_name }}() + # Test client calls underlying transport. + with mock.patch.object(type(client.transport), "__enter__") as enter: + with mock.patch.object(type(client.transport), "__exit__") as exit: + enter.assert_not_called() + exit.assert_not_called() + with client as _: + enter.assert_called_once() + exit.assert_called() + +{% if 'grpc' in opts.transport %} +def test_grpc_transport_enter_exit(): + client = {{ service.client_name }}() + with mock.patch.object(type(client.transport._grpc_channel), 'close') as chan_close: + with client as _: + chan_close.assert_not_called() + chan_close.assert_called_once() +{% endif %} + +{% if 'rest' in opts.transport %} +def test_rest_transport_enter_exit(): + client = {{ service.client_name }}() + with mock.patch.object(type(client.transport._session), 'close') as sess_close: + with client as _: + sess_close.assert_not_called() + sess_close.assert_called_once() +{% endif %} + {% endblock %} From f5dc286845dd67b08e193c050eb136744ad3c419 Mon Sep 17 00:00:00 2001 From: Aza Tulepbergenov Date: Wed, 8 Sep 2021 15:47:40 -0700 Subject: [PATCH 04/14] chore: adds stronger warning. --- .../%name_%version/%sub/services/%service/client.py.j2 | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/gapic/templates/%namespace/%name_%version/%sub/services/%service/client.py.j2 b/gapic/templates/%namespace/%name_%version/%sub/services/%service/client.py.j2 index 3237ad5a6e..daf5e097d0 100644 --- a/gapic/templates/%namespace/%name_%version/%sub/services/%service/client.py.j2 +++ b/gapic/templates/%namespace/%name_%version/%sub/services/%service/client.py.j2 @@ -487,7 +487,9 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta): def __exit__(self, type, value, traceback): """Releases underlying transport's resources. - WARNING: This makes the transport unusable. + WARNING: ONLY use as a context manager if the transport is NOT shared + with other clients! Exiting the with block will CLOSE the transport + and may cause errors in other clients! """ return self.transport.__exit__(type, value, traceback) From c560997ed0c5d20ca61d185f34aefa3efaa450e2 Mon Sep 17 00:00:00 2001 From: Aza Tulepbergenov Date: Mon, 13 Sep 2021 10:17:09 -0700 Subject: [PATCH 05/14] chore: fixes tests. --- .../%name_%version/%sub/test_%service.py.j2 | 45 ++++++++++++++----- tests/system/test_client_context_manager.py | 6 +-- 2 files changed, 36 insertions(+), 15 deletions(-) diff --git a/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 b/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 index c0e3bb5758..26051e17bd 100644 --- a/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 +++ b/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 @@ -2464,8 +2464,22 @@ async def test_test_iam_permissions_from_dict_async(): {% endif %} -def test_client_enter_exit(): - client = {{ service.client_name }}() +{% if 'grpc' in opts.transport %} +def test_grpc_transport_enter_exit(): + client = {{ service.client_name }}( + credentials=ga_credentials.AnonymousCredentials(), + transport='grpc', + ) + with mock.patch.object(type(client.transport._grpc_channel), 'close') as chan_close: + with client as _: + chan_close.assert_not_called() + chan_close.assert_called_once() + +def test_grpc_client_enter_exit(): + client = {{ service.client_name }}( + credentials=ga_credentials.AnonymousCredentials(), + transport='grpc', + ) # Test client calls underlying transport. with mock.patch.object(type(client.transport), "__enter__") as enter: with mock.patch.object(type(client.transport), "__exit__") as exit: @@ -2474,23 +2488,32 @@ def test_client_enter_exit(): with client as _: enter.assert_called_once() exit.assert_called() - -{% if 'grpc' in opts.transport %} -def test_grpc_transport_enter_exit(): - client = {{ service.client_name }}() - with mock.patch.object(type(client.transport._grpc_channel), 'close') as chan_close: - with client as _: - chan_close.assert_not_called() - chan_close.assert_called_once() {% endif %} {% if 'rest' in opts.transport %} def test_rest_transport_enter_exit(): - client = {{ service.client_name }}() + client = {{ service.client_name }}( + credentials=ga_credentials.AnonymousCredentials(), + transport='rest', + ) with mock.patch.object(type(client.transport._session), 'close') as sess_close: with client as _: sess_close.assert_not_called() sess_close.assert_called_once() + +def test_rest_client_enter_exit(): + client = {{ service.client_name }}( + credentials=ga_credentials.AnonymousCredentials(), + transport='rest', + ) + # Test client calls underlying transport. + with mock.patch.object(type(client.transport), "__enter__") as enter: + with mock.patch.object(type(client.transport), "__exit__") as exit: + enter.assert_not_called() + exit.assert_not_called() + with client as _: + enter.assert_called_once() + exit.assert_called() {% endif %} {% endblock %} diff --git a/tests/system/test_client_context_manager.py b/tests/system/test_client_context_manager.py index 08d597acb8..6786b2dd43 100644 --- a/tests/system/test_client_context_manager.py +++ b/tests/system/test_client_context_manager.py @@ -15,11 +15,9 @@ import pytest -from conftest import construct_client, EchoClient - -def test_client(): - with construct_client(EchoClient, False) as c: +def test_client(echo): + with echo as c: resp = c.echo({ 'content': 'hello' }) From 983888b8a5b4fa4aea2ba304d5b32b03f861d8e3 Mon Sep 17 00:00:00 2001 From: Aza Tulepbergenov Date: Mon, 13 Sep 2021 11:12:01 -0700 Subject: [PATCH 06/14] chore: adds auto-generated tests for ads. --- .../%sub/services/%service/client.py.j2 | 12 ++++++++ .../services/%service/transports/base.py.j2 | 5 ++++ .../services/%service/transports/grpc.py.j2 | 8 ++++++ .../%name_%version/%sub/test_%service.py.j2 | 28 +++++++++++++++++++ 4 files changed, 53 insertions(+) diff --git a/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/client.py.j2 b/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/client.py.j2 index 90e5002b68..ad30dbd055 100644 --- a/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/client.py.j2 +++ b/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/client.py.j2 @@ -148,6 +148,18 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta): """ return self._transport + def __enter__(self): + self.transport.__enter__() + return self + + def __exit__(self, type, value, traceback): + """Releases underlying transport's resources. + + WARNING: ONLY use as a context manager if the transport is NOT shared + with other clients! Exiting the with block will CLOSE the transport + and may cause errors in other clients! + """ + return self.transport.__exit__(type, value, traceback) {% for message in service.resource_messages|sort(attribute="resource_type") %} @staticmethod diff --git a/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/transports/base.py.j2 b/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/transports/base.py.j2 index 82ab146438..b5fb45c304 100644 --- a/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/transports/base.py.j2 +++ b/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/transports/base.py.j2 @@ -103,6 +103,11 @@ class {{ service.name }}Transport(metaclass=abc.ABCMeta): {% endfor %} {# precomputed wrappers loop #} } + def __enter__(self): + raise NotImplementedError() + + def __exit__(self, type, value, traceback): + raise NotImplementedError() {% if service.has_lro %} diff --git a/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/transports/grpc.py.j2 b/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/transports/grpc.py.j2 index 1562b6f88c..512c7780c2 100644 --- a/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/transports/grpc.py.j2 +++ b/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/transports/grpc.py.j2 @@ -188,6 +188,14 @@ class {{ service.name }}GrpcTransport({{ service.name }}Transport): **kwargs ) + def __enter__(self): + return self + + def __exit__(self, type, value, traceback): + """Closes underlying gRPC channel. + """ + self.grpc_channel.close() + @property def grpc_channel(self) -> grpc.Channel: """Return the channel designed to connect to this service. diff --git a/gapic/ads-templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 b/gapic/ads-templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 index 8d9af17ae6..da0f6de139 100644 --- a/gapic/ads-templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 +++ b/gapic/ads-templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 @@ -654,6 +654,11 @@ def test_{{ service.name|snake_case }}_base_transport(): with pytest.raises(NotImplementedError): getattr(transport, method)(request=object()) + with pytest.raises(NotImplementedError): + transport.__enter__() + with pytest.raises(NotImplementedError): + transport.__exit__(None, None, None) + {% if service.has_lro %} # Additionally, the LRO client (a property) should # also raise NotImplementedError @@ -903,5 +908,28 @@ def test_client_withDEFAULT_CLIENT_INFO(): ) prep.assert_called_once_with(client_info) +def test_grpc_transport_enter_exit(): + client = {{ service.client_name }}( + credentials=ga_credentials.AnonymousCredentials(), + transport='grpc', + ) + with mock.patch.object(type(client.transport._grpc_channel), 'close') as chan_close: + with client as _: + chan_close.assert_not_called() + chan_close.assert_called_once() + +def test_grpc_client_enter_exit(): + client = {{ service.client_name }}( + credentials=ga_credentials.AnonymousCredentials(), + transport='grpc', + ) + # Test client calls underlying transport. + with mock.patch.object(type(client.transport), "__enter__") as enter: + with mock.patch.object(type(client.transport), "__exit__") as exit: + enter.assert_not_called() + exit.assert_not_called() + with client as _: + enter.assert_called_once() + exit.assert_called() {% endblock %} From 3e49e1aeb36f20feeab137067dafd500e2e8dad0 Mon Sep 17 00:00:00 2001 From: Aza Tulepbergenov Date: Mon, 13 Sep 2021 13:59:40 -0700 Subject: [PATCH 07/14] chore: updates golden files. --- .../asset_v1/services/asset_service/client.py | 11 +++++++ .../services/asset_service/transports/base.py | 6 ++++ .../services/asset_service/transports/grpc.py | 8 +++++ .../unit/gapic/asset_v1/test_asset_service.py | 30 +++++++++++++++++++ .../services/iam_credentials/client.py | 11 +++++++ .../iam_credentials/transports/base.py | 6 ++++ .../iam_credentials/transports/grpc.py | 8 +++++ .../credentials_v1/test_iam_credentials.py | 30 +++++++++++++++++++ .../services/config_service_v2/client.py | 11 +++++++ .../config_service_v2/transports/base.py | 6 ++++ .../config_service_v2/transports/grpc.py | 8 +++++ .../services/logging_service_v2/client.py | 11 +++++++ .../logging_service_v2/transports/base.py | 6 ++++ .../logging_service_v2/transports/grpc.py | 8 +++++ .../services/metrics_service_v2/client.py | 11 +++++++ .../metrics_service_v2/transports/base.py | 6 ++++ .../metrics_service_v2/transports/grpc.py | 8 +++++ .../logging_v2/test_config_service_v2.py | 30 +++++++++++++++++++ .../logging_v2/test_logging_service_v2.py | 30 +++++++++++++++++++ .../logging_v2/test_metrics_service_v2.py | 30 +++++++++++++++++++ .../redis_v1/services/cloud_redis/client.py | 11 +++++++ .../services/cloud_redis/transports/base.py | 6 ++++ .../services/cloud_redis/transports/grpc.py | 8 +++++ .../unit/gapic/redis_v1/test_cloud_redis.py | 30 +++++++++++++++++++ 24 files changed, 330 insertions(+) diff --git a/tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/client.py b/tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/client.py index 98dcd3aa8f..72ef1c0ff5 100644 --- a/tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/client.py +++ b/tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/client.py @@ -1448,7 +1448,18 @@ def analyze_iam_policy_longrunning(self, # Done; return the response. return response + def __enter__(self): + self.transport.__enter__() + return self + def __exit__(self, type, value, traceback): + """Releases underlying transport's resources. + + WARNING: ONLY use as a context manager if the transport is NOT shared + with other clients! Exiting the with block will CLOSE the transport + and may cause errors in other clients! + """ + return self.transport.__exit__(type, value, traceback) diff --git a/tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/transports/base.py b/tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/transports/base.py index 45083d6687..da943c3b2c 100644 --- a/tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/transports/base.py +++ b/tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/transports/base.py @@ -259,6 +259,12 @@ def _prep_wrapped_messages(self, client_info): ), } + def __enter__(self): + raise NotImplementedError() + + def __exit__(self, type, value, traceback): + raise NotImplementedError() + @property def operations_client(self) -> operations_v1.OperationsClient: """Return the client designed to process long-running operations.""" diff --git a/tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/transports/grpc.py b/tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/transports/grpc.py index c95a90bff1..4aee2a74f8 100644 --- a/tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/transports/grpc.py +++ b/tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/transports/grpc.py @@ -224,6 +224,14 @@ def grpc_channel(self) -> grpc.Channel: """ return self._grpc_channel + def __enter__(self): + return self + + def __exit__(self, type, value, traceback): + """Closes underlying gRPC channel. + """ + self.grpc_channel.close() + @property def operations_client(self) -> operations_v1.OperationsClient: """Create the client designed to process long-running operations. diff --git a/tests/integration/goldens/asset/tests/unit/gapic/asset_v1/test_asset_service.py b/tests/integration/goldens/asset/tests/unit/gapic/asset_v1/test_asset_service.py index 87c110ebcb..0bdbc78f46 100644 --- a/tests/integration/goldens/asset/tests/unit/gapic/asset_v1/test_asset_service.py +++ b/tests/integration/goldens/asset/tests/unit/gapic/asset_v1/test_asset_service.py @@ -3550,6 +3550,11 @@ def test_asset_service_base_transport(): with pytest.raises(NotImplementedError): getattr(transport, method)(request=object()) + with pytest.raises(NotImplementedError): + transport.__enter__() + with pytest.raises(NotImplementedError): + transport.__exit__(None, None, None) + # Additionally, the LRO client (a property) should # also raise NotImplementedError with pytest.raises(NotImplementedError): @@ -4048,3 +4053,28 @@ def test_client_withDEFAULT_CLIENT_INFO(): client_info=client_info, ) prep.assert_called_once_with(client_info) + + +def test_grpc_transport_enter_exit(): + client = AssetServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport='grpc', + ) + with mock.patch.object(type(client.transport._grpc_channel), 'close') as chan_close: + with client as _: + chan_close.assert_not_called() + chan_close.assert_called_once() + +def test_grpc_client_enter_exit(): + client = AssetServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport='grpc', + ) + # Test client calls underlying transport. + with mock.patch.object(type(client.transport), "__enter__") as enter: + with mock.patch.object(type(client.transport), "__exit__") as exit: + enter.assert_not_called() + exit.assert_not_called() + with client as _: + enter.assert_called_once() + exit.assert_called() diff --git a/tests/integration/goldens/credentials/google/iam/credentials_v1/services/iam_credentials/client.py b/tests/integration/goldens/credentials/google/iam/credentials_v1/services/iam_credentials/client.py index 1280b71919..06a6d06f8e 100644 --- a/tests/integration/goldens/credentials/google/iam/credentials_v1/services/iam_credentials/client.py +++ b/tests/integration/goldens/credentials/google/iam/credentials_v1/services/iam_credentials/client.py @@ -807,7 +807,18 @@ def sign_jwt(self, # Done; return the response. return response + def __enter__(self): + self.transport.__enter__() + return self + def __exit__(self, type, value, traceback): + """Releases underlying transport's resources. + + WARNING: ONLY use as a context manager if the transport is NOT shared + with other clients! Exiting the with block will CLOSE the transport + and may cause errors in other clients! + """ + return self.transport.__exit__(type, value, traceback) diff --git a/tests/integration/goldens/credentials/google/iam/credentials_v1/services/iam_credentials/transports/base.py b/tests/integration/goldens/credentials/google/iam/credentials_v1/services/iam_credentials/transports/base.py index 99c1477502..07e73fb4a5 100644 --- a/tests/integration/goldens/credentials/google/iam/credentials_v1/services/iam_credentials/transports/base.py +++ b/tests/integration/goldens/credentials/google/iam/credentials_v1/services/iam_credentials/transports/base.py @@ -196,6 +196,12 @@ def _prep_wrapped_messages(self, client_info): ), } + def __enter__(self): + raise NotImplementedError() + + def __exit__(self, type, value, traceback): + raise NotImplementedError() + @property def generate_access_token(self) -> Callable[ [common.GenerateAccessTokenRequest], diff --git a/tests/integration/goldens/credentials/google/iam/credentials_v1/services/iam_credentials/transports/grpc.py b/tests/integration/goldens/credentials/google/iam/credentials_v1/services/iam_credentials/transports/grpc.py index 95c0f5f975..4109a7a99f 100644 --- a/tests/integration/goldens/credentials/google/iam/credentials_v1/services/iam_credentials/transports/grpc.py +++ b/tests/integration/goldens/credentials/google/iam/credentials_v1/services/iam_credentials/transports/grpc.py @@ -229,6 +229,14 @@ def grpc_channel(self) -> grpc.Channel: """ return self._grpc_channel + def __enter__(self): + return self + + def __exit__(self, type, value, traceback): + """Closes underlying gRPC channel. + """ + self.grpc_channel.close() + @property def generate_access_token(self) -> Callable[ [common.GenerateAccessTokenRequest], diff --git a/tests/integration/goldens/credentials/tests/unit/gapic/credentials_v1/test_iam_credentials.py b/tests/integration/goldens/credentials/tests/unit/gapic/credentials_v1/test_iam_credentials.py index 74dacb0923..cfb6288f14 100644 --- a/tests/integration/goldens/credentials/tests/unit/gapic/credentials_v1/test_iam_credentials.py +++ b/tests/integration/goldens/credentials/tests/unit/gapic/credentials_v1/test_iam_credentials.py @@ -1484,6 +1484,11 @@ def test_iam_credentials_base_transport(): with pytest.raises(NotImplementedError): getattr(transport, method)(request=object()) + with pytest.raises(NotImplementedError): + transport.__enter__() + with pytest.raises(NotImplementedError): + transport.__exit__(None, None, None) + @requires_google_auth_gte_1_25_0 def test_iam_credentials_base_transport_with_credentials_file(): @@ -1928,3 +1933,28 @@ def test_client_withDEFAULT_CLIENT_INFO(): client_info=client_info, ) prep.assert_called_once_with(client_info) + + +def test_grpc_transport_enter_exit(): + client = IAMCredentialsClient( + credentials=ga_credentials.AnonymousCredentials(), + transport='grpc', + ) + with mock.patch.object(type(client.transport._grpc_channel), 'close') as chan_close: + with client as _: + chan_close.assert_not_called() + chan_close.assert_called_once() + +def test_grpc_client_enter_exit(): + client = IAMCredentialsClient( + credentials=ga_credentials.AnonymousCredentials(), + transport='grpc', + ) + # Test client calls underlying transport. + with mock.patch.object(type(client.transport), "__enter__") as enter: + with mock.patch.object(type(client.transport), "__exit__") as exit: + enter.assert_not_called() + exit.assert_not_called() + with client as _: + enter.assert_called_once() + exit.assert_called() diff --git a/tests/integration/goldens/logging/google/cloud/logging_v2/services/config_service_v2/client.py b/tests/integration/goldens/logging/google/cloud/logging_v2/services/config_service_v2/client.py index acf10f2292..c51ca1f3b8 100644 --- a/tests/integration/goldens/logging/google/cloud/logging_v2/services/config_service_v2/client.py +++ b/tests/integration/goldens/logging/google/cloud/logging_v2/services/config_service_v2/client.py @@ -2179,7 +2179,18 @@ def update_cmek_settings(self, # Done; return the response. return response + def __enter__(self): + self.transport.__enter__() + return self + def __exit__(self, type, value, traceback): + """Releases underlying transport's resources. + + WARNING: ONLY use as a context manager if the transport is NOT shared + with other clients! Exiting the with block will CLOSE the transport + and may cause errors in other clients! + """ + return self.transport.__exit__(type, value, traceback) diff --git a/tests/integration/goldens/logging/google/cloud/logging_v2/services/config_service_v2/transports/base.py b/tests/integration/goldens/logging/google/cloud/logging_v2/services/config_service_v2/transports/base.py index e7f0db9d40..d7e265cd85 100644 --- a/tests/integration/goldens/logging/google/cloud/logging_v2/services/config_service_v2/transports/base.py +++ b/tests/integration/goldens/logging/google/cloud/logging_v2/services/config_service_v2/transports/base.py @@ -323,6 +323,12 @@ def _prep_wrapped_messages(self, client_info): ), } + def __enter__(self): + raise NotImplementedError() + + def __exit__(self, type, value, traceback): + raise NotImplementedError() + @property def list_buckets(self) -> Callable[ [logging_config.ListBucketsRequest], diff --git a/tests/integration/goldens/logging/google/cloud/logging_v2/services/config_service_v2/transports/grpc.py b/tests/integration/goldens/logging/google/cloud/logging_v2/services/config_service_v2/transports/grpc.py index 94e4af6833..777cb5b841 100644 --- a/tests/integration/goldens/logging/google/cloud/logging_v2/services/config_service_v2/transports/grpc.py +++ b/tests/integration/goldens/logging/google/cloud/logging_v2/services/config_service_v2/transports/grpc.py @@ -221,6 +221,14 @@ def grpc_channel(self) -> grpc.Channel: """ return self._grpc_channel + def __enter__(self): + return self + + def __exit__(self, type, value, traceback): + """Closes underlying gRPC channel. + """ + self.grpc_channel.close() + @property def list_buckets(self) -> Callable[ [logging_config.ListBucketsRequest], diff --git a/tests/integration/goldens/logging/google/cloud/logging_v2/services/logging_service_v2/client.py b/tests/integration/goldens/logging/google/cloud/logging_v2/services/logging_service_v2/client.py index 3549459766..b3a0aee9e0 100644 --- a/tests/integration/goldens/logging/google/cloud/logging_v2/services/logging_service_v2/client.py +++ b/tests/integration/goldens/logging/google/cloud/logging_v2/services/logging_service_v2/client.py @@ -901,7 +901,18 @@ def tail_log_entries(self, # Done; return the response. return response + def __enter__(self): + self.transport.__enter__() + return self + def __exit__(self, type, value, traceback): + """Releases underlying transport's resources. + + WARNING: ONLY use as a context manager if the transport is NOT shared + with other clients! Exiting the with block will CLOSE the transport + and may cause errors in other clients! + """ + return self.transport.__exit__(type, value, traceback) diff --git a/tests/integration/goldens/logging/google/cloud/logging_v2/services/logging_service_v2/transports/base.py b/tests/integration/goldens/logging/google/cloud/logging_v2/services/logging_service_v2/transports/base.py index 222ed3c1f9..f826aa2956 100644 --- a/tests/integration/goldens/logging/google/cloud/logging_v2/services/logging_service_v2/transports/base.py +++ b/tests/integration/goldens/logging/google/cloud/logging_v2/services/logging_service_v2/transports/base.py @@ -231,6 +231,12 @@ def _prep_wrapped_messages(self, client_info): ), } + def __enter__(self): + raise NotImplementedError() + + def __exit__(self, type, value, traceback): + raise NotImplementedError() + @property def delete_log(self) -> Callable[ [logging.DeleteLogRequest], diff --git a/tests/integration/goldens/logging/google/cloud/logging_v2/services/logging_service_v2/transports/grpc.py b/tests/integration/goldens/logging/google/cloud/logging_v2/services/logging_service_v2/transports/grpc.py index f66cb54a21..d03fd97c2c 100644 --- a/tests/integration/goldens/logging/google/cloud/logging_v2/services/logging_service_v2/transports/grpc.py +++ b/tests/integration/goldens/logging/google/cloud/logging_v2/services/logging_service_v2/transports/grpc.py @@ -221,6 +221,14 @@ def grpc_channel(self) -> grpc.Channel: """ return self._grpc_channel + def __enter__(self): + return self + + def __exit__(self, type, value, traceback): + """Closes underlying gRPC channel. + """ + self.grpc_channel.close() + @property def delete_log(self) -> Callable[ [logging.DeleteLogRequest], diff --git a/tests/integration/goldens/logging/google/cloud/logging_v2/services/metrics_service_v2/client.py b/tests/integration/goldens/logging/google/cloud/logging_v2/services/metrics_service_v2/client.py index af554cf6d6..cdcfd92b2e 100644 --- a/tests/integration/goldens/logging/google/cloud/logging_v2/services/metrics_service_v2/client.py +++ b/tests/integration/goldens/logging/google/cloud/logging_v2/services/metrics_service_v2/client.py @@ -780,7 +780,18 @@ def delete_log_metric(self, metadata=metadata, ) + def __enter__(self): + self.transport.__enter__() + return self + def __exit__(self, type, value, traceback): + """Releases underlying transport's resources. + + WARNING: ONLY use as a context manager if the transport is NOT shared + with other clients! Exiting the with block will CLOSE the transport + and may cause errors in other clients! + """ + return self.transport.__exit__(type, value, traceback) diff --git a/tests/integration/goldens/logging/google/cloud/logging_v2/services/metrics_service_v2/transports/base.py b/tests/integration/goldens/logging/google/cloud/logging_v2/services/metrics_service_v2/transports/base.py index b9170bf568..5969e03201 100644 --- a/tests/integration/goldens/logging/google/cloud/logging_v2/services/metrics_service_v2/transports/base.py +++ b/tests/integration/goldens/logging/google/cloud/logging_v2/services/metrics_service_v2/transports/base.py @@ -210,6 +210,12 @@ def _prep_wrapped_messages(self, client_info): ), } + def __enter__(self): + raise NotImplementedError() + + def __exit__(self, type, value, traceback): + raise NotImplementedError() + @property def list_log_metrics(self) -> Callable[ [logging_metrics.ListLogMetricsRequest], diff --git a/tests/integration/goldens/logging/google/cloud/logging_v2/services/metrics_service_v2/transports/grpc.py b/tests/integration/goldens/logging/google/cloud/logging_v2/services/metrics_service_v2/transports/grpc.py index e300d9f532..7a7df33b10 100644 --- a/tests/integration/goldens/logging/google/cloud/logging_v2/services/metrics_service_v2/transports/grpc.py +++ b/tests/integration/goldens/logging/google/cloud/logging_v2/services/metrics_service_v2/transports/grpc.py @@ -221,6 +221,14 @@ def grpc_channel(self) -> grpc.Channel: """ return self._grpc_channel + def __enter__(self): + return self + + def __exit__(self, type, value, traceback): + """Closes underlying gRPC channel. + """ + self.grpc_channel.close() + @property def list_log_metrics(self) -> Callable[ [logging_metrics.ListLogMetricsRequest], diff --git a/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_config_service_v2.py b/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_config_service_v2.py index 979cbd3605..fc5e0b4bd5 100644 --- a/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_config_service_v2.py +++ b/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_config_service_v2.py @@ -5906,6 +5906,11 @@ def test_config_service_v2_base_transport(): with pytest.raises(NotImplementedError): getattr(transport, method)(request=object()) + with pytest.raises(NotImplementedError): + transport.__enter__() + with pytest.raises(NotImplementedError): + transport.__exit__(None, None, None) + @requires_google_auth_gte_1_25_0 def test_config_service_v2_base_transport_with_credentials_file(): @@ -6445,3 +6450,28 @@ def test_client_withDEFAULT_CLIENT_INFO(): client_info=client_info, ) prep.assert_called_once_with(client_info) + + +def test_grpc_transport_enter_exit(): + client = ConfigServiceV2Client( + credentials=ga_credentials.AnonymousCredentials(), + transport='grpc', + ) + with mock.patch.object(type(client.transport._grpc_channel), 'close') as chan_close: + with client as _: + chan_close.assert_not_called() + chan_close.assert_called_once() + +def test_grpc_client_enter_exit(): + client = ConfigServiceV2Client( + credentials=ga_credentials.AnonymousCredentials(), + transport='grpc', + ) + # Test client calls underlying transport. + with mock.patch.object(type(client.transport), "__enter__") as enter: + with mock.patch.object(type(client.transport), "__exit__") as exit: + enter.assert_not_called() + exit.assert_not_called() + with client as _: + enter.assert_called_once() + exit.assert_called() diff --git a/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_logging_service_v2.py b/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_logging_service_v2.py index b952814609..9400667d7c 100644 --- a/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_logging_service_v2.py +++ b/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_logging_service_v2.py @@ -2028,6 +2028,11 @@ def test_logging_service_v2_base_transport(): with pytest.raises(NotImplementedError): getattr(transport, method)(request=object()) + with pytest.raises(NotImplementedError): + transport.__enter__() + with pytest.raises(NotImplementedError): + transport.__exit__(None, None, None) + @requires_google_auth_gte_1_25_0 def test_logging_service_v2_base_transport_with_credentials_file(): @@ -2492,3 +2497,28 @@ def test_client_withDEFAULT_CLIENT_INFO(): client_info=client_info, ) prep.assert_called_once_with(client_info) + + +def test_grpc_transport_enter_exit(): + client = LoggingServiceV2Client( + credentials=ga_credentials.AnonymousCredentials(), + transport='grpc', + ) + with mock.patch.object(type(client.transport._grpc_channel), 'close') as chan_close: + with client as _: + chan_close.assert_not_called() + chan_close.assert_called_once() + +def test_grpc_client_enter_exit(): + client = LoggingServiceV2Client( + credentials=ga_credentials.AnonymousCredentials(), + transport='grpc', + ) + # Test client calls underlying transport. + with mock.patch.object(type(client.transport), "__enter__") as enter: + with mock.patch.object(type(client.transport), "__exit__") as exit: + enter.assert_not_called() + exit.assert_not_called() + with client as _: + enter.assert_called_once() + exit.assert_called() diff --git a/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_metrics_service_v2.py b/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_metrics_service_v2.py index 5ce85b4284..b7a22c9172 100644 --- a/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_metrics_service_v2.py +++ b/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_metrics_service_v2.py @@ -1893,6 +1893,11 @@ def test_metrics_service_v2_base_transport(): with pytest.raises(NotImplementedError): getattr(transport, method)(request=object()) + with pytest.raises(NotImplementedError): + transport.__enter__() + with pytest.raises(NotImplementedError): + transport.__exit__(None, None, None) + @requires_google_auth_gte_1_25_0 def test_metrics_service_v2_base_transport_with_credentials_file(): @@ -2357,3 +2362,28 @@ def test_client_withDEFAULT_CLIENT_INFO(): client_info=client_info, ) prep.assert_called_once_with(client_info) + + +def test_grpc_transport_enter_exit(): + client = MetricsServiceV2Client( + credentials=ga_credentials.AnonymousCredentials(), + transport='grpc', + ) + with mock.patch.object(type(client.transport._grpc_channel), 'close') as chan_close: + with client as _: + chan_close.assert_not_called() + chan_close.assert_called_once() + +def test_grpc_client_enter_exit(): + client = MetricsServiceV2Client( + credentials=ga_credentials.AnonymousCredentials(), + transport='grpc', + ) + # Test client calls underlying transport. + with mock.patch.object(type(client.transport), "__enter__") as enter: + with mock.patch.object(type(client.transport), "__exit__") as exit: + enter.assert_not_called() + exit.assert_not_called() + with client as _: + enter.assert_called_once() + exit.assert_called() diff --git a/tests/integration/goldens/redis/google/cloud/redis_v1/services/cloud_redis/client.py b/tests/integration/goldens/redis/google/cloud/redis_v1/services/cloud_redis/client.py index 2857a5fb9f..486629049c 100644 --- a/tests/integration/goldens/redis/google/cloud/redis_v1/services/cloud_redis/client.py +++ b/tests/integration/goldens/redis/google/cloud/redis_v1/services/cloud_redis/client.py @@ -1269,7 +1269,18 @@ def delete_instance(self, # Done; return the response. return response + def __enter__(self): + self.transport.__enter__() + return self + def __exit__(self, type, value, traceback): + """Releases underlying transport's resources. + + WARNING: ONLY use as a context manager if the transport is NOT shared + with other clients! Exiting the with block will CLOSE the transport + and may cause errors in other clients! + """ + return self.transport.__exit__(type, value, traceback) diff --git a/tests/integration/goldens/redis/google/cloud/redis_v1/services/cloud_redis/transports/base.py b/tests/integration/goldens/redis/google/cloud/redis_v1/services/cloud_redis/transports/base.py index d8161b1e78..62c1c9350b 100644 --- a/tests/integration/goldens/redis/google/cloud/redis_v1/services/cloud_redis/transports/base.py +++ b/tests/integration/goldens/redis/google/cloud/redis_v1/services/cloud_redis/transports/base.py @@ -195,6 +195,12 @@ def _prep_wrapped_messages(self, client_info): ), } + def __enter__(self): + raise NotImplementedError() + + def __exit__(self, type, value, traceback): + raise NotImplementedError() + @property def operations_client(self) -> operations_v1.OperationsClient: """Return the client designed to process long-running operations.""" diff --git a/tests/integration/goldens/redis/google/cloud/redis_v1/services/cloud_redis/transports/grpc.py b/tests/integration/goldens/redis/google/cloud/redis_v1/services/cloud_redis/transports/grpc.py index 5b15755d04..d9810f97ed 100644 --- a/tests/integration/goldens/redis/google/cloud/redis_v1/services/cloud_redis/transports/grpc.py +++ b/tests/integration/goldens/redis/google/cloud/redis_v1/services/cloud_redis/transports/grpc.py @@ -243,6 +243,14 @@ def grpc_channel(self) -> grpc.Channel: """ return self._grpc_channel + def __enter__(self): + return self + + def __exit__(self, type, value, traceback): + """Closes underlying gRPC channel. + """ + self.grpc_channel.close() + @property def operations_client(self) -> operations_v1.OperationsClient: """Create the client designed to process long-running operations. diff --git a/tests/integration/goldens/redis/tests/unit/gapic/redis_v1/test_cloud_redis.py b/tests/integration/goldens/redis/tests/unit/gapic/redis_v1/test_cloud_redis.py index a1c9cc2e64..0306788504 100644 --- a/tests/integration/goldens/redis/tests/unit/gapic/redis_v1/test_cloud_redis.py +++ b/tests/integration/goldens/redis/tests/unit/gapic/redis_v1/test_cloud_redis.py @@ -2859,6 +2859,11 @@ def test_cloud_redis_base_transport(): with pytest.raises(NotImplementedError): getattr(transport, method)(request=object()) + with pytest.raises(NotImplementedError): + transport.__enter__() + with pytest.raises(NotImplementedError): + transport.__exit__(None, None, None) + # Additionally, the LRO client (a property) should # also raise NotImplementedError with pytest.raises(NotImplementedError): @@ -3344,3 +3349,28 @@ def test_client_withDEFAULT_CLIENT_INFO(): client_info=client_info, ) prep.assert_called_once_with(client_info) + + +def test_grpc_transport_enter_exit(): + client = CloudRedisClient( + credentials=ga_credentials.AnonymousCredentials(), + transport='grpc', + ) + with mock.patch.object(type(client.transport._grpc_channel), 'close') as chan_close: + with client as _: + chan_close.assert_not_called() + chan_close.assert_called_once() + +def test_grpc_client_enter_exit(): + client = CloudRedisClient( + credentials=ga_credentials.AnonymousCredentials(), + transport='grpc', + ) + # Test client calls underlying transport. + with mock.patch.object(type(client.transport), "__enter__") as enter: + with mock.patch.object(type(client.transport), "__exit__") as exit: + enter.assert_not_called() + exit.assert_not_called() + with client as _: + enter.assert_called_once() + exit.assert_called() From b00a6a6bb18acdfc738cb405df93ec1369415d23 Mon Sep 17 00:00:00 2001 From: Aza Tulepbergenov Date: Tue, 14 Sep 2021 14:16:49 -0700 Subject: [PATCH 08/14] chore: refactor. --- .../%name_%version/%sub/test_%service.py.j2 | 98 +++++++++---------- .../unit/gapic/asset_v1/test_asset_service.py | 53 +++++----- .../credentials_v1/test_iam_credentials.py | 55 ++++++----- .../logging_v2/test_config_service_v2.py | 55 ++++++----- .../logging_v2/test_logging_service_v2.py | 55 ++++++----- .../logging_v2/test_metrics_service_v2.py | 55 ++++++----- .../unit/gapic/redis_v1/test_cloud_redis.py | 53 +++++----- 7 files changed, 237 insertions(+), 187 deletions(-) diff --git a/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 b/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 index 26051e17bd..3aa657d5f6 100644 --- a/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 +++ b/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 @@ -1510,6 +1510,7 @@ def test_{{ service.name|snake_case }}_base_transport(): with pytest.raises(NotImplementedError): transport.__enter__() + with pytest.raises(NotImplementedError): transport.__exit__(None, None, None) @@ -2464,56 +2465,51 @@ async def test_test_iam_permissions_from_dict_async(): {% endif %} -{% if 'grpc' in opts.transport %} -def test_grpc_transport_enter_exit(): - client = {{ service.client_name }}( - credentials=ga_credentials.AnonymousCredentials(), - transport='grpc', - ) - with mock.patch.object(type(client.transport._grpc_channel), 'close') as chan_close: - with client as _: - chan_close.assert_not_called() - chan_close.assert_called_once() - -def test_grpc_client_enter_exit(): - client = {{ service.client_name }}( - credentials=ga_credentials.AnonymousCredentials(), - transport='grpc', - ) - # Test client calls underlying transport. - with mock.patch.object(type(client.transport), "__enter__") as enter: - with mock.patch.object(type(client.transport), "__exit__") as exit: - enter.assert_not_called() - exit.assert_not_called() - with client as _: - enter.assert_called_once() - exit.assert_called() -{% endif %} - -{% if 'rest' in opts.transport %} -def test_rest_transport_enter_exit(): - client = {{ service.client_name }}( - credentials=ga_credentials.AnonymousCredentials(), - transport='rest', - ) - with mock.patch.object(type(client.transport._session), 'close') as sess_close: - with client as _: - sess_close.assert_not_called() - sess_close.assert_called_once() - -def test_rest_client_enter_exit(): - client = {{ service.client_name }}( - credentials=ga_credentials.AnonymousCredentials(), - transport='rest', - ) - # Test client calls underlying transport. - with mock.patch.object(type(client.transport), "__enter__") as enter: - with mock.patch.object(type(client.transport), "__exit__") as exit: - enter.assert_not_called() - exit.assert_not_called() - with client as _: - enter.assert_called_once() - exit.assert_called() -{% endif %} +def test_transport_ctx(): + transports = [ + {% if 'rest' in opts.transport %} + 'rest', + {% endif %} + {% if 'grpc' in opts.transport %} + 'grpc', + {% endif %} + ] + for transport in transports: + client = {{ service.client_name }}( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport + ) + {% if 'rest' in opts.transport %} + with mock.patch.object(type(client.transport._session), 'close') as close: + {% endif %} + {% if 'grpc' in opts.transport %} + with mock.patch.object(type(client.transport._grpc_channel), 'close') as close: + {% endif %} + with client: + close.assert_not_called() + close.assert_called_once() + +def test_client_ctx(): + transports = [ + {% if 'rest' in opts.transport %} + 'rest', + {% endif %} + {% if 'grpc' in opts.transport %} + 'grpc', + {% endif %} + ] + for transport in transports: + client = {{ service.client_name }}( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport + ) + # Test client calls underlying transport. + with mock.patch.object(type(client.transport), "__enter__") as enter: + with mock.patch.object(type(client.transport), "__exit__") as exit: + enter.assert_not_called() + exit.assert_not_called() + with client: + enter.assert_called_once() + exit.assert_called() {% endblock %} diff --git a/tests/integration/goldens/asset/tests/unit/gapic/asset_v1/test_asset_service.py b/tests/integration/goldens/asset/tests/unit/gapic/asset_v1/test_asset_service.py index 0bdbc78f46..e38b1b38a1 100644 --- a/tests/integration/goldens/asset/tests/unit/gapic/asset_v1/test_asset_service.py +++ b/tests/integration/goldens/asset/tests/unit/gapic/asset_v1/test_asset_service.py @@ -3552,6 +3552,7 @@ def test_asset_service_base_transport(): with pytest.raises(NotImplementedError): transport.__enter__() + with pytest.raises(NotImplementedError): transport.__exit__(None, None, None) @@ -4055,26 +4056,34 @@ def test_client_withDEFAULT_CLIENT_INFO(): prep.assert_called_once_with(client_info) -def test_grpc_transport_enter_exit(): - client = AssetServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - transport='grpc', - ) - with mock.patch.object(type(client.transport._grpc_channel), 'close') as chan_close: - with client as _: - chan_close.assert_not_called() - chan_close.assert_called_once() +def test_transport_ctx(): + transports = [ + 'grpc', + ] + for transport in transports: + client = AssetServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport + ) + with mock.patch.object(type(client.transport._grpc_channel), 'close') as close: + with client: + close.assert_not_called() + close.assert_called_once() -def test_grpc_client_enter_exit(): - client = AssetServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - transport='grpc', - ) - # Test client calls underlying transport. - with mock.patch.object(type(client.transport), "__enter__") as enter: - with mock.patch.object(type(client.transport), "__exit__") as exit: - enter.assert_not_called() - exit.assert_not_called() - with client as _: - enter.assert_called_once() - exit.assert_called() +def test_client_ctx(): + transports = [ + 'grpc', + ] + for transport in transports: + client = AssetServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport + ) + # Test client calls underlying transport. + with mock.patch.object(type(client.transport), "__enter__") as enter: + with mock.patch.object(type(client.transport), "__exit__") as exit: + enter.assert_not_called() + exit.assert_not_called() + with client: + enter.assert_called_once() + exit.assert_called() diff --git a/tests/integration/goldens/credentials/tests/unit/gapic/credentials_v1/test_iam_credentials.py b/tests/integration/goldens/credentials/tests/unit/gapic/credentials_v1/test_iam_credentials.py index cfb6288f14..6dc4d8fbb9 100644 --- a/tests/integration/goldens/credentials/tests/unit/gapic/credentials_v1/test_iam_credentials.py +++ b/tests/integration/goldens/credentials/tests/unit/gapic/credentials_v1/test_iam_credentials.py @@ -1486,6 +1486,7 @@ def test_iam_credentials_base_transport(): with pytest.raises(NotImplementedError): transport.__enter__() + with pytest.raises(NotImplementedError): transport.__exit__(None, None, None) @@ -1935,26 +1936,34 @@ def test_client_withDEFAULT_CLIENT_INFO(): prep.assert_called_once_with(client_info) -def test_grpc_transport_enter_exit(): - client = IAMCredentialsClient( - credentials=ga_credentials.AnonymousCredentials(), - transport='grpc', - ) - with mock.patch.object(type(client.transport._grpc_channel), 'close') as chan_close: - with client as _: - chan_close.assert_not_called() - chan_close.assert_called_once() - -def test_grpc_client_enter_exit(): - client = IAMCredentialsClient( - credentials=ga_credentials.AnonymousCredentials(), - transport='grpc', - ) - # Test client calls underlying transport. - with mock.patch.object(type(client.transport), "__enter__") as enter: - with mock.patch.object(type(client.transport), "__exit__") as exit: - enter.assert_not_called() - exit.assert_not_called() - with client as _: - enter.assert_called_once() - exit.assert_called() +def test_transport_ctx(): + transports = [ + 'grpc', + ] + for transport in transports: + client = IAMCredentialsClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport + ) + with mock.patch.object(type(client.transport._grpc_channel), 'close') as close: + with client: + close.assert_not_called() + close.assert_called_once() + +def test_client_ctx(): + transports = [ + 'grpc', + ] + for transport in transports: + client = IAMCredentialsClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport + ) + # Test client calls underlying transport. + with mock.patch.object(type(client.transport), "__enter__") as enter: + with mock.patch.object(type(client.transport), "__exit__") as exit: + enter.assert_not_called() + exit.assert_not_called() + with client: + enter.assert_called_once() + exit.assert_called() diff --git a/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_config_service_v2.py b/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_config_service_v2.py index fc5e0b4bd5..697f4d3583 100644 --- a/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_config_service_v2.py +++ b/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_config_service_v2.py @@ -5908,6 +5908,7 @@ def test_config_service_v2_base_transport(): with pytest.raises(NotImplementedError): transport.__enter__() + with pytest.raises(NotImplementedError): transport.__exit__(None, None, None) @@ -6452,26 +6453,34 @@ def test_client_withDEFAULT_CLIENT_INFO(): prep.assert_called_once_with(client_info) -def test_grpc_transport_enter_exit(): - client = ConfigServiceV2Client( - credentials=ga_credentials.AnonymousCredentials(), - transport='grpc', - ) - with mock.patch.object(type(client.transport._grpc_channel), 'close') as chan_close: - with client as _: - chan_close.assert_not_called() - chan_close.assert_called_once() - -def test_grpc_client_enter_exit(): - client = ConfigServiceV2Client( - credentials=ga_credentials.AnonymousCredentials(), - transport='grpc', - ) - # Test client calls underlying transport. - with mock.patch.object(type(client.transport), "__enter__") as enter: - with mock.patch.object(type(client.transport), "__exit__") as exit: - enter.assert_not_called() - exit.assert_not_called() - with client as _: - enter.assert_called_once() - exit.assert_called() +def test_transport_ctx(): + transports = [ + 'grpc', + ] + for transport in transports: + client = ConfigServiceV2Client( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport + ) + with mock.patch.object(type(client.transport._grpc_channel), 'close') as close: + with client: + close.assert_not_called() + close.assert_called_once() + +def test_client_ctx(): + transports = [ + 'grpc', + ] + for transport in transports: + client = ConfigServiceV2Client( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport + ) + # Test client calls underlying transport. + with mock.patch.object(type(client.transport), "__enter__") as enter: + with mock.patch.object(type(client.transport), "__exit__") as exit: + enter.assert_not_called() + exit.assert_not_called() + with client: + enter.assert_called_once() + exit.assert_called() diff --git a/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_logging_service_v2.py b/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_logging_service_v2.py index 9400667d7c..cf042657c1 100644 --- a/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_logging_service_v2.py +++ b/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_logging_service_v2.py @@ -2030,6 +2030,7 @@ def test_logging_service_v2_base_transport(): with pytest.raises(NotImplementedError): transport.__enter__() + with pytest.raises(NotImplementedError): transport.__exit__(None, None, None) @@ -2499,26 +2500,34 @@ def test_client_withDEFAULT_CLIENT_INFO(): prep.assert_called_once_with(client_info) -def test_grpc_transport_enter_exit(): - client = LoggingServiceV2Client( - credentials=ga_credentials.AnonymousCredentials(), - transport='grpc', - ) - with mock.patch.object(type(client.transport._grpc_channel), 'close') as chan_close: - with client as _: - chan_close.assert_not_called() - chan_close.assert_called_once() - -def test_grpc_client_enter_exit(): - client = LoggingServiceV2Client( - credentials=ga_credentials.AnonymousCredentials(), - transport='grpc', - ) - # Test client calls underlying transport. - with mock.patch.object(type(client.transport), "__enter__") as enter: - with mock.patch.object(type(client.transport), "__exit__") as exit: - enter.assert_not_called() - exit.assert_not_called() - with client as _: - enter.assert_called_once() - exit.assert_called() +def test_transport_ctx(): + transports = [ + 'grpc', + ] + for transport in transports: + client = LoggingServiceV2Client( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport + ) + with mock.patch.object(type(client.transport._grpc_channel), 'close') as close: + with client: + close.assert_not_called() + close.assert_called_once() + +def test_client_ctx(): + transports = [ + 'grpc', + ] + for transport in transports: + client = LoggingServiceV2Client( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport + ) + # Test client calls underlying transport. + with mock.patch.object(type(client.transport), "__enter__") as enter: + with mock.patch.object(type(client.transport), "__exit__") as exit: + enter.assert_not_called() + exit.assert_not_called() + with client: + enter.assert_called_once() + exit.assert_called() diff --git a/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_metrics_service_v2.py b/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_metrics_service_v2.py index b7a22c9172..b71114b296 100644 --- a/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_metrics_service_v2.py +++ b/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_metrics_service_v2.py @@ -1895,6 +1895,7 @@ def test_metrics_service_v2_base_transport(): with pytest.raises(NotImplementedError): transport.__enter__() + with pytest.raises(NotImplementedError): transport.__exit__(None, None, None) @@ -2364,26 +2365,34 @@ def test_client_withDEFAULT_CLIENT_INFO(): prep.assert_called_once_with(client_info) -def test_grpc_transport_enter_exit(): - client = MetricsServiceV2Client( - credentials=ga_credentials.AnonymousCredentials(), - transport='grpc', - ) - with mock.patch.object(type(client.transport._grpc_channel), 'close') as chan_close: - with client as _: - chan_close.assert_not_called() - chan_close.assert_called_once() - -def test_grpc_client_enter_exit(): - client = MetricsServiceV2Client( - credentials=ga_credentials.AnonymousCredentials(), - transport='grpc', - ) - # Test client calls underlying transport. - with mock.patch.object(type(client.transport), "__enter__") as enter: - with mock.patch.object(type(client.transport), "__exit__") as exit: - enter.assert_not_called() - exit.assert_not_called() - with client as _: - enter.assert_called_once() - exit.assert_called() +def test_transport_ctx(): + transports = [ + 'grpc', + ] + for transport in transports: + client = MetricsServiceV2Client( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport + ) + with mock.patch.object(type(client.transport._grpc_channel), 'close') as close: + with client: + close.assert_not_called() + close.assert_called_once() + +def test_client_ctx(): + transports = [ + 'grpc', + ] + for transport in transports: + client = MetricsServiceV2Client( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport + ) + # Test client calls underlying transport. + with mock.patch.object(type(client.transport), "__enter__") as enter: + with mock.patch.object(type(client.transport), "__exit__") as exit: + enter.assert_not_called() + exit.assert_not_called() + with client: + enter.assert_called_once() + exit.assert_called() diff --git a/tests/integration/goldens/redis/tests/unit/gapic/redis_v1/test_cloud_redis.py b/tests/integration/goldens/redis/tests/unit/gapic/redis_v1/test_cloud_redis.py index 0306788504..c574110d8c 100644 --- a/tests/integration/goldens/redis/tests/unit/gapic/redis_v1/test_cloud_redis.py +++ b/tests/integration/goldens/redis/tests/unit/gapic/redis_v1/test_cloud_redis.py @@ -2861,6 +2861,7 @@ def test_cloud_redis_base_transport(): with pytest.raises(NotImplementedError): transport.__enter__() + with pytest.raises(NotImplementedError): transport.__exit__(None, None, None) @@ -3351,26 +3352,34 @@ def test_client_withDEFAULT_CLIENT_INFO(): prep.assert_called_once_with(client_info) -def test_grpc_transport_enter_exit(): - client = CloudRedisClient( - credentials=ga_credentials.AnonymousCredentials(), - transport='grpc', - ) - with mock.patch.object(type(client.transport._grpc_channel), 'close') as chan_close: - with client as _: - chan_close.assert_not_called() - chan_close.assert_called_once() +def test_transport_ctx(): + transports = [ + 'grpc', + ] + for transport in transports: + client = CloudRedisClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport + ) + with mock.patch.object(type(client.transport._grpc_channel), 'close') as close: + with client: + close.assert_not_called() + close.assert_called_once() -def test_grpc_client_enter_exit(): - client = CloudRedisClient( - credentials=ga_credentials.AnonymousCredentials(), - transport='grpc', - ) - # Test client calls underlying transport. - with mock.patch.object(type(client.transport), "__enter__") as enter: - with mock.patch.object(type(client.transport), "__exit__") as exit: - enter.assert_not_called() - exit.assert_not_called() - with client as _: - enter.assert_called_once() - exit.assert_called() +def test_client_ctx(): + transports = [ + 'grpc', + ] + for transport in transports: + client = CloudRedisClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport + ) + # Test client calls underlying transport. + with mock.patch.object(type(client.transport), "__enter__") as enter: + with mock.patch.object(type(client.transport), "__exit__") as exit: + enter.assert_not_called() + exit.assert_not_called() + with client: + enter.assert_called_once() + exit.assert_called() From 77d92c7009e8bac40419e558d998a3de54ad8565 Mon Sep 17 00:00:00 2001 From: Aza Tulepbergenov Date: Wed, 15 Sep 2021 13:34:26 -0700 Subject: [PATCH 09/14] chore: refactor. --- .../%name_%version/%sub/test_%service.py.j2 | 18 +++++++----------- .../unit/gapic/asset_v1/test_asset_service.py | 11 ++++++----- .../credentials_v1/test_iam_credentials.py | 11 ++++++----- .../gapic/logging_v2/test_config_service_v2.py | 11 ++++++----- .../logging_v2/test_logging_service_v2.py | 11 ++++++----- .../logging_v2/test_metrics_service_v2.py | 11 ++++++----- .../unit/gapic/redis_v1/test_cloud_redis.py | 11 ++++++----- 7 files changed, 43 insertions(+), 41 deletions(-) diff --git a/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 b/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 index 3aa657d5f6..2db9b90447 100644 --- a/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 +++ b/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 @@ -2466,25 +2466,21 @@ async def test_test_iam_permissions_from_dict_async(): {% endif %} def test_transport_ctx(): - transports = [ + transports = { {% if 'rest' in opts.transport %} - 'rest', + "rest": "_session", {% endif %} {% if 'grpc' in opts.transport %} - 'grpc', + "grpc": "_grpc_channel", {% endif %} - ] - for transport in transports: + } + + for transport, close_name in transports.items(): client = {{ service.client_name }}( credentials=ga_credentials.AnonymousCredentials(), transport=transport ) - {% if 'rest' in opts.transport %} - with mock.patch.object(type(client.transport._session), 'close') as close: - {% endif %} - {% if 'grpc' in opts.transport %} - with mock.patch.object(type(client.transport._grpc_channel), 'close') as close: - {% endif %} + with mock.patch.object(type(getattr(client.transport, close_name)), "close") as close: with client: close.assert_not_called() close.assert_called_once() diff --git a/tests/integration/goldens/asset/tests/unit/gapic/asset_v1/test_asset_service.py b/tests/integration/goldens/asset/tests/unit/gapic/asset_v1/test_asset_service.py index e38b1b38a1..7f75e21f35 100644 --- a/tests/integration/goldens/asset/tests/unit/gapic/asset_v1/test_asset_service.py +++ b/tests/integration/goldens/asset/tests/unit/gapic/asset_v1/test_asset_service.py @@ -4057,15 +4057,16 @@ def test_client_withDEFAULT_CLIENT_INFO(): def test_transport_ctx(): - transports = [ - 'grpc', - ] - for transport in transports: + transports = { + "grpc": "_grpc_channel", + } + + for transport, close_name in transports.items(): client = AssetServiceClient( credentials=ga_credentials.AnonymousCredentials(), transport=transport ) - with mock.patch.object(type(client.transport._grpc_channel), 'close') as close: + with mock.patch.object(type(getattr(client.transport, close_name)), "close") as close: with client: close.assert_not_called() close.assert_called_once() diff --git a/tests/integration/goldens/credentials/tests/unit/gapic/credentials_v1/test_iam_credentials.py b/tests/integration/goldens/credentials/tests/unit/gapic/credentials_v1/test_iam_credentials.py index 6dc4d8fbb9..6852469fc0 100644 --- a/tests/integration/goldens/credentials/tests/unit/gapic/credentials_v1/test_iam_credentials.py +++ b/tests/integration/goldens/credentials/tests/unit/gapic/credentials_v1/test_iam_credentials.py @@ -1937,15 +1937,16 @@ def test_client_withDEFAULT_CLIENT_INFO(): def test_transport_ctx(): - transports = [ - 'grpc', - ] - for transport in transports: + transports = { + "grpc": "_grpc_channel", + } + + for transport, close_name in transports.items(): client = IAMCredentialsClient( credentials=ga_credentials.AnonymousCredentials(), transport=transport ) - with mock.patch.object(type(client.transport._grpc_channel), 'close') as close: + with mock.patch.object(type(getattr(client.transport, close_name)), "close") as close: with client: close.assert_not_called() close.assert_called_once() diff --git a/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_config_service_v2.py b/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_config_service_v2.py index 697f4d3583..1f6b5d3965 100644 --- a/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_config_service_v2.py +++ b/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_config_service_v2.py @@ -6454,15 +6454,16 @@ def test_client_withDEFAULT_CLIENT_INFO(): def test_transport_ctx(): - transports = [ - 'grpc', - ] - for transport in transports: + transports = { + "grpc": "_grpc_channel", + } + + for transport, close_name in transports.items(): client = ConfigServiceV2Client( credentials=ga_credentials.AnonymousCredentials(), transport=transport ) - with mock.patch.object(type(client.transport._grpc_channel), 'close') as close: + with mock.patch.object(type(getattr(client.transport, close_name)), "close") as close: with client: close.assert_not_called() close.assert_called_once() diff --git a/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_logging_service_v2.py b/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_logging_service_v2.py index cf042657c1..023e681c4d 100644 --- a/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_logging_service_v2.py +++ b/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_logging_service_v2.py @@ -2501,15 +2501,16 @@ def test_client_withDEFAULT_CLIENT_INFO(): def test_transport_ctx(): - transports = [ - 'grpc', - ] - for transport in transports: + transports = { + "grpc": "_grpc_channel", + } + + for transport, close_name in transports.items(): client = LoggingServiceV2Client( credentials=ga_credentials.AnonymousCredentials(), transport=transport ) - with mock.patch.object(type(client.transport._grpc_channel), 'close') as close: + with mock.patch.object(type(getattr(client.transport, close_name)), "close") as close: with client: close.assert_not_called() close.assert_called_once() diff --git a/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_metrics_service_v2.py b/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_metrics_service_v2.py index b71114b296..9eea60e036 100644 --- a/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_metrics_service_v2.py +++ b/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_metrics_service_v2.py @@ -2366,15 +2366,16 @@ def test_client_withDEFAULT_CLIENT_INFO(): def test_transport_ctx(): - transports = [ - 'grpc', - ] - for transport in transports: + transports = { + "grpc": "_grpc_channel", + } + + for transport, close_name in transports.items(): client = MetricsServiceV2Client( credentials=ga_credentials.AnonymousCredentials(), transport=transport ) - with mock.patch.object(type(client.transport._grpc_channel), 'close') as close: + with mock.patch.object(type(getattr(client.transport, close_name)), "close") as close: with client: close.assert_not_called() close.assert_called_once() diff --git a/tests/integration/goldens/redis/tests/unit/gapic/redis_v1/test_cloud_redis.py b/tests/integration/goldens/redis/tests/unit/gapic/redis_v1/test_cloud_redis.py index c574110d8c..be62e95bf8 100644 --- a/tests/integration/goldens/redis/tests/unit/gapic/redis_v1/test_cloud_redis.py +++ b/tests/integration/goldens/redis/tests/unit/gapic/redis_v1/test_cloud_redis.py @@ -3353,15 +3353,16 @@ def test_client_withDEFAULT_CLIENT_INFO(): def test_transport_ctx(): - transports = [ - 'grpc', - ] - for transport in transports: + transports = { + "grpc": "_grpc_channel", + } + + for transport, close_name in transports.items(): client = CloudRedisClient( credentials=ga_credentials.AnonymousCredentials(), transport=transport ) - with mock.patch.object(type(client.transport._grpc_channel), 'close') as close: + with mock.patch.object(type(getattr(client.transport, close_name)), "close") as close: with client: close.assert_not_called() close.assert_called_once() From 4707af5cd275bdd7a31b687e974a76eb1b6c1a5c Mon Sep 17 00:00:00 2001 From: Aza Tulepbergenov Date: Thu, 23 Sep 2021 11:15:19 -0700 Subject: [PATCH 10/14] feat: adds close() to transport and ctx to async client. --- .../%sub/services/%service/async_client.py.j2 | 6 ++++ .../%sub/services/%service/client.py.j2 | 3 +- .../services/%service/transports/base.py.j2 | 8 +++-- .../services/%service/transports/grpc.py.j2 | 11 ++----- .../%service/transports/grpc_asyncio.py.j2 | 4 +++ .../services/%service/transports/rest.py.j2 | 11 ++----- .../%name_%version/%sub/test_%service.py.j2 | 30 +++++++++++-------- .../services/asset_service/async_client.py | 6 ++-- .../asset_v1/services/asset_service/client.py | 3 +- .../services/asset_service/transports/base.py | 8 +++-- .../services/asset_service/transports/grpc.py | 10 ++----- .../asset_service/transports/grpc_asyncio.py | 3 ++ .../unit/gapic/asset_v1/test_asset_service.py | 30 +++++++++++-------- .../services/iam_credentials/async_client.py | 6 ++-- .../services/iam_credentials/client.py | 3 +- .../iam_credentials/transports/base.py | 8 +++-- .../iam_credentials/transports/grpc.py | 10 ++----- .../transports/grpc_asyncio.py | 3 ++ .../credentials_v1/test_iam_credentials.py | 30 +++++++++++-------- .../config_service_v2/async_client.py | 6 ++-- .../services/config_service_v2/client.py | 3 +- .../config_service_v2/transports/base.py | 8 +++-- .../config_service_v2/transports/grpc.py | 10 ++----- .../transports/grpc_asyncio.py | 3 ++ .../logging_service_v2/async_client.py | 6 ++-- .../services/logging_service_v2/client.py | 3 +- .../logging_service_v2/transports/base.py | 8 +++-- .../logging_service_v2/transports/grpc.py | 10 ++----- .../transports/grpc_asyncio.py | 3 ++ .../metrics_service_v2/async_client.py | 6 ++-- .../services/metrics_service_v2/client.py | 3 +- .../metrics_service_v2/transports/base.py | 8 +++-- .../metrics_service_v2/transports/grpc.py | 10 ++----- .../transports/grpc_asyncio.py | 3 ++ .../logging_v2/test_config_service_v2.py | 30 +++++++++++-------- .../logging_v2/test_logging_service_v2.py | 30 +++++++++++-------- .../logging_v2/test_metrics_service_v2.py | 30 +++++++++++-------- .../services/cloud_redis/async_client.py | 6 ++-- .../redis_v1/services/cloud_redis/client.py | 3 +- .../services/cloud_redis/transports/base.py | 8 +++-- .../services/cloud_redis/transports/grpc.py | 10 ++----- .../cloud_redis/transports/grpc_asyncio.py | 3 ++ .../unit/gapic/redis_v1/test_cloud_redis.py | 30 +++++++++++-------- tests/system/test_client_context_manager.py | 23 +++++++++++++- 44 files changed, 260 insertions(+), 196 deletions(-) diff --git a/gapic/templates/%namespace/%name_%version/%sub/services/%service/async_client.py.j2 b/gapic/templates/%namespace/%name_%version/%sub/services/%service/async_client.py.j2 index ea40ea1c25..3df81e9b05 100644 --- a/gapic/templates/%namespace/%name_%version/%sub/services/%service/async_client.py.j2 +++ b/gapic/templates/%namespace/%name_%version/%sub/services/%service/async_client.py.j2 @@ -599,6 +599,12 @@ class {{ service.async_client_name }}: return response {% endif %} + async def __aenter__(self): + return self + + async def __aexit__(self, exc_type, exc, tb): + await self.transport.close() + try: DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo( gapic_version=pkg_resources.get_distribution( diff --git a/gapic/templates/%namespace/%name_%version/%sub/services/%service/client.py.j2 b/gapic/templates/%namespace/%name_%version/%sub/services/%service/client.py.j2 index daf5e097d0..fff0deab63 100644 --- a/gapic/templates/%namespace/%name_%version/%sub/services/%service/client.py.j2 +++ b/gapic/templates/%namespace/%name_%version/%sub/services/%service/client.py.j2 @@ -481,7 +481,6 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta): {% endfor %} def __enter__(self): - self.transport.__enter__() return self def __exit__(self, type, value, traceback): @@ -491,7 +490,7 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta): with other clients! Exiting the with block will CLOSE the transport and may cause errors in other clients! """ - return self.transport.__exit__(type, value, traceback) + self.transport.close() {% if opts.add_iam_methods %} def set_iam_policy( diff --git a/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/base.py.j2 b/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/base.py.j2 index 3367214378..78224b9138 100644 --- a/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/base.py.j2 +++ b/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/base.py.j2 @@ -183,10 +183,12 @@ class {{ service.name }}Transport(abc.ABC): {% endfor %} {# precomputed wrappers loop #} } - def __enter__(self): - raise NotImplementedError() + def close(self): + """Closes resources associated with the transport. - def __exit__(self, type, value, traceback): + WARNING: Only call this method if the transport is NOT shared + with other clients - this may cause errors in other clients! + """ raise NotImplementedError() {% if service.has_lro %} diff --git a/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/grpc.py.j2 b/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/grpc.py.j2 index c3400fbd66..894f4470d5 100644 --- a/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/grpc.py.j2 +++ b/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/grpc.py.j2 @@ -225,14 +225,6 @@ class {{ service.name }}GrpcTransport({{ service.name }}Transport): """ return self._grpc_channel - def __enter__(self): - return self - - def __exit__(self, type, value, traceback): - """Closes underlying gRPC channel. - """ - self.grpc_channel.close() - {% if service.has_lro %} @property @@ -364,6 +356,9 @@ class {{ service.name }}GrpcTransport({{ service.name }}Transport): return self._stubs["test_iam_permissions"] {% endif %} + def close(self): + self.grpc_channel.close() + __all__ = ( '{{ service.name }}GrpcTransport', ) diff --git a/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/grpc_asyncio.py.j2 b/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/grpc_asyncio.py.j2 index 934a931aff..5c4f8d251f 100644 --- a/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/grpc_asyncio.py.j2 +++ b/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/grpc_asyncio.py.j2 @@ -359,6 +359,10 @@ class {{ service.grpc_asyncio_transport_name }}({{ service.name }}Transport): return self._stubs["test_iam_permissions"] {% endif %} + def close(self): + return self.grpc_channel.close() + + __all__ = ( '{{ service.name }}GrpcAsyncIOTransport', ) diff --git a/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/rest.py.j2 b/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/rest.py.j2 index 3f16d62535..c016055081 100644 --- a/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/rest.py.j2 +++ b/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/rest.py.j2 @@ -133,14 +133,6 @@ class {{ service.name }}RestTransport({{ service.name }}Transport): {% for method in service.methods.values() %} {% if method.http_opt %} - def __enter__(self): - return self - - def __exit__(self, type, value, traceback): - """Closes underlying HTTP session. - """ - self._session.close() - def {{ method.name|snake_case }}(self, request: {{ method.input.ident }}, *, retry: retries.Retry = gapic_v1.method.DEFAULT, @@ -244,6 +236,9 @@ class {{ service.name }}RestTransport({{ service.name }}Transport): {% endif %} {% endfor %} + def close(self): + self._session.close() + __all__ = ( '{{ service.name }}RestTransport', diff --git a/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 b/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 index 2db9b90447..ac76a3016e 100644 --- a/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 +++ b/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 @@ -1509,10 +1509,7 @@ def test_{{ service.name|snake_case }}_base_transport(): getattr(transport, method)(request=object()) with pytest.raises(NotImplementedError): - transport.__enter__() - - with pytest.raises(NotImplementedError): - transport.__exit__(None, None, None) + transport.close() {% if service.has_lro %} # Additionally, the LRO client (a property) should @@ -2465,7 +2462,18 @@ async def test_test_iam_permissions_from_dict_async(): {% endif %} -def test_transport_ctx(): +@pytest.mark.asyncio +async def test_transport_close_async(): + client = {{ service.async_client_name }}( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc_asyncio", + ) + with mock.patch.object(type(getattr(client.transport, "grpc_channel")), "close") as close: + async with client: + close.assert_not_called() + close.assert_called_once() + +def test_transport_close(): transports = { {% if 'rest' in opts.transport %} "rest": "_session", @@ -2500,12 +2508,10 @@ def test_client_ctx(): transport=transport ) # Test client calls underlying transport. - with mock.patch.object(type(client.transport), "__enter__") as enter: - with mock.patch.object(type(client.transport), "__exit__") as exit: - enter.assert_not_called() - exit.assert_not_called() - with client: - enter.assert_called_once() - exit.assert_called() + with mock.patch.object(type(client.transport), "close") as close: + close.assert_not_called() + with client: + pass + close.assert_called() {% endblock %} diff --git a/tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/async_client.py b/tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/async_client.py index 054bfe5565..a6327c26df 100644 --- a/tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/async_client.py +++ b/tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/async_client.py @@ -1292,9 +1292,11 @@ async def analyze_iam_policy_longrunning(self, # Done; return the response. return response + async def __aenter__(self): + return self - - + async def __aexit__(self, exc_type, exc, tb): + await self.transport.close() try: DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo( diff --git a/tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/client.py b/tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/client.py index 72ef1c0ff5..59cc6c9fa5 100644 --- a/tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/client.py +++ b/tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/client.py @@ -1449,7 +1449,6 @@ def analyze_iam_policy_longrunning(self, return response def __enter__(self): - self.transport.__enter__() return self def __exit__(self, type, value, traceback): @@ -1459,7 +1458,7 @@ def __exit__(self, type, value, traceback): with other clients! Exiting the with block will CLOSE the transport and may cause errors in other clients! """ - return self.transport.__exit__(type, value, traceback) + self.transport.close() diff --git a/tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/transports/base.py b/tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/transports/base.py index da943c3b2c..ff2a4a8f39 100644 --- a/tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/transports/base.py +++ b/tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/transports/base.py @@ -259,10 +259,12 @@ def _prep_wrapped_messages(self, client_info): ), } - def __enter__(self): - raise NotImplementedError() + def close(self): + """Closes resources associated with the transport. - def __exit__(self, type, value, traceback): + WARNING: Only call this method if the transport is NOT shared + with other clients - this may cause errors in other clients! + """ raise NotImplementedError() @property diff --git a/tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/transports/grpc.py b/tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/transports/grpc.py index 4aee2a74f8..7ed6de3dc2 100644 --- a/tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/transports/grpc.py +++ b/tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/transports/grpc.py @@ -224,14 +224,6 @@ def grpc_channel(self) -> grpc.Channel: """ return self._grpc_channel - def __enter__(self): - return self - - def __exit__(self, type, value, traceback): - """Closes underlying gRPC channel. - """ - self.grpc_channel.close() - @property def operations_client(self) -> operations_v1.OperationsClient: """Create the client designed to process long-running operations. @@ -600,6 +592,8 @@ def analyze_iam_policy_longrunning(self) -> Callable[ ) return self._stubs['analyze_iam_policy_longrunning'] + def close(self): + self.grpc_channel.close() __all__ = ( 'AssetServiceGrpcTransport', diff --git a/tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/transports/grpc_asyncio.py b/tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/transports/grpc_asyncio.py index 8559391cf0..cc021dfbb0 100644 --- a/tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/transports/grpc_asyncio.py +++ b/tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/transports/grpc_asyncio.py @@ -596,6 +596,9 @@ def analyze_iam_policy_longrunning(self) -> Callable[ ) return self._stubs['analyze_iam_policy_longrunning'] + def close(self): + return self.grpc_channel.close() + __all__ = ( 'AssetServiceGrpcAsyncIOTransport', diff --git a/tests/integration/goldens/asset/tests/unit/gapic/asset_v1/test_asset_service.py b/tests/integration/goldens/asset/tests/unit/gapic/asset_v1/test_asset_service.py index 7f75e21f35..2deac0b7ee 100644 --- a/tests/integration/goldens/asset/tests/unit/gapic/asset_v1/test_asset_service.py +++ b/tests/integration/goldens/asset/tests/unit/gapic/asset_v1/test_asset_service.py @@ -3551,10 +3551,7 @@ def test_asset_service_base_transport(): getattr(transport, method)(request=object()) with pytest.raises(NotImplementedError): - transport.__enter__() - - with pytest.raises(NotImplementedError): - transport.__exit__(None, None, None) + transport.close() # Additionally, the LRO client (a property) should # also raise NotImplementedError @@ -4056,7 +4053,18 @@ def test_client_withDEFAULT_CLIENT_INFO(): prep.assert_called_once_with(client_info) -def test_transport_ctx(): +@pytest.mark.asyncio +async def test_transport_close_async(): + client = AssetServiceAsyncClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc_asyncio", + ) + with mock.patch.object(type(getattr(client.transport, "grpc_channel")), "close") as close: + async with client: + close.assert_not_called() + close.assert_called_once() + +def test_transport_close(): transports = { "grpc": "_grpc_channel", } @@ -4081,10 +4089,8 @@ def test_client_ctx(): transport=transport ) # Test client calls underlying transport. - with mock.patch.object(type(client.transport), "__enter__") as enter: - with mock.patch.object(type(client.transport), "__exit__") as exit: - enter.assert_not_called() - exit.assert_not_called() - with client: - enter.assert_called_once() - exit.assert_called() + with mock.patch.object(type(client.transport), "close") as close: + close.assert_not_called() + with client: + pass + close.assert_called() diff --git a/tests/integration/goldens/credentials/google/iam/credentials_v1/services/iam_credentials/async_client.py b/tests/integration/goldens/credentials/google/iam/credentials_v1/services/iam_credentials/async_client.py index a6390ec743..c7ae75924a 100644 --- a/tests/integration/goldens/credentials/google/iam/credentials_v1/services/iam_credentials/async_client.py +++ b/tests/integration/goldens/credentials/google/iam/credentials_v1/services/iam_credentials/async_client.py @@ -644,9 +644,11 @@ async def sign_jwt(self, # Done; return the response. return response + async def __aenter__(self): + return self - - + async def __aexit__(self, exc_type, exc, tb): + await self.transport.close() try: DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo( diff --git a/tests/integration/goldens/credentials/google/iam/credentials_v1/services/iam_credentials/client.py b/tests/integration/goldens/credentials/google/iam/credentials_v1/services/iam_credentials/client.py index 06a6d06f8e..720f7f7837 100644 --- a/tests/integration/goldens/credentials/google/iam/credentials_v1/services/iam_credentials/client.py +++ b/tests/integration/goldens/credentials/google/iam/credentials_v1/services/iam_credentials/client.py @@ -808,7 +808,6 @@ def sign_jwt(self, return response def __enter__(self): - self.transport.__enter__() return self def __exit__(self, type, value, traceback): @@ -818,7 +817,7 @@ def __exit__(self, type, value, traceback): with other clients! Exiting the with block will CLOSE the transport and may cause errors in other clients! """ - return self.transport.__exit__(type, value, traceback) + self.transport.close() diff --git a/tests/integration/goldens/credentials/google/iam/credentials_v1/services/iam_credentials/transports/base.py b/tests/integration/goldens/credentials/google/iam/credentials_v1/services/iam_credentials/transports/base.py index 07e73fb4a5..684994103c 100644 --- a/tests/integration/goldens/credentials/google/iam/credentials_v1/services/iam_credentials/transports/base.py +++ b/tests/integration/goldens/credentials/google/iam/credentials_v1/services/iam_credentials/transports/base.py @@ -196,10 +196,12 @@ def _prep_wrapped_messages(self, client_info): ), } - def __enter__(self): - raise NotImplementedError() + def close(self): + """Closes resources associated with the transport. - def __exit__(self, type, value, traceback): + WARNING: Only call this method if the transport is NOT shared + with other clients - this may cause errors in other clients! + """ raise NotImplementedError() @property diff --git a/tests/integration/goldens/credentials/google/iam/credentials_v1/services/iam_credentials/transports/grpc.py b/tests/integration/goldens/credentials/google/iam/credentials_v1/services/iam_credentials/transports/grpc.py index 4109a7a99f..4c5159d800 100644 --- a/tests/integration/goldens/credentials/google/iam/credentials_v1/services/iam_credentials/transports/grpc.py +++ b/tests/integration/goldens/credentials/google/iam/credentials_v1/services/iam_credentials/transports/grpc.py @@ -229,14 +229,6 @@ def grpc_channel(self) -> grpc.Channel: """ return self._grpc_channel - def __enter__(self): - return self - - def __exit__(self, type, value, traceback): - """Closes underlying gRPC channel. - """ - self.grpc_channel.close() - @property def generate_access_token(self) -> Callable[ [common.GenerateAccessTokenRequest], @@ -345,6 +337,8 @@ def sign_jwt(self) -> Callable[ ) return self._stubs['sign_jwt'] + def close(self): + self.grpc_channel.close() __all__ = ( 'IAMCredentialsGrpcTransport', diff --git a/tests/integration/goldens/credentials/google/iam/credentials_v1/services/iam_credentials/transports/grpc_asyncio.py b/tests/integration/goldens/credentials/google/iam/credentials_v1/services/iam_credentials/transports/grpc_asyncio.py index c944ba08b8..cebb4866b0 100644 --- a/tests/integration/goldens/credentials/google/iam/credentials_v1/services/iam_credentials/transports/grpc_asyncio.py +++ b/tests/integration/goldens/credentials/google/iam/credentials_v1/services/iam_credentials/transports/grpc_asyncio.py @@ -341,6 +341,9 @@ def sign_jwt(self) -> Callable[ ) return self._stubs['sign_jwt'] + def close(self): + return self.grpc_channel.close() + __all__ = ( 'IAMCredentialsGrpcAsyncIOTransport', diff --git a/tests/integration/goldens/credentials/tests/unit/gapic/credentials_v1/test_iam_credentials.py b/tests/integration/goldens/credentials/tests/unit/gapic/credentials_v1/test_iam_credentials.py index 6852469fc0..ad148dddd7 100644 --- a/tests/integration/goldens/credentials/tests/unit/gapic/credentials_v1/test_iam_credentials.py +++ b/tests/integration/goldens/credentials/tests/unit/gapic/credentials_v1/test_iam_credentials.py @@ -1485,10 +1485,7 @@ def test_iam_credentials_base_transport(): getattr(transport, method)(request=object()) with pytest.raises(NotImplementedError): - transport.__enter__() - - with pytest.raises(NotImplementedError): - transport.__exit__(None, None, None) + transport.close() @requires_google_auth_gte_1_25_0 @@ -1936,7 +1933,18 @@ def test_client_withDEFAULT_CLIENT_INFO(): prep.assert_called_once_with(client_info) -def test_transport_ctx(): +@pytest.mark.asyncio +async def test_transport_close_async(): + client = IAMCredentialsAsyncClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc_asyncio", + ) + with mock.patch.object(type(getattr(client.transport, "grpc_channel")), "close") as close: + async with client: + close.assert_not_called() + close.assert_called_once() + +def test_transport_close(): transports = { "grpc": "_grpc_channel", } @@ -1961,10 +1969,8 @@ def test_client_ctx(): transport=transport ) # Test client calls underlying transport. - with mock.patch.object(type(client.transport), "__enter__") as enter: - with mock.patch.object(type(client.transport), "__exit__") as exit: - enter.assert_not_called() - exit.assert_not_called() - with client: - enter.assert_called_once() - exit.assert_called() + with mock.patch.object(type(client.transport), "close") as close: + close.assert_not_called() + with client: + pass + close.assert_called() diff --git a/tests/integration/goldens/logging/google/cloud/logging_v2/services/config_service_v2/async_client.py b/tests/integration/goldens/logging/google/cloud/logging_v2/services/config_service_v2/async_client.py index 82e84aab81..5d322cb502 100644 --- a/tests/integration/goldens/logging/google/cloud/logging_v2/services/config_service_v2/async_client.py +++ b/tests/integration/goldens/logging/google/cloud/logging_v2/services/config_service_v2/async_client.py @@ -1997,9 +1997,11 @@ async def update_cmek_settings(self, # Done; return the response. return response + async def __aenter__(self): + return self - - + async def __aexit__(self, exc_type, exc, tb): + await self.transport.close() try: DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo( diff --git a/tests/integration/goldens/logging/google/cloud/logging_v2/services/config_service_v2/client.py b/tests/integration/goldens/logging/google/cloud/logging_v2/services/config_service_v2/client.py index c51ca1f3b8..7db9f31861 100644 --- a/tests/integration/goldens/logging/google/cloud/logging_v2/services/config_service_v2/client.py +++ b/tests/integration/goldens/logging/google/cloud/logging_v2/services/config_service_v2/client.py @@ -2180,7 +2180,6 @@ def update_cmek_settings(self, return response def __enter__(self): - self.transport.__enter__() return self def __exit__(self, type, value, traceback): @@ -2190,7 +2189,7 @@ def __exit__(self, type, value, traceback): with other clients! Exiting the with block will CLOSE the transport and may cause errors in other clients! """ - return self.transport.__exit__(type, value, traceback) + self.transport.close() diff --git a/tests/integration/goldens/logging/google/cloud/logging_v2/services/config_service_v2/transports/base.py b/tests/integration/goldens/logging/google/cloud/logging_v2/services/config_service_v2/transports/base.py index d7e265cd85..25e1a25ceb 100644 --- a/tests/integration/goldens/logging/google/cloud/logging_v2/services/config_service_v2/transports/base.py +++ b/tests/integration/goldens/logging/google/cloud/logging_v2/services/config_service_v2/transports/base.py @@ -323,10 +323,12 @@ def _prep_wrapped_messages(self, client_info): ), } - def __enter__(self): - raise NotImplementedError() + def close(self): + """Closes resources associated with the transport. - def __exit__(self, type, value, traceback): + WARNING: Only call this method if the transport is NOT shared + with other clients - this may cause errors in other clients! + """ raise NotImplementedError() @property diff --git a/tests/integration/goldens/logging/google/cloud/logging_v2/services/config_service_v2/transports/grpc.py b/tests/integration/goldens/logging/google/cloud/logging_v2/services/config_service_v2/transports/grpc.py index 777cb5b841..3a640eaf0b 100644 --- a/tests/integration/goldens/logging/google/cloud/logging_v2/services/config_service_v2/transports/grpc.py +++ b/tests/integration/goldens/logging/google/cloud/logging_v2/services/config_service_v2/transports/grpc.py @@ -221,14 +221,6 @@ def grpc_channel(self) -> grpc.Channel: """ return self._grpc_channel - def __enter__(self): - return self - - def __exit__(self, type, value, traceback): - """Closes underlying gRPC channel. - """ - self.grpc_channel.close() - @property def list_buckets(self) -> Callable[ [logging_config.ListBucketsRequest], @@ -880,6 +872,8 @@ def update_cmek_settings(self) -> Callable[ ) return self._stubs['update_cmek_settings'] + def close(self): + self.grpc_channel.close() __all__ = ( 'ConfigServiceV2GrpcTransport', diff --git a/tests/integration/goldens/logging/google/cloud/logging_v2/services/config_service_v2/transports/grpc_asyncio.py b/tests/integration/goldens/logging/google/cloud/logging_v2/services/config_service_v2/transports/grpc_asyncio.py index 78e442d8a4..2f18d861c1 100644 --- a/tests/integration/goldens/logging/google/cloud/logging_v2/services/config_service_v2/transports/grpc_asyncio.py +++ b/tests/integration/goldens/logging/google/cloud/logging_v2/services/config_service_v2/transports/grpc_asyncio.py @@ -876,6 +876,9 @@ def update_cmek_settings(self) -> Callable[ ) return self._stubs['update_cmek_settings'] + def close(self): + return self.grpc_channel.close() + __all__ = ( 'ConfigServiceV2GrpcAsyncIOTransport', diff --git a/tests/integration/goldens/logging/google/cloud/logging_v2/services/logging_service_v2/async_client.py b/tests/integration/goldens/logging/google/cloud/logging_v2/services/logging_service_v2/async_client.py index dd9cbb78dd..f03a8d0c04 100644 --- a/tests/integration/goldens/logging/google/cloud/logging_v2/services/logging_service_v2/async_client.py +++ b/tests/integration/goldens/logging/google/cloud/logging_v2/services/logging_service_v2/async_client.py @@ -762,9 +762,11 @@ def tail_log_entries(self, # Done; return the response. return response + async def __aenter__(self): + return self - - + async def __aexit__(self, exc_type, exc, tb): + await self.transport.close() try: DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo( diff --git a/tests/integration/goldens/logging/google/cloud/logging_v2/services/logging_service_v2/client.py b/tests/integration/goldens/logging/google/cloud/logging_v2/services/logging_service_v2/client.py index b3a0aee9e0..b390f0040d 100644 --- a/tests/integration/goldens/logging/google/cloud/logging_v2/services/logging_service_v2/client.py +++ b/tests/integration/goldens/logging/google/cloud/logging_v2/services/logging_service_v2/client.py @@ -902,7 +902,6 @@ def tail_log_entries(self, return response def __enter__(self): - self.transport.__enter__() return self def __exit__(self, type, value, traceback): @@ -912,7 +911,7 @@ def __exit__(self, type, value, traceback): with other clients! Exiting the with block will CLOSE the transport and may cause errors in other clients! """ - return self.transport.__exit__(type, value, traceback) + self.transport.close() diff --git a/tests/integration/goldens/logging/google/cloud/logging_v2/services/logging_service_v2/transports/base.py b/tests/integration/goldens/logging/google/cloud/logging_v2/services/logging_service_v2/transports/base.py index f826aa2956..2d46a4bad2 100644 --- a/tests/integration/goldens/logging/google/cloud/logging_v2/services/logging_service_v2/transports/base.py +++ b/tests/integration/goldens/logging/google/cloud/logging_v2/services/logging_service_v2/transports/base.py @@ -231,10 +231,12 @@ def _prep_wrapped_messages(self, client_info): ), } - def __enter__(self): - raise NotImplementedError() + def close(self): + """Closes resources associated with the transport. - def __exit__(self, type, value, traceback): + WARNING: Only call this method if the transport is NOT shared + with other clients - this may cause errors in other clients! + """ raise NotImplementedError() @property diff --git a/tests/integration/goldens/logging/google/cloud/logging_v2/services/logging_service_v2/transports/grpc.py b/tests/integration/goldens/logging/google/cloud/logging_v2/services/logging_service_v2/transports/grpc.py index d03fd97c2c..4ec732a268 100644 --- a/tests/integration/goldens/logging/google/cloud/logging_v2/services/logging_service_v2/transports/grpc.py +++ b/tests/integration/goldens/logging/google/cloud/logging_v2/services/logging_service_v2/transports/grpc.py @@ -221,14 +221,6 @@ def grpc_channel(self) -> grpc.Channel: """ return self._grpc_channel - def __enter__(self): - return self - - def __exit__(self, type, value, traceback): - """Closes underlying gRPC channel. - """ - self.grpc_channel.close() - @property def delete_log(self) -> Callable[ [logging.DeleteLogRequest], @@ -404,6 +396,8 @@ def tail_log_entries(self) -> Callable[ ) return self._stubs['tail_log_entries'] + def close(self): + self.grpc_channel.close() __all__ = ( 'LoggingServiceV2GrpcTransport', diff --git a/tests/integration/goldens/logging/google/cloud/logging_v2/services/logging_service_v2/transports/grpc_asyncio.py b/tests/integration/goldens/logging/google/cloud/logging_v2/services/logging_service_v2/transports/grpc_asyncio.py index a19007ab65..9978fe57f7 100644 --- a/tests/integration/goldens/logging/google/cloud/logging_v2/services/logging_service_v2/transports/grpc_asyncio.py +++ b/tests/integration/goldens/logging/google/cloud/logging_v2/services/logging_service_v2/transports/grpc_asyncio.py @@ -400,6 +400,9 @@ def tail_log_entries(self) -> Callable[ ) return self._stubs['tail_log_entries'] + def close(self): + return self.grpc_channel.close() + __all__ = ( 'LoggingServiceV2GrpcAsyncIOTransport', diff --git a/tests/integration/goldens/logging/google/cloud/logging_v2/services/metrics_service_v2/async_client.py b/tests/integration/goldens/logging/google/cloud/logging_v2/services/metrics_service_v2/async_client.py index 764f44f666..d3b11a198e 100644 --- a/tests/integration/goldens/logging/google/cloud/logging_v2/services/metrics_service_v2/async_client.py +++ b/tests/integration/goldens/logging/google/cloud/logging_v2/services/metrics_service_v2/async_client.py @@ -621,9 +621,11 @@ async def delete_log_metric(self, metadata=metadata, ) + async def __aenter__(self): + return self - - + async def __aexit__(self, exc_type, exc, tb): + await self.transport.close() try: DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo( diff --git a/tests/integration/goldens/logging/google/cloud/logging_v2/services/metrics_service_v2/client.py b/tests/integration/goldens/logging/google/cloud/logging_v2/services/metrics_service_v2/client.py index cdcfd92b2e..2b76672f5b 100644 --- a/tests/integration/goldens/logging/google/cloud/logging_v2/services/metrics_service_v2/client.py +++ b/tests/integration/goldens/logging/google/cloud/logging_v2/services/metrics_service_v2/client.py @@ -781,7 +781,6 @@ def delete_log_metric(self, ) def __enter__(self): - self.transport.__enter__() return self def __exit__(self, type, value, traceback): @@ -791,7 +790,7 @@ def __exit__(self, type, value, traceback): with other clients! Exiting the with block will CLOSE the transport and may cause errors in other clients! """ - return self.transport.__exit__(type, value, traceback) + self.transport.close() diff --git a/tests/integration/goldens/logging/google/cloud/logging_v2/services/metrics_service_v2/transports/base.py b/tests/integration/goldens/logging/google/cloud/logging_v2/services/metrics_service_v2/transports/base.py index 5969e03201..509aef079e 100644 --- a/tests/integration/goldens/logging/google/cloud/logging_v2/services/metrics_service_v2/transports/base.py +++ b/tests/integration/goldens/logging/google/cloud/logging_v2/services/metrics_service_v2/transports/base.py @@ -210,10 +210,12 @@ def _prep_wrapped_messages(self, client_info): ), } - def __enter__(self): - raise NotImplementedError() + def close(self): + """Closes resources associated with the transport. - def __exit__(self, type, value, traceback): + WARNING: Only call this method if the transport is NOT shared + with other clients - this may cause errors in other clients! + """ raise NotImplementedError() @property diff --git a/tests/integration/goldens/logging/google/cloud/logging_v2/services/metrics_service_v2/transports/grpc.py b/tests/integration/goldens/logging/google/cloud/logging_v2/services/metrics_service_v2/transports/grpc.py index 7a7df33b10..8aced9e6d3 100644 --- a/tests/integration/goldens/logging/google/cloud/logging_v2/services/metrics_service_v2/transports/grpc.py +++ b/tests/integration/goldens/logging/google/cloud/logging_v2/services/metrics_service_v2/transports/grpc.py @@ -221,14 +221,6 @@ def grpc_channel(self) -> grpc.Channel: """ return self._grpc_channel - def __enter__(self): - return self - - def __exit__(self, type, value, traceback): - """Closes underlying gRPC channel. - """ - self.grpc_channel.close() - @property def list_log_metrics(self) -> Callable[ [logging_metrics.ListLogMetricsRequest], @@ -359,6 +351,8 @@ def delete_log_metric(self) -> Callable[ ) return self._stubs['delete_log_metric'] + def close(self): + self.grpc_channel.close() __all__ = ( 'MetricsServiceV2GrpcTransport', diff --git a/tests/integration/goldens/logging/google/cloud/logging_v2/services/metrics_service_v2/transports/grpc_asyncio.py b/tests/integration/goldens/logging/google/cloud/logging_v2/services/metrics_service_v2/transports/grpc_asyncio.py index 7da832822e..9c07c1172b 100644 --- a/tests/integration/goldens/logging/google/cloud/logging_v2/services/metrics_service_v2/transports/grpc_asyncio.py +++ b/tests/integration/goldens/logging/google/cloud/logging_v2/services/metrics_service_v2/transports/grpc_asyncio.py @@ -355,6 +355,9 @@ def delete_log_metric(self) -> Callable[ ) return self._stubs['delete_log_metric'] + def close(self): + return self.grpc_channel.close() + __all__ = ( 'MetricsServiceV2GrpcAsyncIOTransport', diff --git a/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_config_service_v2.py b/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_config_service_v2.py index 1f6b5d3965..f675138677 100644 --- a/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_config_service_v2.py +++ b/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_config_service_v2.py @@ -5907,10 +5907,7 @@ def test_config_service_v2_base_transport(): getattr(transport, method)(request=object()) with pytest.raises(NotImplementedError): - transport.__enter__() - - with pytest.raises(NotImplementedError): - transport.__exit__(None, None, None) + transport.close() @requires_google_auth_gte_1_25_0 @@ -6453,7 +6450,18 @@ def test_client_withDEFAULT_CLIENT_INFO(): prep.assert_called_once_with(client_info) -def test_transport_ctx(): +@pytest.mark.asyncio +async def test_transport_close_async(): + client = ConfigServiceV2AsyncClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc_asyncio", + ) + with mock.patch.object(type(getattr(client.transport, "grpc_channel")), "close") as close: + async with client: + close.assert_not_called() + close.assert_called_once() + +def test_transport_close(): transports = { "grpc": "_grpc_channel", } @@ -6478,10 +6486,8 @@ def test_client_ctx(): transport=transport ) # Test client calls underlying transport. - with mock.patch.object(type(client.transport), "__enter__") as enter: - with mock.patch.object(type(client.transport), "__exit__") as exit: - enter.assert_not_called() - exit.assert_not_called() - with client: - enter.assert_called_once() - exit.assert_called() + with mock.patch.object(type(client.transport), "close") as close: + close.assert_not_called() + with client: + pass + close.assert_called() diff --git a/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_logging_service_v2.py b/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_logging_service_v2.py index 023e681c4d..74437172a1 100644 --- a/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_logging_service_v2.py +++ b/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_logging_service_v2.py @@ -2029,10 +2029,7 @@ def test_logging_service_v2_base_transport(): getattr(transport, method)(request=object()) with pytest.raises(NotImplementedError): - transport.__enter__() - - with pytest.raises(NotImplementedError): - transport.__exit__(None, None, None) + transport.close() @requires_google_auth_gte_1_25_0 @@ -2500,7 +2497,18 @@ def test_client_withDEFAULT_CLIENT_INFO(): prep.assert_called_once_with(client_info) -def test_transport_ctx(): +@pytest.mark.asyncio +async def test_transport_close_async(): + client = LoggingServiceV2AsyncClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc_asyncio", + ) + with mock.patch.object(type(getattr(client.transport, "grpc_channel")), "close") as close: + async with client: + close.assert_not_called() + close.assert_called_once() + +def test_transport_close(): transports = { "grpc": "_grpc_channel", } @@ -2525,10 +2533,8 @@ def test_client_ctx(): transport=transport ) # Test client calls underlying transport. - with mock.patch.object(type(client.transport), "__enter__") as enter: - with mock.patch.object(type(client.transport), "__exit__") as exit: - enter.assert_not_called() - exit.assert_not_called() - with client: - enter.assert_called_once() - exit.assert_called() + with mock.patch.object(type(client.transport), "close") as close: + close.assert_not_called() + with client: + pass + close.assert_called() diff --git a/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_metrics_service_v2.py b/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_metrics_service_v2.py index 9eea60e036..28fa12beef 100644 --- a/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_metrics_service_v2.py +++ b/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_metrics_service_v2.py @@ -1894,10 +1894,7 @@ def test_metrics_service_v2_base_transport(): getattr(transport, method)(request=object()) with pytest.raises(NotImplementedError): - transport.__enter__() - - with pytest.raises(NotImplementedError): - transport.__exit__(None, None, None) + transport.close() @requires_google_auth_gte_1_25_0 @@ -2365,7 +2362,18 @@ def test_client_withDEFAULT_CLIENT_INFO(): prep.assert_called_once_with(client_info) -def test_transport_ctx(): +@pytest.mark.asyncio +async def test_transport_close_async(): + client = MetricsServiceV2AsyncClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc_asyncio", + ) + with mock.patch.object(type(getattr(client.transport, "grpc_channel")), "close") as close: + async with client: + close.assert_not_called() + close.assert_called_once() + +def test_transport_close(): transports = { "grpc": "_grpc_channel", } @@ -2390,10 +2398,8 @@ def test_client_ctx(): transport=transport ) # Test client calls underlying transport. - with mock.patch.object(type(client.transport), "__enter__") as enter: - with mock.patch.object(type(client.transport), "__exit__") as exit: - enter.assert_not_called() - exit.assert_not_called() - with client: - enter.assert_called_once() - exit.assert_called() + with mock.patch.object(type(client.transport), "close") as close: + close.assert_not_called() + with client: + pass + close.assert_called() diff --git a/tests/integration/goldens/redis/google/cloud/redis_v1/services/cloud_redis/async_client.py b/tests/integration/goldens/redis/google/cloud/redis_v1/services/cloud_redis/async_client.py index 512bab3f12..8d22164a88 100644 --- a/tests/integration/goldens/redis/google/cloud/redis_v1/services/cloud_redis/async_client.py +++ b/tests/integration/goldens/redis/google/cloud/redis_v1/services/cloud_redis/async_client.py @@ -1078,9 +1078,11 @@ async def delete_instance(self, # Done; return the response. return response + async def __aenter__(self): + return self - - + async def __aexit__(self, exc_type, exc, tb): + await self.transport.close() try: DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo( diff --git a/tests/integration/goldens/redis/google/cloud/redis_v1/services/cloud_redis/client.py b/tests/integration/goldens/redis/google/cloud/redis_v1/services/cloud_redis/client.py index 486629049c..99a49407e0 100644 --- a/tests/integration/goldens/redis/google/cloud/redis_v1/services/cloud_redis/client.py +++ b/tests/integration/goldens/redis/google/cloud/redis_v1/services/cloud_redis/client.py @@ -1270,7 +1270,6 @@ def delete_instance(self, return response def __enter__(self): - self.transport.__enter__() return self def __exit__(self, type, value, traceback): @@ -1280,7 +1279,7 @@ def __exit__(self, type, value, traceback): with other clients! Exiting the with block will CLOSE the transport and may cause errors in other clients! """ - return self.transport.__exit__(type, value, traceback) + self.transport.close() diff --git a/tests/integration/goldens/redis/google/cloud/redis_v1/services/cloud_redis/transports/base.py b/tests/integration/goldens/redis/google/cloud/redis_v1/services/cloud_redis/transports/base.py index 62c1c9350b..2262346d57 100644 --- a/tests/integration/goldens/redis/google/cloud/redis_v1/services/cloud_redis/transports/base.py +++ b/tests/integration/goldens/redis/google/cloud/redis_v1/services/cloud_redis/transports/base.py @@ -195,10 +195,12 @@ def _prep_wrapped_messages(self, client_info): ), } - def __enter__(self): - raise NotImplementedError() + def close(self): + """Closes resources associated with the transport. - def __exit__(self, type, value, traceback): + WARNING: Only call this method if the transport is NOT shared + with other clients - this may cause errors in other clients! + """ raise NotImplementedError() @property diff --git a/tests/integration/goldens/redis/google/cloud/redis_v1/services/cloud_redis/transports/grpc.py b/tests/integration/goldens/redis/google/cloud/redis_v1/services/cloud_redis/transports/grpc.py index d9810f97ed..82eeca6212 100644 --- a/tests/integration/goldens/redis/google/cloud/redis_v1/services/cloud_redis/transports/grpc.py +++ b/tests/integration/goldens/redis/google/cloud/redis_v1/services/cloud_redis/transports/grpc.py @@ -243,14 +243,6 @@ def grpc_channel(self) -> grpc.Channel: """ return self._grpc_channel - def __enter__(self): - return self - - def __exit__(self, type, value, traceback): - """Closes underlying gRPC channel. - """ - self.grpc_channel.close() - @property def operations_client(self) -> operations_v1.OperationsClient: """Create the client designed to process long-running operations. @@ -544,6 +536,8 @@ def delete_instance(self) -> Callable[ ) return self._stubs['delete_instance'] + def close(self): + self.grpc_channel.close() __all__ = ( 'CloudRedisGrpcTransport', diff --git a/tests/integration/goldens/redis/google/cloud/redis_v1/services/cloud_redis/transports/grpc_asyncio.py b/tests/integration/goldens/redis/google/cloud/redis_v1/services/cloud_redis/transports/grpc_asyncio.py index 2a2184de64..d89351dcb1 100644 --- a/tests/integration/goldens/redis/google/cloud/redis_v1/services/cloud_redis/transports/grpc_asyncio.py +++ b/tests/integration/goldens/redis/google/cloud/redis_v1/services/cloud_redis/transports/grpc_asyncio.py @@ -540,6 +540,9 @@ def delete_instance(self) -> Callable[ ) return self._stubs['delete_instance'] + def close(self): + return self.grpc_channel.close() + __all__ = ( 'CloudRedisGrpcAsyncIOTransport', diff --git a/tests/integration/goldens/redis/tests/unit/gapic/redis_v1/test_cloud_redis.py b/tests/integration/goldens/redis/tests/unit/gapic/redis_v1/test_cloud_redis.py index be62e95bf8..b0781f715a 100644 --- a/tests/integration/goldens/redis/tests/unit/gapic/redis_v1/test_cloud_redis.py +++ b/tests/integration/goldens/redis/tests/unit/gapic/redis_v1/test_cloud_redis.py @@ -2860,10 +2860,7 @@ def test_cloud_redis_base_transport(): getattr(transport, method)(request=object()) with pytest.raises(NotImplementedError): - transport.__enter__() - - with pytest.raises(NotImplementedError): - transport.__exit__(None, None, None) + transport.close() # Additionally, the LRO client (a property) should # also raise NotImplementedError @@ -3352,7 +3349,18 @@ def test_client_withDEFAULT_CLIENT_INFO(): prep.assert_called_once_with(client_info) -def test_transport_ctx(): +@pytest.mark.asyncio +async def test_transport_close_async(): + client = CloudRedisAsyncClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc_asyncio", + ) + with mock.patch.object(type(getattr(client.transport, "grpc_channel")), "close") as close: + async with client: + close.assert_not_called() + close.assert_called_once() + +def test_transport_close(): transports = { "grpc": "_grpc_channel", } @@ -3377,10 +3385,8 @@ def test_client_ctx(): transport=transport ) # Test client calls underlying transport. - with mock.patch.object(type(client.transport), "__enter__") as enter: - with mock.patch.object(type(client.transport), "__exit__") as exit: - enter.assert_not_called() - exit.assert_not_called() - with client: - enter.assert_called_once() - exit.assert_called() + with mock.patch.object(type(client.transport), "close") as close: + close.assert_not_called() + with client: + pass + close.assert_called() diff --git a/tests/system/test_client_context_manager.py b/tests/system/test_client_context_manager.py index 6786b2dd43..88ced947ed 100644 --- a/tests/system/test_client_context_manager.py +++ b/tests/system/test_client_context_manager.py @@ -12,8 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. - +import os import pytest +import grpc +import distutils def test_client(echo): @@ -30,3 +32,22 @@ def test_client_destroyed(echo): echo.echo({ 'content': 'hello' }) + + +if distutils.util.strtobool(os.environ.get("GAPIC_PYTHON_ASYNC", "true")): + + @pytest.mark.asyncio + async def test_client_async(async_echo): + async with async_echo: + response = await async_echo.echo({ + 'content': 'hello' + }) + assert response.content == 'hello' + + @pytest.mark.asyncio + async def test_client_destroyed_async(async_echo): + await async_echo.__aexit__(None, None, None) + with pytest.raises(grpc._cython.cygrpc.UsageError): + await async_echo.echo({ + 'content': 'hello' + }) From a136e61a5e1d67c65c58541ff63015a47f557791 Mon Sep 17 00:00:00 2001 From: Aza Tulepbergenov Date: Fri, 24 Sep 2021 12:02:15 -0700 Subject: [PATCH 11/14] feat: adds close method and removes ctx from transport in ads. --- .../%sub/services/%service/client.py.j2 | 3 +-- .../services/%service/transports/base.py.j2 | 8 +++++--- .../services/%service/transports/grpc.py.j2 | 7 +------ .../%name_%version/%sub/test_%service.py.j2 | 20 ++++++++----------- 4 files changed, 15 insertions(+), 23 deletions(-) diff --git a/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/client.py.j2 b/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/client.py.j2 index ad30dbd055..37a85891af 100644 --- a/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/client.py.j2 +++ b/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/client.py.j2 @@ -149,7 +149,6 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta): return self._transport def __enter__(self): - self.transport.__enter__() return self def __exit__(self, type, value, traceback): @@ -159,7 +158,7 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta): with other clients! Exiting the with block will CLOSE the transport and may cause errors in other clients! """ - return self.transport.__exit__(type, value, traceback) + self.transport.close() {% for message in service.resource_messages|sort(attribute="resource_type") %} @staticmethod diff --git a/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/transports/base.py.j2 b/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/transports/base.py.j2 index b5fb45c304..49015d3155 100644 --- a/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/transports/base.py.j2 +++ b/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/transports/base.py.j2 @@ -103,10 +103,12 @@ class {{ service.name }}Transport(metaclass=abc.ABCMeta): {% endfor %} {# precomputed wrappers loop #} } - def __enter__(self): - raise NotImplementedError() + def close(self): + """Closes resources associated with the transport. - def __exit__(self, type, value, traceback): + WARNING: Only call this method if the transport is NOT shared + with other clients - this may cause errors in other clients! + """ raise NotImplementedError() {% if service.has_lro %} diff --git a/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/transports/grpc.py.j2 b/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/transports/grpc.py.j2 index 512c7780c2..59a2219ca9 100644 --- a/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/transports/grpc.py.j2 +++ b/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/transports/grpc.py.j2 @@ -188,12 +188,7 @@ class {{ service.name }}GrpcTransport({{ service.name }}Transport): **kwargs ) - def __enter__(self): - return self - - def __exit__(self, type, value, traceback): - """Closes underlying gRPC channel. - """ + def close(self): self.grpc_channel.close() @property diff --git a/gapic/ads-templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 b/gapic/ads-templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 index da0f6de139..0392324b06 100644 --- a/gapic/ads-templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 +++ b/gapic/ads-templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 @@ -655,9 +655,7 @@ def test_{{ service.name|snake_case }}_base_transport(): getattr(transport, method)(request=object()) with pytest.raises(NotImplementedError): - transport.__enter__() - with pytest.raises(NotImplementedError): - transport.__exit__(None, None, None) + transport.close() {% if service.has_lro %} # Additionally, the LRO client (a property) should @@ -908,7 +906,7 @@ def test_client_withDEFAULT_CLIENT_INFO(): ) prep.assert_called_once_with(client_info) -def test_grpc_transport_enter_exit(): +def test_grpc_transport_close(): client = {{ service.client_name }}( credentials=ga_credentials.AnonymousCredentials(), transport='grpc', @@ -918,18 +916,16 @@ def test_grpc_transport_enter_exit(): chan_close.assert_not_called() chan_close.assert_called_once() -def test_grpc_client_enter_exit(): +def test_grpc_client_ctx(): client = {{ service.client_name }}( credentials=ga_credentials.AnonymousCredentials(), transport='grpc', ) # Test client calls underlying transport. - with mock.patch.object(type(client.transport), "__enter__") as enter: - with mock.patch.object(type(client.transport), "__exit__") as exit: - enter.assert_not_called() - exit.assert_not_called() - with client as _: - enter.assert_called_once() - exit.assert_called() + with mock.patch.object(type(client.transport), "close") as close: + close.assert_not_called() + with client as _: + pass + close.assert_called() {% endblock %} From 3ab120036a3533309ad20237c64836483f61d9fe Mon Sep 17 00:00:00 2001 From: Aza Tulepbergenov Date: Mon, 27 Sep 2021 11:25:15 -0700 Subject: [PATCH 12/14] chore: adds warning infobox to docstring. --- .../%name/%version/%sub/services/%service/client.py.j2 | 7 ++++--- .../%version/%sub/services/%service/transports/base.py.j2 | 5 +++-- .../%name_%version/%sub/services/%service/client.py.j2 | 7 ++++--- .../%sub/services/%service/transports/base.py.j2 | 5 +++-- 4 files changed, 14 insertions(+), 10 deletions(-) diff --git a/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/client.py.j2 b/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/client.py.j2 index 37a85891af..3d14c341a5 100644 --- a/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/client.py.j2 +++ b/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/client.py.j2 @@ -154,9 +154,10 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta): def __exit__(self, type, value, traceback): """Releases underlying transport's resources. - WARNING: ONLY use as a context manager if the transport is NOT shared - with other clients! Exiting the with block will CLOSE the transport - and may cause errors in other clients! + .. warning:: + ONLY use as a context manager if the transport is NOT shared + with other clients! Exiting the with block will CLOSE the transport + and may cause errors in other clients! """ self.transport.close() diff --git a/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/transports/base.py.j2 b/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/transports/base.py.j2 index 49015d3155..95b0701f19 100644 --- a/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/transports/base.py.j2 +++ b/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/transports/base.py.j2 @@ -106,8 +106,9 @@ class {{ service.name }}Transport(metaclass=abc.ABCMeta): def close(self): """Closes resources associated with the transport. - WARNING: Only call this method if the transport is NOT shared - with other clients - this may cause errors in other clients! + :: warning:: + Only call this method if the transport is NOT shared + with other clients - this may cause errors in other clients! """ raise NotImplementedError() diff --git a/gapic/templates/%namespace/%name_%version/%sub/services/%service/client.py.j2 b/gapic/templates/%namespace/%name_%version/%sub/services/%service/client.py.j2 index fff0deab63..9f76c285e6 100644 --- a/gapic/templates/%namespace/%name_%version/%sub/services/%service/client.py.j2 +++ b/gapic/templates/%namespace/%name_%version/%sub/services/%service/client.py.j2 @@ -486,9 +486,10 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta): def __exit__(self, type, value, traceback): """Releases underlying transport's resources. - WARNING: ONLY use as a context manager if the transport is NOT shared - with other clients! Exiting the with block will CLOSE the transport - and may cause errors in other clients! + .. warning:: + ONLY use as a context manager if the transport is NOT shared + with other clients! Exiting the with block will CLOSE the transport + and may cause errors in other clients! """ self.transport.close() diff --git a/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/base.py.j2 b/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/base.py.j2 index 78224b9138..894392ae90 100644 --- a/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/base.py.j2 +++ b/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/base.py.j2 @@ -186,8 +186,9 @@ class {{ service.name }}Transport(abc.ABC): def close(self): """Closes resources associated with the transport. - WARNING: Only call this method if the transport is NOT shared - with other clients - this may cause errors in other clients! + :: warning:: + Only call this method if the transport is NOT shared + with other clients - this may cause errors in other clients! """ raise NotImplementedError() From db48daff5e01058fb26f56abd1fb4be31bd080b7 Mon Sep 17 00:00:00 2001 From: Aza Tulepbergenov Date: Mon, 27 Sep 2021 11:56:30 -0700 Subject: [PATCH 13/14] chore: updates integration tests. --- .../google/cloud/asset_v1/services/asset_service/client.py | 7 ++++--- .../asset_v1/services/asset_service/transports/base.py | 5 +++-- .../iam/credentials_v1/services/iam_credentials/client.py | 7 ++++--- .../services/iam_credentials/transports/base.py | 5 +++-- .../cloud/logging_v2/services/config_service_v2/client.py | 7 ++++--- .../services/config_service_v2/transports/base.py | 5 +++-- .../cloud/logging_v2/services/logging_service_v2/client.py | 7 ++++--- .../services/logging_service_v2/transports/base.py | 5 +++-- .../cloud/logging_v2/services/metrics_service_v2/client.py | 7 ++++--- .../services/metrics_service_v2/transports/base.py | 5 +++-- .../google/cloud/redis_v1/services/cloud_redis/client.py | 7 ++++--- .../cloud/redis_v1/services/cloud_redis/transports/base.py | 5 +++-- 12 files changed, 42 insertions(+), 30 deletions(-) diff --git a/tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/client.py b/tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/client.py index 59cc6c9fa5..3435d4d91a 100644 --- a/tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/client.py +++ b/tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/client.py @@ -1454,9 +1454,10 @@ def __enter__(self): def __exit__(self, type, value, traceback): """Releases underlying transport's resources. - WARNING: ONLY use as a context manager if the transport is NOT shared - with other clients! Exiting the with block will CLOSE the transport - and may cause errors in other clients! + .. warning:: + ONLY use as a context manager if the transport is NOT shared + with other clients! Exiting the with block will CLOSE the transport + and may cause errors in other clients! """ self.transport.close() diff --git a/tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/transports/base.py b/tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/transports/base.py index ff2a4a8f39..3b56de3886 100644 --- a/tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/transports/base.py +++ b/tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/transports/base.py @@ -262,8 +262,9 @@ def _prep_wrapped_messages(self, client_info): def close(self): """Closes resources associated with the transport. - WARNING: Only call this method if the transport is NOT shared - with other clients - this may cause errors in other clients! + :: warning:: + Only call this method if the transport is NOT shared + with other clients - this may cause errors in other clients! """ raise NotImplementedError() diff --git a/tests/integration/goldens/credentials/google/iam/credentials_v1/services/iam_credentials/client.py b/tests/integration/goldens/credentials/google/iam/credentials_v1/services/iam_credentials/client.py index 720f7f7837..ba0618a6c3 100644 --- a/tests/integration/goldens/credentials/google/iam/credentials_v1/services/iam_credentials/client.py +++ b/tests/integration/goldens/credentials/google/iam/credentials_v1/services/iam_credentials/client.py @@ -813,9 +813,10 @@ def __enter__(self): def __exit__(self, type, value, traceback): """Releases underlying transport's resources. - WARNING: ONLY use as a context manager if the transport is NOT shared - with other clients! Exiting the with block will CLOSE the transport - and may cause errors in other clients! + .. warning:: + ONLY use as a context manager if the transport is NOT shared + with other clients! Exiting the with block will CLOSE the transport + and may cause errors in other clients! """ self.transport.close() diff --git a/tests/integration/goldens/credentials/google/iam/credentials_v1/services/iam_credentials/transports/base.py b/tests/integration/goldens/credentials/google/iam/credentials_v1/services/iam_credentials/transports/base.py index 684994103c..091546377c 100644 --- a/tests/integration/goldens/credentials/google/iam/credentials_v1/services/iam_credentials/transports/base.py +++ b/tests/integration/goldens/credentials/google/iam/credentials_v1/services/iam_credentials/transports/base.py @@ -199,8 +199,9 @@ def _prep_wrapped_messages(self, client_info): def close(self): """Closes resources associated with the transport. - WARNING: Only call this method if the transport is NOT shared - with other clients - this may cause errors in other clients! + :: warning:: + Only call this method if the transport is NOT shared + with other clients - this may cause errors in other clients! """ raise NotImplementedError() diff --git a/tests/integration/goldens/logging/google/cloud/logging_v2/services/config_service_v2/client.py b/tests/integration/goldens/logging/google/cloud/logging_v2/services/config_service_v2/client.py index 7db9f31861..9b507df4ab 100644 --- a/tests/integration/goldens/logging/google/cloud/logging_v2/services/config_service_v2/client.py +++ b/tests/integration/goldens/logging/google/cloud/logging_v2/services/config_service_v2/client.py @@ -2185,9 +2185,10 @@ def __enter__(self): def __exit__(self, type, value, traceback): """Releases underlying transport's resources. - WARNING: ONLY use as a context manager if the transport is NOT shared - with other clients! Exiting the with block will CLOSE the transport - and may cause errors in other clients! + .. warning:: + ONLY use as a context manager if the transport is NOT shared + with other clients! Exiting the with block will CLOSE the transport + and may cause errors in other clients! """ self.transport.close() diff --git a/tests/integration/goldens/logging/google/cloud/logging_v2/services/config_service_v2/transports/base.py b/tests/integration/goldens/logging/google/cloud/logging_v2/services/config_service_v2/transports/base.py index 25e1a25ceb..c48857438a 100644 --- a/tests/integration/goldens/logging/google/cloud/logging_v2/services/config_service_v2/transports/base.py +++ b/tests/integration/goldens/logging/google/cloud/logging_v2/services/config_service_v2/transports/base.py @@ -326,8 +326,9 @@ def _prep_wrapped_messages(self, client_info): def close(self): """Closes resources associated with the transport. - WARNING: Only call this method if the transport is NOT shared - with other clients - this may cause errors in other clients! + :: warning:: + Only call this method if the transport is NOT shared + with other clients - this may cause errors in other clients! """ raise NotImplementedError() diff --git a/tests/integration/goldens/logging/google/cloud/logging_v2/services/logging_service_v2/client.py b/tests/integration/goldens/logging/google/cloud/logging_v2/services/logging_service_v2/client.py index b390f0040d..2403e432e4 100644 --- a/tests/integration/goldens/logging/google/cloud/logging_v2/services/logging_service_v2/client.py +++ b/tests/integration/goldens/logging/google/cloud/logging_v2/services/logging_service_v2/client.py @@ -907,9 +907,10 @@ def __enter__(self): def __exit__(self, type, value, traceback): """Releases underlying transport's resources. - WARNING: ONLY use as a context manager if the transport is NOT shared - with other clients! Exiting the with block will CLOSE the transport - and may cause errors in other clients! + .. warning:: + ONLY use as a context manager if the transport is NOT shared + with other clients! Exiting the with block will CLOSE the transport + and may cause errors in other clients! """ self.transport.close() diff --git a/tests/integration/goldens/logging/google/cloud/logging_v2/services/logging_service_v2/transports/base.py b/tests/integration/goldens/logging/google/cloud/logging_v2/services/logging_service_v2/transports/base.py index 2d46a4bad2..1a5994577c 100644 --- a/tests/integration/goldens/logging/google/cloud/logging_v2/services/logging_service_v2/transports/base.py +++ b/tests/integration/goldens/logging/google/cloud/logging_v2/services/logging_service_v2/transports/base.py @@ -234,8 +234,9 @@ def _prep_wrapped_messages(self, client_info): def close(self): """Closes resources associated with the transport. - WARNING: Only call this method if the transport is NOT shared - with other clients - this may cause errors in other clients! + :: warning:: + Only call this method if the transport is NOT shared + with other clients - this may cause errors in other clients! """ raise NotImplementedError() diff --git a/tests/integration/goldens/logging/google/cloud/logging_v2/services/metrics_service_v2/client.py b/tests/integration/goldens/logging/google/cloud/logging_v2/services/metrics_service_v2/client.py index 2b76672f5b..87199396bb 100644 --- a/tests/integration/goldens/logging/google/cloud/logging_v2/services/metrics_service_v2/client.py +++ b/tests/integration/goldens/logging/google/cloud/logging_v2/services/metrics_service_v2/client.py @@ -786,9 +786,10 @@ def __enter__(self): def __exit__(self, type, value, traceback): """Releases underlying transport's resources. - WARNING: ONLY use as a context manager if the transport is NOT shared - with other clients! Exiting the with block will CLOSE the transport - and may cause errors in other clients! + .. warning:: + ONLY use as a context manager if the transport is NOT shared + with other clients! Exiting the with block will CLOSE the transport + and may cause errors in other clients! """ self.transport.close() diff --git a/tests/integration/goldens/logging/google/cloud/logging_v2/services/metrics_service_v2/transports/base.py b/tests/integration/goldens/logging/google/cloud/logging_v2/services/metrics_service_v2/transports/base.py index 509aef079e..c19331ea17 100644 --- a/tests/integration/goldens/logging/google/cloud/logging_v2/services/metrics_service_v2/transports/base.py +++ b/tests/integration/goldens/logging/google/cloud/logging_v2/services/metrics_service_v2/transports/base.py @@ -213,8 +213,9 @@ def _prep_wrapped_messages(self, client_info): def close(self): """Closes resources associated with the transport. - WARNING: Only call this method if the transport is NOT shared - with other clients - this may cause errors in other clients! + :: warning:: + Only call this method if the transport is NOT shared + with other clients - this may cause errors in other clients! """ raise NotImplementedError() diff --git a/tests/integration/goldens/redis/google/cloud/redis_v1/services/cloud_redis/client.py b/tests/integration/goldens/redis/google/cloud/redis_v1/services/cloud_redis/client.py index 99a49407e0..d4ebdd93fb 100644 --- a/tests/integration/goldens/redis/google/cloud/redis_v1/services/cloud_redis/client.py +++ b/tests/integration/goldens/redis/google/cloud/redis_v1/services/cloud_redis/client.py @@ -1275,9 +1275,10 @@ def __enter__(self): def __exit__(self, type, value, traceback): """Releases underlying transport's resources. - WARNING: ONLY use as a context manager if the transport is NOT shared - with other clients! Exiting the with block will CLOSE the transport - and may cause errors in other clients! + .. warning:: + ONLY use as a context manager if the transport is NOT shared + with other clients! Exiting the with block will CLOSE the transport + and may cause errors in other clients! """ self.transport.close() diff --git a/tests/integration/goldens/redis/google/cloud/redis_v1/services/cloud_redis/transports/base.py b/tests/integration/goldens/redis/google/cloud/redis_v1/services/cloud_redis/transports/base.py index 2262346d57..c32889d3b1 100644 --- a/tests/integration/goldens/redis/google/cloud/redis_v1/services/cloud_redis/transports/base.py +++ b/tests/integration/goldens/redis/google/cloud/redis_v1/services/cloud_redis/transports/base.py @@ -198,8 +198,9 @@ def _prep_wrapped_messages(self, client_info): def close(self): """Closes resources associated with the transport. - WARNING: Only call this method if the transport is NOT shared - with other clients - this may cause errors in other clients! + :: warning:: + Only call this method if the transport is NOT shared + with other clients - this may cause errors in other clients! """ raise NotImplementedError() From 73f162ca18bfba40c593de335c707edc0e21b9c7 Mon Sep 17 00:00:00 2001 From: Aza Tulepbergenov Date: Thu, 30 Sep 2021 10:38:25 -0700 Subject: [PATCH 14/14] chore: fixes typo. --- .../%name/%version/%sub/services/%service/transports/base.py.j2 | 2 +- .../%name_%version/%sub/services/%service/transports/base.py.j2 | 2 +- .../cloud/asset_v1/services/asset_service/transports/base.py | 2 +- .../credentials_v1/services/iam_credentials/transports/base.py | 2 +- .../logging_v2/services/config_service_v2/transports/base.py | 2 +- .../logging_v2/services/logging_service_v2/transports/base.py | 2 +- .../logging_v2/services/metrics_service_v2/transports/base.py | 2 +- .../cloud/redis_v1/services/cloud_redis/transports/base.py | 2 +- 8 files changed, 8 insertions(+), 8 deletions(-) diff --git a/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/transports/base.py.j2 b/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/transports/base.py.j2 index 95b0701f19..a87e3b6717 100644 --- a/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/transports/base.py.j2 +++ b/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/transports/base.py.j2 @@ -106,7 +106,7 @@ class {{ service.name }}Transport(metaclass=abc.ABCMeta): def close(self): """Closes resources associated with the transport. - :: warning:: + .. warning:: Only call this method if the transport is NOT shared with other clients - this may cause errors in other clients! """ diff --git a/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/base.py.j2 b/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/base.py.j2 index 894392ae90..78fa1e2ba6 100644 --- a/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/base.py.j2 +++ b/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/base.py.j2 @@ -186,7 +186,7 @@ class {{ service.name }}Transport(abc.ABC): def close(self): """Closes resources associated with the transport. - :: warning:: + .. warning:: Only call this method if the transport is NOT shared with other clients - this may cause errors in other clients! """ diff --git a/tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/transports/base.py b/tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/transports/base.py index 3b56de3886..1da08824f8 100644 --- a/tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/transports/base.py +++ b/tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/transports/base.py @@ -262,7 +262,7 @@ def _prep_wrapped_messages(self, client_info): def close(self): """Closes resources associated with the transport. - :: warning:: + .. warning:: Only call this method if the transport is NOT shared with other clients - this may cause errors in other clients! """ diff --git a/tests/integration/goldens/credentials/google/iam/credentials_v1/services/iam_credentials/transports/base.py b/tests/integration/goldens/credentials/google/iam/credentials_v1/services/iam_credentials/transports/base.py index 091546377c..5a0c487eb6 100644 --- a/tests/integration/goldens/credentials/google/iam/credentials_v1/services/iam_credentials/transports/base.py +++ b/tests/integration/goldens/credentials/google/iam/credentials_v1/services/iam_credentials/transports/base.py @@ -199,7 +199,7 @@ def _prep_wrapped_messages(self, client_info): def close(self): """Closes resources associated with the transport. - :: warning:: + .. warning:: Only call this method if the transport is NOT shared with other clients - this may cause errors in other clients! """ diff --git a/tests/integration/goldens/logging/google/cloud/logging_v2/services/config_service_v2/transports/base.py b/tests/integration/goldens/logging/google/cloud/logging_v2/services/config_service_v2/transports/base.py index c48857438a..92a840f2a3 100644 --- a/tests/integration/goldens/logging/google/cloud/logging_v2/services/config_service_v2/transports/base.py +++ b/tests/integration/goldens/logging/google/cloud/logging_v2/services/config_service_v2/transports/base.py @@ -326,7 +326,7 @@ def _prep_wrapped_messages(self, client_info): def close(self): """Closes resources associated with the transport. - :: warning:: + .. warning:: Only call this method if the transport is NOT shared with other clients - this may cause errors in other clients! """ diff --git a/tests/integration/goldens/logging/google/cloud/logging_v2/services/logging_service_v2/transports/base.py b/tests/integration/goldens/logging/google/cloud/logging_v2/services/logging_service_v2/transports/base.py index 1a5994577c..195d115497 100644 --- a/tests/integration/goldens/logging/google/cloud/logging_v2/services/logging_service_v2/transports/base.py +++ b/tests/integration/goldens/logging/google/cloud/logging_v2/services/logging_service_v2/transports/base.py @@ -234,7 +234,7 @@ def _prep_wrapped_messages(self, client_info): def close(self): """Closes resources associated with the transport. - :: warning:: + .. warning:: Only call this method if the transport is NOT shared with other clients - this may cause errors in other clients! """ diff --git a/tests/integration/goldens/logging/google/cloud/logging_v2/services/metrics_service_v2/transports/base.py b/tests/integration/goldens/logging/google/cloud/logging_v2/services/metrics_service_v2/transports/base.py index c19331ea17..2a4ff25e1a 100644 --- a/tests/integration/goldens/logging/google/cloud/logging_v2/services/metrics_service_v2/transports/base.py +++ b/tests/integration/goldens/logging/google/cloud/logging_v2/services/metrics_service_v2/transports/base.py @@ -213,7 +213,7 @@ def _prep_wrapped_messages(self, client_info): def close(self): """Closes resources associated with the transport. - :: warning:: + .. warning:: Only call this method if the transport is NOT shared with other clients - this may cause errors in other clients! """ diff --git a/tests/integration/goldens/redis/google/cloud/redis_v1/services/cloud_redis/transports/base.py b/tests/integration/goldens/redis/google/cloud/redis_v1/services/cloud_redis/transports/base.py index c32889d3b1..240133be80 100644 --- a/tests/integration/goldens/redis/google/cloud/redis_v1/services/cloud_redis/transports/base.py +++ b/tests/integration/goldens/redis/google/cloud/redis_v1/services/cloud_redis/transports/base.py @@ -198,7 +198,7 @@ def _prep_wrapped_messages(self, client_info): def close(self): """Closes resources associated with the transport. - :: warning:: + .. warning:: Only call this method if the transport is NOT shared with other clients - this may cause errors in other clients! """