Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

5.4 KB
Raw Blame
"""Synchronous scheduling-failure contracts for the Socket.IO wrappers.

The public helpers first check that uvicorn's loop is running and then hand a
new coroutine to ``asyncio.run_coroutine_threadsafe``.  Shutdown can land in
between those operations, making the scheduler itself raise.  These tests pin
the wrappers' best-effort contract at that exact seam: the exception never
reaches a worker thread, no routing state is mutated, and rejected coroutine
objects are closed by production rather than leaking RuntimeWarnings.
"""

from __future__ import annotations

import inspect

import pytest

from local_deep_research.web.services import socketio_asgi as sio_mod


class _RunningLoop:
    def is_running(self) -> bool:
        return True


@pytest.fixture
def rejected_scheduler(monkeypatch):
    """Install a scheduler that rejects after receiving the coroutine."""
    loop = _RunningLoop()
    rejected = []
    logging_states = []

    def _reject(coro, scheduled_loop):
        assert scheduled_loop is loop
        rejected.append(coro)
        logging_states.append(sio_mod._logging_is_enabled())
        # The real scheduler takes ownership only on success.  The production
        # wrapper must close this rejected coroutine after our stub raises.
        raise RuntimeError("event loop stopped during scheduling")

    monkeypatch.setattr(sio_mod, "_get_main_loop", lambda: loop)
    monkeypatch.setattr(sio_mod.asyncio, "run_coroutine_threadsafe", _reject)
    return rejected, logging_states


@pytest.fixture
def isolated_socket_state(monkeypatch):
    subscriptions = {("alice", "research-1"): {"sid-alice"}}
    sid_users = {"sid-alice": "alice", "sid-bob": "bob"}
    sid_sessions = {"sid-alice": "session-a", "sid-bob": "session-b"}
    monkeypatch.setattr(sio_mod, "_subscriptions", subscriptions)
    monkeypatch.setattr(sio_mod, "_sid_users", sid_users)
    monkeypatch.setattr(sio_mod, "_sid_sessions", sid_sessions)
    return subscriptions, sid_users, sid_sessions


def _snapshot(state):
    subscriptions, sid_users, sid_sessions = state
    return (
        {key: set(sids) for key, sids in subscriptions.items()},
        dict(sid_users),
        dict(sid_sessions),
    )


def _assert_one_closed_coroutine(rejected_scheduler) -> None:
    rejected, _logging_states = rejected_scheduler
    assert len(rejected) == 1
    assert inspect.getcoroutinestate(rejected[0]) == inspect.CORO_CLOSED


def test_emit_socket_event_reports_synchronous_scheduling_failure(
    rejected_scheduler, isolated_socket_state
):
    before = _snapshot(isolated_socket_state)

    result = sio_mod.emit_socket_event(
        "chat_response", {"text": "hello"}, room="sid-alice"
    )

    assert result is False
    assert _snapshot(isolated_socket_state) == before
    _assert_one_closed_coroutine(rejected_scheduler)


def test_emit_to_user_reports_synchronous_scheduling_failure(
    rejected_scheduler, isolated_socket_state
):
    before = _snapshot(isolated_socket_state)

    result = sio_mod.emit_to_user("settings_changed", "alice", {"k": "v"})

    assert result is False
    assert _snapshot(isolated_socket_state) == before
    _assert_one_closed_coroutine(rejected_scheduler)


@pytest.mark.parametrize(
    "disconnect",
    [
        pytest.param(lambda: sio_mod.disconnect_user("alice"), id="user"),
        pytest.param(
            lambda: sio_mod.disconnect_session("session-a"), id="session"
        ),
    ],
)
def test_public_disconnect_wrappers_report_synchronous_scheduling_failure(
    rejected_scheduler, isolated_socket_state, disconnect
):
    before = _snapshot(isolated_socket_state)

    result = disconnect()

    assert result is False
    assert _snapshot(isolated_socket_state) == before
    _assert_one_closed_coroutine(rejected_scheduler)


def test_emit_to_subscribers_restores_logging_after_scheduling_failure(
    rejected_scheduler, isolated_socket_state
):
    before = _snapshot(isolated_socket_state)
    outer_token = sio_mod._logging_enabled_var.set(True)
    try:
        result = sio_mod.emit_to_subscribers(
            "research_progress",
            "research-1",
            {"progress": 25},
            owner="alice",
            enable_logging=False,
        )

        assert result is False
        assert rejected_scheduler[1] == [False]
        assert sio_mod._logging_is_enabled() is True
    finally:
        sio_mod._logging_enabled_var.reset(outer_token)
    assert _snapshot(isolated_socket_state) == before
    _assert_one_closed_coroutine(rejected_scheduler)


def test_emit_to_subscribers_logs_scheduling_failure_when_enabled(
    rejected_scheduler, isolated_socket_state, monkeypatch
):
    debug_messages = []
    monkeypatch.setattr(
        sio_mod.logger, "debug", lambda message: debug_messages.append(message)
    )

    result = sio_mod.emit_to_subscribers(
        "research_progress",
        "research-1",
        {"progress": 25},
        owner="alice",
    )

    assert result is False
    assert rejected_scheduler[1] == [True]
    assert debug_messages == [
        "Error emitting to subscribers for research research-1"
    ]
    _assert_one_closed_coroutine(rejected_scheduler)


def test_remove_subscriptions_is_total_when_scheduling_fails(
    rejected_scheduler, isolated_socket_state
):
    before = _snapshot(isolated_socket_state)

    result = sio_mod.remove_subscriptions_for_research("research-1", "alice")

    assert result is None
    assert _snapshot(isolated_socket_state) == before
    _assert_one_closed_coroutine(rejected_scheduler)