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.0 KB
Raw Blame
"""Ownership handoff when a shared content cache is cleared mid-fetch."""

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("first_outcome", ["empty", "exception", "cancelled"])
def test_clear_keeps_the_actual_replacement_owner_and_waiter(
    monkeypatch, first_outcome
):
    cache = ResearchContentCache()
    url = "https://example.com/source"
    key = content_cache_key(url)
    first_started, release_first = Event(), Event()
    second_started, release_second = Event(), Event()
    waiter_started = Event()
    unexpected_fetch = Mock(side_effect=AssertionError("duplicate fetch"))

    def first_fetch():
        first_started.set()
        assert release_first.wait(10)
        if first_outcome == "exception":
            raise OSError("synthetic failure")
        if first_outcome == "cancelled":
            raise CancelledError("synthetic cancellation")
        return

    def second_fetch():
        second_started.set()
        assert release_second.wait(10)
        return {"content": "replacement result"}

    with ThreadPoolExecutor(max_workers=3) as executor:
        first = executor.submit(cache.get_or_fetch, url, first_fetch)
        try:
            assert first_started.wait(5)
            cache.clear()
            second = executor.submit(cache.get_or_fetch, url, second_fetch)
            assert second_started.wait(5)
            with cache._lock:
                second_event = cache._inflight[key]
            release_first.set()
            if first_outcome == "empty":
                assert first.result(timeout=5) is None
            else:
                error = (
                    OSError if first_outcome == "exception" else CancelledError
                )
                with pytest.raises(error):
                    first.result(timeout=5)
            assert cache._inflight.get(key) is second_event

            real_wait = second_event.wait

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

            monkeypatch.setattr(second_event, "wait", observe_wait)
            waiter = executor.submit(
                cache.get_or_fetch, url, unexpected_fetch, wait_timeout=5
            )
            assert waiter_started.wait(5)
            assert not waiter.done()
            unexpected_fetch.assert_not_called()
            release_second.set()
            assert second.result(timeout=5)["content"] == "replacement result"
            assert waiter.result(timeout=5)["content"] == "replacement result"
        finally:
            release_first.set()
            release_second.set()

    unexpected_fetch.assert_not_called()
    assert cache._inflight == {}
    assert (
        cache.get_or_fetch(url, unexpected_fetch)["content"]
        == "replacement result"
    )