Skip to content

Certain WebSocket unit testing scenarios hang #2585

@vytas7

Description

@vytas7

It seems that certain WebSocket patterns are tricky to write proper tests for.

For instance, consider an emitter that mostly streams updates to the client:

import asyncio

import falcon.asgi
import falcon.testing


class Channel:
    async def on_websocket(self, req, ws):
        await ws.accept()

        while True:
            await asyncio.sleep(1)
            await ws.send_text('Hello')


@falcon.runs_sync
async def test_ws():
    async with falcon.testing.ASGIConductor(falcon.asgi.App()) as ac:
        ac.app.add_route('/', Channel())
        async with ac.websocket('/') as ws:
            await ws.wait_ready(timeout=3.0)

The above test works just fine.

However, let's say we want to simulate the scenario where no messages are made available for a while (the ws.send_text part is commented out):

import asyncio

import falcon.asgi
import falcon.testing


class Channel:
    async def on_websocket(self, req, ws):
        await ws.accept()

        while True:
            await asyncio.sleep(1)
            # await ws.send_text('Hello')


@falcon.runs_sync
async def test_ws():
    async with falcon.testing.ASGIConductor(falcon.asgi.App()) as ac:
        ac.app.add_route('/', Channel())
        async with ac.websocket('/') as ws:
            await ws.wait_ready(timeout=3.0)

Pytest now hangs forever, the timeout parameter has no effect.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions