test_connection_pools.py
This file
Path
tests/dispatch/test_connection_pools.py
133 lines · 4.5 KiB · Python
Last changed by
Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
History of this fileTake it
1import pytest23import httpx456@pytest.mark.asyncio7async def test_keepalive_connections(server):8 """9 Connections should default to staying in a keep-alive state.10 """11 async with httpx.ConnectionPool() as http:12 response = await http.request("GET", "http://127.0.0.1:8000/")13 await response.read()14 assert len(http.active_connections) == 015 assert len(http.keepalive_connections) == 11617 response = await http.request("GET", "http://127.0.0.1:8000/")18 await response.read()19 assert len(http.active_connections) == 020 assert len(http.keepalive_connections) == 1212223@pytest.mark.asyncio24async def test_differing_connection_keys(server):25 """26 Connnections to differing connection keys should result in multiple connections.27 """28 async with httpx.ConnectionPool() as http:29 response = await http.request("GET", "http://127.0.0.1:8000/")30 await response.read()31 assert len(http.active_connections) == 032 assert len(http.keepalive_connections) == 13334 response = await http.request("GET", "http://localhost:8000/")35 await response.read()36 assert len(http.active_connections) == 037 assert len(http.keepalive_connections) == 2383940@pytest.mark.asyncio41async def test_soft_limit(server):42 """43 The soft_limit config should limit the maximum number of keep-alive connections.44 """45 pool_limits = httpx.PoolLimits(soft_limit=1)4647 async with httpx.ConnectionPool(pool_limits=pool_limits) as http:48 response = await http.request("GET", "http://127.0.0.1:8000/")49 await response.read()50 assert len(http.active_connections) == 051 assert len(http.keepalive_connections) == 15253 response = await http.request("GET", "http://localhost:8000/")54 await response.read()55 assert len(http.active_connections) == 056 assert len(http.keepalive_connections) == 1575859@pytest.mark.asyncio60async def test_streaming_response_holds_connection(server):61 """62 A streaming request should hold the connection open until the response is read.63 """64 async with httpx.ConnectionPool() as http:65 response = await http.request("GET", "http://127.0.0.1:8000/")66 assert len(http.active_connections) == 167 assert len(http.keepalive_connections) == 06869 await response.read()7071 assert len(http.active_connections) == 072 assert len(http.keepalive_connections) == 1737475@pytest.mark.asyncio76async def test_multiple_concurrent_connections(server):77 """78 Multiple conncurrent requests should open multiple conncurrent connections.79 """80 async with httpx.ConnectionPool() as http:81 response_a = await http.request("GET", "http://127.0.0.1:8000/")82 assert len(http.active_connections) == 183 assert len(http.keepalive_connections) == 08485 response_b = await http.request("GET", "http://127.0.0.1:8000/")86 assert len(http.active_connections) == 287 assert len(http.keepalive_connections) == 08889 await response_b.read()90 assert len(http.active_connections) == 191 assert len(http.keepalive_connections) == 19293 await response_a.read()94 assert len(http.active_connections) == 095 assert len(http.keepalive_connections) == 2969798@pytest.mark.asyncio99async def test_close_connections(server):100 """101 Using a `Connection: close` header should close the connection.102 """103 headers = [(b"connection", b"close")]104 async with httpx.ConnectionPool() as http:105 response = await http.request("GET", "http://127.0.0.1:8000/", headers=headers)106 await response.read()107 assert len(http.active_connections) == 0108 assert len(http.keepalive_connections) == 0109110111@pytest.mark.asyncio112async def test_standard_response_close(server):113 """114 A standard close should keep the connection open.115 """116 async with httpx.ConnectionPool() as http:117 response = await http.request("GET", "http://127.0.0.1:8000/")118 await response.read()119 await response.close()120 assert len(http.active_connections) == 0121 assert len(http.keepalive_connections) == 1122123124@pytest.mark.asyncio125async def test_premature_response_close(server):126 """127 A premature close should close the connection.128 """129 async with httpx.ConnectionPool() as http:130 response = await http.request("GET", "http://127.0.0.1:8000/")131 await response.close()132 assert len(http.active_connections) == 0133 assert len(http.keepalive_connections) == 0