test_connection_pools.py
This file
Path
tests/dispatch/test_connection_pools.py
246 lines · 8.3 KiB · Python
Last changed by
Drop `run` and `run_in_threadpool` (#710)Tom Christiebefore adoption6ac49da
History of this fileTake it
1import pytest23import httpx4from httpx.dispatch.connection_pool import ConnectionPool567@pytest.mark.usefixtures("async_environment")8async def test_keepalive_connections(server):9 """10 Connections should default to staying in a keep-alive state.11 """12 async with ConnectionPool() as http:13 response = await http.request("GET", server.url)14 await response.aread()15 assert len(http.active_connections) == 016 assert len(http.keepalive_connections) == 11718 response = await http.request("GET", server.url)19 await response.aread()20 assert len(http.active_connections) == 021 assert len(http.keepalive_connections) == 1222324@pytest.mark.usefixtures("async_environment")25async def test_keepalive_timeout(server):26 """27 Keep-alive connections should timeout.28 """29 async with ConnectionPool() as http:30 response = await http.request("GET", server.url)31 await response.aread()32 assert len(http.active_connections) == 033 assert len(http.keepalive_connections) == 13435 http.next_keepalive_check = 0.036 await http.check_keepalive_expiry()3738 assert len(http.active_connections) == 039 assert len(http.keepalive_connections) == 14041 async with ConnectionPool() as http:42 http.KEEP_ALIVE_EXPIRY = 0.04344 response = await http.request("GET", server.url)45 await response.aread()46 assert len(http.active_connections) == 047 assert len(http.keepalive_connections) == 14849 http.next_keepalive_check = 0.050 await http.check_keepalive_expiry()5152 assert len(http.active_connections) == 053 assert len(http.keepalive_connections) == 0545556@pytest.mark.usefixtures("async_environment")57async def test_differing_connection_keys(server):58 """59 Connections to differing connection keys should result in multiple connections.60 """61 async with ConnectionPool() as http:62 response = await http.request("GET", server.url)63 await response.aread()64 assert len(http.active_connections) == 065 assert len(http.keepalive_connections) == 16667 response = await http.request("GET", "http://localhost:8000/")68 await response.aread()69 assert len(http.active_connections) == 070 assert len(http.keepalive_connections) == 2717273@pytest.mark.usefixtures("async_environment")74async def test_soft_limit(server):75 """76 The soft_limit config should limit the maximum number of keep-alive connections.77 """78 pool_limits = httpx.PoolLimits(soft_limit=1)7980 async with ConnectionPool(pool_limits=pool_limits) as http:81 response = await http.request("GET", server.url)82 await response.aread()83 assert len(http.active_connections) == 084 assert len(http.keepalive_connections) == 18586 response = await http.request("GET", "http://localhost:8000/")87 await response.aread()88 assert len(http.active_connections) == 089 assert len(http.keepalive_connections) == 1909192@pytest.mark.usefixtures("async_environment")93async def test_streaming_response_holds_connection(server):94 """95 A streaming request should hold the connection open until the response is read.96 """97 async with ConnectionPool() as http:98 response = await http.request("GET", server.url)99 assert len(http.active_connections) == 1100 assert len(http.keepalive_connections) == 0101102 await response.aread()103104 assert len(http.active_connections) == 0105 assert len(http.keepalive_connections) == 1106107108@pytest.mark.usefixtures("async_environment")109async def test_multiple_concurrent_connections(server):110 """111 Multiple conncurrent requests should open multiple conncurrent connections.112 """113 async with ConnectionPool() as http:114 response_a = await http.request("GET", server.url)115 assert len(http.active_connections) == 1116 assert len(http.keepalive_connections) == 0117118 response_b = await http.request("GET", server.url)119 assert len(http.active_connections) == 2120 assert len(http.keepalive_connections) == 0121122 await response_b.aread()123 assert len(http.active_connections) == 1124 assert len(http.keepalive_connections) == 1125126 await response_a.aread()127 assert len(http.active_connections) == 0128 assert len(http.keepalive_connections) == 2129130131@pytest.mark.usefixtures("async_environment")132async def test_close_connections(server):133 """134 Using a `Connection: close` header should close the connection.135 """136 headers = [(b"connection", b"close")]137 async with ConnectionPool() as http:138 response = await http.request("GET", server.url, headers=headers)139 await response.aread()140 assert len(http.active_connections) == 0141 assert len(http.keepalive_connections) == 0142143144@pytest.mark.usefixtures("async_environment")145async def test_standard_response_close(server):146 """147 A standard close should keep the connection open.148 """149 async with ConnectionPool() as http:150 response = await http.request("GET", server.url)151 await response.aread()152 await response.aclose()153 assert len(http.active_connections) == 0154 assert len(http.keepalive_connections) == 1155156157@pytest.mark.usefixtures("async_environment")158async def test_premature_response_close(server):159 """160 A premature close should close the connection.161 """162 async with ConnectionPool() as http:163 response = await http.request("GET", server.url)164 await response.aclose()165 assert len(http.active_connections) == 0166 assert len(http.keepalive_connections) == 0167168169@pytest.mark.usefixtures("async_environment")170async def test_keepalive_connection_closed_by_server_is_reestablished(server):171 """172 Upon keep-alive connection closed by remote a new connection173 should be reestablished.174 """175 async with ConnectionPool() as http:176 response = await http.request("GET", server.url)177 await response.aread()178179 # Shutdown the server to close the keep-alive connection180 await server.restart()181182 response = await http.request("GET", server.url)183 await response.aread()184 assert len(http.active_connections) == 0185 assert len(http.keepalive_connections) == 1186187188@pytest.mark.usefixtures("async_environment")189async def test_keepalive_http2_connection_closed_by_server_is_reestablished(server):190 """191 Upon keep-alive connection closed by remote a new connection192 should be reestablished.193 """194 async with ConnectionPool() as http:195 response = await http.request("GET", server.url)196 await response.aread()197198 # Shutdown the server to close the keep-alive connection199 await server.restart()200201 response = await http.request("GET", server.url)202 await response.aread()203 assert len(http.active_connections) == 0204 assert len(http.keepalive_connections) == 1205206207@pytest.mark.usefixtures("async_environment")208async def test_connection_closed_free_semaphore_on_acquire(server):209 """210 Verify that max_connections semaphore is released211 properly on a disconnected connection.212 """213 async with ConnectionPool(pool_limits=httpx.PoolLimits(hard_limit=1)) as http:214 response = await http.request("GET", server.url)215 await response.aread()216217 # Close the connection so we're forced to recycle it218 await server.restart()219220 response = await http.request("GET", server.url)221 assert response.status_code == 200222223224@pytest.mark.usefixtures("async_environment")225async def test_connection_pool_closed_close_keepalive_and_free_semaphore(server):226 """227 Closing the connection pool should close remaining keepalive connections and228 release the max_connections semaphore.229 """230 http = ConnectionPool(pool_limits=httpx.PoolLimits(hard_limit=1))231232 async with http:233 response = await http.request("GET", server.url)234 await response.aread()235 assert response.status_code == 200236 assert len(http.keepalive_connections) == 1237238 assert len(http.keepalive_connections) == 0239240 # Perform a second round of requests to make sure the max_connections semaphore241 # was released properly.242243 async with http:244 response = await http.request("GET", server.url)245 await response.aread()246 assert response.status_code == 200