async def test_background_task_no_block(mock_app: rx.App, token: str):
    """Test that a background task does not block other events.

    Args:
        mock_app: An app that will be returned by `get_app()`
        token: A token.
    """
    router_data = {"query": {}, "token": token}
    sid = "test_sid"
    namespace = mock_app.event_namespace
    assert namespace is not None
    namespace.sid_to_token[sid] = token
    mock_app.state_manager.state = mock_app._state = BackgroundTaskState
    async for update in rx.app.process(
        mock_app,
        Event(
            token=token,
            name=f"{BackgroundTaskState.get_full_name()}.background_task",
            router_data=router_data,
            payload={},
        ),
        sid=sid,
        headers={},
        client_ip="",
    ):
        # background task returns empty update immediately
        assert update == StateUpdate()

    # wait for the coroutine to start
    await asyncio.sleep(0.5 if CI else 0.1)
    assert len(mock_app._background_tasks) == 1

    # Process another normal event
    async for update in rx.app.process(
        mock_app,
        Event(
            token=token,
            name=f"{BackgroundTaskState.get_full_name()}.other",
            router_data=router_data,
            payload={},
        ),
        sid=sid,
        headers={},
        client_ip="",
    ):
        # other task returns delta
        assert update == StateUpdate(
            delta={
                BackgroundTaskState.get_full_name(): {
                    "order": [
                        "background_task:start",
                        "other",
                    ],
                    "computed_order": [
                        "background_task:start",
                        "other",
                    ],
                }
            }
        )

    # Explicit wait for background tasks
    for task in tuple(mock_app._background_tasks):
        await task
    assert not mock_app._background_tasks

    exp_order = [
        "background_task:start",
        "other",
        "background_task:stop",
        "other",
        "private",
    ]

    assert (
        await mock_app.state_manager.get_state(
            _substate_key(token, BackgroundTaskState)
        )
    ).order == exp_order

    assert mock_app.event_namespace is not None
    emit_mock = mock_app.event_namespace.emit

    first_ws_message = emit_mock.mock_calls[0].args[1]  # pyright: ignore [reportFunctionMemberAccess]
    assert (
        first_ws_message.delta[BackgroundTaskState.get_full_name()].pop("router")
        is not None
    )
    assert first_ws_message == StateUpdate(
        delta={
            BackgroundTaskState.get_full_name(): {
                "order": ["background_task:start"],
                "computed_order": ["background_task:start"],
            }
        },
        events=[],
        final=True,
    )
    for call in emit_mock.mock_calls[1:5]:  # pyright: ignore [reportFunctionMemberAccess]
        assert call.args[1] == StateUpdate(
            delta={
                BackgroundTaskState.get_full_name(): {
                    "computed_order": ["background_task:start"],
                }
            },
            events=[],
            final=True,
        )
    assert emit_mock.mock_calls[-2].args[1] == StateUpdate(  # pyright: ignore [reportFunctionMemberAccess]
        delta={
            BackgroundTaskState.get_full_name(): {
                "order": exp_order,
                "computed_order": exp_order,
                "dict_list": {},
            }
        },
        events=[],
        final=True,
    )
    assert emit_mock.mock_calls[-1].args[1] == StateUpdate(  # pyright: ignore [reportFunctionMemberAccess]
        delta={
            BackgroundTaskState.get_full_name(): {
                "computed_order": exp_order,
            },
        },
        events=[],
        final=True,
    )
Test that a background task does not block other events.

    Args:
        mock_app: An app that will be returned by `get_app()`
        token: A token.
    
tests/units/test_state.py