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.3 KB
Raw Blame
"""Pin the result delivered to an actual waiter when its fetch owner ends."""

from asyncio import CancelledError
from concurrent.futures import ThreadPoolExecutor
from threading import Event
from unittest.mock import Mock

import pytest

from local_deep_research.utilities.research_content_cache import (
    ResearchContentCache,
    content_cache_key,
)


@pytest.mark.parametrize("outcome", ["success", "empty", "error", "cancelled"])
def test_waiter_reuses_success_or_recovers_from_failed_owner(
    monkeypatch, outcome
):
    cache = ResearchContentCache()
    url = "https://example.com/waiter-contract"
    owner_started = Event()
    release_owner = Event()
    waiter_entered_wait = Event()
    recover = Mock(return_value={"content": "recovered content"})

    def fetch_owner():
        owner_started.set()
        assert release_owner.wait(5), "owner was not released"
        if outcome == "error":
            raise OSError("synthetic fetch failure")
        if outcome == "cancelled":
            raise CancelledError("synthetic fetch cancellation")
        if outcome == "empty":
            return None
        return {"content": "owner content"}

    with ThreadPoolExecutor(max_workers=2) as executor:
        owner = executor.submit(cache.get_or_fetch, url, fetch_owner)
        try:
            assert owner_started.wait(2), "owner did not begin fetching"
            # Observe the existing event, without replacing cache coordination.
            # This proves the second caller took the waiter path before the
            # owner ends; scheduling sleeps cannot establish that ordering.
            with cache._lock:
                owner_event = cache._inflight[content_cache_key(url)]
            real_wait = owner_event.wait

            def observe_wait(timeout=None):
                waiter_entered_wait.set()
                return real_wait(timeout)

            monkeypatch.setattr(owner_event, "wait", observe_wait)
            waiter = executor.submit(
                cache.get_or_fetch, url, recover, wait_timeout=2
            )
            assert waiter_entered_wait.wait(2), "caller did not enter wait"
            assert not waiter.done()
            recover.assert_not_called()
            release_owner.set()

            if outcome in {"error", "cancelled"}:
                expected_error = (
                    OSError if outcome == "error" else CancelledError
                )
                with pytest.raises(expected_error, match="synthetic fetch"):
                    owner.result(timeout=3)
            elif outcome == "empty":
                assert owner.result(timeout=3) is None
            else:
                assert owner.result(timeout=3)["content"] == "owner content"

            expected_content = (
                "owner content" if outcome == "success" else "recovered content"
            )
            assert waiter.result(timeout=3)["content"] == expected_content
        finally:
            release_owner.set()

    if outcome == "success":
        recover.assert_not_called()
    else:
        recover.assert_called_once_with()

    unexpected_fetch = Mock(
        side_effect=AssertionError("cached result was lost")
    )
    assert (
        cache.get_or_fetch(url, unexpected_fetch)["content"] == expected_content
    )
    unexpected_fetch.assert_not_called()