async def UnaryStream(self, stream): request = await stream.recv_message() self.log.append(request) assert await stream.recv_message() is None await stream.send_message(DummyReply(value='pong1')) await stream.send_message(DummyReply(value='pong2')) await stream.send_message(DummyReply(value='pong3'))
async def test_unary_stream_simple(loop): async with ClientServer(loop=loop) as (handler, stub): replies = await stub.UnaryStream(DummyRequest(value='ping')) assert handler.log == [DummyRequest(value='ping')] assert replies == [DummyReply(value='pong1'), DummyReply(value='pong2'), DummyReply(value='pong3')]
async def test_unary_stream_advanced(loop): async with ClientServer(loop=loop) as (handler, stub): async with stub.UnaryStream.open() as stream: await stream.send_message(DummyRequest(value='ping'), end=True) replies = await _to_list(stream) assert handler.log == [DummyRequest(value='ping')] assert replies == [DummyReply(value='pong1'), DummyReply(value='pong2'), DummyReply(value='pong3')]
async def test_stream_stream_simple(): async with ClientServer() as (_, stub): replies = await stub.StreamStream([ DummyRequest(value='foo'), DummyRequest(value='bar'), DummyRequest(value='baz'), ]) assert replies == [ DummyReply(value='foo'), DummyReply(value='bar'), DummyReply(value='baz'), ]
async def test_stream_stream_advanced(): async with ClientServer() as (_, stub): async with stub.StreamStream.open() as stream: await stream.send_message(DummyRequest(value='foo')) assert await stream.recv_message() == DummyReply(value='foo') await stream.send_message(DummyRequest(value='bar')) assert await stream.recv_message() == DummyReply(value='bar') await stream.send_message(DummyRequest(value='baz'), end=True) assert await stream.recv_message() == DummyReply(value='baz') assert await stream.recv_message() is None
async def test_unary_unary_advanced(): async with ClientServer() as (handler, stub): async with stub.UnaryUnary.open() as stream: await stream.send_message(DummyRequest(value='ping'), end=True) reply = await stream.recv_message() assert reply == DummyReply(value='pong') assert handler.log == [DummyRequest(value='ping')]
async def test_server_stream(): cf = ChannelFor([WorkingDummyService()]) async with cf as channel: stub = DummyServiceStub(channel) handler, = cf._server._handlers async with stub.UnaryUnary.open() as stream: await stream.send_request() while not handler._tasks: await asyncio.sleep(0.001) server_stream, = handler._tasks.keys() _is_recent(server_stream.created) assert server_stream.data_sent == 0 assert server_stream.data_received == 0 assert server_stream.connection.messages_sent == 0 assert server_stream.connection.messages_received == 0 assert server_stream.connection.last_message_sent is None assert server_stream.connection.last_message_received is None await stream.send_message(DummyRequest(value='whatever'), end=True) await asyncio.sleep(0.01) assert server_stream.data_sent > 0 assert server_stream.data_received > 0 assert server_stream.connection.messages_sent == 1 assert server_stream.connection.messages_received == 1 _is_recent(server_stream.connection.last_data_sent) _is_recent(server_stream.connection.last_data_received) _is_recent(server_stream.connection.last_message_sent) _is_recent(server_stream.connection.last_message_received) reply = await stream.recv_message() assert reply == DummyReply(value='test')
async def test_exit_and_stream_was_closed(loop): client_h2c, server_h2c = create_connections() to_client_transport = TransportStub(client_h2c) to_server_transport = TransportStub(server_h2c) client_conn = Connection(client_h2c, to_server_transport, loop=loop) server_conn = Connection(server_h2c, to_client_transport, loop=loop) server_proc = EventsProcessor(DummyHandler(), server_conn) client_proc = EventsProcessor(DummyHandler(), client_conn) client_h2_stream = client_conn.create_stream() await client_h2_stream.send_request(create_headers(), _processor=client_proc) request = DummyRequest(value='ping') await send_message(client_h2_stream, ProtoCodec(), request, DummyRequest, end=True) to_server_transport.process(server_proc) server_h2_stream = server_proc.handler.stream request_metadata = decode_metadata(server_proc.handler.headers) async with mk_stream(server_h2_stream, request_metadata) as server_stream: await server_stream.recv_message() # simulating client closing stream await client_h2_stream.reset() to_server_transport.process(server_proc) # we should fail here on this attempt to send something await server_stream.send_message(DummyReply(value='pong'))
async def worker2(): cs = ClientStream(client_conn=client_conn, send_type=DummyRequest, recv_type=DummyReply) async with cs.client_stream as stream: await stream.send_message(DummyRequest(value='ping'), end=True) assert await stream.recv_message() == DummyReply(value='pong')
async def test_stream_unary_simple(): async with ClientServer() as (handler, stub): reply = await stub.StreamUnary([ DummyRequest(value='ping1'), DummyRequest(value='ping2'), DummyRequest(value='ping3'), ]) assert reply == DummyReply(value='pong') assert handler.log == [ DummyRequest(value='ping1'), DummyRequest(value='ping2'), DummyRequest(value='ping3') ]
async def test_concurrent_connect(loop): count = 5 reqs = [DummyRequest(value='ping') for _ in range(count)] reps = [DummyReply(value='pong') for _ in range(count)] channel = Channel() stub = DummyServiceStub(channel) async with ChannelFor([DummyService()]) as _channel: with patch.object(loop, 'create_connection') as po: po.side_effect = _create_connection_gen(_channel._protocol) tasks = [loop.create_task(stub.UnaryUnary(req)) for req in reqs] replies = await asyncio.gather(*tasks) assert replies == reps po.assert_called_once_with(ANY, '127.0.0.1', 50051, ssl=None)
async def test_client_stream(): async with ChannelFor([WorkingDummyService()]) as channel: proto = await channel.__connect__() stub = DummyServiceStub(channel) async with stub.UnaryUnary.open() as stream: assert proto.connection.streams_started == 0 assert proto.connection.streams_succeeded == 0 assert proto.connection.streams_failed == 0 assert proto.connection.last_stream_created is None await stream.send_request() _is_recent(stream._stream.created) assert proto.connection.streams_started == 1 assert proto.connection.streams_succeeded == 0 assert proto.connection.streams_failed == 0 _is_recent(proto.connection.last_stream_created) assert stream._messages_sent == 0 assert stream._stream.data_sent == 0 assert proto.connection.messages_sent == 0 assert proto.connection.data_sent == 0 assert proto.connection.last_message_sent is None await stream.send_message(DummyRequest(value='whatever'), end=True) assert stream._messages_sent == 1 assert stream._stream.data_sent > 0 assert proto.connection.messages_sent == 1 assert proto.connection.data_sent > 0 _is_recent(proto.connection.last_message_sent) _is_recent(proto.connection.last_data_sent) assert stream._messages_received == 0 assert stream._stream.data_received == 0 assert proto.connection.messages_received == 0 assert proto.connection.data_received == 0 assert proto.connection.last_message_received is None reply = await stream.recv_message() assert stream._messages_received == 1 assert stream._stream.data_received > 0 assert proto.connection.messages_received == 1 assert proto.connection.data_received > 0 _is_recent(proto.connection.last_message_received) _is_recent(proto.connection.last_data_received) assert proto.connection.streams_started == 1 assert proto.connection.streams_succeeded == 1 assert proto.connection.streams_failed == 0 assert reply == DummyReply(value='test')
async def test_channel_calls_succeeded(): async with ChannelFor([WorkingDummyService()]) as channel: stub = DummyServiceStub(channel) assert channel._calls_started == 0 assert channel._calls_succeeded == 0 assert channel._calls_failed == 0 assert channel._last_call_started is None reply = await stub.UnaryUnary(DummyRequest(value='whatever')) assert channel._calls_started == 1 assert channel._calls_succeeded == 1 assert channel._calls_failed == 0 _is_recent(channel._last_call_started) assert reply == DummyReply(value='test')
async def _test(event_type): service = DummyService() events = [] async def callback(event_): events.append(event_) async with ChannelFor([service]) as channel: listen(channel, event_type, callback) stub = DummyServiceStub(channel) reply = await stub.UnaryUnary(DummyRequest(value='ping'), timeout=1, metadata={'request': 'true'}) assert reply == DummyReply(value='pong') event, = events return event
async def _test(event_type): service = DummyService() events = [] async def callback(event_): events.append(event_) channel_for = ChannelFor([service]) async with channel_for as channel: server = channel_for._server listen(server, event_type, callback) stub = DummyServiceStub(channel) reply = await stub.UnaryUnary(DummyRequest(value='ping'), timeout=1, metadata={'foo': 'bar'}) assert reply == DummyReply(value='pong') event, = events return event
async def StreamUnary(self, stream): async for request in stream: self.log.append(request) await stream.send_message(DummyReply(value='pong'))