test_connection_pools.py
This file
Path
tests/dispatch/test_connection_pools.py
126 lines · 4.3 KiB · Python
Last changed by
Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
History of this fileTake it
Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 1–126why, one step back1import pytest23import httpcore456@pytest.mark.asyncio7async def test_keepalive_connections(server):8 """9 Connections should default to staying in a keep-alive state.10 """11 async with httpcore.ConnectionPool() as http:12 response = await http.request("GET", "http://127.0.0.1:8000/")13 assert len(http.active_connections) == 014 assert len(http.keepalive_connections) == 11516 response = await http.request("GET", "http://127.0.0.1:8000/")17 assert len(http.active_connections) == 018 assert len(http.keepalive_connections) == 1192021@pytest.mark.asyncio22async def test_differing_connection_keys(server):23 """24 Connnections to differing connection keys should result in multiple connections.25 """26 async with httpcore.ConnectionPool() as http:27 response = await http.request("GET", "http://127.0.0.1:8000/")28 assert len(http.active_connections) == 029 assert len(http.keepalive_connections) == 13031 response = await http.request("GET", "http://localhost:8000/")32 assert len(http.active_connections) == 033 assert len(http.keepalive_connections) == 2343536@pytest.mark.asyncio37async def test_soft_limit(server):38 """39 The soft_limit config should limit the maximum number of keep-alive connections.40 """41 limits = httpcore.PoolLimits(soft_limit=1)4243 async with httpcore.ConnectionPool(limits=limits) as http:44 response = await http.request("GET", "http://127.0.0.1:8000/")45 assert len(http.active_connections) == 046 assert len(http.keepalive_connections) == 14748 response = await http.request("GET", "http://localhost:8000/")49 assert len(http.active_connections) == 050 assert len(http.keepalive_connections) == 1515253@pytest.mark.asyncio54async def test_streaming_response_holds_connection(server):55 """56 A streaming request should hold the connection open until the response is read.57 """58 async with httpcore.ConnectionPool() as http:59 response = await http.request("GET", "http://127.0.0.1:8000/", stream=True)60 assert len(http.active_connections) == 161 assert len(http.keepalive_connections) == 06263 await response.read()6465 assert len(http.active_connections) == 066 assert len(http.keepalive_connections) == 1676869@pytest.mark.asyncio70async def test_multiple_concurrent_connections(server):71 """72 Multiple conncurrent requests should open multiple conncurrent connections.73 """74 async with httpcore.ConnectionPool() as http:75 response_a = await http.request("GET", "http://127.0.0.1:8000/", stream=True)76 assert len(http.active_connections) == 177 assert len(http.keepalive_connections) == 07879 response_b = await http.request("GET", "http://127.0.0.1:8000/", stream=True)80 assert len(http.active_connections) == 281 assert len(http.keepalive_connections) == 08283 await response_b.read()84 assert len(http.active_connections) == 185 assert len(http.keepalive_connections) == 18687 await response_a.read()88 assert len(http.active_connections) == 089 assert len(http.keepalive_connections) == 2909192@pytest.mark.asyncio93async def test_close_connections(server):94 """95 Using a `Connection: close` header should close the connection.96 """97 headers = [(b"connection", b"close")]98 async with httpcore.ConnectionPool() as http:99 response = await http.request("GET", "http://127.0.0.1:8000/", headers=headers)100 assert len(http.active_connections) == 0101 assert len(http.keepalive_connections) == 0102103104@pytest.mark.asyncio105async def test_standard_response_close(server):106 """107 A standard close should keep the connection open.108 """109 async with httpcore.ConnectionPool() as http:110 response = await http.request("GET", "http://127.0.0.1:8000/", stream=True)111 await response.read()112 await response.close()113 assert len(http.active_connections) == 0114 assert len(http.keepalive_connections) == 1115116117@pytest.mark.asyncio118async def test_premature_response_close(server):119 """120 A premature close should close the connection.121 """122 async with httpcore.ConnectionPool() as http:123 response = await http.request("GET", "http://127.0.0.1:8000/", stream=True)124 await response.close()125 assert len(http.active_connections) == 0126 assert len(http.keepalive_connections) == 0