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

Commit 16ea766

Browse files
authored
fix: get channel target for a gRPC request (#1339)
1 parent 59965a4 commit 16ea766

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

tests/unit/pubsub_v1/publisher/test_publisher_client.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@
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
66+
67+
6068
def _assert_retries_equal(retry, retry2):
6169
# Retry instances cannot be directly compared, because their predicates are
6270
# different instances of the same function. We thus manually compare their other
@@ -416,17 +424,27 @@ def init(self, *args, **kwargs):
416424
assert client.transport._ssl_channel_credentials == mock_ssl_creds
417425

418426

419-
def test_init_emulator(monkeypatch):
427+
def test_init_emulator(monkeypatch, creds):
420428
monkeypatch.setenv("PUBSUB_EMULATOR_HOST", "/foo/bar:123")
421429
# NOTE: When the emulator host is set, a custom channel will be used, so
422430
# no credentials (mock ot otherwise) can be passed in.
423-
client = publisher.Client()
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)
424442

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

tests/unit/pubsub_v1/subscriber/test_subscriber_client.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@
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
45+
46+
3947
def test_init_default_client_info(creds):
4048
client = subscriber.Client(credentials=creds)
4149

@@ -119,17 +127,27 @@ def init(self, *args, **kwargs):
119127
assert client.transport._ssl_channel_credentials == mock_ssl_creds
120128

121129

122-
def test_init_emulator(monkeypatch):
130+
def test_init_emulator(monkeypatch, creds):
123131
monkeypatch.setenv("PUBSUB_EMULATOR_HOST", "/baz/bacon:123")
124132
# NOTE: When the emulator host is set, a custom channel will be used, so
125133
# no credentials (mock ot otherwise) can be passed in.
126-
client = subscriber.Client()
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)
127145

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

0 commit comments

Comments
 (0)