From 953ddb176f7e21fe48ea037c72237da39aadc1b7 Mon Sep 17 00:00:00 2001 From: ohmayr Date: Thu, 6 Mar 2025 03:47:53 +0500 Subject: [PATCH] chore(revert): Revert "fix: get channel target for a gRPC request" (#1371) --- .../publisher/test_publisher_client.py | 32 ++++++++----------- .../subscriber/test_subscriber_client.py | 32 ++++++++----------- 2 files changed, 26 insertions(+), 38 deletions(-) diff --git a/tests/unit/pubsub_v1/publisher/test_publisher_client.py b/tests/unit/pubsub_v1/publisher/test_publisher_client.py index 1e1cc61b3..d1b7d4a81 100644 --- a/tests/unit/pubsub_v1/publisher/test_publisher_client.py +++ b/tests/unit/pubsub_v1/publisher/test_publisher_client.py @@ -57,12 +57,16 @@ typed_flaky = cast(Callable[[C], C], flaky(max_runs=5, min_passes=1)) -# NOTE: This interceptor is required to create an intercept channel. -class _PublisherClientGrpcInterceptor( - grpc.UnaryUnaryClientInterceptor, -): - def intercept_unary_unary(self, continuation, client_call_details, request): - pass +# Attempt to use `_thunk` to obtain the underlying grpc channel from +# the intercept channel. Default to obtaining the grpc channel directly +# for backwards compatibility. +# TODO(https://github.com/grpc/grpc/issues/38519): Workaround to obtain a channel +# until a public API is available. +def get_publish_channel(client): + try: + return client._transport.publish._thunk("")._channel + except AttributeError: + return client._transport.publish._channel def _assert_retries_equal(retry, retry2): @@ -424,27 +428,17 @@ def init(self, *args, **kwargs): assert client.transport._ssl_channel_credentials == mock_ssl_creds -def test_init_emulator(monkeypatch, creds): +def test_init_emulator(monkeypatch): monkeypatch.setenv("PUBSUB_EMULATOR_HOST", "/foo/bar:123") # NOTE: When the emulator host is set, a custom channel will be used, so # no credentials (mock ot otherwise) can be passed in. - - # TODO(https://github.com/grpc/grpc/issues/38519): Workaround to create an intercept - # channel (for forwards compatibility) with a channel created by the publisher client - # where target is set to the emulator host. - channel = publisher.Client().transport.grpc_channel - interceptor = _PublisherClientGrpcInterceptor() - intercept_channel = grpc.intercept_channel(channel, interceptor) - transport = publisher.Client.get_transport_class("grpc")( - credentials=creds, channel=intercept_channel - ) - client = publisher.Client(transport=transport) + client = publisher.Client() # Establish that a gRPC request would attempt to hit the emulator host. # # Sadly, there seems to be no good way to do this without poking at # the private API of gRPC. - channel = client._transport.publish._thunk("")._channel + channel = get_publish_channel(client) # Behavior to include dns prefix changed in gRPCv1.63 grpc_major, grpc_minor = [int(part) for part in grpc.__version__.split(".")[0:2]] if grpc_major > 1 or (grpc_major == 1 and grpc_minor >= 63): diff --git a/tests/unit/pubsub_v1/subscriber/test_subscriber_client.py b/tests/unit/pubsub_v1/subscriber/test_subscriber_client.py index 4b381245d..3d3ff0111 100644 --- a/tests/unit/pubsub_v1/subscriber/test_subscriber_client.py +++ b/tests/unit/pubsub_v1/subscriber/test_subscriber_client.py @@ -36,12 +36,16 @@ from google.pubsub_v1.types import PubsubMessage -# NOTE: This interceptor is required to create an intercept channel. -class _SubscriberClientGrpcInterceptor( - grpc.UnaryUnaryClientInterceptor, -): - def intercept_unary_unary(self, continuation, client_call_details, request): - pass +# Attempt to use `_thunk` to obtain the underlying grpc channel from +# the intercept channel. Default to obtaining the grpc channel directly +# for backwards compatibility. +# TODO(https://github.com/grpc/grpc/issues/38519): Workaround to obtain a channel +# until a public API is available. +def get_pull_channel(client): + try: + return client._transport.pull._thunk("")._channel + except AttributeError: + return client._transport.pull._channel def test_init_default_client_info(creds): @@ -127,27 +131,17 @@ def init(self, *args, **kwargs): assert client.transport._ssl_channel_credentials == mock_ssl_creds -def test_init_emulator(monkeypatch, creds): +def test_init_emulator(monkeypatch): monkeypatch.setenv("PUBSUB_EMULATOR_HOST", "/baz/bacon:123") # NOTE: When the emulator host is set, a custom channel will be used, so # no credentials (mock ot otherwise) can be passed in. - - # TODO(https://github.com/grpc/grpc/issues/38519): Workaround to create an intercept - # channel (for forwards compatibility) with a channel created by the publisher client - # where target is set to the emulator host. - channel = subscriber.Client().transport.grpc_channel - interceptor = _SubscriberClientGrpcInterceptor() - intercept_channel = grpc.intercept_channel(channel, interceptor) - transport = subscriber.Client.get_transport_class("grpc")( - credentials=creds, channel=intercept_channel - ) - client = subscriber.Client(transport=transport) + client = subscriber.Client() # Establish that a gRPC request would attempt to hit the emulator host. # # Sadly, there seems to be no good way to do this without poking at # the private API of gRPC. - channel = client._transport.pull._thunk("")._channel + channel = get_pull_channel(client) # Behavior to include dns prefix changed in gRPCv1.63 grpc_major, grpc_minor = [int(part) for part in grpc.__version__.split(".")[0:2]] if grpc_major > 1 or (grpc_major == 1 and grpc_minor >= 63):