Skip to content
Myr

test_connection_pools.py

This file

Path

tests/dispatch/test_connection_pools.py

180 lines · 6.0 KiB · Python

Last changed by

Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d

History of this file

Take 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_differing_connection_keys(server, backend):22    """23    Connections to differing connection keys should result in multiple connections.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        response = await http.request("GET", "http://localhost:8000/")32        await response.read()33        assert len(http.active_connections) == 034        assert len(http.keepalive_connections) == 2353637async def test_soft_limit(server, backend):38    """39    The soft_limit config should limit the maximum number of keep-alive connections.40    """41    pool_limits = httpx.PoolLimits(soft_limit=1)4243    async with ConnectionPool(pool_limits=pool_limits) as http:44        response = await http.request("GET", server.url)45        await response.read()46        assert len(http.active_connections) == 047        assert len(http.keepalive_connections) == 14849        response = await http.request("GET", "http://localhost:8000/")50        await response.read()51        assert len(http.active_connections) == 052        assert len(http.keepalive_connections) == 1535455async def test_streaming_response_holds_connection(server, backend):56    """57    A streaming request should hold the connection open until the response is read.58    """59    async with ConnectionPool() as http:60        response = await http.request("GET", server.url)61        assert len(http.active_connections) == 162        assert len(http.keepalive_connections) == 06364        await response.read()6566        assert len(http.active_connections) == 067        assert len(http.keepalive_connections) == 1686970async def test_multiple_concurrent_connections(server, backend):71    """72    Multiple conncurrent requests should open multiple conncurrent connections.73    """74    async with ConnectionPool() as http:75        response_a = await http.request("GET", server.url)76        assert len(http.active_connections) == 177        assert len(http.keepalive_connections) == 07879        response_b = await http.request("GET", server.url)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) == 2909192async def test_close_connections(server, backend):93    """94    Using a `Connection: close` header should close the connection.95    """96    headers = [(b"connection", b"close")]97    async with ConnectionPool() as http:98        response = await http.request("GET", server.url, headers=headers)99        await response.read()100        assert len(http.active_connections) == 0101        assert len(http.keepalive_connections) == 0102103104async def test_standard_response_close(server, backend):105    """106    A standard close should keep the connection open.107    """108    async with ConnectionPool() as http:109        response = await http.request("GET", server.url)110        await response.read()111        await response.close()112        assert len(http.active_connections) == 0113        assert len(http.keepalive_connections) == 1114115116async def test_premature_response_close(server, backend):117    """118    A premature close should close the connection.119    """120    async with ConnectionPool() as http:121        response = await http.request("GET", server.url)122        await response.close()123        assert len(http.active_connections) == 0124        assert len(http.keepalive_connections) == 0125126127async def test_keepalive_connection_closed_by_server_is_reestablished(128    server, restart, backend129):130    """131    Upon keep-alive connection closed by remote a new connection132    should be reestablished.133    """134    async with ConnectionPool() as http:135        response = await http.request("GET", server.url)136        await response.read()137138        # Shutdown the server to close the keep-alive connection139        await restart(server)140141        response = await http.request("GET", server.url)142        await response.read()143        assert len(http.active_connections) == 0144        assert len(http.keepalive_connections) == 1145146147async def test_keepalive_http2_connection_closed_by_server_is_reestablished(148    server, restart, backend149):150    """151    Upon keep-alive connection closed by remote a new connection152    should be reestablished.153    """154    async with ConnectionPool() as http:155        response = await http.request("GET", server.url)156        await response.read()157158        # Shutdown the server to close the keep-alive connection159        await restart(server)160161        response = await http.request("GET", server.url)162        await response.read()163        assert len(http.active_connections) == 0164        assert len(http.keepalive_connections) == 1165166167async def test_connection_closed_free_semaphore_on_acquire(server, restart, backend):168    """169    Verify that max_connections semaphore is released170    properly on a disconnected connection.171    """172    async with ConnectionPool(pool_limits=httpx.PoolLimits(hard_limit=1)) as http:173        response = await http.request("GET", server.url)174        await response.read()175176        # Close the connection so we're forced to recycle it177        await restart(server)178179        response = await http.request("GET", server.url)180        assert response.status_code == 200