更新bug
This commit is contained in:
@@ -3,7 +3,10 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from pathlib import Path
|
||||
from threading import Barrier, get_ident
|
||||
from typing import Any
|
||||
|
||||
import httpx
|
||||
import pytest
|
||||
@@ -17,6 +20,7 @@ from doctor_workstation.core.errors import (
|
||||
OpenPageRequiredError,
|
||||
WorkWechatBindingRequiredError,
|
||||
)
|
||||
from doctor_workstation.services import api_client as api_client_module
|
||||
from doctor_workstation.services.api_client import ApiClient
|
||||
from doctor_workstation.services.token_store import TokenStore
|
||||
|
||||
@@ -48,6 +52,41 @@ def test_get_normalises_adminapi_and_sends_contract_headers() -> None:
|
||||
)
|
||||
|
||||
|
||||
def test_default_client_allows_parallel_requests_with_independent_transports(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
"""Production workers must not queue behind one process-wide HTTP lock."""
|
||||
|
||||
rendezvous = Barrier(2, timeout=2)
|
||||
created: list[Any] = []
|
||||
request_threads: set[int] = set()
|
||||
|
||||
class PooledClient:
|
||||
def __init__(self, **_options: Any) -> None:
|
||||
self.closed = False
|
||||
created.append(self)
|
||||
|
||||
def request(self, _method: str, url: str, **_options: Any) -> httpx.Response:
|
||||
request_threads.add(get_ident())
|
||||
rendezvous.wait()
|
||||
return httpx.Response(200, json={"code": 1, "data": url.rsplit("/", 1)[-1]})
|
||||
|
||||
def close(self) -> None:
|
||||
self.closed = True
|
||||
|
||||
monkeypatch.setattr(api_client_module.httpx, "Client", PooledClient)
|
||||
client = ApiClient("https://example.test", max_retries=0)
|
||||
with ThreadPoolExecutor(max_workers=2) as executor:
|
||||
first = executor.submit(client.get, "patient/first")
|
||||
second = executor.submit(client.get, "patient/second")
|
||||
assert {first.result(timeout=3), second.result(timeout=3)} == {"first", "second"}
|
||||
client.close()
|
||||
|
||||
assert len(created) == 2
|
||||
assert len(request_threads) == 2
|
||||
assert all(item.closed for item in created)
|
||||
|
||||
|
||||
def test_post_uses_json_and_never_retries_timeout() -> None:
|
||||
"""Writes use JSON and a timeout never causes an automatic duplicate POST."""
|
||||
|
||||
|
||||
Reference in New Issue
Block a user