From e88a6643be39c3cdbf4be3718e101ba0cb36f6f9 Mon Sep 17 00:00:00 2001 From: "gcf-owl-bot[bot]" <78513119+gcf-owl-bot[bot]@users.noreply.github.com> Date: Thu, 7 Oct 2021 00:38:21 +0000 Subject: [PATCH] feat: add context manager support in client (#32) - [ ] Regenerate this pull request now. chore: fix docstring for first attribute of protos committer: @busunkim96 PiperOrigin-RevId: 401271153 Source-Link: https://github.com/googleapis/googleapis/commit/787f8c9a731f44e74a90b9847d48659ca9462d10 Source-Link: https://github.com/googleapis/googleapis-gen/commit/81decffe9fc72396a8153e756d1d67a6eecfd620 Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiODFkZWNmZmU5ZmM3MjM5NmE4MTUzZTc1NmQxZDY3YTZlZWNmZDYyMCJ9 --- .../services/environments/async_client.py | 6 +++ .../services/environments/client.py | 18 +++++-- .../services/environments/transports/base.py | 9 ++++ .../services/environments/transports/grpc.py | 3 ++ .../environments/transports/grpc_asyncio.py | 3 ++ .../services/image_versions/async_client.py | 6 +++ .../services/image_versions/client.py | 18 +++++-- .../image_versions/transports/base.py | 9 ++++ .../image_versions/transports/grpc.py | 3 ++ .../image_versions/transports/grpc_asyncio.py | 3 ++ .../airflow/service_v1/types/environments.py | 1 + .../service_v1/types/image_versions.py | 2 +- .../services/environments/async_client.py | 6 +++ .../services/environments/client.py | 18 +++++-- .../services/environments/transports/base.py | 9 ++++ .../services/environments/transports/grpc.py | 3 ++ .../environments/transports/grpc_asyncio.py | 3 ++ .../services/image_versions/async_client.py | 6 +++ .../services/image_versions/client.py | 18 +++++-- .../image_versions/transports/base.py | 9 ++++ .../image_versions/transports/grpc.py | 3 ++ .../image_versions/transports/grpc_asyncio.py | 3 ++ .../service_v1beta1/types/environments.py | 4 ++ .../service_v1beta1/types/image_versions.py | 2 +- .../gapic/service_v1/test_environments.py | 50 +++++++++++++++++++ .../gapic/service_v1/test_image_versions.py | 50 +++++++++++++++++++ .../service_v1beta1/test_environments.py | 50 +++++++++++++++++++ .../service_v1beta1/test_image_versions.py | 50 +++++++++++++++++++ 28 files changed, 347 insertions(+), 18 deletions(-) diff --git a/google/cloud/orchestration/airflow/service_v1/services/environments/async_client.py b/google/cloud/orchestration/airflow/service_v1/services/environments/async_client.py index 776c37d..ef1b199 100644 --- a/google/cloud/orchestration/airflow/service_v1/services/environments/async_client.py +++ b/google/cloud/orchestration/airflow/service_v1/services/environments/async_client.py @@ -759,6 +759,12 @@ async def delete_environment( # 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/google/cloud/orchestration/airflow/service_v1/services/environments/client.py b/google/cloud/orchestration/airflow/service_v1/services/environments/client.py index d1bfba4..6371c38 100644 --- a/google/cloud/orchestration/airflow/service_v1/services/environments/client.py +++ b/google/cloud/orchestration/airflow/service_v1/services/environments/client.py @@ -348,10 +348,7 @@ def __init__( client_cert_source_for_mtls=client_cert_source_func, quota_project_id=client_options.quota_project_id, client_info=client_info, - always_use_jwt_access=( - Transport == type(self).get_transport_class("grpc") - or Transport == type(self).get_transport_class("grpc_asyncio") - ), + always_use_jwt_access=True, ) def create_environment( @@ -950,6 +947,19 @@ def delete_environment( # Done; return the response. return response + def __enter__(self): + 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! + """ + self.transport.close() + try: DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo( diff --git a/google/cloud/orchestration/airflow/service_v1/services/environments/transports/base.py b/google/cloud/orchestration/airflow/service_v1/services/environments/transports/base.py index d7a3dc9..bbf2037 100644 --- a/google/cloud/orchestration/airflow/service_v1/services/environments/transports/base.py +++ b/google/cloud/orchestration/airflow/service_v1/services/environments/transports/base.py @@ -173,6 +173,15 @@ 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! + """ + raise NotImplementedError() + @property def operations_client(self) -> operations_v1.OperationsClient: """Return the client designed to process long-running operations.""" diff --git a/google/cloud/orchestration/airflow/service_v1/services/environments/transports/grpc.py b/google/cloud/orchestration/airflow/service_v1/services/environments/transports/grpc.py index cc5370a..a80d06b 100644 --- a/google/cloud/orchestration/airflow/service_v1/services/environments/transports/grpc.py +++ b/google/cloud/orchestration/airflow/service_v1/services/environments/transports/grpc.py @@ -374,5 +374,8 @@ def delete_environment( ) return self._stubs["delete_environment"] + def close(self): + self.grpc_channel.close() + __all__ = ("EnvironmentsGrpcTransport",) diff --git a/google/cloud/orchestration/airflow/service_v1/services/environments/transports/grpc_asyncio.py b/google/cloud/orchestration/airflow/service_v1/services/environments/transports/grpc_asyncio.py index 9e3a6c3..f210586 100644 --- a/google/cloud/orchestration/airflow/service_v1/services/environments/transports/grpc_asyncio.py +++ b/google/cloud/orchestration/airflow/service_v1/services/environments/transports/grpc_asyncio.py @@ -388,5 +388,8 @@ def delete_environment( ) return self._stubs["delete_environment"] + def close(self): + return self.grpc_channel.close() + __all__ = ("EnvironmentsGrpcAsyncIOTransport",) diff --git a/google/cloud/orchestration/airflow/service_v1/services/image_versions/async_client.py b/google/cloud/orchestration/airflow/service_v1/services/image_versions/async_client.py index d67d0d5..c953e4b 100644 --- a/google/cloud/orchestration/airflow/service_v1/services/image_versions/async_client.py +++ b/google/cloud/orchestration/airflow/service_v1/services/image_versions/async_client.py @@ -240,6 +240,12 @@ async def list_image_versions( # 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/google/cloud/orchestration/airflow/service_v1/services/image_versions/client.py b/google/cloud/orchestration/airflow/service_v1/services/image_versions/client.py index 18fdd0b..7480b0f 100644 --- a/google/cloud/orchestration/airflow/service_v1/services/image_versions/client.py +++ b/google/cloud/orchestration/airflow/service_v1/services/image_versions/client.py @@ -326,10 +326,7 @@ def __init__( client_cert_source_for_mtls=client_cert_source_func, quota_project_id=client_options.quota_project_id, client_info=client_info, - always_use_jwt_access=( - Transport == type(self).get_transport_class("grpc") - or Transport == type(self).get_transport_class("grpc_asyncio") - ), + always_use_jwt_access=True, ) def list_image_versions( @@ -413,6 +410,19 @@ def list_image_versions( # Done; return the response. return response + def __enter__(self): + 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! + """ + self.transport.close() + try: DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo( diff --git a/google/cloud/orchestration/airflow/service_v1/services/image_versions/transports/base.py b/google/cloud/orchestration/airflow/service_v1/services/image_versions/transports/base.py index e3b68e7..3ec017c 100644 --- a/google/cloud/orchestration/airflow/service_v1/services/image_versions/transports/base.py +++ b/google/cloud/orchestration/airflow/service_v1/services/image_versions/transports/base.py @@ -159,6 +159,15 @@ 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! + """ + raise NotImplementedError() + @property def list_image_versions( self, diff --git a/google/cloud/orchestration/airflow/service_v1/services/image_versions/transports/grpc.py b/google/cloud/orchestration/airflow/service_v1/services/image_versions/transports/grpc.py index b409f92..e011310 100644 --- a/google/cloud/orchestration/airflow/service_v1/services/image_versions/transports/grpc.py +++ b/google/cloud/orchestration/airflow/service_v1/services/image_versions/transports/grpc.py @@ -254,5 +254,8 @@ def list_image_versions( ) return self._stubs["list_image_versions"] + def close(self): + self.grpc_channel.close() + __all__ = ("ImageVersionsGrpcTransport",) diff --git a/google/cloud/orchestration/airflow/service_v1/services/image_versions/transports/grpc_asyncio.py b/google/cloud/orchestration/airflow/service_v1/services/image_versions/transports/grpc_asyncio.py index e229017..6d20107 100644 --- a/google/cloud/orchestration/airflow/service_v1/services/image_versions/transports/grpc_asyncio.py +++ b/google/cloud/orchestration/airflow/service_v1/services/image_versions/transports/grpc_asyncio.py @@ -257,5 +257,8 @@ def list_image_versions( ) return self._stubs["list_image_versions"] + def close(self): + return self.grpc_channel.close() + __all__ = ("ImageVersionsGrpcAsyncIOTransport",) diff --git a/google/cloud/orchestration/airflow/service_v1/types/environments.py b/google/cloud/orchestration/airflow/service_v1/types/environments.py index acb2a12..c78d2eb 100644 --- a/google/cloud/orchestration/airflow/service_v1/types/environments.py +++ b/google/cloud/orchestration/airflow/service_v1/types/environments.py @@ -380,6 +380,7 @@ class WebServerNetworkAccessControl(proto.Message): class AllowedIpRange(proto.Message): r"""Allowed IP range with user-provided description. + Attributes: value (str): IP address or range, defined using CIDR notation, of diff --git a/google/cloud/orchestration/airflow/service_v1/types/image_versions.py b/google/cloud/orchestration/airflow/service_v1/types/image_versions.py index b54c114..0f47815 100644 --- a/google/cloud/orchestration/airflow/service_v1/types/image_versions.py +++ b/google/cloud/orchestration/airflow/service_v1/types/image_versions.py @@ -71,7 +71,7 @@ def raw_page(self): class ImageVersion(proto.Message): - r"""ImageVersion informatio. + r"""ImageVersion information Attributes: image_version_id (str): diff --git a/google/cloud/orchestration/airflow/service_v1beta1/services/environments/async_client.py b/google/cloud/orchestration/airflow/service_v1beta1/services/environments/async_client.py index 182ee8e..09c5208 100644 --- a/google/cloud/orchestration/airflow/service_v1beta1/services/environments/async_client.py +++ b/google/cloud/orchestration/airflow/service_v1beta1/services/environments/async_client.py @@ -904,6 +904,12 @@ async def check_upgrade( # 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/google/cloud/orchestration/airflow/service_v1beta1/services/environments/client.py b/google/cloud/orchestration/airflow/service_v1beta1/services/environments/client.py index b6aeba1..969c8b6 100644 --- a/google/cloud/orchestration/airflow/service_v1beta1/services/environments/client.py +++ b/google/cloud/orchestration/airflow/service_v1beta1/services/environments/client.py @@ -350,10 +350,7 @@ def __init__( client_cert_source_for_mtls=client_cert_source_func, quota_project_id=client_options.quota_project_id, client_info=client_info, - always_use_jwt_access=( - Transport == type(self).get_transport_class("grpc") - or Transport == type(self).get_transport_class("grpc_asyncio") - ), + always_use_jwt_access=True, ) def create_environment( @@ -1097,6 +1094,19 @@ def check_upgrade( # Done; return the response. return response + def __enter__(self): + 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! + """ + self.transport.close() + try: DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo( diff --git a/google/cloud/orchestration/airflow/service_v1beta1/services/environments/transports/base.py b/google/cloud/orchestration/airflow/service_v1beta1/services/environments/transports/base.py index 44e4b20..cb3063e 100644 --- a/google/cloud/orchestration/airflow/service_v1beta1/services/environments/transports/base.py +++ b/google/cloud/orchestration/airflow/service_v1beta1/services/environments/transports/base.py @@ -179,6 +179,15 @@ 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! + """ + raise NotImplementedError() + @property def operations_client(self) -> operations_v1.OperationsClient: """Return the client designed to process long-running operations.""" diff --git a/google/cloud/orchestration/airflow/service_v1beta1/services/environments/transports/grpc.py b/google/cloud/orchestration/airflow/service_v1beta1/services/environments/transports/grpc.py index ce91a6a..2354fde 100644 --- a/google/cloud/orchestration/airflow/service_v1beta1/services/environments/transports/grpc.py +++ b/google/cloud/orchestration/airflow/service_v1beta1/services/environments/transports/grpc.py @@ -429,5 +429,8 @@ def check_upgrade( ) return self._stubs["check_upgrade"] + def close(self): + self.grpc_channel.close() + __all__ = ("EnvironmentsGrpcTransport",) diff --git a/google/cloud/orchestration/airflow/service_v1beta1/services/environments/transports/grpc_asyncio.py b/google/cloud/orchestration/airflow/service_v1beta1/services/environments/transports/grpc_asyncio.py index cdf5629..56b3307 100644 --- a/google/cloud/orchestration/airflow/service_v1beta1/services/environments/transports/grpc_asyncio.py +++ b/google/cloud/orchestration/airflow/service_v1beta1/services/environments/transports/grpc_asyncio.py @@ -447,5 +447,8 @@ def check_upgrade( ) return self._stubs["check_upgrade"] + def close(self): + return self.grpc_channel.close() + __all__ = ("EnvironmentsGrpcAsyncIOTransport",) diff --git a/google/cloud/orchestration/airflow/service_v1beta1/services/image_versions/async_client.py b/google/cloud/orchestration/airflow/service_v1beta1/services/image_versions/async_client.py index b3bc1ac..c424397 100644 --- a/google/cloud/orchestration/airflow/service_v1beta1/services/image_versions/async_client.py +++ b/google/cloud/orchestration/airflow/service_v1beta1/services/image_versions/async_client.py @@ -242,6 +242,12 @@ async def list_image_versions( # 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/google/cloud/orchestration/airflow/service_v1beta1/services/image_versions/client.py b/google/cloud/orchestration/airflow/service_v1beta1/services/image_versions/client.py index ac25e23..1f5d56d 100644 --- a/google/cloud/orchestration/airflow/service_v1beta1/services/image_versions/client.py +++ b/google/cloud/orchestration/airflow/service_v1beta1/services/image_versions/client.py @@ -328,10 +328,7 @@ def __init__( client_cert_source_for_mtls=client_cert_source_func, quota_project_id=client_options.quota_project_id, client_info=client_info, - always_use_jwt_access=( - Transport == type(self).get_transport_class("grpc") - or Transport == type(self).get_transport_class("grpc_asyncio") - ), + always_use_jwt_access=True, ) def list_image_versions( @@ -415,6 +412,19 @@ def list_image_versions( # Done; return the response. return response + def __enter__(self): + 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! + """ + self.transport.close() + try: DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo( diff --git a/google/cloud/orchestration/airflow/service_v1beta1/services/image_versions/transports/base.py b/google/cloud/orchestration/airflow/service_v1beta1/services/image_versions/transports/base.py index 568a77e..683bd93 100644 --- a/google/cloud/orchestration/airflow/service_v1beta1/services/image_versions/transports/base.py +++ b/google/cloud/orchestration/airflow/service_v1beta1/services/image_versions/transports/base.py @@ -159,6 +159,15 @@ 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! + """ + raise NotImplementedError() + @property def list_image_versions( self, diff --git a/google/cloud/orchestration/airflow/service_v1beta1/services/image_versions/transports/grpc.py b/google/cloud/orchestration/airflow/service_v1beta1/services/image_versions/transports/grpc.py index 415ca99..09fe602 100644 --- a/google/cloud/orchestration/airflow/service_v1beta1/services/image_versions/transports/grpc.py +++ b/google/cloud/orchestration/airflow/service_v1beta1/services/image_versions/transports/grpc.py @@ -254,5 +254,8 @@ def list_image_versions( ) return self._stubs["list_image_versions"] + def close(self): + self.grpc_channel.close() + __all__ = ("ImageVersionsGrpcTransport",) diff --git a/google/cloud/orchestration/airflow/service_v1beta1/services/image_versions/transports/grpc_asyncio.py b/google/cloud/orchestration/airflow/service_v1beta1/services/image_versions/transports/grpc_asyncio.py index 56710da..3747e4e 100644 --- a/google/cloud/orchestration/airflow/service_v1beta1/services/image_versions/transports/grpc_asyncio.py +++ b/google/cloud/orchestration/airflow/service_v1beta1/services/image_versions/transports/grpc_asyncio.py @@ -257,5 +257,8 @@ def list_image_versions( ) return self._stubs["list_image_versions"] + def close(self): + return self.grpc_channel.close() + __all__ = ("ImageVersionsGrpcAsyncIOTransport",) diff --git a/google/cloud/orchestration/airflow/service_v1beta1/types/environments.py b/google/cloud/orchestration/airflow/service_v1beta1/types/environments.py index 799b943..23cf9ca 100644 --- a/google/cloud/orchestration/airflow/service_v1beta1/types/environments.py +++ b/google/cloud/orchestration/airflow/service_v1beta1/types/environments.py @@ -462,6 +462,7 @@ class WebServerNetworkAccessControl(proto.Message): class AllowedIpRange(proto.Message): r"""Allowed IP range with user-provided description. + Attributes: value (str): IP address or range, defined using CIDR notation, of @@ -964,6 +965,7 @@ class WorkloadsConfig(proto.Message): class SchedulerResource(proto.Message): r"""Configuration for resources used by Airflow schedulers. + Attributes: cpu (float): Optional. CPU request and limit for a single @@ -985,6 +987,7 @@ class SchedulerResource(proto.Message): class WebServerResource(proto.Message): r"""Configuration for resources used by Airflow web server. + Attributes: cpu (float): Optional. CPU request and limit for Airflow @@ -1003,6 +1006,7 @@ class WebServerResource(proto.Message): class WorkerResource(proto.Message): r"""Configuration for resources used by Airflow workers. + Attributes: cpu (float): Optional. CPU request and limit for a single diff --git a/google/cloud/orchestration/airflow/service_v1beta1/types/image_versions.py b/google/cloud/orchestration/airflow/service_v1beta1/types/image_versions.py index 3747997..ec50f91 100644 --- a/google/cloud/orchestration/airflow/service_v1beta1/types/image_versions.py +++ b/google/cloud/orchestration/airflow/service_v1beta1/types/image_versions.py @@ -71,7 +71,7 @@ def raw_page(self): class ImageVersion(proto.Message): - r"""Image Version informatio. + r"""Image Version information Attributes: image_version_id (str): diff --git a/tests/unit/gapic/service_v1/test_environments.py b/tests/unit/gapic/service_v1/test_environments.py index 324df3a..ed313cb 100644 --- a/tests/unit/gapic/service_v1/test_environments.py +++ b/tests/unit/gapic/service_v1/test_environments.py @@ -32,6 +32,7 @@ from google.api_core import grpc_helpers_async from google.api_core import operation_async # type: ignore from google.api_core import operations_v1 +from google.api_core import path_template from google.auth import credentials as ga_credentials from google.auth.exceptions import MutualTLSChannelError from google.cloud.orchestration.airflow.service_v1.services.environments import ( @@ -1831,6 +1832,9 @@ def test_environments_base_transport(): with pytest.raises(NotImplementedError): getattr(transport, method)(request=object()) + with pytest.raises(NotImplementedError): + transport.close() + # Additionally, the LRO client (a property) should # also raise NotImplementedError with pytest.raises(NotImplementedError): @@ -2333,3 +2337,49 @@ def test_client_withDEFAULT_CLIENT_INFO(): credentials=ga_credentials.AnonymousCredentials(), client_info=client_info, ) prep.assert_called_once_with(client_info) + + +@pytest.mark.asyncio +async def test_transport_close_async(): + client = EnvironmentsAsyncClient( + 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", + } + + for transport, close_name in transports.items(): + client = EnvironmentsClient( + credentials=ga_credentials.AnonymousCredentials(), transport=transport + ) + with mock.patch.object( + type(getattr(client.transport, close_name)), "close" + ) as close: + with client: + close.assert_not_called() + close.assert_called_once() + + +def test_client_ctx(): + transports = [ + "grpc", + ] + for transport in transports: + client = EnvironmentsClient( + credentials=ga_credentials.AnonymousCredentials(), transport=transport + ) + # Test client calls underlying transport. + with mock.patch.object(type(client.transport), "close") as close: + close.assert_not_called() + with client: + pass + close.assert_called() diff --git a/tests/unit/gapic/service_v1/test_image_versions.py b/tests/unit/gapic/service_v1/test_image_versions.py index 5bb571e..44a10b0 100644 --- a/tests/unit/gapic/service_v1/test_image_versions.py +++ b/tests/unit/gapic/service_v1/test_image_versions.py @@ -29,6 +29,7 @@ from google.api_core import gapic_v1 from google.api_core import grpc_helpers from google.api_core import grpc_helpers_async +from google.api_core import path_template from google.auth import credentials as ga_credentials from google.auth.exceptions import MutualTLSChannelError from google.cloud.orchestration.airflow.service_v1.services.image_versions import ( @@ -988,6 +989,9 @@ def test_image_versions_base_transport(): with pytest.raises(NotImplementedError): getattr(transport, method)(request=object()) + with pytest.raises(NotImplementedError): + transport.close() + @requires_google_auth_gte_1_25_0 def test_image_versions_base_transport_with_credentials_file(): @@ -1444,3 +1448,49 @@ def test_client_withDEFAULT_CLIENT_INFO(): credentials=ga_credentials.AnonymousCredentials(), client_info=client_info, ) prep.assert_called_once_with(client_info) + + +@pytest.mark.asyncio +async def test_transport_close_async(): + client = ImageVersionsAsyncClient( + 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", + } + + for transport, close_name in transports.items(): + client = ImageVersionsClient( + credentials=ga_credentials.AnonymousCredentials(), transport=transport + ) + with mock.patch.object( + type(getattr(client.transport, close_name)), "close" + ) as close: + with client: + close.assert_not_called() + close.assert_called_once() + + +def test_client_ctx(): + transports = [ + "grpc", + ] + for transport in transports: + client = ImageVersionsClient( + credentials=ga_credentials.AnonymousCredentials(), transport=transport + ) + # Test client calls underlying transport. + with mock.patch.object(type(client.transport), "close") as close: + close.assert_not_called() + with client: + pass + close.assert_called() diff --git a/tests/unit/gapic/service_v1beta1/test_environments.py b/tests/unit/gapic/service_v1beta1/test_environments.py index dbed304..454bf42 100644 --- a/tests/unit/gapic/service_v1beta1/test_environments.py +++ b/tests/unit/gapic/service_v1beta1/test_environments.py @@ -32,6 +32,7 @@ from google.api_core import grpc_helpers_async from google.api_core import operation_async # type: ignore from google.api_core import operations_v1 +from google.api_core import path_template from google.auth import credentials as ga_credentials from google.auth.exceptions import MutualTLSChannelError from google.cloud.orchestration.airflow.service_v1beta1.services.environments import ( @@ -2105,6 +2106,9 @@ def test_environments_base_transport(): with pytest.raises(NotImplementedError): getattr(transport, method)(request=object()) + with pytest.raises(NotImplementedError): + transport.close() + # Additionally, the LRO client (a property) should # also raise NotImplementedError with pytest.raises(NotImplementedError): @@ -2607,3 +2611,49 @@ def test_client_withDEFAULT_CLIENT_INFO(): credentials=ga_credentials.AnonymousCredentials(), client_info=client_info, ) prep.assert_called_once_with(client_info) + + +@pytest.mark.asyncio +async def test_transport_close_async(): + client = EnvironmentsAsyncClient( + 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", + } + + for transport, close_name in transports.items(): + client = EnvironmentsClient( + credentials=ga_credentials.AnonymousCredentials(), transport=transport + ) + with mock.patch.object( + type(getattr(client.transport, close_name)), "close" + ) as close: + with client: + close.assert_not_called() + close.assert_called_once() + + +def test_client_ctx(): + transports = [ + "grpc", + ] + for transport in transports: + client = EnvironmentsClient( + credentials=ga_credentials.AnonymousCredentials(), transport=transport + ) + # Test client calls underlying transport. + with mock.patch.object(type(client.transport), "close") as close: + close.assert_not_called() + with client: + pass + close.assert_called() diff --git a/tests/unit/gapic/service_v1beta1/test_image_versions.py b/tests/unit/gapic/service_v1beta1/test_image_versions.py index b6557c8..a616b44 100644 --- a/tests/unit/gapic/service_v1beta1/test_image_versions.py +++ b/tests/unit/gapic/service_v1beta1/test_image_versions.py @@ -29,6 +29,7 @@ from google.api_core import gapic_v1 from google.api_core import grpc_helpers from google.api_core import grpc_helpers_async +from google.api_core import path_template from google.auth import credentials as ga_credentials from google.auth.exceptions import MutualTLSChannelError from google.cloud.orchestration.airflow.service_v1beta1.services.image_versions import ( @@ -990,6 +991,9 @@ def test_image_versions_base_transport(): with pytest.raises(NotImplementedError): getattr(transport, method)(request=object()) + with pytest.raises(NotImplementedError): + transport.close() + @requires_google_auth_gte_1_25_0 def test_image_versions_base_transport_with_credentials_file(): @@ -1446,3 +1450,49 @@ def test_client_withDEFAULT_CLIENT_INFO(): credentials=ga_credentials.AnonymousCredentials(), client_info=client_info, ) prep.assert_called_once_with(client_info) + + +@pytest.mark.asyncio +async def test_transport_close_async(): + client = ImageVersionsAsyncClient( + 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", + } + + for transport, close_name in transports.items(): + client = ImageVersionsClient( + credentials=ga_credentials.AnonymousCredentials(), transport=transport + ) + with mock.patch.object( + type(getattr(client.transport, close_name)), "close" + ) as close: + with client: + close.assert_not_called() + close.assert_called_once() + + +def test_client_ctx(): + transports = [ + "grpc", + ] + for transport in transports: + client = ImageVersionsClient( + credentials=ga_credentials.AnonymousCredentials(), transport=transport + ) + # Test client calls underlying transport. + with mock.patch.object(type(client.transport), "close") as close: + close.assert_not_called() + with client: + pass + close.assert_called()