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.1 KB
Raw Blame
"""One document pass per user at a time (#6722).

The interval job ``{username}_document_processing`` and the manual trigger
``{username}_document_processing_manual`` are separate APScheduler jobs, and
``max_instances=1`` limits each job only by its own id. A manual trigger during
a scheduled pass therefore started a second ``_process_user_documents`` for the
same user, concurrently with the first. The pass itself now skips while another
one for the same user is running.
"""

import threading
from unittest.mock import MagicMock, patch

import pytest

from local_deep_research.scheduler.background import (
    BackgroundJobScheduler,
    _one_document_pass_per_user,
)


@pytest.fixture
def scheduler():
    BackgroundJobScheduler._instance = None
    with patch(
        "local_deep_research.scheduler.background.BackgroundScheduler"
    ) as background_scheduler:
        background_scheduler.return_value = MagicMock()
        instance = BackgroundJobScheduler()
        yield instance
    BackgroundJobScheduler._instance = None


def test_a_second_pass_for_the_same_user_skips_while_the_first_runs(
    scheduler,
):
    scheduler.user_sessions = {
        "alice": {"scheduled_jobs": set()},
        "bob": {"scheduled_jobs": set()},
    }
    inside = threading.Event()
    release = threading.Event()
    lookups = []

    def retrieve(username):
        # The first thing a pass does after finding the user's session. Hold
        # alice's first pass here; no credentials then end it cleanly.
        lookups.append(username)
        if username == "alice" and not release.is_set():
            inside.set()
            release.wait(5)

    scheduler._credential_store = MagicMock()
    scheduler._credential_store.retrieve.side_effect = retrieve

    first = threading.Thread(
        target=scheduler._process_user_documents, args=("alice",)
    )
    first.start()
    try:
        assert inside.wait(2), "the first pass never started"

        # The same user again, as a manual trigger mid-pass would: skipped,
        # at once rather than after waiting for the first pass.
        second = threading.Thread(
            target=scheduler._process_user_documents, args=("alice",)
        )
        second.start()
        second.join(0.5)
        assert not second.is_alive(), "the second pass waited for the first"
        assert lookups == ["alice"]

        # Another user's pass is not held up by alice's.
        scheduler._process_user_documents("bob")
        assert lookups == ["alice", "bob"]
    finally:
        release.set()
        first.join(2)

    # Once the first pass is over, alice's next one runs.
    scheduler._process_user_documents("alice")
    assert lookups == ["alice", "bob", "alice"]


def test_a_pass_that_raises_still_lets_the_next_one_run():
    runs = []

    class Owner:
        @_one_document_pass_per_user
        def process(self, username):
            runs.append(username)
            raise RuntimeError("pass failed")

    owner = Owner()
    with pytest.raises(RuntimeError):
        owner.process("carol")
    with pytest.raises(RuntimeError):
        owner.process("carol")

    assert runs == ["carol", "carol"]