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.

6.3 KB
Raw Blame
"""Tests for threading_utils module."""

from concurrent.futures import ThreadPoolExecutor  # noqa: E402

import pytest  # noqa: E402

from local_deep_research.utilities.threading_utils import (  # noqa: E402
    g_thread_local_store,
    thread_specific_cache,
)


class TestThreadSpecificCache:
    """Tests for thread_specific_cache decorator."""

    def setup_method(self):
        """Clear thread-local data before each test."""
        if hasattr(g_thread_local_store, "thread_id"):
            delattr(g_thread_local_store, "thread_id")

    def test_caches_result_in_same_thread(self):
        """Should cache results within the same thread."""
        call_count = 0

        @thread_specific_cache({})
        def expensive_func(x):
            nonlocal call_count
            call_count += 1
            return x * 2

        # First call
        result1 = expensive_func(5)
        # Second call with same arg
        result2 = expensive_func(5)

        assert result1 == 10
        assert result2 == 10
        assert call_count == 1  # Should only be called once

    def test_different_args_not_cached(self):
        """Different arguments should not use cached result."""
        call_count = 0

        @thread_specific_cache({})
        def func(x):
            nonlocal call_count
            call_count += 1
            return x * 2

        result1 = func(5)
        result2 = func(10)

        assert result1 == 10
        assert result2 == 20
        assert call_count == 2

    def test_cache_isolated_between_threads(self):
        """Cache should be isolated between threads."""
        call_counts = {"main": 0, "other": 0}

        @thread_specific_cache({})
        def func(thread_name, x):
            call_counts[thread_name] += 1
            return x * 2

        # Call in main thread
        result_main = func("main", 5)

        # Call in other thread with same args
        def other_thread():
            return func("other", 5)

        with ThreadPoolExecutor(max_workers=1) as executor:
            future = executor.submit(other_thread)
            result_other = future.result()

        assert result_main == 10
        assert result_other == 10
        # Both threads should have called the function
        assert call_counts["main"] == 1
        assert call_counts["other"] == 1

    def test_assigns_unique_thread_id(self):
        """Should assign unique thread ID to each thread."""
        thread_ids = []

        @thread_specific_cache({})
        def func():
            thread_ids.append(g_thread_local_store.thread_id)
            return "done"

        func()
        main_id = g_thread_local_store.thread_id

        def other_thread():
            func()
            return g_thread_local_store.thread_id

        with ThreadPoolExecutor(max_workers=1) as executor:
            future = executor.submit(other_thread)
            other_id = future.result()

        assert main_id != other_id

    def test_reuses_thread_id_within_thread(self):
        """Should reuse same thread ID for multiple calls in same thread."""

        @thread_specific_cache({})
        def func(x):
            return x

        func(1)
        id1 = g_thread_local_store.thread_id
        func(2)
        id2 = g_thread_local_store.thread_id

        assert id1 == id2

    def test_handles_kwargs(self):
        """Should properly handle keyword arguments."""
        call_count = 0

        @thread_specific_cache({})
        def func(a, b=None):
            nonlocal call_count
            call_count += 1
            return (a, b)

        result1 = func(1, b=2)
        result2 = func(1, b=2)

        assert result1 == (1, 2)
        assert result2 == (1, 2)
        assert call_count == 1


class TestIntegration:
    """Integration tests for threading utilities."""

    def test_thread_specific_cache_with_thread_context(self):
        """Should work correctly with thread context propagation."""
        call_count = 0

        @thread_specific_cache({})
        def cached_func(x):
            nonlocal call_count
            call_count += 1
            return x * 2

        # Main thread calls
        cached_func(5)
        cached_func(5)  # Should use cache

        results = []

        def worker():
            # Worker thread should have its own cache
            return cached_func(5)

        with ThreadPoolExecutor(max_workers=2) as executor:
            futures = [executor.submit(worker) for _ in range(3)]
            results = [f.result() for f in futures]

        # All results should be correct
        assert all(r == 10 for r in results)
        # Main thread called once, each worker thread called once
        # (some workers may share thread due to pool reuse)
        assert call_count >= 2  # At minimum: main + 1 worker


class TestEdgeCases:
    """Edge case tests for threading utilities."""

    def setup_method(self):
        """Clear thread-local data before each test."""
        if hasattr(g_thread_local_store, "thread_id"):
            delattr(g_thread_local_store, "thread_id")

    def test_thread_specific_cache_with_unhashable_args(self):
        """Should raise error for unhashable arguments."""

        @thread_specific_cache({})
        def func(x):
            return x

        # Lists are unhashable
        with pytest.raises(TypeError):
            func([1, 2, 3])

    def test_thread_specific_cache_with_none_arg(self):
        """Should handle None arguments correctly."""
        call_count = 0

        @thread_specific_cache({})
        def func(x):
            nonlocal call_count
            call_count += 1
            return x is None

        result1 = func(None)
        result2 = func(None)

        assert result1 is True
        assert result2 is True
        assert call_count == 1

    def test_nested_decorators(self):
        """Should work with other decorators."""
        from functools import wraps  # noqa: E402

        def logging_decorator(func):
            @wraps(func)
            def wrapper(*args, **kwargs):
                return func(*args, **kwargs)

            return wrapper

        call_count = 0

        @logging_decorator
        @thread_specific_cache({})
        def func(x):
            nonlocal call_count
            call_count += 1
            return x

        result1 = func(5)
        result2 = func(5)

        assert result1 == 5
        assert result2 == 5
        assert call_count == 1