test_connection_pools.py
This file
Path
tests/dispatch/test_connection_pools.py
133 lines · 4.5 KiB · Python
Last changed by
Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
History of this fileTake it
Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 1–2why, one step back1import pytest2Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 3why, one step back3import httpxRejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 4–10why, one step back456@pytest.mark.asyncio7async def test_keepalive_connections(server):8 """9 Connections should default to staying in a keep-alive state.10 """Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 11why, one step back11 async with httpx.ConnectionPool() as http:Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
line 12why, one step back12 response = await http.request("GET", "http://127.0.0.1:8000/")Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 13why, one step back13 await response.read()Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 14–17why, one step back14 assert len(http.active_connections) == 015 assert len(http.keepalive_connections) == 11617 response = await http.request("GET", "http://127.0.0.1:8000/")Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 18why, one step back18 await response.read()Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 19–27why, one step back19 assert len(http.active_connections) == 020 assert len(http.keepalive_connections) == 1212223@pytest.mark.asyncio24async def test_differing_connection_keys(server):25 """26 Connnections to differing connection keys should result in multiple connections.27 """Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 28why, one step back28 async with httpx.ConnectionPool() as http:Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
line 29why, one step back29 response = await http.request("GET", "http://127.0.0.1:8000/")Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 30why, one step back30 await response.read()Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 31–34why, one step back31 assert len(http.active_connections) == 032 assert len(http.keepalive_connections) == 13334 response = await http.request("GET", "http://localhost:8000/")Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 35why, one step back35 await response.read()Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 36–44why, one step back36 assert len(http.active_connections) == 037 assert len(http.keepalive_connections) == 2383940@pytest.mark.asyncio41async def test_soft_limit(server):42 """43 The soft_limit config should limit the maximum number of keep-alive connections.44 """Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 45why, one step back45 pool_limits = httpx.PoolLimits(soft_limit=1)Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
line 46why, one step back46Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 47why, one step back47 async with httpx.ConnectionPool(pool_limits=pool_limits) as http:Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
line 48why, one step back48 response = await http.request("GET", "http://127.0.0.1:8000/")Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 49why, one step back49 await response.read()Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 50–53why, one step back50 assert len(http.active_connections) == 051 assert len(http.keepalive_connections) == 15253 response = await http.request("GET", "http://localhost:8000/")Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 54why, one step back54 await response.read()Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 55–63why, one step back55 assert len(http.active_connections) == 056 assert len(http.keepalive_connections) == 1575859@pytest.mark.asyncio60async def test_streaming_response_holds_connection(server):61 """62 A streaming request should hold the connection open until the response is read.63 """Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 64why, one step back64 async with httpx.ConnectionPool() as http:Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 65why, one step back65 response = await http.request("GET", "http://127.0.0.1:8000/")Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 66–79why, one step back66 assert len(http.active_connections) == 167 assert len(http.keepalive_connections) == 06869 await response.read()7071 assert len(http.active_connections) == 072 assert len(http.keepalive_connections) == 1737475@pytest.mark.asyncio76async def test_multiple_concurrent_connections(server):77 """78 Multiple conncurrent requests should open multiple conncurrent connections.79 """Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 80why, one step back80 async with httpx.ConnectionPool() as http:Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 81why, one step back81 response_a = await http.request("GET", "http://127.0.0.1:8000/")Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 82–84why, one step back82 assert len(http.active_connections) == 183 assert len(http.keepalive_connections) == 084Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 85why, one step back85 response_b = await http.request("GET", "http://127.0.0.1:8000/")Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 86–103why, one step back86 assert len(http.active_connections) == 287 assert len(http.keepalive_connections) == 08889 await response_b.read()90 assert len(http.active_connections) == 191 assert len(http.keepalive_connections) == 19293 await response_a.read()94 assert len(http.active_connections) == 095 assert len(http.keepalive_connections) == 2969798@pytest.mark.asyncio99async def test_close_connections(server):100 """101 Using a `Connection: close` header should close the connection.102 """103 headers = [(b"connection", b"close")]Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 104why, one step back104 async with httpx.ConnectionPool() as http:Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
line 105why, one step back105 response = await http.request("GET", "http://127.0.0.1:8000/", headers=headers)Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 106why, one step back106 await response.read()Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 107–115why, one step back107 assert len(http.active_connections) == 0108 assert len(http.keepalive_connections) == 0109110111@pytest.mark.asyncio112async def test_standard_response_close(server):113 """114 A standard close should keep the connection open.115 """Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 116why, one step back116 async with httpx.ConnectionPool() as http:Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 117why, one step back117 response = await http.request("GET", "http://127.0.0.1:8000/")Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 118–128why, one step back118 await response.read()119 await response.close()120 assert len(http.active_connections) == 0121 assert len(http.keepalive_connections) == 1122123124@pytest.mark.asyncio125async def test_premature_response_close(server):126 """127 A premature close should close the connection.128 """Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 129why, one step back129 async with httpx.ConnectionPool() as http:Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 130why, one step back130 response = await http.request("GET", "http://127.0.0.1:8000/")Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 131–133why, one step back131 await response.close()132 assert len(http.active_connections) == 0133 assert len(http.keepalive_connections) == 0