0

我有一个基本的消费者,它从 websocket 接收消息,然后将这些消息广播到消息中指定的通道。

消费者.py

class BasicConsumer(AsyncJsonWebsocketConsumer):
    async def receive_json(self, content, **kwargs):
        channel = content['channel']
        await self.channel_layer.send(
            channel,
            {
                'type': 'some-message-type',
                'body': content,
            },
        )

然后我想用 pytest 和 pytest-asyncio 像这样测试它

@pytest.mark.asyncio
async def test_basic_consumer():
    channel_layer = get_channel_layer()

    # connect to consumer
    communicator = WebsocketCommunicator(BasicConsumer, "/my-url/")
    connected, subprotocol = await communicator.connect()
    assert connected

    # generate valid channel name
    channel_name = await channel_layer.new_channel()

    await communicator.send_json_to({
        'channel': channel_name,
        'data': 'some-data',
    })

    from_channel_layer = await communicator.receive_output(timeout=5)
    assert from_channel_layer == {
        'type': 'some-message-type',
        'body': {
            'channel': channel_name,
            'data': 'some-data',
        },
    }

我的理解是,communicator.receive_output()将检索消费者发送到通道层的事件。但是我收到以下错误:

___________________________ test_basic_consumer ___________________________

self = <channels.testing.websocket.WebsocketCommunicator object at 0x7f901b5a87b8>
timeout = 5

    async def receive_output(self, timeout=1):
        """
        Receives a single message from the application, with optional timeout.
        """
        # Make sure there's not an exception to raise from the task
        if self.future.done():
            self.future.result()
        # Wait and receive the message
        try:
            async with async_timeout(timeout):
>               return await self.output_queue.get()

vp3test/lib/python3.7/site-packages/asgiref/testing.py:75: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <Queue at 0x7f901b5a8898 maxsize=0 tasks=1>

    async def get(self):
        """Remove and return an item from the queue.

        If queue is empty, wait until an item is available.
        """
        while self.empty():
            getter = self._loop.create_future()
            self._getters.append(getter)
            try:
>               await getter
E               concurrent.futures._base.CancelledError

/usr/lib/python3.7/asyncio/queues.py:159: CancelledError

During handling of the above exception, another exception occurred:

    @pytest.mark.asyncio
    async def test_basic_consumer():
        channel_layer = get_channel_layer()

        # connect to consumer
        communicator = WebsocketCommunicator(BasicConsumer, "/my-url/")
        connected, subprotocol = await communicator.connect()
        assert connected

        # generate valid channel name
        channel_name = await channel_layer.new_channel()

        await communicator.send_json_to({
            'channel': channel_name,
            'data': 'some-data',
        })

>       from_channel_layer = await communicator.receive_output(timeout=5)

test_consumers.py:150: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
vp3test/lib/python3.7/site-packages/asgiref/testing.py:86: in receive_output
    raise e
vp3test/lib/python3.7/site-packages/asgiref/testing.py:75: in receive_output
    return await self.output_queue.get()
vp3test/lib/python3.7/site-packages/asgiref/timeout.py:68: in __aexit__
    self._do_exit(exc_type)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <asgiref.timeout.timeout object at 0x7f901b5a8ba8>
exc_type = <class 'concurrent.futures._base.CancelledError'>

    def _do_exit(self, exc_type: Type[BaseException]) -> None:
        if exc_type is asyncio.CancelledError and self._cancelled:
            self._cancel_handler = None
            self._task = None
>           raise asyncio.TimeoutError
E           concurrent.futures._base.TimeoutError

vp3test/lib/python3.7/site-packages/asgiref/timeout.py:105: TimeoutError

在我的测试中我需要做更多的设置/配置吗?还是我用receive_output()错了?

我正在InMemoryChannelLayer用于测试和所有最新发布的版本。

4

1 回答 1

0

我的问题是我在打电话

from_channel_layer = await communicator.receive_output(timeout=5)

这正在等待消费者正在向 websocket 发送消息。


相反,我可以打电话

from_channel_layer = await channel_layer.receive(channel_name)

获取消费者通过通道层发送的消息。

于 2020-08-18T06:08:00.610 回答