diff --git a/.github/.OwlBot.lock.yaml b/.github/.OwlBot.lock.yaml index ee94722..7d98291 100644 --- a/.github/.OwlBot.lock.yaml +++ b/.github/.OwlBot.lock.yaml @@ -1,3 +1,3 @@ docker: image: gcr.io/cloud-devrel-public-resources/owlbot-python:latest - digest: sha256:6e7328583be8edd3ba8f35311c76a1ecbc823010279ccb6ab46b7a76e25eafcc + digest: sha256:58f73ba196b5414782605236dd0712a73541b44ff2ff4d3a36ec41092dd6fa5b diff --git a/.kokoro/samples/python3.10/common.cfg b/.kokoro/samples/python3.10/common.cfg new file mode 100644 index 0000000..5cd7df8 --- /dev/null +++ b/.kokoro/samples/python3.10/common.cfg @@ -0,0 +1,40 @@ +# Format: //devtools/kokoro/config/proto/build.proto + +# Build logs will be here +action { + define_artifacts { + regex: "**/*sponge_log.xml" + } +} + +# Specify which tests to run +env_vars: { + key: "RUN_TESTS_SESSION" + value: "py-3.10" +} + +# Declare build specific Cloud project. +env_vars: { + key: "BUILD_SPECIFIC_GCLOUD_PROJECT" + value: "python-docs-samples-tests-310" +} + +env_vars: { + key: "TRAMPOLINE_BUILD_FILE" + value: "github/python-debugger-client/.kokoro/test-samples.sh" +} + +# Configure the docker image for kokoro-trampoline. +env_vars: { + key: "TRAMPOLINE_IMAGE" + value: "gcr.io/cloud-devrel-kokoro-resources/python-samples-testing-docker" +} + +# Download secrets for samples +gfile_resources: "/bigstore/cloud-devrel-kokoro-resources/python-docs-samples" + +# Download trampoline resources. +gfile_resources: "/bigstore/cloud-devrel-kokoro-resources/trampoline" + +# Use the trampoline script to run in docker. +build_file: "python-debugger-client/.kokoro/trampoline_v2.sh" \ No newline at end of file diff --git a/.kokoro/samples/python3.10/continuous.cfg b/.kokoro/samples/python3.10/continuous.cfg new file mode 100644 index 0000000..a1c8d97 --- /dev/null +++ b/.kokoro/samples/python3.10/continuous.cfg @@ -0,0 +1,6 @@ +# Format: //devtools/kokoro/config/proto/build.proto + +env_vars: { + key: "INSTALL_LIBRARY_FROM_SOURCE" + value: "True" +} \ No newline at end of file diff --git a/.kokoro/samples/python3.10/periodic-head.cfg b/.kokoro/samples/python3.10/periodic-head.cfg new file mode 100644 index 0000000..846320e --- /dev/null +++ b/.kokoro/samples/python3.10/periodic-head.cfg @@ -0,0 +1,11 @@ +# Format: //devtools/kokoro/config/proto/build.proto + +env_vars: { + key: "INSTALL_LIBRARY_FROM_SOURCE" + value: "True" +} + +env_vars: { + key: "TRAMPOLINE_BUILD_FILE" + value: "github/python-debugger-client/.kokoro/test-samples-against-head.sh" +} diff --git a/.kokoro/samples/python3.10/periodic.cfg b/.kokoro/samples/python3.10/periodic.cfg new file mode 100644 index 0000000..71cd1e5 --- /dev/null +++ b/.kokoro/samples/python3.10/periodic.cfg @@ -0,0 +1,6 @@ +# Format: //devtools/kokoro/config/proto/build.proto + +env_vars: { + key: "INSTALL_LIBRARY_FROM_SOURCE" + value: "False" +} diff --git a/.kokoro/samples/python3.10/presubmit.cfg b/.kokoro/samples/python3.10/presubmit.cfg new file mode 100644 index 0000000..a1c8d97 --- /dev/null +++ b/.kokoro/samples/python3.10/presubmit.cfg @@ -0,0 +1,6 @@ +# Format: //devtools/kokoro/config/proto/build.proto + +env_vars: { + key: "INSTALL_LIBRARY_FROM_SOURCE" + value: "True" +} \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 9ec76ab..222ed8f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Changelog +## [1.1.0](https://www.github.com/googleapis/python-debugger-client/compare/v1.0.2...v1.1.0) (2021-10-08) + + +### Features + +* add context manager support in client ([#38](https://www.github.com/googleapis/python-debugger-client/issues/38)) ([d8ef19f](https://www.github.com/googleapis/python-debugger-client/commit/d8ef19fdee913a1b8988fd54938bf2b8f4b11233)) + ### [1.0.2](https://www.github.com/googleapis/python-debugger-client/compare/v1.0.1...v1.0.2) (2021-10-07) diff --git a/CONTRIBUTING.rst b/CONTRIBUTING.rst index 7319a38..a1476f4 100644 --- a/CONTRIBUTING.rst +++ b/CONTRIBUTING.rst @@ -22,7 +22,7 @@ In order to add a feature: documentation. - The feature must work fully on the following CPython versions: - 3.6, 3.7, 3.8 and 3.9 on both UNIX and Windows. + 3.6, 3.7, 3.8, 3.9 and 3.10 on both UNIX and Windows. - The feature must not add unnecessary dependencies (where "unnecessary" is of course subjective, but new dependencies should @@ -72,7 +72,7 @@ We use `nox `__ to instrument our tests. - To run a single unit test:: - $ nox -s unit-3.9 -- -k + $ nox -s unit-3.10 -- -k .. note:: @@ -225,11 +225,13 @@ We support: - `Python 3.7`_ - `Python 3.8`_ - `Python 3.9`_ +- `Python 3.10`_ .. _Python 3.6: https://docs.python.org/3.6/ .. _Python 3.7: https://docs.python.org/3.7/ .. _Python 3.8: https://docs.python.org/3.8/ .. _Python 3.9: https://docs.python.org/3.9/ +.. _Python 3.10: https://docs.python.org/3.10/ Supported versions can be found in our ``noxfile.py`` `config`_. diff --git a/google/cloud/debugger_v2/services/controller2/async_client.py b/google/cloud/debugger_v2/services/controller2/async_client.py index 2e81ac8..2f7ad87 100644 --- a/google/cloud/debugger_v2/services/controller2/async_client.py +++ b/google/cloud/debugger_v2/services/controller2/async_client.py @@ -431,6 +431,12 @@ async def update_active_breakpoint( # 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/debugger_v2/services/controller2/client.py b/google/cloud/debugger_v2/services/controller2/client.py index 6528b1b..ce38144 100644 --- a/google/cloud/debugger_v2/services/controller2/client.py +++ b/google/cloud/debugger_v2/services/controller2/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 register_debuggee( @@ -588,6 +585,19 @@ def update_active_breakpoint( # 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/debugger_v2/services/controller2/transports/base.py b/google/cloud/debugger_v2/services/controller2/transports/base.py index ffcdc9c..317a320 100644 --- a/google/cloud/debugger_v2/services/controller2/transports/base.py +++ b/google/cloud/debugger_v2/services/controller2/transports/base.py @@ -192,6 +192,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 register_debuggee( self, diff --git a/google/cloud/debugger_v2/services/controller2/transports/grpc.py b/google/cloud/debugger_v2/services/controller2/transports/grpc.py index b17fe89..eb82c5f 100644 --- a/google/cloud/debugger_v2/services/controller2/transports/grpc.py +++ b/google/cloud/debugger_v2/services/controller2/transports/grpc.py @@ -364,5 +364,8 @@ def update_active_breakpoint( ) return self._stubs["update_active_breakpoint"] + def close(self): + self.grpc_channel.close() + __all__ = ("Controller2GrpcTransport",) diff --git a/google/cloud/debugger_v2/services/controller2/transports/grpc_asyncio.py b/google/cloud/debugger_v2/services/controller2/transports/grpc_asyncio.py index 214a5f2..16f8839 100644 --- a/google/cloud/debugger_v2/services/controller2/transports/grpc_asyncio.py +++ b/google/cloud/debugger_v2/services/controller2/transports/grpc_asyncio.py @@ -368,5 +368,8 @@ def update_active_breakpoint( ) return self._stubs["update_active_breakpoint"] + def close(self): + return self.grpc_channel.close() + __all__ = ("Controller2GrpcAsyncIOTransport",) diff --git a/google/cloud/debugger_v2/services/debugger2/async_client.py b/google/cloud/debugger_v2/services/debugger2/async_client.py index 56a2c1f..b65ef52 100644 --- a/google/cloud/debugger_v2/services/debugger2/async_client.py +++ b/google/cloud/debugger_v2/services/debugger2/async_client.py @@ -599,6 +599,12 @@ async def list_debuggees( # 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/debugger_v2/services/debugger2/client.py b/google/cloud/debugger_v2/services/debugger2/client.py index 9cd0fdf..7037fc4 100644 --- a/google/cloud/debugger_v2/services/debugger2/client.py +++ b/google/cloud/debugger_v2/services/debugger2/client.py @@ -339,10 +339,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 set_breakpoint( @@ -738,6 +735,19 @@ def list_debuggees( # 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/debugger_v2/services/debugger2/transports/base.py b/google/cloud/debugger_v2/services/debugger2/transports/base.py index 03adca6..4f95b3f 100644 --- a/google/cloud/debugger_v2/services/debugger2/transports/base.py +++ b/google/cloud/debugger_v2/services/debugger2/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 set_breakpoint( self, diff --git a/google/cloud/debugger_v2/services/debugger2/transports/grpc.py b/google/cloud/debugger_v2/services/debugger2/transports/grpc.py index 448a33e..3015b8f 100644 --- a/google/cloud/debugger_v2/services/debugger2/transports/grpc.py +++ b/google/cloud/debugger_v2/services/debugger2/transports/grpc.py @@ -368,5 +368,8 @@ def list_debuggees( ) return self._stubs["list_debuggees"] + def close(self): + self.grpc_channel.close() + __all__ = ("Debugger2GrpcTransport",) diff --git a/google/cloud/debugger_v2/services/debugger2/transports/grpc_asyncio.py b/google/cloud/debugger_v2/services/debugger2/transports/grpc_asyncio.py index 5145d29..47b39fc 100644 --- a/google/cloud/debugger_v2/services/debugger2/transports/grpc_asyncio.py +++ b/google/cloud/debugger_v2/services/debugger2/transports/grpc_asyncio.py @@ -379,5 +379,8 @@ def list_debuggees( ) return self._stubs["list_debuggees"] + def close(self): + return self.grpc_channel.close() + __all__ = ("Debugger2GrpcAsyncIOTransport",) diff --git a/google/cloud/debugger_v2/types/controller.py b/google/cloud/debugger_v2/types/controller.py index 9c653aa..0e10db8 100644 --- a/google/cloud/debugger_v2/types/controller.py +++ b/google/cloud/debugger_v2/types/controller.py @@ -33,6 +33,7 @@ class RegisterDebuggeeRequest(proto.Message): r"""Request to register a debuggee. + Attributes: debuggee (google.cloud.debugger_v2.types.Debuggee): Required. Debuggee information to register. The fields @@ -45,6 +46,7 @@ class RegisterDebuggeeRequest(proto.Message): class RegisterDebuggeeResponse(proto.Message): r"""Response for registering a debuggee. + Attributes: debuggee (google.cloud.debugger_v2.types.Debuggee): Debuggee resource. The field ``id`` is guaranteed to be set @@ -60,6 +62,7 @@ class RegisterDebuggeeResponse(proto.Message): class ListActiveBreakpointsRequest(proto.Message): r"""Request to list active breakpoints. + Attributes: debuggee_id (str): Required. Identifies the debuggee. @@ -87,6 +90,7 @@ class ListActiveBreakpointsRequest(proto.Message): class ListActiveBreakpointsResponse(proto.Message): r"""Response for listing active breakpoints. + Attributes: breakpoints (Sequence[google.cloud.debugger_v2.types.Breakpoint]): List of all active breakpoints. The fields ``id`` and @@ -109,6 +113,7 @@ class ListActiveBreakpointsResponse(proto.Message): class UpdateActiveBreakpointRequest(proto.Message): r"""Request to update an active breakpoint. + Attributes: debuggee_id (str): Required. Identifies the debuggee being @@ -126,7 +131,8 @@ class UpdateActiveBreakpointRequest(proto.Message): class UpdateActiveBreakpointResponse(proto.Message): r"""Response for updating an active breakpoint. The message is defined to allow future extensions. - """ + + """ __all__ = tuple(sorted(__protobuf__.manifest)) diff --git a/google/cloud/debugger_v2/types/debugger.py b/google/cloud/debugger_v2/types/debugger.py index 993160d..281d2a7 100644 --- a/google/cloud/debugger_v2/types/debugger.py +++ b/google/cloud/debugger_v2/types/debugger.py @@ -35,7 +35,7 @@ class SetBreakpointRequest(proto.Message): - r"""Request to set a breakpoin. + r"""Request to set a breakpoint Attributes: debuggee_id (str): diff --git a/noxfile.py b/noxfile.py index 93ea9b1..f885c68 100644 --- a/noxfile.py +++ b/noxfile.py @@ -29,7 +29,7 @@ DEFAULT_PYTHON_VERSION = "3.8" SYSTEM_TEST_PYTHON_VERSIONS = ["3.8"] -UNIT_TEST_PYTHON_VERSIONS = ["3.6", "3.7", "3.8", "3.9"] +UNIT_TEST_PYTHON_VERSIONS = ["3.6", "3.7", "3.8", "3.9", "3.10"] CURRENT_DIRECTORY = pathlib.Path(__file__).parent.absolute() diff --git a/setup.py b/setup.py index f79d546..7a4f9aa 100644 --- a/setup.py +++ b/setup.py @@ -21,7 +21,7 @@ name = "google-cloud-debugger-client" description = "Cloud Debugger API client library" -version = "1.0.2" +version = "1.1.0" release_status = "Development Status :: 5 - Production/Stable" url = "https://github.com/googleapis/python-debugger-client" dependencies = [ diff --git a/tests/unit/gapic/debugger_v2/test_controller2.py b/tests/unit/gapic/debugger_v2/test_controller2.py index b34526e..7cd23ea 100644 --- a/tests/unit/gapic/debugger_v2/test_controller2.py +++ b/tests/unit/gapic/debugger_v2/test_controller2.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.debugger_v2.services.controller2 import Controller2AsyncClient @@ -1069,6 +1070,9 @@ def test_controller2_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_controller2_base_transport_with_credentials_file(): @@ -1531,3 +1535,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 = Controller2AsyncClient( + 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 = Controller2Client( + 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 = Controller2Client( + 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/debugger_v2/test_debugger2.py b/tests/unit/gapic/debugger_v2/test_debugger2.py index 454f151..ad935b1 100644 --- a/tests/unit/gapic/debugger_v2/test_debugger2.py +++ b/tests/unit/gapic/debugger_v2/test_debugger2.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.debugger_v2.services.debugger2 import Debugger2AsyncClient @@ -1382,6 +1383,9 @@ def test_debugger2_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_debugger2_base_transport_with_credentials_file(): @@ -1844,3 +1848,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 = Debugger2AsyncClient( + 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 = Debugger2Client( + 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 = Debugger2Client( + 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()