Skip to content
This repository was archived by the owner on Mar 9, 2026. It is now read-only.

Commit 953ddb1

Browse files
authored
chore(revert): Revert "fix: get channel target for a gRPC request" (#1371)
1 parent 06be31a commit 953ddb1

File tree

2 files changed

+26
-38
lines changed

2 files changed

+26
-38
lines changed

tests/unit/pubsub_v1/publisher/test_publisher_client.py

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,16 @@
5757
typed_flaky = cast(Callable[[C], C], flaky(max_runs=5, min_passes=1))
5858

5959

60-
# NOTE: This interceptor is required to create an intercept channel.
61-
class _PublisherClientGrpcInterceptor(
62-
grpc.UnaryUnaryClientInterceptor,
63-
):
64-
def intercept_unary_unary(self, continuation, client_call_details, request):
65-
pass
60+
# Attempt to use `_thunk` to obtain the underlying grpc channel from
61+
# the intercept channel. Default to obtaining the grpc channel directly
62+
# for backwards compatibility.
63+
# TODO(https://github.com/grpc/grpc/issues/38519): Workaround to obtain a channel
64+
# until a public API is available.
65+
def get_publish_channel(client):
66+
try:
67+
return client._transport.publish._thunk("")._channel
68+
except AttributeError:
69+
return client._transport.publish._channel
6670

6771

6872
def _assert_retries_equal(retry, retry2):
@@ -424,27 +428,17 @@ def init(self, *args, **kwargs):
424428
assert client.transport._ssl_channel_credentials == mock_ssl_creds
425429

426430

427-
def test_init_emulator(monkeypatch, creds):
431+
def test_init_emulator(monkeypatch):
428432
monkeypatch.setenv("PUBSUB_EMULATOR_HOST", "/foo/bar:123")
429433
# NOTE: When the emulator host is set, a custom channel will be used, so
430434
# no credentials (mock ot otherwise) can be passed in.
431-
432-
# TODO(https://github.com/grpc/grpc/issues/38519): Workaround to create an intercept
433-
# channel (for forwards compatibility) with a channel created by the publisher client
434-
# where target is set to the emulator host.
435-
channel = publisher.Client().transport.grpc_channel
436-
interceptor = _PublisherClientGrpcInterceptor()
437-
intercept_channel = grpc.intercept_channel(channel, interceptor)
438-
transport = publisher.Client.get_transport_class("grpc")(
439-
credentials=creds, channel=intercept_channel
440-
)
441-
client = publisher.Client(transport=transport)
435+
client = publisher.Client()
442436

443437
# Establish that a gRPC request would attempt to hit the emulator host.
444438
#
445439
# Sadly, there seems to be no good way to do this without poking at
446440
# the private API of gRPC.
447-
channel = client._transport.publish._thunk("")._channel
441+
channel = get_publish_channel(client)
448442
# Behavior to include dns prefix changed in gRPCv1.63
449443
grpc_major, grpc_minor = [int(part) for part in grpc.__version__.split(".")[0:2]]
450444
if grpc_major > 1 or (grpc_major == 1 and grpc_minor >= 63):

tests/unit/pubsub_v1/subscriber/test_subscriber_client.py

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,16 @@
3636
from google.pubsub_v1.types import PubsubMessage
3737

3838

39-
# NOTE: This interceptor is required to create an intercept channel.
40-
class _SubscriberClientGrpcInterceptor(
41-
grpc.UnaryUnaryClientInterceptor,
42-
):
43-
def intercept_unary_unary(self, continuation, client_call_details, request):
44-
pass
39+
# Attempt to use `_thunk` to obtain the underlying grpc channel from
40+
# the intercept channel. Default to obtaining the grpc channel directly
41+
# for backwards compatibility.
42+
# TODO(https://github.com/grpc/grpc/issues/38519): Workaround to obtain a channel
43+
# until a public API is available.
44+
def get_pull_channel(client):
45+
try:
46+
return client._transport.pull._thunk("")._channel
47+
except AttributeError:
48+
return client._transport.pull._channel
4549

4650

4751
def test_init_default_client_info(creds):
@@ -127,27 +131,17 @@ def init(self, *args, **kwargs):
127131
assert client.transport._ssl_channel_credentials == mock_ssl_creds
128132

129133

130-
def test_init_emulator(monkeypatch, creds):
134+
def test_init_emulator(monkeypatch):
131135
monkeypatch.setenv("PUBSUB_EMULATOR_HOST", "/baz/bacon:123")
132136
# NOTE: When the emulator host is set, a custom channel will be used, so
133137
# no credentials (mock ot otherwise) can be passed in.
134-
135-
# TODO(https://github.com/grpc/grpc/issues/38519): Workaround to create an intercept
136-
# channel (for forwards compatibility) with a channel created by the publisher client
137-
# where target is set to the emulator host.
138-
channel = subscriber.Client().transport.grpc_channel
139-
interceptor = _SubscriberClientGrpcInterceptor()
140-
intercept_channel = grpc.intercept_channel(channel, interceptor)
141-
transport = subscriber.Client.get_transport_class("grpc")(
142-
credentials=creds, channel=intercept_channel
143-
)
144-
client = subscriber.Client(transport=transport)
138+
client = subscriber.Client()
145139

146140
# Establish that a gRPC request would attempt to hit the emulator host.
147141
#
148142
# Sadly, there seems to be no good way to do this without poking at
149143
# the private API of gRPC.
150-
channel = client._transport.pull._thunk("")._channel
144+
channel = get_pull_channel(client)
151145
# Behavior to include dns prefix changed in gRPCv1.63
152146
grpc_major, grpc_minor = [int(part) for part in grpc.__version__.split(".")[0:2]]
153147
if grpc_major > 1 or (grpc_major == 1 and grpc_minor >= 63):

0 commit comments

Comments
 (0)