~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
fix(tests): skip queue processor startup under pytest (#5055)
Chanhyo Jungf4aac887b323
diff --git a/changelog.d/+test-queue-processor-env-gate.misc.md b/changelog.d/+test-queue-processor-env-gate.misc.mdnew file mode 100644index 0000000000..ec4925139c--- /dev/null+++ b/changelog.d/+test-queue-processor-env-gate.misc.md@@ -0,0 +1 @@+Pytest app fixtures now skip starting the background research queue processor by default, while `LDR_WEB_QUEUE_PROCESSOR_ENABLED=true` keeps an explicit opt-in for tests that exercise queue behavior.diff --git a/docs/CONFIGURATION.md b/docs/CONFIGURATION.mdindex e58207d334..d399fda712 100644--- a/docs/CONFIGURATION.md+++ b/docs/CONFIGURATION.md@@ -57,6 +57,7 @@ They are not available in the Web UI because they are needed to start the applic| `LDR_SECURITY_WEBSOCKET_ALLOWED_ORIGINS` | String | `None` | No | | Allowed origins for WebSocket/Socket.IO connections (comma-separated). Unset or empty means same-origin only (default); use '*' to allow all origins. Example: 'https://example.com,https://app.example.com' | Security | || `LDR_SERVER_MAX_CONCURRENT_RESEARCH` | Integer | `10` | No | 1..1000 | Server-wide maximum concurrent research operations. Requires restart. | Server | LDR_MAX_GLOBAL_CONCURRENT || `LDR_TESTING_TEST_MODE` | Boolean | `False` | No | | Enable test mode (adds delays for testing concurrency) | Testing | |+| `LDR_WEB_QUEUE_PROCESSOR_ENABLED` | Boolean | `True` | No | | Enable or disable the background research queue processor | Queue Processor | |## Settings Listdiff --git a/src/local_deep_research/settings/env_definitions/__init__.py b/src/local_deep_research/settings/env_definitions/__init__.pyindex f650d190a7..f7760f12d3 100644--- a/src/local_deep_research/settings/env_definitions/__init__.py+++ b/src/local_deep_research/settings/env_definitions/__init__.py@@ -11,6 +11,7 @@ from .db_config import DB_CONFIG_SETTINGSfrom .news_scheduler import NEWS_SCHEDULER_SETTINGSfrom .security import SECURITY_SETTINGSfrom .server import SERVER_SETTINGS+from .queue_processor import QUEUE_PROCESSOR_SETTINGS# Combine all settings for easy accessALL_SETTINGS: Dict[str, List[EnvSetting[Any]]] = {@@ -20,6 +21,7 @@ ALL_SETTINGS: Dict[str, List[EnvSetting[Any]]] = {"news_scheduler": cast(List[EnvSetting[Any]], NEWS_SCHEDULER_SETTINGS),"security": cast(List[EnvSetting[Any]], SECURITY_SETTINGS),"server": cast(List[EnvSetting[Any]], SERVER_SETTINGS),+ "queue_processor": cast(List[EnvSetting[Any]], QUEUE_PROCESSOR_SETTINGS),}__all__ = [@@ -29,5 +31,6 @@ __all__ = ["NEWS_SCHEDULER_SETTINGS","SECURITY_SETTINGS","SERVER_SETTINGS",+ "QUEUE_PROCESSOR_SETTINGS","ALL_SETTINGS",]diff --git a/src/local_deep_research/settings/env_definitions/queue_processor.py b/src/local_deep_research/settings/env_definitions/queue_processor.pynew file mode 100644index 0000000000..738267a71e--- /dev/null+++ b/src/local_deep_research/settings/env_definitions/queue_processor.py@@ -0,0 +1,12 @@+"""Background queue processor environment settings."""++from ..env_settings import BooleanSetting+++QUEUE_PROCESSOR_SETTINGS = [+ BooleanSetting(+ key="web.queue_processor.enabled",+ description="Enable or disable the background research queue processor",+ default=True,+ ),+]diff --git a/src/local_deep_research/web/app_factory.py b/src/local_deep_research/web/app_factory.pyindex 6e54509cc0..e5dcaf3412 100644--- a/src/local_deep_research/web/app_factory.py+++ b/src/local_deep_research/web/app_factory.py@@ -383,12 +383,32 @@ def create_app():logger.info("Error handlers registered successfully")# Start the queue processor v2 (uses encrypted databases)- # Always start the processor - it will handle per-user queue modes- logger.info("Starting queue processor v2...")- from .queue.processor_v2 import queue_processor+ # Enabled by default in normal app runs, but default-off under tests so+ # function-scoped app fixtures don't spawn a daemon thread per test.+ logger.info("Checking queue processor v2 startup...")+ from ..settings.env_registry import get_env_setting, registry- queue_processor.start()- logger.info("Started research queue processor v2")+ queue_processor_enabled = get_env_setting(+ "web.queue_processor.enabled", True+ )+ queue_processor_setting = registry.get_setting_object(+ "web.queue_processor.enabled"+ )+ queue_processor_env_set = bool(+ queue_processor_setting and queue_processor_setting.is_set+ )+ test_env_default_off = bool(os.getenv("PYTEST_CURRENT_TEST"))+ if test_env_default_off and not queue_processor_env_set:+ queue_processor_enabled = False++ logger.info(f"Queue processor v2 enabled: {queue_processor_enabled}")+ if queue_processor_enabled:+ from .queue.processor_v2 import queue_processor++ queue_processor.start()+ logger.info("Started research queue processor v2")+ else:+ logger.info("Queue processor v2 disabled - not starting")logger.info("App factory completed successfully")diff --git a/src/local_deep_research/web/auth/queue_middleware.py b/src/local_deep_research/web/auth/queue_middleware.pyindex 367d8416d6..91001e5679 100644--- a/src/local_deep_research/web/auth/queue_middleware.py+++ b/src/local_deep_research/web/auth/queue_middleware.py@@ -7,7 +7,11 @@ from loguru import loggerfrom ...database.encrypted_db import db_managerfrom ...database.session_context import get_user_db_session-from ..queue.processor_v2 import queue_processor++# Keep the singleton import lazy so disabled pytest app startup does not+# initialize the queue processor. Tests can still replace this module-level+# seam with a lightweight mock.+queue_processor = Nonedef process_pending_queue_operations():@@ -31,6 +35,10 @@ def process_pending_queue_operations():returntry:+ processor = queue_processor+ if processor is None:+ from ..queue.processor_v2 import queue_processor as processor+# Use the session context manager to properly handle the sessionwith get_user_db_session(username) as db_session:if not db_session:@@ -40,7 +48,7 @@ def process_pending_queue_operations():return# Process any pending operations for this user- started_count = queue_processor.process_pending_operations_for_user(+ started_count = processor.process_pending_operations_for_user(username, db_session)diff --git a/tests/settings/test_generate_config_docs.py b/tests/settings/test_generate_config_docs.pyindex f4ad08a1ed..4dc692978f 100644--- a/tests/settings/test_generate_config_docs.py+++ b/tests/settings/test_generate_config_docs.py@@ -44,6 +44,7 @@ EXPECTED_ENV_MODULES = {"news_scheduler","security","testing",+ "queue_processor",}@@ -101,7 +102,7 @@ class TestGetEnvOnlySettings:"""Test env_definitions AST extraction."""def test_returns_all_modules(self):- """All 5 env_definitions modules should be represented."""+ """All env_definitions modules should be represented."""settings = get_env_only_settings()categories = {s["category"].lower().replace(" ", "_") for s in settings}for module in EXPECTED_ENV_MODULES:diff --git a/tests/web/test_app_factory_coverage.py b/tests/web/test_app_factory_coverage.pyindex 57e53193d4..d448b67672 100644--- a/tests/web/test_app_factory_coverage.py+++ b/tests/web/test_app_factory_coverage.py@@ -16,9 +16,6 @@ from unittest.mock import MagicMock, patchfrom flask import FlaskMODULE = "local_deep_research.web.app_factory"-_QUEUE_PROCESSOR_MOD = (- "local_deep_research.web.queue.processor_v2.queue_processor"-)# ---------------------------------------------------------------------------@@ -254,7 +251,6 @@ class TestCreateAppSecretKeyBranches:"local_deep_research.config.paths.get_data_directory",return_value=tmp_path,),- patch(_QUEUE_PROCESSOR_MOD),patch("atexit.register"),):from local_deep_research.web.app_factory import create_app@@ -273,7 +269,6 @@ class TestCreateAppSecretKeyBranches:"local_deep_research.config.paths.get_data_directory",return_value=key_dir,),- patch(_QUEUE_PROCESSOR_MOD),patch("atexit.register"),):from local_deep_research.web.app_factory import create_app@@ -305,7 +300,6 @@ class TestCreateAppSecretKeyBranches:"local_deep_research.config.paths.get_data_directory",return_value=tmp_path,),- patch(_QUEUE_PROCESSOR_MOD),patch("atexit.register"),patch("builtins.open", side_effect=_open_raises_for_secret_key),):@@ -329,7 +323,9 @@ class TestCreateAppNewsScheduler:"""When news scheduler is disabled, app.background_job_scheduler is None."""with (patch(MODULE + ".SocketIOService"),- patch(_QUEUE_PROCESSOR_MOD),+ patch(MODULE + ".apply_middleware"),+ patch(MODULE + ".register_blueprints"),+ patch(MODULE + ".register_error_handlers"),patch("atexit.register"),patch("local_deep_research.settings.env_registry.get_env_setting",@@ -345,7 +341,9 @@ class TestCreateAppNewsScheduler:"""When scheduler init throws, app.background_job_scheduler is None."""with (patch(MODULE + ".SocketIOService"),- patch(_QUEUE_PROCESSOR_MOD),+ patch(MODULE + ".apply_middleware"),+ patch(MODULE + ".register_blueprints"),+ patch(MODULE + ".register_error_handlers"),patch("atexit.register"),patch("local_deep_research.settings.env_registry.get_env_setting",@@ -362,6 +360,181 @@ class TestCreateAppNewsScheduler:assert app.background_job_scheduler is None+class TestCreateAppQueueProcessor:+ """Tests for queue processor startup gating."""++ @staticmethod+ def _stub_queue_processor(monkeypatch):+ """Install a lightweight queue processor stub for app_factory imports."""+ import sys+ import types++ mock_queue_processor = MagicMock()+ queue_pkg = types.ModuleType("local_deep_research.web.queue")+ queue_pkg.__path__ = []+ processor_mod = types.ModuleType(+ "local_deep_research.web.queue.processor_v2"+ )+ setattr(processor_mod, "queue_processor", mock_queue_processor)++ monkeypatch.setitem(+ sys.modules, "local_deep_research.web.queue", queue_pkg+ )+ monkeypatch.setitem(+ sys.modules,+ "local_deep_research.web.queue.processor_v2",+ processor_mod,+ )+ return mock_queue_processor++ def test_queue_processor_env_setting_is_registered(self):+ """The queue processor has an env-only setting operators can flip."""+ from local_deep_research.settings.env_registry import registry++ assert (+ registry.get_env_var("web.queue_processor.enabled")+ == "LDR_WEB_QUEUE_PROCESSOR_ENABLED"+ )++ def test_queue_processor_disabled_during_pytest(self, monkeypatch):+ """Real pytest app startup does not import or start the queue processor."""+ import sys++ monkeypatch.setenv(+ "PYTEST_CURRENT_TEST", "tests/web/test_app_factory.py::test_app"+ )+ monkeypatch.setenv("LDR_NEWS_SCHEDULER_ENABLED", "false")+ monkeypatch.delenv("LDR_WEB_QUEUE_PROCESSOR_ENABLED", raising=False)+ for module_name in (+ "local_deep_research.web.auth.queue_middleware",+ "local_deep_research.web.queue.processor_v2",+ ):+ monkeypatch.delitem(sys.modules, module_name, raising=False)++ with (+ patch(MODULE + ".SocketIOService"),+ patch(MODULE + ".register_blueprints"),+ patch(MODULE + ".register_error_handlers"),+ patch("atexit.register"),+ ):+ from local_deep_research.web.app_factory import create_app++ create_app()++ assert "local_deep_research.web.queue.processor_v2" not in sys.modules++ def test_queue_processor_can_opt_in_under_pytest(self, monkeypatch):+ """Tests that need the real queue processor can opt in explicitly."""+ monkeypatch.setenv(+ "PYTEST_CURRENT_TEST", "tests/web/test_app_factory.py::test_app"+ )+ monkeypatch.setenv("LDR_NEWS_SCHEDULER_ENABLED", "false")+ monkeypatch.setenv("LDR_WEB_QUEUE_PROCESSOR_ENABLED", "true")+ mock_queue_processor = self._stub_queue_processor(monkeypatch)++ with (+ patch(MODULE + ".SocketIOService"),+ patch(MODULE + ".apply_middleware"),+ patch(MODULE + ".register_blueprints"),+ patch(MODULE + ".register_error_handlers"),+ patch("atexit.register"),+ ):+ from local_deep_research.web.app_factory import create_app++ create_app()+ mock_queue_processor.start.assert_called_once_with()++ def test_queue_processor_still_starts_when_only_ci_is_set(+ self, monkeypatch+ ):+ """CI alone is not treated as test-mode opt-out for the queue."""+ monkeypatch.setenv("CI", "true")+ monkeypatch.setenv("LDR_NEWS_SCHEDULER_ENABLED", "false")+ monkeypatch.delenv("TESTING", raising=False)+ monkeypatch.delenv("PYTEST_CURRENT_TEST", raising=False)+ monkeypatch.delenv("LDR_TESTING_TEST_MODE", raising=False)+ monkeypatch.delenv("LDR_WEB_QUEUE_PROCESSOR_ENABLED", raising=False)+ mock_queue_processor = self._stub_queue_processor(monkeypatch)++ with (+ patch(MODULE + ".SocketIOService"),+ patch(MODULE + ".apply_middleware"),+ patch(MODULE + ".register_blueprints"),+ patch(MODULE + ".register_error_handlers"),+ patch("atexit.register"),+ ):+ from local_deep_research.web.app_factory import create_app++ create_app()+ mock_queue_processor.start.assert_called_once_with()++ def test_queue_processor_still_starts_when_testing_env_is_false(+ self, monkeypatch+ ):+ """TESTING=false is parsed as false, not as an opt-out signal."""+ monkeypatch.setenv("TESTING", "false")+ monkeypatch.setenv("LDR_NEWS_SCHEDULER_ENABLED", "false")+ monkeypatch.delenv("PYTEST_CURRENT_TEST", raising=False)+ monkeypatch.delenv("LDR_TESTING_TEST_MODE", raising=False)+ monkeypatch.delenv("LDR_WEB_QUEUE_PROCESSOR_ENABLED", raising=False)+ mock_queue_processor = self._stub_queue_processor(monkeypatch)++ with (+ patch(MODULE + ".SocketIOService"),+ patch(MODULE + ".apply_middleware"),+ patch(MODULE + ".register_blueprints"),+ patch(MODULE + ".register_error_handlers"),+ patch("atexit.register"),+ ):+ from local_deep_research.web.app_factory import create_app++ create_app()+ mock_queue_processor.start.assert_called_once_with()++ def test_queue_processor_still_starts_when_testing_env_is_true(+ self, monkeypatch+ ):+ """TESTING=true is a dev-server mode, not a pytest opt-out signal."""+ monkeypatch.setenv("TESTING", "true")+ monkeypatch.setenv("LDR_NEWS_SCHEDULER_ENABLED", "false")+ monkeypatch.delenv("PYTEST_CURRENT_TEST", raising=False)+ monkeypatch.delenv("LDR_TESTING_TEST_MODE", raising=False)+ monkeypatch.delenv("LDR_WEB_QUEUE_PROCESSOR_ENABLED", raising=False)+ mock_queue_processor = self._stub_queue_processor(monkeypatch)++ with (+ patch(MODULE + ".SocketIOService"),+ patch(MODULE + ".apply_middleware"),+ patch(MODULE + ".register_blueprints"),+ patch(MODULE + ".register_error_handlers"),+ patch("atexit.register"),+ ):+ from local_deep_research.web.app_factory import create_app++ create_app()+ mock_queue_processor.start.assert_called_once_with()++ def test_queue_processor_starts_with_runtime_test_mode(self, monkeypatch):+ """Runtime concurrency test mode does not disable queue processing."""+ monkeypatch.setenv("LDR_TESTING_TEST_MODE", "true")+ monkeypatch.setenv("LDR_NEWS_SCHEDULER_ENABLED", "false")+ monkeypatch.delenv("PYTEST_CURRENT_TEST", raising=False)+ monkeypatch.delenv("LDR_WEB_QUEUE_PROCESSOR_ENABLED", raising=False)+ mock_queue_processor = self._stub_queue_processor(monkeypatch)++ with (+ patch(MODULE + ".SocketIOService"),+ patch(MODULE + ".apply_middleware"),+ patch(MODULE + ".register_blueprints"),+ patch(MODULE + ".register_error_handlers"),+ patch("atexit.register"),+ ):+ from local_deep_research.web.app_factory import create_app++ create_app()+ mock_queue_processor.start.assert_called_once_with()++# ---------------------------------------------------------------------------# 5. apply_middleware: teardown and WebSocket handler# ---------------------------------------------------------------------------@@ -375,7 +548,6 @@ class TestApplyMiddlewareTeardown:"""Create a full app and return it for request-context testing."""with (patch(MODULE + ".SocketIOService"),- patch(_QUEUE_PROCESSOR_MOD),patch("atexit.register"),):from local_deep_research.web.app_factory import create_app@@ -431,7 +603,6 @@ class TestRegisterBlueprintsCsrfExemption:"""api_v1 blueprint is exempted from CSRF when extension is present."""with (patch(MODULE + ".SocketIOService"),- patch(_QUEUE_PROCESSOR_MOD),patch("atexit.register"),):from local_deep_research.web.app_factory import create_app@@ -479,7 +650,6 @@ class TestStaticAndFaviconRoutes:def app(self):with (patch(MODULE + ".SocketIOService"),- patch(_QUEUE_PROCESSOR_MOD),patch("atexit.register"),):from local_deep_research.web.app_factory import create_app