~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
test(queue): make _process_queue_loop test exit unconditional, not mock-dependent (#6195)
LearningCircuit6ad3ec8250b9
diff --git a/tests/web/queue/test_processor_v2_real.py b/tests/web/queue/test_processor_v2_real.pyindex 7abdf25c58..298e282c4b 100644--- a/tests/web/queue/test_processor_v2_real.py+++ b/tests/web/queue/test_processor_v2_real.py@@ -16,6 +16,7 @@ Tests cover:"""import threading+import timefrom unittest.mock import Mock, MagicMock, patchimport pytest@@ -35,7 +36,7 @@ def _make_processor():def _stop_loop_after_call(processor, fn=None):"""Wrap a ``_process_user_queue`` replacement so the FIRST call itreceives deterministically stops ``_process_queue_loop`` after the- current iteration, with no thread and no sleep.+ current iteration, without needing any sleep.``_process_queue_loop`` captures the whole ``_users_to_check`` set atthe top of each iteration and processes all of it before re-checking@@ -63,6 +64,113 @@ def _stop_loop_after_call(processor, fn=None):return _wrapped+# Hard wall-clock bound for one _process_queue_loop run in tests. Must stay+# far below pytest's 180s global timeout (pyproject.toml) so a hung loop+# fails THIS test fast instead of stalling the whole suite/CI run.+LOOP_BOUND_TIMEOUT = 10.0+++def _run_queue_loop_bounded(processor, timeout=LOOP_BOUND_TIMEOUT):+ """Run ``_process_queue_loop`` in a daemon thread under a hard+ wall-clock bound, mirroring how production ``start()`` runs it.++ The bound is a REAL external deadline enforced from the TEST thread:+ ``Thread.join(timeout)`` expires no matter what the loop does or+ fails to do inside, so it does not depend on execution reaching ANY+ production seam - not the tail ``self._stop_event.wait(...)``, not+ ``_process_user_queue``, nor any other call a regression could+ skip, move, or block. The shapes that actually hang without this+ bound - a ``continue`` or early ``return`` placed before the body's+ ``_process_user_queue`` call (so the mock stop never fires), a+ blocking call in the loop body, or a loop that ignores both stop+ signals - become a fast FAILURE via ``pytest.fail`` after+ ``timeout`` seconds instead of a hang until pytest's 180s global+ timeout. Shapes that leave the ``_process_user_queue`` call+ reachable (a ``continue`` after the body, a deleted or moved tail+ wait) do not need the bound: ``_stop_loop_after_call`` flips+ ``running = False`` during that call, so ``while self.running:``+ ends the loop on its next check regardless.++ The happy path is unchanged: ``_stop_loop_after_call`` flips+ ``running`` off during the first ``_process_user_queue`` call, the+ loop's tail wait returns immediately (the event is already set),+ and the thread finishes in milliseconds. ``join`` returns only once+ the thread is dead, so caller assertions (mock call counts,+ ``_users_to_check`` contents) run after full thread termination -+ no race with loop code still in flight.++ A worker exception is never silently lost: ``Thread.join()`` does+ NOT propagate exceptions raised by the target, so the loop runs+ inside a capturing shim and any ``BaseException`` the worker died+ from is re-raised in THIS thread after a completed join. A+ production change that makes ``_process_queue_loop`` raise (e.g.+ an error handler that stops swallowing ``_process_user_queue``+ failures) therefore fails the test, exactly like the direct+ synchronous invocation used before the loop moved behind a thread+ - instead of killing the worker silently while the caller's+ assertions still pass.++ On timeout, both production stop signals (``running = False`` and+ ``_stop_event.set()`` - exactly what ``stop()`` sends) are fired as+ a best-effort teardown before failing, plus a short grace join, so+ a loop that merely lost its pacing exits cleanly instead of+ spinning through the rest of the suite. A loop that ignores even+ those signals is daemon: it dies with the test process. That is a+ weaker guarantee than it sounds - "the test process" is the whole+ xdist worker, and ``pytest.fail`` unwinds the caller's+ ``patch.object`` block, restoring the REAL ``_process_user_queue``,+ so a worker outliving the grace join would run production code+ against live singletons for the rest of that worker's life. The+ current loop exits within milliseconds of ``running = False`` ++ ``_stop_event.set()``, so reaching that state needs a second,+ simultaneous regression that ignores both stop signals. If the+ worker nevertheless died from an exception (during the timeout or+ the grace join), that exception is named in the failure message+ rather than silently dropped.+ """+ worker_error = []++ def _loop_target():+ # Thread.join() does NOT propagate a target's exception: capture+ # the worker's death cause so it can be re-raised in this test+ # thread after a completed join, or named in the timeout failure.+ try:+ processor._process_queue_loop()+ except BaseException as exc:+ worker_error.append(exc)++ processor.running = True+ loop_thread = threading.Thread(+ target=_loop_target,+ name="test-bounded-queue-loop",+ daemon=True,+ )+ loop_thread.start()+ loop_thread.join(timeout)+ if loop_thread.is_alive():+ # Best-effort teardown first: production stop()'s two signals.+ processor.running = False+ processor._stop_event.set()+ loop_thread.join(timeout=2.0)+ failure = (+ f"_process_queue_loop did not return within {timeout}s "+ "wall-clock - loop-hang regression (e.g. an early "+ "'continue' bypassing the tail wait, or a blocking call in "+ "the loop body). This bound does not depend on any mocked "+ "production seam. Stop signals were fired after the "+ "timeout; loop thread still alive after 2s grace: "+ f"{loop_thread.is_alive()}"+ )+ if worker_error:+ failure += f" Worker thread also raised: {worker_error[0]!r}"+ pytest.fail(failure)+ if worker_error:+ # Completed join and the worker died from an exception: re-raise+ # it here so pytest reports the worker failure instead of the+ # silently-dead-thread false green the bare join() would give.+ raise worker_error[0]++def test_constructor_is_inert_until_explicit_start():"""Construction initializes state without leaving behind any newthread that is still alive when this function's second snapshot is@@ -509,10 +617,9 @@ class TestProcessQueueLoop:"_process_user_queue",side_effect=_stop_loop_after_call(processor, lambda u, s: True),) as mock_pq:- processor.running = True- processor._process_queue_loop()+ _run_queue_loop_bounded(processor)- mock_pq.assert_called_with("alice", "sess-abc")+ mock_pq.assert_called_with("alice", "sess-abc")def test_removes_users_with_empty_queues(self):"""Users whose queues are empty get removed from check set."""@@ -528,8 +635,7 @@ class TestProcessQueueLoop:"_process_user_queue",side_effect=_stop_loop_after_call(processor, side_effect),):- processor.running = True- processor._process_queue_loop()+ _run_queue_loop_bounded(processor)assert ("alice", "sess1") not in processor._users_to_checkassert ("bob", "sess2") in processor._users_to_check@@ -546,12 +652,108 @@ class TestProcessQueueLoop:processor,"_process_user_queue",side_effect=_stop_loop_after_call(processor, raise_transient),- ):- processor.running = True- processor._process_queue_loop()+ ) as mock_pq:+ _run_queue_loop_bounded(processor)+ # Positive control: the "kept" assertion below is vacuous on its+ # own (the test populated the set itself), so pin that the loop+ # actually reached the raising call with the real arguments.+ mock_pq.assert_called_once_with("alice", "sess1")assert ("alice", "sess1") in processor._users_to_check+ def test_unstoppable_loop_fails_fast_not_global_timeout(self):+ """The wall-clock bound itself must catch a loop that nothing+ ever stops - the successor of the old seam-patch test.++ With an empty ``_users_to_check`` the real+ ``_process_queue_loop`` never calls ``_process_user_queue``, so+ no mock side effect ever flips ``running``: the loop keeps+ tail-waiting forever. For a daemon worker between ``stop()``+ calls that is honest production behavior, so what the test must+ guarantee is the BOUND: ``_run_queue_loop_bounded`` turns an+ unstoppable loop into a fast ``Failed`` (2s budget here, ~90x+ under pytest's 180s global timeout), never a suite-stalling+ hang. Nothing inside the loop is trusted to end it.+ """+ processor = _make_processor()+ assert processor._users_to_check == set()+ timeout = 2.0++ with patch.object(processor, "_process_user_queue") as mock_pq:+ started = time.monotonic()+ with pytest.raises(pytest.fail.Exception):+ _run_queue_loop_bounded(processor, timeout=timeout)+ elapsed = time.monotonic() - started++ mock_pq.assert_not_called()+ # Tied to the timeout this test passes, not a loose constant: a+ # bound that silently ignored `timeout=` (falling back to+ # LOOP_BOUND_TIMEOUT) or was raised must fail here rather than+ # burn extra CI time unnoticed. The mechanism resolves at+ # timeout + the 2s grace join; +4.0 is the CI-jitter margin.+ assert elapsed < timeout + 4.0++ def test_early_continue_regression_fails_fast_not_global_timeout(+ self,+ ):+ """A regression that bypasses every stop seam is still bounded.++ TEST-ONLY simulation (production code untouched): the loop is+ replaced with a regressed shape that ``continue``s before the+ body's ``_process_user_queue`` call, and therefore also before+ the tail ``_stop_event.wait(...)``. That loop never reaches the+ mock-driven exit - ``_stop_loop_after_call`` can only flip+ ``running`` from inside the call it wraps - never reaches the+ tail wait, and never sleeps: it hot-spins while ``running``+ stays True. This is the shape the bound exists for, and the one+ no in-loop seam can catch. Only the bound's own deadline and+ its ``running = False`` flip can end it, which is precisely+ what this test exercises: the failure must arrive in seconds,+ not at pytest's 180s global timeout.+ """+ processor = _make_processor()+ timeout = 2.0++ def _loop_with_early_continue():+ while processor.running:+ # The regression under test: this continue skips the+ # tail self._stop_event.wait(self.check_interval).+ continue++ with patch.object(+ processor, "_process_queue_loop", _loop_with_early_continue+ ):+ started = time.monotonic()+ with pytest.raises(pytest.fail.Exception):+ _run_queue_loop_bounded(processor, timeout=timeout)+ elapsed = time.monotonic() - started++ # Tied to the timeout this test passes, so a bound that was+ # silently weakened (ignoring `timeout=`, or raised) fails here.+ assert elapsed < timeout + 4.0++ def test_worker_exception_surfaces_to_test_thread(self):+ """An exception raised inside the loop thread fails the test.++ ``Thread.join()`` does NOT propagate exceptions raised by the+ thread target. Without the helper's capture-and-re-raise+ wrapper, a production change that makes ``_process_queue_loop``+ raise (for example an error handler that stops swallowing+ ``_process_user_queue`` failures) would kill the worker thread+ silently: ``join`` returns normally and the caller's assertions+ still run. Before this file moved the loop behind a thread, the+ direct synchronous invocation propagated the exception and+ failed the test - this guard keeps that regression coverage.+ """+ processor = _make_processor()++ def _loop_that_raises():+ raise RuntimeError("worker-side failure must surface")++ with patch.object(processor, "_process_queue_loop", _loop_that_raises):+ with pytest.raises(RuntimeError, match="worker-side failure"):+ _run_queue_loop_bounded(processor)+# ---------------------------------------------------------------------------# _process_user_queue — slot calculation, empty queue, no password