Skip to content
Myr

test_connection_pools.py

This file

Path

tests/dispatch/test_connection_pools.py

211 lines · 7.0 KiB · Python

Last changed by

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

History of this file

Take it

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

Serve test server in thread (#292)Florimond Mancabefore adoption994403b

line 1why, one step back
1import httpx

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

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

Serve test server in thread (#292)Florimond Mancabefore adoption994403b

lines 3–8why, one step back
3
45async def test_keepalive_connections(server, backend):6 """7 Connections should default to staying in a keep-alive state.8 """

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

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

Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280

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

Serve test server in thread (#292)Florimond Mancabefore adoption994403b

lines 11–14why, one step back
11 await response.read()
12 assert len(http.active_connections) == 013 assert len(http.keepalive_connections) == 114

Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280

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

Serve test server in thread (#292)Florimond Mancabefore adoption994403b

lines 16–20why, one step back
16 await response.read()
17 assert len(http.active_connections) == 018 assert len(http.keepalive_connections) == 11920

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

lines 21–51why, one step back
21async def test_keepalive_timeout(server, backend):
22 """23 Keep-alive connections should timeout.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 http.next_keepalive_check = 0.032 await http.check_keepalive_expiry()3334 assert len(http.active_connections) == 035 assert len(http.keepalive_connections) == 13637 async with ConnectionPool() as http:38 http.KEEP_ALIVE_EXPIRY = 0.03940 response = await http.request("GET", server.url)41 await response.read()42 assert len(http.active_connections) == 043 assert len(http.keepalive_connections) == 14445 http.next_keepalive_check = 0.046 await http.check_keepalive_expiry()4748 assert len(http.active_connections) == 049 assert len(http.keepalive_connections) == 05051

Serve test server in thread (#292)Florimond Mancabefore adoption994403b

lines 52–55why, one step back
52async def test_differing_connection_keys(server, backend):
53 """54 Connections to differing connection keys should result in multiple connections.55 """

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

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

Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280

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

Serve test server in thread (#292)Florimond Mancabefore adoption994403b

lines 58–73why, one step back
58 await response.read()
59 assert len(http.active_connections) == 060 assert len(http.keepalive_connections) == 16162 response = await http.request("GET", "http://localhost:8000/")63 await response.read()64 assert len(http.active_connections) == 065 assert len(http.keepalive_connections) == 2666768async def test_soft_limit(server, backend):69 """70 The soft_limit config should limit the maximum number of keep-alive connections.71 """72 pool_limits = httpx.PoolLimits(soft_limit=1)73

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

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

Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280

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

Serve test server in thread (#292)Florimond Mancabefore adoption994403b

lines 76–89why, one step back
76 await response.read()
77 assert len(http.active_connections) == 078 assert len(http.keepalive_connections) == 17980 response = await http.request("GET", "http://localhost:8000/")81 await response.read()82 assert len(http.active_connections) == 083 assert len(http.keepalive_connections) == 1848586async def test_streaming_response_holds_connection(server, backend):87 """88 A streaming request should hold the connection open until the response is read.89 """

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

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

Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280

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

Serve test server in thread (#292)Florimond Mancabefore adoption994403b

lines 92–104why, one step back
92 assert len(http.active_connections) == 1
93 assert len(http.keepalive_connections) == 09495 await response.read()9697 assert len(http.active_connections) == 098 assert len(http.keepalive_connections) == 199100101async def test_multiple_concurrent_connections(server, backend):102 """103 Multiple conncurrent requests should open multiple conncurrent connections.104 """

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

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

Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280

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

Serve test server in thread (#292)Florimond Mancabefore adoption994403b

lines 107–109why, one step back
107 assert len(http.active_connections) == 1
108 assert len(http.keepalive_connections) == 0109

Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280

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

Serve test server in thread (#292)Florimond Mancabefore adoption994403b

lines 111–127why, one step back
111 assert len(http.active_connections) == 2
112 assert len(http.keepalive_connections) == 0113114 await response_b.read()115 assert len(http.active_connections) == 1116 assert len(http.keepalive_connections) == 1117118 await response_a.read()119 assert len(http.active_connections) == 0120 assert len(http.keepalive_connections) == 2121122123async def test_close_connections(server, backend):124 """125 Using a `Connection: close` header should close the connection.126 """127 headers = [(b"connection", b"close")]

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

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

Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280

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

Serve test server in thread (#292)Florimond Mancabefore adoption994403b

lines 130–138why, one step back
130 await response.read()
131 assert len(http.active_connections) == 0132 assert len(http.keepalive_connections) == 0133134135async def test_standard_response_close(server, backend):136 """137 A standard close should keep the connection open.138 """

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

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

Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280

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

Serve test server in thread (#292)Florimond Mancabefore adoption994403b

lines 141–150why, one step back
141 await response.read()
142 await response.close()143 assert len(http.active_connections) == 0144 assert len(http.keepalive_connections) == 1145146147async def test_premature_response_close(server, backend):148 """149 A premature close should close the connection.150 """

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

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

Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280

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

Serve test server in thread (#292)Florimond Mancabefore adoption994403b

lines 153–164why, one step back
153 await response.close()
154 assert len(http.active_connections) == 0155 assert len(http.keepalive_connections) == 0156157158async def test_keepalive_connection_closed_by_server_is_reestablished(159 server, restart, backend160):161 """162 Upon keep-alive connection closed by remote a new connection163 should be reestablished.164 """

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

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

Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280

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

Serve test server in thread (#292)Florimond Mancabefore adoption994403b

lines 167–171why, one step back
167 await response.read()
168169 # Shutdown the server to close the keep-alive connection170 await restart(server)171

Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280

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

Serve test server in thread (#292)Florimond Mancabefore adoption994403b

lines 173–184why, one step back
173 await response.read()
174 assert len(http.active_connections) == 0175 assert len(http.keepalive_connections) == 1176177178async def test_keepalive_http2_connection_closed_by_server_is_reestablished(179 server, restart, backend180):181 """182 Upon keep-alive connection closed by remote a new connection183 should be reestablished.184 """

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

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

Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280

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

Serve test server in thread (#292)Florimond Mancabefore adoption994403b

lines 187–191why, one step back
187 await response.read()
188189 # Shutdown the server to close the keep-alive connection190 await restart(server)191

Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280

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

Serve test server in thread (#292)Florimond Mancabefore adoption994403b

lines 193–202why, one step back
193 await response.read()
194 assert len(http.active_connections) == 0195 assert len(http.keepalive_connections) == 1196197198async def test_connection_closed_free_semaphore_on_acquire(server, restart, backend):199 """200 Verify that max_connections semaphore is released201 properly on a disconnected connection.202 """

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

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

Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280

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

Serve test server in thread (#292)Florimond Mancabefore adoption994403b

lines 205–209why, one step back
205 await response.read()
206207 # Close the connection so we're forced to recycle it208 await restart(server)209

Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280

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

Serve test server in thread (#292)Florimond Mancabefore adoption994403b

line 211why, one step back
211 assert response.status_code == 200