fix: 保持搜索线程任务的请求上下文 (#6451)

This commit is contained in:
InfinityPacer
2026-08-25 16:06:17 +08:00
committed by GitHub
parent d58c8d2b17
commit f3622de82e
7 changed files with 197 additions and 10 deletions
+42
View File
@@ -2,9 +2,11 @@ import asyncio
from types import SimpleNamespace
from unittest.mock import AsyncMock, patch
from app.adapters.network.http import RequestUtils
from app.chain.search import SearchChain
from app.modules.indexer import IndexerModule
from app.runtime.config import settings
from app.runtime.correlation import CORRELATION_ID_HEADER, correlation_scope
def make_chain() -> SearchChain:
@@ -67,6 +69,46 @@ def test_search_invokes_plugin_once_with_multiple_indexers():
assert len(results) == 3
def test_sync_site_search_propagates_request_context():
"""同步站点 worker 的出站请求头应保留触发搜索的关联 ID。"""
chain = make_chain()
observed_headers = []
chain.search_plugin_torrents = lambda **_kwargs: []
def search_site_torrents(**_kwargs):
RequestUtils().get_res("https://indexer.example/search")
return []
def request(_method, _url, **kwargs):
observed_headers.append(kwargs["headers"])
return object()
chain.search_site_torrents = search_site_torrents
with (
patch.object(settings, "SEARCH_RESOURCE_PAGES", 1, create=True),
patch("app.adapters.network.http.requests.request", side_effect=request),
patch("app.chain.search.get_configured_system_config") as system_config_oper,
patch("app.chain.search.SitesHelper") as sites_helper,
patch("app.chain.search.ProgressHelper") as progress_helper,
):
system_config_oper.return_value.get.return_value = [1, 2]
sites_helper.return_value.get_indexers.return_value = [
{"id": 1, "name": "站点一"},
{"id": 2, "name": "站点二"},
]
progress_helper.return_value = SimpleNamespace(
start=lambda: None, update=lambda **_kwargs: None, end=lambda: None
)
with correlation_scope("search-request"):
chain._SearchChain__search_all_sites(keyword="keyword")
assert [headers[CORRELATION_ID_HEADER] for headers in observed_headers] == [
"search-request",
"search-request",
]
def test_async_search_returns_plugin_results_without_indexers():
"""异步搜索应支持只有插件资源源的部署方式。"""
chain = make_chain()