Skip to content
Myr

Merge pull request #7 from encode/timeout-tests

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Add PoolTimeout, and timeout tests

authored by Tom Christie · committed by GitHub · parents 68c468a, 148818c · 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

5 files+63 added−7 removed

modified:httpcore/__init__.py+8 added-1 removed
1from .config import PoolLimits, SSLConfig, TimeoutConfig2from .datastructures import URL, Request, Response3from .exceptions import ResponseClosed, StreamConsumed3from .exceptions import (4 ConnectTimeout,5 PoolTimeout,6 ReadTimeout,7 ResponseClosed,8 StreamConsumed,9 Timeout,10)11from .pool import ConnectionPool1213__version__ = "0.0.3"
modified:httpcore/compat.py+2 added-2 removed
1try:2 import brotli3except ImportError:4 brotli = None # pragma: nocover3except ImportError: # pragma: nocover4 brotli = None
modified:httpcore/pool.py+8 added-3 removed
14 unmodified lines
15 TimeoutConfig,16)17from .connections import Connection18from .datastructures import URL, Request, Response19from .exceptions import PoolTimeout2021ConnectionKey = typing.Tuple[str, str, int] # (scheme, host, port)222324class 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)2930 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()3637 def release(self) -> None:38 if hasattr(self, "semaphore"):39 self.semaphore.release()
16 unmodified lines
56 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 )6263 async def request(64 self,
135 unmodified lines
modified:tests/conftest.py+19 added-1 removed
5 unmodified lines
678async 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)141516async def hello_world(scope, receive, send):17 await send(18 {19 "type": "http.response.start",20 "status": 200,
2 unmodified lines
23 )24 await send({"type": "http.response.body", "body": b"Hello, world!"})252627async 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!"})373839@pytest.fixture40async def server():41 config = Config(app=app, lifespan="off")42 server = Server(config=config)
3 unmodified lines
46 await asyncio.sleep(0.0001)47 yield server48 finally:49 server.should_exit = True31 server.force_exit = True50 await task
added:tests/test_timeouts.py+26 added
1import pytest23import httpcore456@pytest.mark.asyncio7async def test_read_timeout(server):8 timeout = httpcore.TimeoutConfig(read_timeout=0.0001)910 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")131415@pytest.mark.asyncio16async def test_pool_timeout(server):17 timeout = httpcore.TimeoutConfig(pool_timeout=0.0001)18 limits = httpcore.PoolLimits(hard_limit=1)1920 async with httpcore.ConnectionPool(timeout=timeout, limits=limits) as http:21 response = await http.request("GET", "http://127.0.0.1:8000/", stream=True)2223 with pytest.raises(httpcore.PoolTimeout):24 await http.request("GET", "http://localhost:8000/")2526 await response.read()