From 9ad887f16b4eb93d822db2ea831f7e8ea8a195e0 Mon Sep 17 00:00:00 2001 From: "gcf-owl-bot[bot]" <78513119+gcf-owl-bot[bot]@users.noreply.github.com> Date: Sat, 9 Oct 2021 16:44:13 +0000 Subject: [PATCH] feat: add context manager support in client (#255) - [x] 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/admin_service/async_client.py | 6 +++ .../services/admin_service/client.py | 18 +++++-- .../services/admin_service/transports/base.py | 9 ++++ .../services/admin_service/transports/grpc.py | 3 ++ .../admin_service/transports/grpc_asyncio.py | 3 ++ .../services/cursor_service/async_client.py | 6 +++ .../services/cursor_service/client.py | 18 +++++-- .../cursor_service/transports/base.py | 9 ++++ .../cursor_service/transports/grpc.py | 3 ++ .../cursor_service/transports/grpc_asyncio.py | 3 ++ .../async_client.py | 6 +++ .../partition_assignment_service/client.py | 18 +++++-- .../transports/base.py | 9 ++++ .../transports/grpc.py | 3 ++ .../transports/grpc_asyncio.py | 3 ++ .../publisher_service/async_client.py | 6 +++ .../services/publisher_service/client.py | 18 +++++-- .../publisher_service/transports/base.py | 9 ++++ .../publisher_service/transports/grpc.py | 3 ++ .../transports/grpc_asyncio.py | 3 ++ .../subscriber_service/async_client.py | 6 +++ .../services/subscriber_service/client.py | 18 +++++-- .../subscriber_service/transports/base.py | 9 ++++ .../subscriber_service/transports/grpc.py | 3 ++ .../transports/grpc_asyncio.py | 3 ++ .../topic_stats_service/async_client.py | 6 +++ .../services/topic_stats_service/client.py | 18 +++++-- .../topic_stats_service/transports/base.py | 9 ++++ .../topic_stats_service/transports/grpc.py | 3 ++ .../transports/grpc_asyncio.py | 3 ++ google/cloud/pubsublite_v1/types/admin.py | 29 ++++++++++- google/cloud/pubsublite_v1/types/cursor.py | 13 ++++- google/cloud/pubsublite_v1/types/publisher.py | 8 ++- .../cloud/pubsublite_v1/types/subscriber.py | 8 ++- .../cloud/pubsublite_v1/types/topic_stats.py | 1 + .../gapic/pubsublite_v1/test_admin_service.py | 50 +++++++++++++++++++ .../pubsublite_v1/test_cursor_service.py | 50 +++++++++++++++++++ .../test_partition_assignment_service.py | 50 +++++++++++++++++++ .../pubsublite_v1/test_publisher_service.py | 50 +++++++++++++++++++ .../pubsublite_v1/test_subscriber_service.py | 50 +++++++++++++++++++ .../pubsublite_v1/test_topic_stats_service.py | 50 +++++++++++++++++++ 41 files changed, 564 insertions(+), 29 deletions(-) diff --git a/google/cloud/pubsublite_v1/services/admin_service/async_client.py b/google/cloud/pubsublite_v1/services/admin_service/async_client.py index bd98ad22..80285489 100644 --- a/google/cloud/pubsublite_v1/services/admin_service/async_client.py +++ b/google/cloud/pubsublite_v1/services/admin_service/async_client.py @@ -1641,6 +1641,12 @@ async def list_reservation_topics( # 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/pubsublite_v1/services/admin_service/client.py b/google/cloud/pubsublite_v1/services/admin_service/client.py index 9a8f2364..3caf53b0 100644 --- a/google/cloud/pubsublite_v1/services/admin_service/client.py +++ b/google/cloud/pubsublite_v1/services/admin_service/client.py @@ -381,10 +381,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_topic( @@ -1861,6 +1858,19 @@ def list_reservation_topics( # 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/pubsublite_v1/services/admin_service/transports/base.py b/google/cloud/pubsublite_v1/services/admin_service/transports/base.py index 70e597a8..f6757685 100644 --- a/google/cloud/pubsublite_v1/services/admin_service/transports/base.py +++ b/google/cloud/pubsublite_v1/services/admin_service/transports/base.py @@ -223,6 +223,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/pubsublite_v1/services/admin_service/transports/grpc.py b/google/cloud/pubsublite_v1/services/admin_service/transports/grpc.py index e10979b1..d526776a 100644 --- a/google/cloud/pubsublite_v1/services/admin_service/transports/grpc.py +++ b/google/cloud/pubsublite_v1/services/admin_service/transports/grpc.py @@ -767,5 +767,8 @@ def list_reservation_topics( ) return self._stubs["list_reservation_topics"] + def close(self): + self.grpc_channel.close() + __all__ = ("AdminServiceGrpcTransport",) diff --git a/google/cloud/pubsublite_v1/services/admin_service/transports/grpc_asyncio.py b/google/cloud/pubsublite_v1/services/admin_service/transports/grpc_asyncio.py index 14896976..3866b9eb 100644 --- a/google/cloud/pubsublite_v1/services/admin_service/transports/grpc_asyncio.py +++ b/google/cloud/pubsublite_v1/services/admin_service/transports/grpc_asyncio.py @@ -784,5 +784,8 @@ def list_reservation_topics( ) return self._stubs["list_reservation_topics"] + def close(self): + return self.grpc_channel.close() + __all__ = ("AdminServiceGrpcAsyncIOTransport",) diff --git a/google/cloud/pubsublite_v1/services/cursor_service/async_client.py b/google/cloud/pubsublite_v1/services/cursor_service/async_client.py index 8dbe0083..88a02149 100644 --- a/google/cloud/pubsublite_v1/services/cursor_service/async_client.py +++ b/google/cloud/pubsublite_v1/services/cursor_service/async_client.py @@ -370,6 +370,12 @@ async def list_partition_cursors( # 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/pubsublite_v1/services/cursor_service/client.py b/google/cloud/pubsublite_v1/services/cursor_service/client.py index c0940bba..8adfb4d5 100644 --- a/google/cloud/pubsublite_v1/services/cursor_service/client.py +++ b/google/cloud/pubsublite_v1/services/cursor_service/client.py @@ -346,10 +346,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 streaming_commit_cursor( @@ -519,6 +516,19 @@ def list_partition_cursors( # 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/pubsublite_v1/services/cursor_service/transports/base.py b/google/cloud/pubsublite_v1/services/cursor_service/transports/base.py index 91ebeb67..a17304ec 100644 --- a/google/cloud/pubsublite_v1/services/cursor_service/transports/base.py +++ b/google/cloud/pubsublite_v1/services/cursor_service/transports/base.py @@ -197,6 +197,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 streaming_commit_cursor( self, diff --git a/google/cloud/pubsublite_v1/services/cursor_service/transports/grpc.py b/google/cloud/pubsublite_v1/services/cursor_service/transports/grpc.py index 4b3dca5a..85450c69 100644 --- a/google/cloud/pubsublite_v1/services/cursor_service/transports/grpc.py +++ b/google/cloud/pubsublite_v1/services/cursor_service/transports/grpc.py @@ -312,5 +312,8 @@ def list_partition_cursors( ) return self._stubs["list_partition_cursors"] + def close(self): + self.grpc_channel.close() + __all__ = ("CursorServiceGrpcTransport",) diff --git a/google/cloud/pubsublite_v1/services/cursor_service/transports/grpc_asyncio.py b/google/cloud/pubsublite_v1/services/cursor_service/transports/grpc_asyncio.py index 9c90420b..7940a71c 100644 --- a/google/cloud/pubsublite_v1/services/cursor_service/transports/grpc_asyncio.py +++ b/google/cloud/pubsublite_v1/services/cursor_service/transports/grpc_asyncio.py @@ -317,5 +317,8 @@ def list_partition_cursors( ) return self._stubs["list_partition_cursors"] + def close(self): + return self.grpc_channel.close() + __all__ = ("CursorServiceGrpcAsyncIOTransport",) diff --git a/google/cloud/pubsublite_v1/services/partition_assignment_service/async_client.py b/google/cloud/pubsublite_v1/services/partition_assignment_service/async_client.py index fc6ff05a..0109bd78 100644 --- a/google/cloud/pubsublite_v1/services/partition_assignment_service/async_client.py +++ b/google/cloud/pubsublite_v1/services/partition_assignment_service/async_client.py @@ -228,6 +228,12 @@ def assign_partitions( # 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/pubsublite_v1/services/partition_assignment_service/client.py b/google/cloud/pubsublite_v1/services/partition_assignment_service/client.py index 53a09070..804db7c2 100644 --- a/google/cloud/pubsublite_v1/services/partition_assignment_service/client.py +++ b/google/cloud/pubsublite_v1/services/partition_assignment_service/client.py @@ -331,10 +331,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 assign_partitions( @@ -385,6 +382,19 @@ def assign_partitions( # 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/pubsublite_v1/services/partition_assignment_service/transports/base.py b/google/cloud/pubsublite_v1/services/partition_assignment_service/transports/base.py index ebb7c247..ff39d6d6 100644 --- a/google/cloud/pubsublite_v1/services/partition_assignment_service/transports/base.py +++ b/google/cloud/pubsublite_v1/services/partition_assignment_service/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 assign_partitions( self, diff --git a/google/cloud/pubsublite_v1/services/partition_assignment_service/transports/grpc.py b/google/cloud/pubsublite_v1/services/partition_assignment_service/transports/grpc.py index d7919627..04a60449 100644 --- a/google/cloud/pubsublite_v1/services/partition_assignment_service/transports/grpc.py +++ b/google/cloud/pubsublite_v1/services/partition_assignment_service/transports/grpc.py @@ -262,5 +262,8 @@ def assign_partitions( ) return self._stubs["assign_partitions"] + def close(self): + self.grpc_channel.close() + __all__ = ("PartitionAssignmentServiceGrpcTransport",) diff --git a/google/cloud/pubsublite_v1/services/partition_assignment_service/transports/grpc_asyncio.py b/google/cloud/pubsublite_v1/services/partition_assignment_service/transports/grpc_asyncio.py index e3f68a35..6211f0b3 100644 --- a/google/cloud/pubsublite_v1/services/partition_assignment_service/transports/grpc_asyncio.py +++ b/google/cloud/pubsublite_v1/services/partition_assignment_service/transports/grpc_asyncio.py @@ -268,5 +268,8 @@ def assign_partitions( ) return self._stubs["assign_partitions"] + def close(self): + return self.grpc_channel.close() + __all__ = ("PartitionAssignmentServiceGrpcAsyncIOTransport",) diff --git a/google/cloud/pubsublite_v1/services/publisher_service/async_client.py b/google/cloud/pubsublite_v1/services/publisher_service/async_client.py index 00da9a71..6fe5116a 100644 --- a/google/cloud/pubsublite_v1/services/publisher_service/async_client.py +++ b/google/cloud/pubsublite_v1/services/publisher_service/async_client.py @@ -219,6 +219,12 @@ def publish( # 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/pubsublite_v1/services/publisher_service/client.py b/google/cloud/pubsublite_v1/services/publisher_service/client.py index c5d1e49d..0c6f6fa4 100644 --- a/google/cloud/pubsublite_v1/services/publisher_service/client.py +++ b/google/cloud/pubsublite_v1/services/publisher_service/client.py @@ -332,10 +332,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 publish( @@ -381,6 +378,19 @@ def publish( # 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/pubsublite_v1/services/publisher_service/transports/base.py b/google/cloud/pubsublite_v1/services/publisher_service/transports/base.py index 04d89b03..604580b2 100644 --- a/google/cloud/pubsublite_v1/services/publisher_service/transports/base.py +++ b/google/cloud/pubsublite_v1/services/publisher_service/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 publish( self, diff --git a/google/cloud/pubsublite_v1/services/publisher_service/transports/grpc.py b/google/cloud/pubsublite_v1/services/publisher_service/transports/grpc.py index fd738a38..b15d6129 100644 --- a/google/cloud/pubsublite_v1/services/publisher_service/transports/grpc.py +++ b/google/cloud/pubsublite_v1/services/publisher_service/transports/grpc.py @@ -263,5 +263,8 @@ def publish( ) return self._stubs["publish"] + def close(self): + self.grpc_channel.close() + __all__ = ("PublisherServiceGrpcTransport",) diff --git a/google/cloud/pubsublite_v1/services/publisher_service/transports/grpc_asyncio.py b/google/cloud/pubsublite_v1/services/publisher_service/transports/grpc_asyncio.py index 7da63e06..f036207e 100644 --- a/google/cloud/pubsublite_v1/services/publisher_service/transports/grpc_asyncio.py +++ b/google/cloud/pubsublite_v1/services/publisher_service/transports/grpc_asyncio.py @@ -266,5 +266,8 @@ def publish( ) return self._stubs["publish"] + def close(self): + return self.grpc_channel.close() + __all__ = ("PublisherServiceGrpcAsyncIOTransport",) diff --git a/google/cloud/pubsublite_v1/services/subscriber_service/async_client.py b/google/cloud/pubsublite_v1/services/subscriber_service/async_client.py index dddd1048..ff170718 100644 --- a/google/cloud/pubsublite_v1/services/subscriber_service/async_client.py +++ b/google/cloud/pubsublite_v1/services/subscriber_service/async_client.py @@ -209,6 +209,12 @@ def subscribe( # 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/pubsublite_v1/services/subscriber_service/client.py b/google/cloud/pubsublite_v1/services/subscriber_service/client.py index 129dc01c..152d82d4 100644 --- a/google/cloud/pubsublite_v1/services/subscriber_service/client.py +++ b/google/cloud/pubsublite_v1/services/subscriber_service/client.py @@ -331,10 +331,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 subscribe( @@ -373,6 +370,19 @@ def subscribe( # 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/pubsublite_v1/services/subscriber_service/transports/base.py b/google/cloud/pubsublite_v1/services/subscriber_service/transports/base.py index 01f3f2b0..37b5f8e9 100644 --- a/google/cloud/pubsublite_v1/services/subscriber_service/transports/base.py +++ b/google/cloud/pubsublite_v1/services/subscriber_service/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 subscribe( self, diff --git a/google/cloud/pubsublite_v1/services/subscriber_service/transports/grpc.py b/google/cloud/pubsublite_v1/services/subscriber_service/transports/grpc.py index cf1838b2..f166b907 100644 --- a/google/cloud/pubsublite_v1/services/subscriber_service/transports/grpc.py +++ b/google/cloud/pubsublite_v1/services/subscriber_service/transports/grpc.py @@ -253,5 +253,8 @@ def subscribe( ) return self._stubs["subscribe"] + def close(self): + self.grpc_channel.close() + __all__ = ("SubscriberServiceGrpcTransport",) diff --git a/google/cloud/pubsublite_v1/services/subscriber_service/transports/grpc_asyncio.py b/google/cloud/pubsublite_v1/services/subscriber_service/transports/grpc_asyncio.py index 3cfe2cfe..7bb8bfb0 100644 --- a/google/cloud/pubsublite_v1/services/subscriber_service/transports/grpc_asyncio.py +++ b/google/cloud/pubsublite_v1/services/subscriber_service/transports/grpc_asyncio.py @@ -258,5 +258,8 @@ def subscribe( ) return self._stubs["subscribe"] + def close(self): + return self.grpc_channel.close() + __all__ = ("SubscriberServiceGrpcAsyncIOTransport",) diff --git a/google/cloud/pubsublite_v1/services/topic_stats_service/async_client.py b/google/cloud/pubsublite_v1/services/topic_stats_service/async_client.py index e1bc4d16..7525688c 100644 --- a/google/cloud/pubsublite_v1/services/topic_stats_service/async_client.py +++ b/google/cloud/pubsublite_v1/services/topic_stats_service/async_client.py @@ -321,6 +321,12 @@ async def compute_time_cursor( # 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/pubsublite_v1/services/topic_stats_service/client.py b/google/cloud/pubsublite_v1/services/topic_stats_service/client.py index 4302e21b..acb531bc 100644 --- a/google/cloud/pubsublite_v1/services/topic_stats_service/client.py +++ b/google/cloud/pubsublite_v1/services/topic_stats_service/client.py @@ -349,10 +349,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 compute_message_stats( @@ -515,6 +512,19 @@ def compute_time_cursor( # 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/pubsublite_v1/services/topic_stats_service/transports/base.py b/google/cloud/pubsublite_v1/services/topic_stats_service/transports/base.py index 08f43a84..57bb2035 100644 --- a/google/cloud/pubsublite_v1/services/topic_stats_service/transports/base.py +++ b/google/cloud/pubsublite_v1/services/topic_stats_service/transports/base.py @@ -167,6 +167,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 compute_message_stats( self, diff --git a/google/cloud/pubsublite_v1/services/topic_stats_service/transports/grpc.py b/google/cloud/pubsublite_v1/services/topic_stats_service/transports/grpc.py index fe99e860..af581255 100644 --- a/google/cloud/pubsublite_v1/services/topic_stats_service/transports/grpc.py +++ b/google/cloud/pubsublite_v1/services/topic_stats_service/transports/grpc.py @@ -319,5 +319,8 @@ def compute_time_cursor( ) return self._stubs["compute_time_cursor"] + def close(self): + self.grpc_channel.close() + __all__ = ("TopicStatsServiceGrpcTransport",) diff --git a/google/cloud/pubsublite_v1/services/topic_stats_service/transports/grpc_asyncio.py b/google/cloud/pubsublite_v1/services/topic_stats_service/transports/grpc_asyncio.py index 2ca2fd3d..b17e8220 100644 --- a/google/cloud/pubsublite_v1/services/topic_stats_service/transports/grpc_asyncio.py +++ b/google/cloud/pubsublite_v1/services/topic_stats_service/transports/grpc_asyncio.py @@ -324,5 +324,8 @@ def compute_time_cursor( ) return self._stubs["compute_time_cursor"] + def close(self): + return self.grpc_channel.close() + __all__ = ("TopicStatsServiceGrpcAsyncIOTransport",) diff --git a/google/cloud/pubsublite_v1/types/admin.py b/google/cloud/pubsublite_v1/types/admin.py index 140b0b9c..e4d05f83 100644 --- a/google/cloud/pubsublite_v1/types/admin.py +++ b/google/cloud/pubsublite_v1/types/admin.py @@ -56,6 +56,7 @@ class CreateTopicRequest(proto.Message): r"""Request for CreateTopic. + Attributes: parent (str): Required. The parent location in which to create the topic. @@ -78,6 +79,7 @@ class CreateTopicRequest(proto.Message): class GetTopicRequest(proto.Message): r"""Request for GetTopic. + Attributes: name (str): Required. The name of the topic whose @@ -89,6 +91,7 @@ class GetTopicRequest(proto.Message): class GetTopicPartitionsRequest(proto.Message): r"""Request for GetTopicPartitions. + Attributes: name (str): Required. The topic whose partition @@ -100,6 +103,7 @@ class GetTopicPartitionsRequest(proto.Message): class TopicPartitions(proto.Message): r"""Response for GetTopicPartitions. + Attributes: partition_count (int): The number of partitions in the topic. @@ -110,6 +114,7 @@ class TopicPartitions(proto.Message): class ListTopicsRequest(proto.Message): r"""Request for ListTopics. + Attributes: parent (str): Required. The parent whose topics are to be listed. @@ -136,6 +141,7 @@ class ListTopicsRequest(proto.Message): class ListTopicsResponse(proto.Message): r"""Response for ListTopics. + Attributes: topics (Sequence[google.cloud.pubsublite_v1.types.Topic]): The list of topic in the requested parent. @@ -156,6 +162,7 @@ def raw_page(self): class UpdateTopicRequest(proto.Message): r"""Request for UpdateTopic. + Attributes: topic (google.cloud.pubsublite_v1.types.Topic): Required. The topic to update. Its ``name`` field must be @@ -173,6 +180,7 @@ class UpdateTopicRequest(proto.Message): class DeleteTopicRequest(proto.Message): r"""Request for DeleteTopic. + Attributes: name (str): Required. The name of the topic to delete. @@ -183,6 +191,7 @@ class DeleteTopicRequest(proto.Message): class ListTopicSubscriptionsRequest(proto.Message): r"""Request for ListTopicSubscriptions. + Attributes: name (str): Required. The name of the topic whose @@ -209,6 +218,7 @@ class ListTopicSubscriptionsRequest(proto.Message): class ListTopicSubscriptionsResponse(proto.Message): r"""Response for ListTopicSubscriptions. + Attributes: subscriptions (Sequence[str]): The names of subscriptions attached to the @@ -230,6 +240,7 @@ def raw_page(self): class CreateSubscriptionRequest(proto.Message): r"""Request for CreateSubscription. + Attributes: parent (str): Required. The parent location in which to create the @@ -259,6 +270,7 @@ class CreateSubscriptionRequest(proto.Message): class GetSubscriptionRequest(proto.Message): r"""Request for GetSubscription. + Attributes: name (str): Required. The name of the subscription whose @@ -270,6 +282,7 @@ class GetSubscriptionRequest(proto.Message): class ListSubscriptionsRequest(proto.Message): r"""Request for ListSubscriptions. + Attributes: parent (str): Required. The parent whose subscriptions are to be listed. @@ -296,6 +309,7 @@ class ListSubscriptionsRequest(proto.Message): class ListSubscriptionsResponse(proto.Message): r"""Response for ListSubscriptions. + Attributes: subscriptions (Sequence[google.cloud.pubsublite_v1.types.Subscription]): The list of subscriptions in the requested @@ -319,6 +333,7 @@ def raw_page(self): class UpdateSubscriptionRequest(proto.Message): r"""Request for UpdateSubscription. + Attributes: subscription (google.cloud.pubsublite_v1.types.Subscription): Required. The subscription to update. Its ``name`` field @@ -336,6 +351,7 @@ class UpdateSubscriptionRequest(proto.Message): class DeleteSubscriptionRequest(proto.Message): r"""Request for DeleteSubscription. + Attributes: name (str): Required. The name of the subscription to @@ -347,6 +363,7 @@ class DeleteSubscriptionRequest(proto.Message): class SeekSubscriptionRequest(proto.Message): r"""Request for SeekSubscription. + Attributes: name (str): Required. The name of the subscription to @@ -376,11 +393,13 @@ class NamedTarget(proto.Enum): class SeekSubscriptionResponse(proto.Message): - r"""Response for SeekSubscription long running operation. """ + r"""Response for SeekSubscription long running operation. + """ class OperationMetadata(proto.Message): r"""Metadata for long running operations. + Attributes: create_time (google.protobuf.timestamp_pb2.Timestamp): The time the operation was created. @@ -404,6 +423,7 @@ class OperationMetadata(proto.Message): class CreateReservationRequest(proto.Message): r"""Request for CreateReservation. + Attributes: parent (str): Required. The parent location in which to create the @@ -426,6 +446,7 @@ class CreateReservationRequest(proto.Message): class GetReservationRequest(proto.Message): r"""Request for GetReservation. + Attributes: name (str): Required. The name of the reservation whose configuration to @@ -438,6 +459,7 @@ class GetReservationRequest(proto.Message): class ListReservationsRequest(proto.Message): r"""Request for ListReservations. + Attributes: parent (str): Required. The parent whose reservations are to be listed. @@ -464,6 +486,7 @@ class ListReservationsRequest(proto.Message): class ListReservationsResponse(proto.Message): r"""Response for ListReservations. + Attributes: reservations (Sequence[google.cloud.pubsublite_v1.types.Reservation]): The list of reservation in the requested @@ -487,6 +510,7 @@ def raw_page(self): class UpdateReservationRequest(proto.Message): r"""Request for UpdateReservation. + Attributes: reservation (google.cloud.pubsublite_v1.types.Reservation): Required. The reservation to update. Its ``name`` field must @@ -504,6 +528,7 @@ class UpdateReservationRequest(proto.Message): class DeleteReservationRequest(proto.Message): r"""Request for DeleteReservation. + Attributes: name (str): Required. The name of the reservation to delete. Structured @@ -516,6 +541,7 @@ class DeleteReservationRequest(proto.Message): class ListReservationTopicsRequest(proto.Message): r"""Request for ListReservationTopics. + Attributes: name (str): Required. The name of the reservation whose topics to list. @@ -543,6 +569,7 @@ class ListReservationTopicsRequest(proto.Message): class ListReservationTopicsResponse(proto.Message): r"""Response for ListReservationTopics. + Attributes: topics (Sequence[str]): The names of topics attached to the diff --git a/google/cloud/pubsublite_v1/types/cursor.py b/google/cloud/pubsublite_v1/types/cursor.py index 435336c4..72a47ad6 100644 --- a/google/cloud/pubsublite_v1/types/cursor.py +++ b/google/cloud/pubsublite_v1/types/cursor.py @@ -56,7 +56,8 @@ class InitialCommitCursorRequest(proto.Message): class InitialCommitCursorResponse(proto.Message): - r"""Response to an InitialCommitCursorRequest. """ + r"""Response to an InitialCommitCursorRequest. + """ class SequencedCommitCursorRequest(proto.Message): @@ -73,6 +74,7 @@ class SequencedCommitCursorRequest(proto.Message): class SequencedCommitCursorResponse(proto.Message): r"""Response to a SequencedCommitCursorRequest. + Attributes: acknowledged_commits (int): The number of outstanding @@ -87,6 +89,7 @@ class SequencedCommitCursorResponse(proto.Message): class StreamingCommitCursorRequest(proto.Message): r"""A request sent from the client to the server on a stream. + Attributes: initial (google.cloud.pubsublite_v1.types.InitialCommitCursorRequest): Initial request on the stream. @@ -107,6 +110,7 @@ class StreamingCommitCursorRequest(proto.Message): class StreamingCommitCursorResponse(proto.Message): r"""Response to a StreamingCommitCursorRequest. + Attributes: initial (google.cloud.pubsublite_v1.types.InitialCommitCursorResponse): Initial response on the stream. @@ -127,6 +131,7 @@ class StreamingCommitCursorResponse(proto.Message): class CommitCursorRequest(proto.Message): r"""Request for CommitCursor. + Attributes: subscription (str): The subscription for which to update the @@ -145,11 +150,13 @@ class CommitCursorRequest(proto.Message): class CommitCursorResponse(proto.Message): - r"""Response for CommitCursor. """ + r"""Response for CommitCursor. + """ class ListPartitionCursorsRequest(proto.Message): r"""Request for ListPartitionCursors. + Attributes: parent (str): Required. The subscription for which to retrieve cursors. @@ -177,6 +184,7 @@ class ListPartitionCursorsRequest(proto.Message): class PartitionCursor(proto.Message): r"""A pair of a Cursor and the partition it is for. + Attributes: partition (int): The partition this is for. @@ -190,6 +198,7 @@ class PartitionCursor(proto.Message): class ListPartitionCursorsResponse(proto.Message): r"""Response for ListPartitionCursors + Attributes: partition_cursors (Sequence[google.cloud.pubsublite_v1.types.PartitionCursor]): The partition cursors from this request. diff --git a/google/cloud/pubsublite_v1/types/publisher.py b/google/cloud/pubsublite_v1/types/publisher.py index 0e6730c8..b48a7074 100644 --- a/google/cloud/pubsublite_v1/types/publisher.py +++ b/google/cloud/pubsublite_v1/types/publisher.py @@ -33,6 +33,7 @@ class InitialPublishRequest(proto.Message): r"""The first request that must be sent on a newly-opened stream. + Attributes: topic (str): The topic to which messages will be written. @@ -47,11 +48,13 @@ class InitialPublishRequest(proto.Message): class InitialPublishResponse(proto.Message): - r"""Response to an InitialPublishRequest. """ + r"""Response to an InitialPublishRequest. + """ class MessagePublishRequest(proto.Message): r"""Request to publish messages to the topic. + Attributes: messages (Sequence[google.cloud.pubsublite_v1.types.PubSubMessage]): The messages to publish. @@ -64,6 +67,7 @@ class MessagePublishRequest(proto.Message): class MessagePublishResponse(proto.Message): r"""Response to a MessagePublishRequest. + Attributes: start_cursor (google.cloud.pubsublite_v1.types.Cursor): The cursor of the first published message in @@ -77,6 +81,7 @@ class MessagePublishResponse(proto.Message): class PublishRequest(proto.Message): r"""Request sent from the client to the server on a stream. + Attributes: initial_request (google.cloud.pubsublite_v1.types.InitialPublishRequest): Initial request on the stream. @@ -94,6 +99,7 @@ class PublishRequest(proto.Message): class PublishResponse(proto.Message): r"""Response to a PublishRequest. + Attributes: initial_response (google.cloud.pubsublite_v1.types.InitialPublishResponse): Initial response on the stream. diff --git a/google/cloud/pubsublite_v1/types/subscriber.py b/google/cloud/pubsublite_v1/types/subscriber.py index a815eb5f..645842ae 100644 --- a/google/cloud/pubsublite_v1/types/subscriber.py +++ b/google/cloud/pubsublite_v1/types/subscriber.py @@ -64,6 +64,7 @@ class InitialSubscribeRequest(proto.Message): class InitialSubscribeResponse(proto.Message): r"""Response to an InitialSubscribeRequest. + Attributes: cursor (google.cloud.pubsublite_v1.types.Cursor): The cursor from which the subscriber will @@ -107,6 +108,7 @@ class NamedTarget(proto.Enum): class SeekResponse(proto.Message): r"""Response to a SeekRequest. + Attributes: cursor (google.cloud.pubsublite_v1.types.Cursor): The new delivery cursor for the current @@ -135,6 +137,7 @@ class FlowControlRequest(proto.Message): class SubscribeRequest(proto.Message): r"""A request sent from the client to the server on a stream. + Attributes: initial (google.cloud.pubsublite_v1.types.InitialSubscribeRequest): Initial request on the stream. @@ -175,6 +178,7 @@ class MessageResponse(proto.Message): class SubscribeResponse(proto.Message): r"""Response to SubscribeRequest. + Attributes: initial (google.cloud.pubsublite_v1.types.InitialSubscribeResponse): Initial response on the stream. @@ -244,11 +248,13 @@ class PartitionAssignmentAck(proto.Message): assignment, partitions may remain unassigned for a period of time until the client is known to be inactive, after which time the server will break the stream. - """ + + """ class PartitionAssignmentRequest(proto.Message): r"""A request on the PartitionAssignment stream. + Attributes: initial (google.cloud.pubsublite_v1.types.InitialPartitionAssignmentRequest): Initial request on the stream. diff --git a/google/cloud/pubsublite_v1/types/topic_stats.py b/google/cloud/pubsublite_v1/types/topic_stats.py index 7cbc3d5b..a8517348 100644 --- a/google/cloud/pubsublite_v1/types/topic_stats.py +++ b/google/cloud/pubsublite_v1/types/topic_stats.py @@ -94,6 +94,7 @@ class ComputeMessageStatsResponse(proto.Message): class ComputeHeadCursorRequest(proto.Message): r"""Compute the current head cursor for a partition. + Attributes: topic (str): Required. The topic for which we should diff --git a/tests/unit/gapic/pubsublite_v1/test_admin_service.py b/tests/unit/gapic/pubsublite_v1/test_admin_service.py index 95cc1f76..5f8bb8d0 100644 --- a/tests/unit/gapic/pubsublite_v1/test_admin_service.py +++ b/tests/unit/gapic/pubsublite_v1/test_admin_service.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.pubsublite_v1.services.admin_service import AdminServiceAsyncClient @@ -5142,6 +5143,9 @@ def test_admin_service_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): @@ -5692,3 +5696,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 = AdminServiceAsyncClient( + 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 = AdminServiceClient( + 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 = AdminServiceClient( + 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/pubsublite_v1/test_cursor_service.py b/tests/unit/gapic/pubsublite_v1/test_cursor_service.py index e43737af..f6b343ac 100644 --- a/tests/unit/gapic/pubsublite_v1/test_cursor_service.py +++ b/tests/unit/gapic/pubsublite_v1/test_cursor_service.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.pubsublite_v1.services.cursor_service import CursorServiceAsyncClient @@ -1183,6 +1184,9 @@ def test_cursor_service_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_cursor_service_base_transport_with_credentials_file(): @@ -1663,3 +1667,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 = CursorServiceAsyncClient( + 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 = CursorServiceClient( + 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 = CursorServiceClient( + 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/pubsublite_v1/test_partition_assignment_service.py b/tests/unit/gapic/pubsublite_v1/test_partition_assignment_service.py index 9da6bc5f..9c8a4905 100644 --- a/tests/unit/gapic/pubsublite_v1/test_partition_assignment_service.py +++ b/tests/unit/gapic/pubsublite_v1/test_partition_assignment_service.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.pubsublite_v1.services.partition_assignment_service import ( @@ -706,6 +707,9 @@ def test_partition_assignment_service_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_partition_assignment_service_base_transport_with_credentials_file(): @@ -1172,3 +1176,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 = PartitionAssignmentServiceAsyncClient( + 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 = PartitionAssignmentServiceClient( + 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 = PartitionAssignmentServiceClient( + 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/pubsublite_v1/test_publisher_service.py b/tests/unit/gapic/pubsublite_v1/test_publisher_service.py index b56e0efc..057eabe3 100644 --- a/tests/unit/gapic/pubsublite_v1/test_publisher_service.py +++ b/tests/unit/gapic/pubsublite_v1/test_publisher_service.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.pubsublite_v1.services.publisher_service import ( @@ -676,6 +677,9 @@ def test_publisher_service_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_publisher_service_base_transport_with_credentials_file(): @@ -1134,3 +1138,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 = PublisherServiceAsyncClient( + 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 = PublisherServiceClient( + 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 = PublisherServiceClient( + 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/pubsublite_v1/test_subscriber_service.py b/tests/unit/gapic/pubsublite_v1/test_subscriber_service.py index 2cf97b42..2806922e 100644 --- a/tests/unit/gapic/pubsublite_v1/test_subscriber_service.py +++ b/tests/unit/gapic/pubsublite_v1/test_subscriber_service.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.pubsublite_v1.services.subscriber_service import ( @@ -677,6 +678,9 @@ def test_subscriber_service_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_subscriber_service_base_transport_with_credentials_file(): @@ -1135,3 +1139,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 = SubscriberServiceAsyncClient( + 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 = SubscriberServiceClient( + 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 = SubscriberServiceClient( + 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/pubsublite_v1/test_topic_stats_service.py b/tests/unit/gapic/pubsublite_v1/test_topic_stats_service.py index 50ed6df5..834c6e03 100644 --- a/tests/unit/gapic/pubsublite_v1/test_topic_stats_service.py +++ b/tests/unit/gapic/pubsublite_v1/test_topic_stats_service.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.pubsublite_v1.services.topic_stats_service import ( @@ -1049,6 +1050,9 @@ def test_topic_stats_service_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_topic_stats_service_base_transport_with_credentials_file(): @@ -1533,3 +1537,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 = TopicStatsServiceAsyncClient( + 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 = TopicStatsServiceClient( + 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 = TopicStatsServiceClient( + 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()