|
2 | 2 | import json |
3 | 3 | import ssl |
4 | 4 | import threading |
| 5 | +import time |
5 | 6 | from concurrent.futures import ThreadPoolExecutor |
6 | 7 | from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer |
7 | 8 | from typing import cast |
@@ -364,9 +365,12 @@ async def test_async_envd_transport_uses_separate_stack(test_api_key): |
364 | 365 |
|
365 | 366 |
|
366 | 367 | class _EchoHandler(BaseHTTPRequestHandler): |
367 | | - """Answers every GET with a JSON echo of the request headers.""" |
| 368 | + """Answers every GET with a JSON echo of the request headers; a path |
| 369 | + starting with ``/slow`` sleeps 5 seconds first.""" |
368 | 370 |
|
369 | 371 | def do_GET(self): |
| 372 | + if self.path.startswith("/slow"): |
| 373 | + time.sleep(5) |
370 | 374 | headers = {k.lower(): v for k, v in self.headers.items()} |
371 | 375 | body = json.dumps({"path": self.path, "headers": headers}).encode() |
372 | 376 | self.send_response(200) |
@@ -469,3 +473,36 @@ async def test_async_api_client_round_trips_through_pyqwest(test_api_key, echo_s |
469 | 473 | finally: |
470 | 474 | await httpx_client.aclose() |
471 | 475 | reset_async_api_transports() |
| 476 | + |
| 477 | + |
| 478 | +def test_sync_api_client_timeout_raises_httpx_read_timeout(test_api_key, echo_server): |
| 479 | + # pyqwest raises the builtin TimeoutError; the transport re-raises it as |
| 480 | + # httpx.ReadTimeout to keep the httpx.TimeoutException contract. |
| 481 | + reset_sync_api_transports() |
| 482 | + config = ConnectionConfig(api_key=test_api_key, api_url=echo_server) |
| 483 | + api_client = get_sync_api_client(config) |
| 484 | + httpx_client = api_client.get_httpx_client() |
| 485 | + |
| 486 | + try: |
| 487 | + with pytest.raises(httpx.ReadTimeout): |
| 488 | + httpx_client.request("GET", "/slow", timeout=0.2) |
| 489 | + finally: |
| 490 | + httpx_client.close() |
| 491 | + reset_sync_api_transports() |
| 492 | + |
| 493 | + |
| 494 | +@pytest.mark.asyncio |
| 495 | +async def test_async_api_client_timeout_raises_httpx_read_timeout( |
| 496 | + test_api_key, echo_server |
| 497 | +): |
| 498 | + reset_async_api_transports() |
| 499 | + config = ConnectionConfig(api_key=test_api_key, api_url=echo_server) |
| 500 | + api_client = get_async_api_client(config) |
| 501 | + httpx_client = api_client.get_async_httpx_client() |
| 502 | + |
| 503 | + try: |
| 504 | + with pytest.raises(httpx.ReadTimeout): |
| 505 | + await httpx_client.request("GET", "/slow", timeout=0.2) |
| 506 | + finally: |
| 507 | + await httpx_client.aclose() |
| 508 | + reset_async_api_transports() |
0 commit comments