test_connection_pools.py
This file
Path
tests/dispatch/test_connection_pools.py
179 lines · 6.2 KiB · Python
Last changed by
Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280
History of this fileTake it
1import httpx234async def test_keepalive_connections(server, backend):5 """6 Connections should default to staying in a keep-alive state.7 """8 async with httpx.ConnectionPool(backend=backend) as http:9 response = await http.request("GET", server.url)10 await response.read()11 assert len(http.active_connections) == 012 assert len(http.keepalive_connections) == 11314 response = await http.request("GET", server.url)15 await response.read()16 assert len(http.active_connections) == 017 assert len(http.keepalive_connections) == 1181920async def test_differing_connection_keys(server, backend):21 """22 Connections to differing connection keys should result in multiple connections.23 """24 async with httpx.ConnectionPool(backend=backend) as http:25 response = await http.request("GET", server.url)26 await response.read()27 assert len(http.active_connections) == 028 assert len(http.keepalive_connections) == 12930 response = await http.request("GET", "http://localhost:8000/")31 await response.read()32 assert len(http.active_connections) == 033 assert len(http.keepalive_connections) == 2343536async def test_soft_limit(server, backend):37 """38 The soft_limit config should limit the maximum number of keep-alive connections.39 """40 pool_limits = httpx.PoolLimits(soft_limit=1)4142 async with httpx.ConnectionPool(pool_limits=pool_limits, backend=backend) as http:43 response = await http.request("GET", server.url)44 await response.read()45 assert len(http.active_connections) == 046 assert len(http.keepalive_connections) == 14748 response = await http.request("GET", "http://localhost:8000/")49 await response.read()50 assert len(http.active_connections) == 051 assert len(http.keepalive_connections) == 1525354async def test_streaming_response_holds_connection(server, backend):55 """56 A streaming request should hold the connection open until the response is read.57 """58 async with httpx.ConnectionPool(backend=backend) as http:59 response = await http.request("GET", server.url)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) == 1676869async def test_multiple_concurrent_connections(server, backend):70 """71 Multiple conncurrent requests should open multiple conncurrent connections.72 """73 async with httpx.ConnectionPool(backend=backend) as http:74 response_a = await http.request("GET", server.url)75 assert len(http.active_connections) == 176 assert len(http.keepalive_connections) == 07778 response_b = await http.request("GET", server.url)79 assert len(http.active_connections) == 280 assert len(http.keepalive_connections) == 08182 await response_b.read()83 assert len(http.active_connections) == 184 assert len(http.keepalive_connections) == 18586 await response_a.read()87 assert len(http.active_connections) == 088 assert len(http.keepalive_connections) == 2899091async def test_close_connections(server, backend):92 """93 Using a `Connection: close` header should close the connection.94 """95 headers = [(b"connection", b"close")]96 async with httpx.ConnectionPool(backend=backend) as http:97 response = await http.request("GET", server.url, headers=headers)98 await response.read()99 assert len(http.active_connections) == 0100 assert len(http.keepalive_connections) == 0101102103async def test_standard_response_close(server, backend):104 """105 A standard close should keep the connection open.106 """107 async with httpx.ConnectionPool(backend=backend) as http:108 response = await http.request("GET", server.url)109 await response.read()110 await response.close()111 assert len(http.active_connections) == 0112 assert len(http.keepalive_connections) == 1113114115async def test_premature_response_close(server, backend):116 """117 A premature close should close the connection.118 """119 async with httpx.ConnectionPool(backend=backend) as http:120 response = await http.request("GET", server.url)121 await response.close()122 assert len(http.active_connections) == 0123 assert len(http.keepalive_connections) == 0124125126async def test_keepalive_connection_closed_by_server_is_reestablished(127 server, restart, backend128):129 """130 Upon keep-alive connection closed by remote a new connection131 should be reestablished.132 """133 async with httpx.ConnectionPool(backend=backend) as http:134 response = await http.request("GET", server.url)135 await response.read()136137 # Shutdown the server to close the keep-alive connection138 await restart(server)139140 response = await http.request("GET", server.url)141 await response.read()142 assert len(http.active_connections) == 0143 assert len(http.keepalive_connections) == 1144145146async def test_keepalive_http2_connection_closed_by_server_is_reestablished(147 server, restart, backend148):149 """150 Upon keep-alive connection closed by remote a new connection151 should be reestablished.152 """153 async with httpx.ConnectionPool(backend=backend) as http:154 response = await http.request("GET", server.url)155 await response.read()156157 # Shutdown the server to close the keep-alive connection158 await restart(server)159160 response = await http.request("GET", server.url)161 await response.read()162 assert len(http.active_connections) == 0163 assert len(http.keepalive_connections) == 1164165166async def test_connection_closed_free_semaphore_on_acquire(server, restart, backend):167 """168 Verify that max_connections semaphore is released169 properly on a disconnected connection.170 """171 async with httpx.ConnectionPool(pool_limits=httpx.PoolLimits(hard_limit=1)) as http:172 response = await http.request("GET", server.url)173 await response.read()174175 # Close the connection so we're forced to recycle it176 await restart(server)177178 response = await http.request("GET", server.url)179 assert response.status_code == 200