Transforms complex documents like PDFs and Office docs into LLM-ready markdown/JSON for your Agentic workflows.
Python ai4sciencedocument-analysisdocxextract-datalayout-analysisocrparserpdfpdf-converterpdf-extractor-llmpdf-extractor-pretrainpdf-extractor-ragpdf-parserpptxpythonxlsx
feat(tests): enhance async parsing with background sleep and improve SDK isolation
myhlolif9ab614aa5db
diff --git a/tests/unittest/test_file_parse_http.py b/tests/unittest/test_file_parse_http.pyindex c18ff70f..279f27ab 100644--- a/tests/unittest/test_file_parse_http.py+++ b/tests/unittest/test_file_parse_http.py@@ -109,9 +109,9 @@ def test_saas_storage_all_formats_and_recovery(tmp_path: Path) -> None:state = Upstream()api, storage = _Server(cloud_bridge(state)), _Server(cloud_bridge(state))state.api_url, state.storage_url = api.url + "/api", storage.url- api.start()- storage.start()try:+ api.start()+ storage.start()with wrapper(tmp_path, state.api_url, "secret") as (client, app):app.state.service.client.retry_delay = 0state.redirect = True@@ -195,9 +195,9 @@ def test_tcp_disconnect_keeps_accepted_request(tmp_path: Path) -> None:state.upload_delay = 0.2api, storage = _Server(cloud_bridge(state)), _Server(cloud_bridge(state))state.api_url, state.storage_url = api.url + "/api", storage.url- api.start()- storage.start()try:+ api.start()+ storage.start()with wrapper(tmp_path, state.api_url, "secret") as (client, app):request = httpx.Request("POST",diff --git a/tests/unittest/test_http_api_example_script.py b/tests/unittest/test_http_api_example_script.pyindex e402f945..29bc4626 100644--- a/tests/unittest/test_http_api_example_script.py+++ b/tests/unittest/test_http_api_example_script.py@@ -10,33 +10,70 @@ import osfrom pathlib import Pathfrom typing import Any+import httpx+import pytest+from fastapi import FastAPI+class _Server:- """Run a FastAPI app on a pre-picked localhost port in a daemon thread."""+ """持有已绑定的本地 socket,在独立线程运行真实 FastAPI 服务。"""def __init__(self, app: Any, host: str = "127.0.0.1") -> None:+ """从分配端口到 Uvicorn 接管始终保留 socket,消除重复绑定的竞争窗口。"""import uvicorn- with socket.socket() as sock:- sock.bind((host, 0))- self.port = sock.getsockname()[1]- self.host = host- self.url = f"http://{host}:{self.port}"- self._server = uvicorn.Server(uvicorn.Config(app, host=host, port=self.port, log_level="error"))- self._thread = threading.Thread(target=self._server.run, daemon=True)+ self._socket = socket.socket()+ try:+ self._socket.bind((host, 0))+ self.port = self._socket.getsockname()[1]+ self.host = host+ self.url = f"http://{host}:{self.port}"+ self._server = uvicorn.Server(uvicorn.Config(app, host=host, port=self.port, log_level="error"))+ self._thread = threading.Thread(target=self._server.run, kwargs={"sockets": [self._socket]}, daemon=True)+ except BaseException:+ self._socket.close()+ raisedef start(self, timeout: float = 20.0) -> None:- self._thread.start()- deadline = time.monotonic() + timeout- while time.monotonic() < deadline:- if self._server.started:- return- time.sleep(0.05)- raise RuntimeError("test server did not start within timeout")+ """等待监听就绪;启动退出或超时时释放线程和预留 socket。"""+ try:+ self._thread.start()+ deadline = time.monotonic() + timeout+ while time.monotonic() < deadline:+ if self._server.started:+ return+ if not self._thread.is_alive():+ raise RuntimeError("test server exited before startup")+ time.sleep(0.05)+ raise RuntimeError("test server did not start within timeout")+ except BaseException:+ self.stop()+ raisedef stop(self, timeout: float = 10.0) -> None:+ """回收已启动的服务,也允许清理尚未启动或已经关闭的实例。"""self._server.should_exit = True- self._thread.join(timeout=timeout)+ try:+ if self._thread.ident is not None:+ self._thread.join(timeout=timeout)+ if self._thread.is_alive():+ raise RuntimeError("test server did not stop within timeout")+ finally:+ self._socket.close()+++def test_server_reserves_port_before_start() -> None:+ """服务创建后持续占用端口,并把同一 socket 交给真实 HTTP 服务。"""+ server = _Server(FastAPI())+ try:+ with socket.socket() as contender:+ with pytest.raises(OSError):+ contender.bind((server.host, server.port))+ server.start()+ with httpx.Client(trust_env=False) as client:+ assert client.get(f"{server.url}/openapi.json").status_code == 200+ finally:+ server.stop()def test_http_example_delegates_to_tasks(tmp_path: Path) -> None:diff --git a/tests/unittest/test_task_http.py b/tests/unittest/test_task_http.pyindex 09c2f456..6abdce1e 100644--- a/tests/unittest/test_task_http.py+++ b/tests/unittest/test_task_http.py@@ -678,7 +678,9 @@ class _CloudState:"tier": batch_effective_parse_tier(record["tier"], item["file_name"]),"duration_ms": 0,"parser_version": "test",- } if status == "completed" else None,+ }+ if status == "completed"+ else None,})states = [item["status"] for item in files]@@ -721,7 +723,8 @@ def _cloud_apps(state: _CloudState) -> tuple[FastAPI, FastAPI]:async def health() -> dict[str, Any]:"""原生支持是共同契约,不增加格式能力字段。"""return {- "status": "ok", "version": "test",+ "status": "ok",+ "version": "test","features": {"webhook": False, "task_output_formats": ["default"], "sources": ["upload"]},}@@ -743,7 +746,8 @@ def _cloud_apps(state: _CloudState) -> tuple[FastAPI, FastAPI]:try:selected_tier = resolve_task_tier(UploadTaskOptions.model_validate({key: value for key, value in submitted.items() if key != "files"}),- [TaskFileInput.model_validate(item) for item in manifest], set(state.pdf_tiers),+ [TaskFileInput.model_validate(item) for item in manifest],+ set(state.pdf_tiers),)except ApiServerError as error:return JSONResponse({"error": error.error.model_dump()}, status_code=error.status_code)@@ -843,15 +847,42 @@ def cloud() -> Iterator[_CloudState]:gateway, storage = _cloud_apps(state)api_server_instance, storage_server = _Server(gateway), _Server(storage)state.gateway_url, state.storage_url = api_server_instance.url, storage_server.url- storage_server.start()- api_server_instance.start()try:+ storage_server.start()+ api_server_instance.start()yield statefinally:api_server_instance.stop()storage_server.stop()+def test_cloud_start_failure_stops_started_servers(monkeypatch: pytest.MonkeyPatch) -> None:+ """网关启动失败时,已经运行的对象存储服务也必须回收。"""+ servers: list[_Server] = []+ original_start = _Server.start++ def start(server: _Server, timeout: float = 20.0) -> None:+ """保留真实 OSS 服务,只在第二个服务启动时注入失败。"""+ servers.append(server)+ if len(servers) == 2:+ raise RuntimeError("gateway startup failed")+ original_start(server, timeout)++ monkeypatch.setattr(_Server, "start", start)+ lifecycle = cloud.__wrapped__()+ try:+ with pytest.raises(RuntimeError, match="gateway startup failed"):+ next(lifecycle)+ assert not any(server._thread.is_alive() for server in servers)+ for server in servers:+ with socket.socket() as replacement:+ replacement.bind((server.host, server.port))+ finally:+ lifecycle.close()+ for server in servers:+ server.stop()++@pytest.mark.parametrize("tier", [None, "basic", "advanced"])def test_example_script_forwards_optional_tier(cloud: _CloudState, tmp_path: Path, tier: str | None) -> None:"""执行真实示例脚本,验证缺省不发送 tier、显式选档不由客户端替换。"""@@ -903,10 +934,13 @@ def test_saas_standard_only_with_native_and_oss(cloud: _CloudState) -> None:rejected = client.post("/v1/tasks", json={"files": [{"file_name": "test.pdf", "bytes": 1}], "tier": tier})assert rejected.status_code == 400 and rejected.json()["error"]["param"] == "tier"assert not cloud.tasks- receipt = client.post("/v1/tasks", json={- "files": [{"file_name": name, "bytes": 1} for name in ("test.pdf", "test.docx")],- "output_formats": ["default"],- })+ receipt = client.post(+ "/v1/tasks",+ json={+ "files": [{"file_name": name, "bytes": 1} for name in ("test.pdf", "test.docx")],+ "output_formats": ["default"],+ },+ )assert receipt.status_code == 202, receipt.textfor item in receipt.json()["files"]:upload = item["upload"]diff --git a/tests/unittest/test_tasks_sdk.py b/tests/unittest/test_tasks_sdk.pyindex 00c2d02a..264e179e 100644--- a/tests/unittest/test_tasks_sdk.py+++ b/tests/unittest/test_tasks_sdk.py@@ -9,6 +9,7 @@ import ioimport jsonimport zipfilefrom pathlib import Path+from types import SimpleNamespacefrom typing import Anyimport httpx@@ -466,15 +467,31 @@ def test_tasks_sdk_retries_transient_poll_responses(delays.append(delay)bind_transport(monkeypatch, handle)+ # 仅替换 SDK 模块持有的引用,避免污染 Uvicorn 等后台服务共享的标准库模块。+ monkeypatch.setattr(api_client, "time", SimpleNamespace(**vars(api_client.time)))+ monkeypatch.setattr(api_client, "asyncio", SimpleNamespace(**vars(api_client.asyncio)))monkeypatch.setattr(api_client.time, "sleep", delays.append)monkeypatch.setattr(api_client.asyncio, "sleep", sleep)parser = MinerUApiParser(api_url="https://api.test", tier="flash")++ async def parse_with_background_sleep() -> ParseResult:+ """同时运行独立后台等待,确保 SDK 的退避记录不会接收其他协程的 sleep。"""+ background = asyncio.create_task(asyncio.sleep(0.1))+ try:+ return await parser.parse_async(source)+ finally:+ await background++ def run_parse() -> ParseResult:+ """异步入口增加后台协程,同步入口继续覆盖原有重试协议。"""+ return asyncio.run(parse_with_background_sleep()) if asynchronous else parser.parse(source)+if status == 400 or failures > 3:with pytest.raises(api_client._V1APIError) as error:- parse(parser, source, asynchronous)+ run_parse()assert error.value.code == "temporary"else:- assert len(parse(parser, source, asynchronous).pages) == 1+ assert len(run_parse().pages) == 1assert counts == {"post": 1, "query": expected_queries}assert [delay for delay in delays if delay] == [2] * (expected_queries - 1)