Skip to content

Commit e309513

Browse files
authored
Fix _stream_unary not working with empty iterables (#422)
* Fixed `do_many_things` method in `ThingService` * Added test for empty iterables * Fixed `_stream_unary` not sending request first
1 parent 24db532 commit e309513

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

src/betterproto/grpc/grpclib_client.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ async def _stream_unary(
127127
response_type,
128128
**self.__resolve_request_kwargs(timeout, deadline, metadata),
129129
) as stream:
130+
await stream.send_request()
130131
await self._send_messages(stream, request_iterator)
131132
response = await stream.recv_message()
132133
assert response is not None

tests/grpc/test_grpclib_client.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,3 +272,27 @@ async def test_async_gen_for_stream_stream_request():
272272
assert response_index == len(
273273
expected_things
274274
), "Didn't receive all expected responses"
275+
276+
277+
@pytest.mark.asyncio
278+
async def test_stream_unary_with_empty_iterable():
279+
things = [] # empty
280+
281+
async with ChannelFor([ThingService()]) as channel:
282+
client = ThingServiceClient(channel)
283+
requests = [DoThingRequest(name) for name in things]
284+
response = await client.do_many_things(requests)
285+
assert len(response.names) == 0
286+
287+
288+
@pytest.mark.asyncio
289+
async def test_stream_stream_with_empty_iterable():
290+
things = [] # empty
291+
292+
async with ChannelFor([ThingService()]) as channel:
293+
client = ThingServiceClient(channel)
294+
requests = [GetThingRequest(name) for name in things]
295+
responses = [
296+
response async for response in client.get_different_things(requests)
297+
]
298+
assert len(responses) == 0

tests/grpc/thing_service.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ async def do_thing(
2727
async def do_many_things(
2828
self, stream: "grpclib.server.Stream[DoThingRequest, DoThingResponse]"
2929
):
30-
thing_names = [request.name for request in stream]
30+
thing_names = [request.name async for request in stream]
3131
if self.test_hook is not None:
3232
self.test_hook(stream)
3333
await stream.send_message(DoThingResponse(thing_names))

0 commit comments

Comments
 (0)