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.

3.2 KB
Raw Blame
"""Regression tests: async route handlers must not run blocking work inline.

uvicorn serves every HTTP request and WebSocket on one event loop. An
``async def`` handler that calls synchronous DB loops or network sends
inline stalls that loop for the full duration — a whole-library
conversion or a slow Apprise endpoint freezes the entire app for every
user. These handlers were flagged doing exactly that after the FastAPI
port; they must offload via run_db_sync / asyncio.to_thread.
"""

import asyncio
import inspect
import threading
from unittest.mock import AsyncMock, Mock, patch

import pytest


@pytest.mark.parametrize(
    "module_path,func_name",
    [
        (
            "local_deep_research.web.routers.library_search",
            "convert_all_research",
        ),
        (
            "local_deep_research.web.routers.library_delete",
            "delete_documents_bulk",
        ),
        (
            "local_deep_research.web.routers.library_delete",
            "delete_documents_blobs_bulk",
        ),
        (
            "local_deep_research.web.routers.library_delete",
            "remove_documents_from_collection_bulk",
        ),
        (
            "local_deep_research.web.routers.library_delete",
            "get_bulk_deletion_preview",
        ),
    ],
)
def test_async_handler_offloads_db_work(module_path, func_name):
    """Each flagged async handler must route its DB work through
    run_db_sync (threadpool + per-task session cleanup)."""
    import importlib

    module = importlib.import_module(module_path)
    source = inspect.getsource(getattr(module, func_name))
    assert "run_db_sync" in source, (
        f"{module_path}.{func_name} runs blocking DB work inline on the "
        "event loop"
    )


def test_notification_url_test_offloads_network_send():
    from local_deep_research.web.routers import settings as settings_module

    source = inspect.getsource(settings_module.api_test_notification_url)
    assert "to_thread" in source, (
        "api_test_notification_url performs the Apprise network send "
        "inline on the event loop"
    )


def test_convert_all_research_runs_off_the_event_loop():
    """Behavioral check for the representative case: the indexer must
    execute on a worker thread, not the loop thread."""
    from local_deep_research.web.routers.library_search import (
        convert_all_research,
    )

    seen = {}

    class FakeIndexer:
        def __init__(self, username, db_password):
            pass

        def convert_all_research(self, force=False):
            seen["thread"] = threading.current_thread()
            return {"converted": 0, "skipped": 0, "failed": 0}

    request = Mock()
    request.session = {"session_id": None}
    request.json = AsyncMock(return_value={})

    with patch(
        "local_deep_research.research_library.search.services"
        ".research_history_indexer.ResearchHistoryIndexer",
        FakeIndexer,
    ):
        loop_thread = {}

        async def _run():
            loop_thread["thread"] = threading.current_thread()
            return await convert_all_research(request, username="alice")

        result = asyncio.run(_run())

    assert result["success"] is True
    assert seen["thread"] is not loop_thread["thread"]