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_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_failure(): class FailingService(DummyService): async def UnaryUnary(self, stream): raise GRPCError(Status.FAILED_PRECONDITION) async with ChannelFor([FailingService()]) as channel: stub = DummyServiceStub(channel) with pytest.raises(GRPCError) as err: await stub.UnaryUnary(DummyRequest(value='ping')) assert err.value.status is Status.FAILED_PRECONDITION
async def __aenter__(self): self.temp = tempfile.mkdtemp() self.sock = os.path.join(self.temp, 'grpclib.sock') dummy_service = DummyService() self.server = Server([dummy_service]) await self.server.start(path=self.sock) self.channel = Channel(path=self.sock) dummy_stub = DummyServiceStub(self.channel) return dummy_service, dummy_stub
async def __aenter__(self): host = '127.0.0.1' with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.bind(('127.0.0.1', 0)) _, port = s.getsockname() dummy_service = DummyService() self.server = Server([dummy_service]) await self.server.start(host, port) self.channel = Channel(host=host, port=port) dummy_stub = DummyServiceStub(self.channel) return dummy_service, dummy_stub
async def test_send_trailing_metadata(loop, svc_type): async with ChannelFor( [svc_type()], codec=ProtoCodec(), status_details_codec=ProtoStatusDetailsCodec(), ) as channel: stub = DummyServiceStub(channel) with pytest.raises(GRPCError) as error: await stub.UnaryUnary(DummyRequest(value='ping')) assert error.value.status is Status.DATA_LOSS assert error.value.message == 'Some data loss occurred' assert error.value.details == [ Help(links=[Help.Link(url='https://example.com')]), ]
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_failed(): async with ChannelFor([FailingDummyService()]) 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 with pytest.raises(GRPCError, match='Internal Server Error'): await stub.UnaryUnary(DummyRequest(value='whatever')) assert channel._calls_started == 1 assert channel._calls_succeeded == 0 assert channel._calls_failed == 1 _is_recent(channel._last_call_started)
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_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_) 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 test(): async with ChannelFor([DummyService()]) as channel: stub = DummyServiceStub(channel) reply = await stub.UnaryUnary(DummyRequest(value='ping')) assert reply == DummyReply(value='pong')
async def example(): async with ChannelFor([DummyService()]) as channel: stub = DummyServiceStub(channel) await stub.UnaryUnary(DummyRequest(value='ping'))
def stub_fixture(loop): with channel_for([DummyService()], loop=loop) as channel: yield DummyServiceStub(channel)