Add PoolTimeout, and timeout tests
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Tom Christie · committed by Tom Christie · parent 68c468a · Browse files · 5 files·+63 added−7 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+63 added−7 removed
- tests:+45 added−1 removed
- other:+18 added−6 removed
Provenance
- Commit
148818c- Tree
759610c- Author
Tom Christie <tom@tomchristie.com>- Committer
Tom Christie <tom@tomchristie.com>- Parent
68c468a
5 files changed+63 added−7 removed
modified:httpcore/__init__.py+8 added-1 removed
1 from .config import PoolLimits, SSLConfig, TimeoutConfig2 from .datastructures import URL, Request, Response3-from .exceptions import ResponseClosed, StreamConsumed3+from .exceptions import (4+ ConnectTimeout,5+ PoolTimeout,6+ ReadTimeout,7+ ResponseClosed,8+ StreamConsumed,9+ Timeout,10+)11 from .pool import ConnectionPool12 13 __version__ = "0.0.3"modified:httpcore/compat.py+2 added-2 removed
1 try:2 import brotli3-except ImportError:4- brotli = None # pragma: nocover3+except ImportError: # pragma: nocover4+ brotli = Nonemodified:httpcore/pool.py+8 added-3 removed
14 unmodified lines15 TimeoutConfig,16 )17 from .connections import Connection18 from .datastructures import URL, Request, Response19+from .exceptions import PoolTimeout20 21 ConnectionKey = typing.Tuple[str, str, int] # (scheme, host, port)22 23 24 class ConnectionSemaphore:24- def __init__(self, max_connections: int = None):25+ def __init__(self, max_connections: int = None, timeout: float = None):26+ self.timeout = timeout27 if max_connections is not None:28 self.semaphore = asyncio.BoundedSemaphore(value=max_connections)29 30 async def acquire(self) -> None:31 if hasattr(self, "semaphore"):30- await self.semaphore.acquire()32+ try:33+ await asyncio.wait_for(self.semaphore.acquire(), self.timeout)34+ except asyncio.TimeoutError:35+ raise PoolTimeout()36 37 def release(self) -> None:38 if hasattr(self, "semaphore"):39 self.semaphore.release()16 unmodified lines56 self._connections = (57 {}58 ) # type: typing.Dict[ConnectionKey, typing.List[Connection]]59 self._connection_semaphore = ConnectionSemaphore(55- max_connections=self.limits.hard_limit60+ max_connections=self.limits.hard_limit, timeout=self.timeout.pool_timeout61 )62 63 async def request(64 self,135 unmodified linesmodified:tests/conftest.py+19 added-1 removed
5 unmodified lines6 7 8 async def app(scope, receive, send):9 assert scope["type"] == "http"10+ if scope["path"] == "/slow_response":11+ await slow_response(scope, receive, send)12+ else:13+ await hello_world(scope, receive, send)14+15+16+async def hello_world(scope, receive, send):17 await send(18 {19 "type": "http.response.start",20 "status": 200,2 unmodified lines23 )24 await send({"type": "http.response.body", "body": b"Hello, world!"})25 26 27+async def slow_response(scope, receive, send):28+ await asyncio.sleep(0.01)29+ await send(30+ {31+ "type": "http.response.start",32+ "status": 200,33+ "headers": [[b"content-type", b"text/plain"]],34+ }35+ )36+ await send({"type": "http.response.body", "body": b"Hello, world!"})37+38+39 @pytest.fixture40 async def server():41 config = Config(app=app, lifespan="off")42 server = Server(config=config)3 unmodified lines46 await asyncio.sleep(0.0001)47 yield server48 finally:49 server.should_exit = True31- server.force_exit = True50 await taskadded:tests/test_timeouts.py+26 added
1+import pytest2+3+import httpcore4+5+6+@pytest.mark.asyncio7+async def test_read_timeout(server):8+ timeout = httpcore.TimeoutConfig(read_timeout=0.0001)9+10+ async with httpcore.ConnectionPool(timeout=timeout) as http:11+ with pytest.raises(httpcore.ReadTimeout):12+ await http.request("GET", "http://127.0.0.1:8000/slow_response")13+14+15+@pytest.mark.asyncio16+async def test_pool_timeout(server):17+ timeout = httpcore.TimeoutConfig(pool_timeout=0.0001)18+ limits = httpcore.PoolLimits(hard_limit=1)19+20+ async with httpcore.ConnectionPool(timeout=timeout, limits=limits) as http:21+ response = await http.request("GET", "http://127.0.0.1:8000/", stream=True)22+23+ with pytest.raises(httpcore.PoolTimeout):24+ await http.request("GET", "http://localhost:8000/")25+26+ await response.read()