Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Bidi uses metadata from start_rpc #205

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions google/api_core/bidi.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,8 @@ class BidiRpc(object):
yield. This is useful if an initial request is needed to start the
stream.
metadata (Sequence[Tuple(str, str)]): RPC metadata to include in
the request.
the request. If no metadata is provided, metadata from
start_rpc will be used.
"""

def __init__(self, start_rpc, initial_request=None, metadata=None):
Expand Down Expand Up @@ -277,7 +278,11 @@ def open(self):
request_generator = _RequestQueueGenerator(
self._request_queue, initial_request=self._initial_request
)
call = self._start_rpc(iter(request_generator), metadata=self._rpc_metadata)
if self._rpc_metadata:
call = self._start_rpc(iter(request_generator), metadata=self._rpc_metadata)
# use metadata from self._start_rpc if no other metadata is specified
else:
call = self._start_rpc(iter(request_generator))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I set up BidiRPC with LoggingV2Transport().tail_log_entries, that's the unwrapped method, not the wrapped method. Will this still get the metadata from the wrapped method?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also, I tried passing in LoggingServiceV2Client().tail_log_entries and it didn't seem to work.

I'm actually not sure how pubsub passes in the wrapped method successfully.
https://github.com/googleapis/python-pubsub/blob/e907f6e05f59f64a3b08df3304e92ec960997be6/google/cloud/pubsub_v1/subscriber/_protocol/streaming_pull_manager.py#L524

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I set up BidiRPC with LoggingV2Transport().tail_log_entries, that's the unwrapped method, not the wrapped method. Will this still get the metadata from the wrapped method?

I believe the wrapped one has to be passed in (the wrapper adds the metadata to the call)

also, I tried passing in LoggingServiceV2Client().tail_log_entries and it didn't seem to work.

I'm actually not sure how pubsub passes in the wrapped method successfully.

Hmm I'll go back and poke at what is actually expected to be passed in to Bidi.


request_generator.call = call

Expand Down
17 changes: 13 additions & 4 deletions tests/unit/test_bidi.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,12 +202,12 @@ class _CallAndFuture(grpc.Call, grpc.Future):
pass


def make_rpc():
def make_rpc(metadata=None):
"""Makes a mock RPC used to test Bidi classes."""
call = mock.create_autospec(_CallAndFuture, instance=True)
rpc = mock.create_autospec(grpc.StreamStreamMultiCallable, instance=True)

def rpc_side_effect(request, metadata=None):
def rpc_side_effect(request, metadata=metadata):
call.is_active.return_value = True
call.request = request
call.metadata = metadata
Expand Down Expand Up @@ -265,12 +265,13 @@ def test_metadata(self):
assert bidi_rpc.call.metadata == mock.sentinel.A

def test_open(self):
rpc, call = make_rpc()
bidi_rpc = bidi.BidiRpc(rpc)
rpc, call = make_rpc(metadata=[(1, 2)])
bidi_rpc = bidi.BidiRpc(rpc, metadata=[(3, 4)])

bidi_rpc.open()

assert bidi_rpc.call == call
assert bidi_rpc.call.metadata == [(3, 4)]
assert bidi_rpc.is_active
call.add_done_callback.assert_called_once_with(bidi_rpc._on_call_done)

Expand All @@ -283,6 +284,14 @@ def test_open_error_already_open(self):
with pytest.raises(ValueError):
bidi_rpc.open()

def test_open_use_start_rpc_metadata(self):
rpc, _ = make_rpc(metadata=[(1, 2)])
bidi_rpc = bidi.BidiRpc(rpc)

bidi_rpc.open()

assert bidi_rpc.call.metadata == [(1, 2)]

def test_close(self):
rpc, call = make_rpc()
bidi_rpc = bidi.BidiRpc(rpc)
Expand Down