Skip to content
Myr

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 file

Take it

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 1–2why, one step back
1import pytest
2

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 3why, one step back
3import httpx

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 4–10why, one step back
4
56@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 back
11 async with httpx.ConnectionPool() as http:

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

line 12why, one step back
12 response = await http.request("GET", "http://127.0.0.1:8000/")

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 13why, one step back
13 await response.read()

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 14–17why, one step back
14 assert len(http.active_connections) == 0
15 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 back
18 await response.read()

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 19–27why, one step back
19 assert len(http.active_connections) == 0
20 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 back
28 async with httpx.ConnectionPool() as http:

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

line 29why, one step back
29 response = await http.request("GET", "http://127.0.0.1:8000/")

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 30why, one step back
30 await response.read()

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 31–34why, one step back
31 assert len(http.active_connections) == 0
32 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 back
35 await response.read()

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 36–44why, one step back
36 assert len(http.active_connections) == 0
37 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 back
45 pool_limits = httpx.PoolLimits(soft_limit=1)

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

line 46why, one step back
46

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 47why, one step back
47 async with httpx.ConnectionPool(pool_limits=pool_limits) as http:

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

line 48why, one step back
48 response = await http.request("GET", "http://127.0.0.1:8000/")

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 49why, one step back
49 await response.read()

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 50–53why, one step back
50 assert len(http.active_connections) == 0
51 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 back
54 await response.read()

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 55–63why, one step back
55 assert len(http.active_connections) == 0
56 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 back
64 async with httpx.ConnectionPool() as http:

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 65why, one step back
65 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 back
66 assert len(http.active_connections) == 1
67 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 back
80 async with httpx.ConnectionPool() as http:

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 81why, one step back
81 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 back
82 assert len(http.active_connections) == 1
83 assert len(http.keepalive_connections) == 084

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 85why, one step back
85 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 back
86 assert len(http.active_connections) == 2
87 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 back
104 async with httpx.ConnectionPool() as http:

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

line 105why, one step back
105 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 back
106 await response.read()

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 107–115why, one step back
107 assert len(http.active_connections) == 0
108 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 back
116 async with httpx.ConnectionPool() as http:

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 117why, one step back
117 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 back
118 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 back
129 async with httpx.ConnectionPool() as http:

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 130why, one step back
130 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 back
131 await response.close()
132 assert len(http.active_connections) == 0133 assert len(http.keepalive_connections) == 0