Skip to content
Myr

test_connection_pools.py

This file

Path

tests/dispatch/test_connection_pools.py

246 lines · 8.3 KiB · Python

Last changed by

Drop `run` and `run_in_threadpool` (#710)Tom Christiebefore adoption6ac49da

History of this file

Take it

The walk stopped after 200 commits: lines older than e1da6b9 point at it.

Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be

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

Add tool for profiling (#364)Florimond Mancabefore adoptione1da6b9

line 3why, one step back
3import httpx

Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b

line 4why, one step back
4from httpx.dispatch.connection_pool import ConnectionPool

Add tool for profiling (#364)Florimond Mancabefore adoptione1da6b9

lines 5–6why, one step back
5
6

Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be

lines 7–8why, one step back
7@pytest.mark.usefixtures("async_environment")
8async def test_keepalive_connections(server):

Add tool for profiling (#364)Florimond Mancabefore adoptione1da6b9

lines 9–11why, one step back
9 """
10 Connections should default to staying in a keep-alive state.11 """

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

line 12why, one step back
12 async with ConnectionPool() as http:

Add tool for profiling (#364)Florimond Mancabefore adoptione1da6b9

line 13why, one step back
13 response = await http.request("GET", server.url)

Rename 'read/close' to 'aread/aclose' on Response (#674)Florimond Mancabefore adoptionf9d18a8

line 14why, one step back
14 await response.aread()

Add tool for profiling (#364)Florimond Mancabefore adoptione1da6b9

lines 15–18why, one step back
15 assert len(http.active_connections) == 0
16 assert len(http.keepalive_connections) == 11718 response = await http.request("GET", server.url)

Rename 'read/close' to 'aread/aclose' on Response (#674)Florimond Mancabefore adoptionf9d18a8

line 19why, one step back
19 await response.aread()

Add tool for profiling (#364)Florimond Mancabefore adoptione1da6b9

lines 20–23why, one step back
20 assert len(http.active_connections) == 0
21 assert len(http.keepalive_connections) == 12223

Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be

lines 24–25why, one step back
24@pytest.mark.usefixtures("async_environment")
25async def test_keepalive_timeout(server):

Keep-alive timeouts. (#627)Tom Christiebefore adoption499de51

lines 26–30why, one step back
26 """
27 Keep-alive connections should timeout.28 """29 async with ConnectionPool() as http:30 response = await http.request("GET", server.url)

Rename 'read/close' to 'aread/aclose' on Response (#674)Florimond Mancabefore adoptionf9d18a8

line 31why, one step back
31 await response.aread()

Keep-alive timeouts. (#627)Tom Christiebefore adoption499de51

lines 32–44why, one step back
32 assert len(http.active_connections) == 0
33 assert len(http.keepalive_connections) == 13435 http.next_keepalive_check = 0.036 await http.check_keepalive_expiry()3738 assert len(http.active_connections) == 039 assert len(http.keepalive_connections) == 14041 async with ConnectionPool() as http:42 http.KEEP_ALIVE_EXPIRY = 0.04344 response = await http.request("GET", server.url)

Rename 'read/close' to 'aread/aclose' on Response (#674)Florimond Mancabefore adoptionf9d18a8

line 45why, one step back
45 await response.aread()

Keep-alive timeouts. (#627)Tom Christiebefore adoption499de51

lines 46–55why, one step back
46 assert len(http.active_connections) == 0
47 assert len(http.keepalive_connections) == 14849 http.next_keepalive_check = 0.050 await http.check_keepalive_expiry()5152 assert len(http.active_connections) == 053 assert len(http.keepalive_connections) == 05455

Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be

lines 56–57why, one step back
56@pytest.mark.usefixtures("async_environment")
57async def test_differing_connection_keys(server):

Add tool for profiling (#364)Florimond Mancabefore adoptione1da6b9

lines 58–60why, one step back
58 """
59 Connections to differing connection keys should result in multiple connections.60 """

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

line 61why, one step back
61 async with ConnectionPool() as http:

Add tool for profiling (#364)Florimond Mancabefore adoptione1da6b9

line 62why, one step back
62 response = await http.request("GET", server.url)

Rename 'read/close' to 'aread/aclose' on Response (#674)Florimond Mancabefore adoptionf9d18a8

line 63why, one step back
63 await response.aread()

Add tool for profiling (#364)Florimond Mancabefore adoptione1da6b9

lines 64–67why, one step back
64 assert len(http.active_connections) == 0
65 assert len(http.keepalive_connections) == 16667 response = await http.request("GET", "http://localhost:8000/")

Rename 'read/close' to 'aread/aclose' on Response (#674)Florimond Mancabefore adoptionf9d18a8

line 68why, one step back
68 await response.aread()

Add tool for profiling (#364)Florimond Mancabefore adoptione1da6b9

lines 69–72why, one step back
69 assert len(http.active_connections) == 0
70 assert len(http.keepalive_connections) == 27172

Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be

lines 73–74why, one step back
73@pytest.mark.usefixtures("async_environment")
74async def test_soft_limit(server):

Add tool for profiling (#364)Florimond Mancabefore adoptione1da6b9

lines 75–79why, one step back
75 """
76 The soft_limit config should limit the maximum number of keep-alive connections.77 """78 pool_limits = httpx.PoolLimits(soft_limit=1)79

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

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

Add tool for profiling (#364)Florimond Mancabefore adoptione1da6b9

line 81why, one step back
81 response = await http.request("GET", server.url)

Rename 'read/close' to 'aread/aclose' on Response (#674)Florimond Mancabefore adoptionf9d18a8

line 82why, one step back
82 await response.aread()

Add tool for profiling (#364)Florimond Mancabefore adoptione1da6b9

lines 83–86why, one step back
83 assert len(http.active_connections) == 0
84 assert len(http.keepalive_connections) == 18586 response = await http.request("GET", "http://localhost:8000/")

Rename 'read/close' to 'aread/aclose' on Response (#674)Florimond Mancabefore adoptionf9d18a8

line 87why, one step back
87 await response.aread()

Add tool for profiling (#364)Florimond Mancabefore adoptione1da6b9

lines 88–91why, one step back
88 assert len(http.active_connections) == 0
89 assert len(http.keepalive_connections) == 19091

Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be

lines 92–93why, one step back
92@pytest.mark.usefixtures("async_environment")
93async def test_streaming_response_holds_connection(server):

Add tool for profiling (#364)Florimond Mancabefore adoptione1da6b9

lines 94–96why, one step back
94 """
95 A streaming request should hold the connection open until the response is read.96 """

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

line 97why, one step back
97 async with ConnectionPool() as http:

Add tool for profiling (#364)Florimond Mancabefore adoptione1da6b9

lines 98–101why, one step back
98 response = await http.request("GET", server.url)
99 assert len(http.active_connections) == 1100 assert len(http.keepalive_connections) == 0101

Rename 'read/close' to 'aread/aclose' on Response (#674)Florimond Mancabefore adoptionf9d18a8

line 102why, one step back
102 await response.aread()

Add tool for profiling (#364)Florimond Mancabefore adoptione1da6b9

lines 103–107why, one step back
103
104 assert len(http.active_connections) == 0105 assert len(http.keepalive_connections) == 1106107

Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be

lines 108–109why, one step back
108@pytest.mark.usefixtures("async_environment")
109async def test_multiple_concurrent_connections(server):

Add tool for profiling (#364)Florimond Mancabefore adoptione1da6b9

lines 110–112why, one step back
110 """
111 Multiple conncurrent requests should open multiple conncurrent connections.112 """

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

line 113why, one step back
113 async with ConnectionPool() as http:

Add tool for profiling (#364)Florimond Mancabefore adoptione1da6b9

lines 114–121why, one step back
114 response_a = await http.request("GET", server.url)
115 assert len(http.active_connections) == 1116 assert len(http.keepalive_connections) == 0117118 response_b = await http.request("GET", server.url)119 assert len(http.active_connections) == 2120 assert len(http.keepalive_connections) == 0121

Rename 'read/close' to 'aread/aclose' on Response (#674)Florimond Mancabefore adoptionf9d18a8

line 122why, one step back
122 await response_b.aread()

Add tool for profiling (#364)Florimond Mancabefore adoptione1da6b9

lines 123–125why, one step back
123 assert len(http.active_connections) == 1
124 assert len(http.keepalive_connections) == 1125

Rename 'read/close' to 'aread/aclose' on Response (#674)Florimond Mancabefore adoptionf9d18a8

line 126why, one step back
126 await response_a.aread()

Add tool for profiling (#364)Florimond Mancabefore adoptione1da6b9

lines 127–130why, one step back
127 assert len(http.active_connections) == 0
128 assert len(http.keepalive_connections) == 2129130

Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be

lines 131–132why, one step back
131@pytest.mark.usefixtures("async_environment")
132async def test_close_connections(server):

Add tool for profiling (#364)Florimond Mancabefore adoptione1da6b9

lines 133–136why, one step back
133 """
134 Using a `Connection: close` header should close the connection.135 """136 headers = [(b"connection", b"close")]

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

line 137why, one step back
137 async with ConnectionPool() as http:

Add tool for profiling (#364)Florimond Mancabefore adoptione1da6b9

line 138why, one step back
138 response = await http.request("GET", server.url, headers=headers)

Rename 'read/close' to 'aread/aclose' on Response (#674)Florimond Mancabefore adoptionf9d18a8

line 139why, one step back
139 await response.aread()

Add tool for profiling (#364)Florimond Mancabefore adoptione1da6b9

lines 140–143why, one step back
140 assert len(http.active_connections) == 0
141 assert len(http.keepalive_connections) == 0142143

Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be

lines 144–145why, one step back
144@pytest.mark.usefixtures("async_environment")
145async def test_standard_response_close(server):

Add tool for profiling (#364)Florimond Mancabefore adoptione1da6b9

lines 146–148why, one step back
146 """
147 A standard close should keep the connection open.148 """

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

line 149why, one step back
149 async with ConnectionPool() as http:

Add tool for profiling (#364)Florimond Mancabefore adoptione1da6b9

line 150why, one step back
150 response = await http.request("GET", server.url)

Rename 'read/close' to 'aread/aclose' on Response (#674)Florimond Mancabefore adoptionf9d18a8

lines 151–152why, one step back
151 await response.aread()
152 await response.aclose()

Add tool for profiling (#364)Florimond Mancabefore adoptione1da6b9

lines 153–156why, one step back
153 assert len(http.active_connections) == 0
154 assert len(http.keepalive_connections) == 1155156

Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be

lines 157–158why, one step back
157@pytest.mark.usefixtures("async_environment")
158async def test_premature_response_close(server):

Add tool for profiling (#364)Florimond Mancabefore adoptione1da6b9

lines 159–161why, one step back
159 """
160 A premature close should close the connection.161 """

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

line 162why, one step back
162 async with ConnectionPool() as http:

Add tool for profiling (#364)Florimond Mancabefore adoptione1da6b9

line 163why, one step back
163 response = await http.request("GET", server.url)

Rename 'read/close' to 'aread/aclose' on Response (#674)Florimond Mancabefore adoptionf9d18a8

line 164why, one step back
164 await response.aclose()

Add tool for profiling (#364)Florimond Mancabefore adoptione1da6b9

lines 165–168why, one step back
165 assert len(http.active_connections) == 0
166 assert len(http.keepalive_connections) == 0167168

Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be

line 169why, one step back
169@pytest.mark.usefixtures("async_environment")

Drop `run` and `run_in_threadpool` (#710)Tom Christiebefore adoption6ac49da

line 170why, one step back
170async def test_keepalive_connection_closed_by_server_is_reestablished(server):

Add tool for profiling (#364)Florimond Mancabefore adoptione1da6b9

lines 171–174why, one step back
171 """
172 Upon keep-alive connection closed by remote a new connection173 should be reestablished.174 """

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

line 175why, one step back
175 async with ConnectionPool() as http:

Add tool for profiling (#364)Florimond Mancabefore adoptione1da6b9

line 176why, one step back
176 response = await http.request("GET", server.url)

Rename 'read/close' to 'aread/aclose' on Response (#674)Florimond Mancabefore adoptionf9d18a8

line 177why, one step back
177 await response.aread()

Add tool for profiling (#364)Florimond Mancabefore adoptione1da6b9

lines 178–179why, one step back
178
179 # Shutdown the server to close the keep-alive connection

Drop `run` and `run_in_threadpool` (#710)Tom Christiebefore adoption6ac49da

line 180why, one step back
180 await server.restart()

Add tool for profiling (#364)Florimond Mancabefore adoptione1da6b9

lines 181–182why, one step back
181
182 response = await http.request("GET", server.url)

Rename 'read/close' to 'aread/aclose' on Response (#674)Florimond Mancabefore adoptionf9d18a8

line 183why, one step back
183 await response.aread()

Add tool for profiling (#364)Florimond Mancabefore adoptione1da6b9

lines 184–187why, one step back
184 assert len(http.active_connections) == 0
185 assert len(http.keepalive_connections) == 1186187

Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be

line 188why, one step back
188@pytest.mark.usefixtures("async_environment")

Drop `run` and `run_in_threadpool` (#710)Tom Christiebefore adoption6ac49da

line 189why, one step back
189async def test_keepalive_http2_connection_closed_by_server_is_reestablished(server):

Add tool for profiling (#364)Florimond Mancabefore adoptione1da6b9

lines 190–193why, one step back
190 """
191 Upon keep-alive connection closed by remote a new connection192 should be reestablished.193 """

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

line 194why, one step back
194 async with ConnectionPool() as http:

Add tool for profiling (#364)Florimond Mancabefore adoptione1da6b9

line 195why, one step back
195 response = await http.request("GET", server.url)

Rename 'read/close' to 'aread/aclose' on Response (#674)Florimond Mancabefore adoptionf9d18a8

line 196why, one step back
196 await response.aread()

Add tool for profiling (#364)Florimond Mancabefore adoptione1da6b9

lines 197–198why, one step back
197
198 # Shutdown the server to close the keep-alive connection

Drop `run` and `run_in_threadpool` (#710)Tom Christiebefore adoption6ac49da

line 199why, one step back
199 await server.restart()

Add tool for profiling (#364)Florimond Mancabefore adoptione1da6b9

lines 200–201why, one step back
200
201 response = await http.request("GET", server.url)

Rename 'read/close' to 'aread/aclose' on Response (#674)Florimond Mancabefore adoptionf9d18a8

line 202why, one step back
202 await response.aread()

Add tool for profiling (#364)Florimond Mancabefore adoptione1da6b9

lines 203–206why, one step back
203 assert len(http.active_connections) == 0
204 assert len(http.keepalive_connections) == 1205206

Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be

line 207why, one step back
207@pytest.mark.usefixtures("async_environment")

Drop `run` and `run_in_threadpool` (#710)Tom Christiebefore adoption6ac49da

line 208why, one step back
208async def test_connection_closed_free_semaphore_on_acquire(server):

Add tool for profiling (#364)Florimond Mancabefore adoptione1da6b9

lines 209–212why, one step back
209 """
210 Verify that max_connections semaphore is released211 properly on a disconnected connection.212 """

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

line 213why, one step back
213 async with ConnectionPool(pool_limits=httpx.PoolLimits(hard_limit=1)) as http:

Add tool for profiling (#364)Florimond Mancabefore adoptione1da6b9

line 214why, one step back
214 response = await http.request("GET", server.url)

Rename 'read/close' to 'aread/aclose' on Response (#674)Florimond Mancabefore adoptionf9d18a8

line 215why, one step back
215 await response.aread()

Add tool for profiling (#364)Florimond Mancabefore adoptione1da6b9

lines 216–217why, one step back
216
217 # Close the connection so we're forced to recycle it

Drop `run` and `run_in_threadpool` (#710)Tom Christiebefore adoption6ac49da

line 218why, one step back
218 await server.restart()

Add tool for profiling (#364)Florimond Mancabefore adoptione1da6b9

lines 219–221why, one step back
219
220 response = await http.request("GET", server.url)221 assert response.status_code == 200

Release max_connections for keepalive connections when closing the connection pool (#721)Florimond Mancabefore adoptionbd57b65

lines 222–246why, one step back
222
223224@pytest.mark.usefixtures("async_environment")225async def test_connection_pool_closed_close_keepalive_and_free_semaphore(server):226 """227 Closing the connection pool should close remaining keepalive connections and228 release the max_connections semaphore.229 """230 http = ConnectionPool(pool_limits=httpx.PoolLimits(hard_limit=1))231232 async with http:233 response = await http.request("GET", server.url)234 await response.aread()235 assert response.status_code == 200236 assert len(http.keepalive_connections) == 1237238 assert len(http.keepalive_connections) == 0239240 # Perform a second round of requests to make sure the max_connections semaphore241 # was released properly.242243 async with http:244 response = await http.request("GET", server.url)245 await response.aread()246 assert response.status_code == 200