test_connection_pools.py
This file
Path
tests/dispatch/test_connection_pools.py
211 lines · 7.0 KiB · Python
Last changed by
Keep-alive timeouts. (#627)Tom Christiebefore adoption499de51
History of this fileTake it
1import httpx2from httpx.dispatch.connection_pool import ConnectionPool345async def test_keepalive_connections(server, backend):6 """7 Connections should default to staying in a keep-alive state.8 """9 async with ConnectionPool() as http:10 response = await http.request("GET", server.url)11 await response.read()12 assert len(http.active_connections) == 013 assert len(http.keepalive_connections) == 11415 response = await http.request("GET", server.url)16 await response.read()17 assert len(http.active_connections) == 018 assert len(http.keepalive_connections) == 1192021async def test_keepalive_timeout(server, backend):22 """23 Keep-alive connections should timeout.24 """25 async with ConnectionPool() as http:26 response = await http.request("GET", server.url)27 await response.read()28 assert len(http.active_connections) == 029 assert len(http.keepalive_connections) == 13031 http.next_keepalive_check = 0.032 await http.check_keepalive_expiry()3334 assert len(http.active_connections) == 035 assert len(http.keepalive_connections) == 13637 async with ConnectionPool() as http:38 http.KEEP_ALIVE_EXPIRY = 0.03940 response = await http.request("GET", server.url)41 await response.read()42 assert len(http.active_connections) == 043 assert len(http.keepalive_connections) == 14445 http.next_keepalive_check = 0.046 await http.check_keepalive_expiry()4748 assert len(http.active_connections) == 049 assert len(http.keepalive_connections) == 0505152async def test_differing_connection_keys(server, backend):53 """54 Connections to differing connection keys should result in multiple connections.55 """56 async with ConnectionPool() as http:57 response = await http.request("GET", server.url)58 await response.read()59 assert len(http.active_connections) == 060 assert len(http.keepalive_connections) == 16162 response = await http.request("GET", "http://localhost:8000/")63 await response.read()64 assert len(http.active_connections) == 065 assert len(http.keepalive_connections) == 2666768async def test_soft_limit(server, backend):69 """70 The soft_limit config should limit the maximum number of keep-alive connections.71 """72 pool_limits = httpx.PoolLimits(soft_limit=1)7374 async with ConnectionPool(pool_limits=pool_limits) as http:75 response = await http.request("GET", server.url)76 await response.read()77 assert len(http.active_connections) == 078 assert len(http.keepalive_connections) == 17980 response = await http.request("GET", "http://localhost:8000/")81 await response.read()82 assert len(http.active_connections) == 083 assert len(http.keepalive_connections) == 1848586async def test_streaming_response_holds_connection(server, backend):87 """88 A streaming request should hold the connection open until the response is read.89 """90 async with ConnectionPool() as http:91 response = await http.request("GET", server.url)92 assert len(http.active_connections) == 193 assert len(http.keepalive_connections) == 09495 await response.read()9697 assert len(http.active_connections) == 098 assert len(http.keepalive_connections) == 199100101async def test_multiple_concurrent_connections(server, backend):102 """103 Multiple conncurrent requests should open multiple conncurrent connections.104 """105 async with ConnectionPool() as http:106 response_a = await http.request("GET", server.url)107 assert len(http.active_connections) == 1108 assert len(http.keepalive_connections) == 0109110 response_b = await http.request("GET", server.url)111 assert len(http.active_connections) == 2112 assert len(http.keepalive_connections) == 0113114 await response_b.read()115 assert len(http.active_connections) == 1116 assert len(http.keepalive_connections) == 1117118 await response_a.read()119 assert len(http.active_connections) == 0120 assert len(http.keepalive_connections) == 2121122123async def test_close_connections(server, backend):124 """125 Using a `Connection: close` header should close the connection.126 """127 headers = [(b"connection", b"close")]128 async with ConnectionPool() as http:129 response = await http.request("GET", server.url, headers=headers)130 await response.read()131 assert len(http.active_connections) == 0132 assert len(http.keepalive_connections) == 0133134135async def test_standard_response_close(server, backend):136 """137 A standard close should keep the connection open.138 """139 async with ConnectionPool() as http:140 response = await http.request("GET", server.url)141 await response.read()142 await response.close()143 assert len(http.active_connections) == 0144 assert len(http.keepalive_connections) == 1145146147async def test_premature_response_close(server, backend):148 """149 A premature close should close the connection.150 """151 async with ConnectionPool() as http:152 response = await http.request("GET", server.url)153 await response.close()154 assert len(http.active_connections) == 0155 assert len(http.keepalive_connections) == 0156157158async def test_keepalive_connection_closed_by_server_is_reestablished(159 server, restart, backend160):161 """162 Upon keep-alive connection closed by remote a new connection163 should be reestablished.164 """165 async with ConnectionPool() as http:166 response = await http.request("GET", server.url)167 await response.read()168169 # Shutdown the server to close the keep-alive connection170 await restart(server)171172 response = await http.request("GET", server.url)173 await response.read()174 assert len(http.active_connections) == 0175 assert len(http.keepalive_connections) == 1176177178async def test_keepalive_http2_connection_closed_by_server_is_reestablished(179 server, restart, backend180):181 """182 Upon keep-alive connection closed by remote a new connection183 should be reestablished.184 """185 async with ConnectionPool() as http:186 response = await http.request("GET", server.url)187 await response.read()188189 # Shutdown the server to close the keep-alive connection190 await restart(server)191192 response = await http.request("GET", server.url)193 await response.read()194 assert len(http.active_connections) == 0195 assert len(http.keepalive_connections) == 1196197198async def test_connection_closed_free_semaphore_on_acquire(server, restart, backend):199 """200 Verify that max_connections semaphore is released201 properly on a disconnected connection.202 """203 async with ConnectionPool(pool_limits=httpx.PoolLimits(hard_limit=1)) as http:204 response = await http.request("GET", server.url)205 await response.read()206207 # Close the connection so we're forced to recycle it208 await restart(server)209210 response = await http.request("GET", server.url)211 assert response.status_code == 200