2019-03-23 07:02:32 +00:00
|
|
|
import pytest
|
2019-03-23 08:47:01 +00:00
|
|
|
|
2019-03-23 07:02:32 +00:00
|
|
|
from multidict import MultiDict
|
|
|
|
|
2019-03-23 08:47:01 +00:00
|
|
|
from grpclib.const import Status
|
2019-03-23 07:02:32 +00:00
|
|
|
from grpclib.events import listen, RecvRequest, RecvMessage, SendMessage
|
2019-03-23 08:47:01 +00:00
|
|
|
from grpclib.events import SendInitialMetadata, SendTrailingMetadata
|
|
|
|
from grpclib.exceptions import GRPCError
|
2019-03-23 07:02:32 +00:00
|
|
|
from grpclib.testing import ChannelFor
|
|
|
|
|
|
|
|
from dummy_pb2 import DummyRequest, DummyReply
|
2019-03-23 08:47:01 +00:00
|
|
|
from dummy_grpc import DummyServiceStub, DummyServiceBase
|
|
|
|
|
|
|
|
|
|
|
|
class DummyService(DummyServiceBase):
|
|
|
|
|
|
|
|
async def UnaryUnary(self, stream):
|
|
|
|
await stream.recv_message()
|
|
|
|
await stream.send_initial_metadata(metadata={'initial': 'true'})
|
|
|
|
await stream.send_message(DummyReply(value='pong'))
|
|
|
|
await stream.send_trailing_metadata(metadata={'trailing': 'true'})
|
|
|
|
|
|
|
|
async def UnaryStream(self, stream):
|
|
|
|
raise GRPCError(Status.UNIMPLEMENTED)
|
|
|
|
|
|
|
|
async def StreamUnary(self, stream):
|
|
|
|
raise GRPCError(Status.UNIMPLEMENTED)
|
|
|
|
|
|
|
|
async def StreamStream(self, stream):
|
|
|
|
raise GRPCError(Status.UNIMPLEMENTED)
|
2019-03-23 07:02:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_recv_request():
|
|
|
|
event = await _test(RecvRequest)
|
|
|
|
assert event.metadata == MultiDict({'foo': 'bar'})
|
|
|
|
assert event.method_name == '/dummy.DummyService/UnaryUnary'
|
|
|
|
assert event.deadline.time_remaining() > 0
|
|
|
|
assert event.content_type == 'application/grpc+proto'
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_recv_message():
|
|
|
|
event = await _test(RecvMessage)
|
|
|
|
assert event.message == DummyRequest(value='ping')
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_send_message():
|
|
|
|
event = await _test(SendMessage)
|
|
|
|
assert event.message == DummyReply(value='pong')
|
2019-03-23 08:47:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_send_initial_metadata():
|
|
|
|
event = await _test(SendInitialMetadata)
|
|
|
|
assert event.metadata == MultiDict({'initial': 'true'})
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_send_trailing_metadata():
|
|
|
|
event = await _test(SendTrailingMetadata)
|
|
|
|
assert event.metadata == MultiDict({'trailing': 'true'})
|