Skip to content
Myr

test_connection_pools.py

This file

Path

tests/dispatch/test_connection_pools.py

182 lines · 6.2 KiB · Python

Last changed by

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

History of this file

Take it

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

Install package locally in test-requirements.txt (#231)Florimond Mancabefore adoptionb46d9ea

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

Install package locally in test-requirements.txt (#231)Florimond Mancabefore adoptionb46d9ea

lines 3–4why, one step back
3
4

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

line 5why, one step back
5async def test_keepalive_connections(server, backend):

Install package locally in test-requirements.txt (#231)Florimond Mancabefore adoptionb46d9ea

lines 6–8why, one step back
6 """
7 Connections should default to staying in a keep-alive state.8 """

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

line 9why, one step back
9 async with ConnectionPool(backend=backend) 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)

Install package locally in test-requirements.txt (#231)Florimond Mancabefore adoptionb46d9ea

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)

Install package locally in test-requirements.txt (#231)Florimond Mancabefore adoptionb46d9ea

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

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

line 21why, one step back
21async def test_differing_connection_keys(server, backend):

Install package locally in test-requirements.txt (#231)Florimond Mancabefore adoptionb46d9ea

lines 22–24why, one step back
22 """
23 Connections to differing connection keys should result in multiple connections.24 """

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

line 25why, one step back
25 async with ConnectionPool(backend=backend) as http:

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

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

Install package locally in test-requirements.txt (#231)Florimond Mancabefore adoptionb46d9ea

lines 27–36why, one step back
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) == 23536

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

line 37why, one step back
37async def test_soft_limit(server, backend):

Install package locally in test-requirements.txt (#231)Florimond Mancabefore adoptionb46d9ea

lines 38–42why, one step back
38 """
39 The soft_limit config should limit the maximum number of keep-alive connections.40 """41 pool_limits = httpx.PoolLimits(soft_limit=1)42

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

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

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

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

Install package locally in test-requirements.txt (#231)Florimond Mancabefore adoptionb46d9ea

lines 45–54why, one step back
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) == 15354

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

line 55why, one step back
55async def test_streaming_response_holds_connection(server, backend):

Install package locally in test-requirements.txt (#231)Florimond Mancabefore adoptionb46d9ea

lines 56–58why, one step back
56 """
57 A streaming request should hold the connection open until the response is read.58 """

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

line 59why, one step back
59 async with ConnectionPool(backend=backend) as http:

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

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

Install package locally in test-requirements.txt (#231)Florimond Mancabefore adoptionb46d9ea

lines 61–69why, one step back
61 assert len(http.active_connections) == 1
62 assert len(http.keepalive_connections) == 06364 await response.read()6566 assert len(http.active_connections) == 067 assert len(http.keepalive_connections) == 16869

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

line 70why, one step back
70async def test_multiple_concurrent_connections(server, backend):

Install package locally in test-requirements.txt (#231)Florimond Mancabefore adoptionb46d9ea

lines 71–73why, one step back
71 """
72 Multiple conncurrent requests should open multiple conncurrent connections.73 """

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

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

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

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

Install package locally in test-requirements.txt (#231)Florimond Mancabefore adoptionb46d9ea

lines 76–78why, one step back
76 assert len(http.active_connections) == 1
77 assert len(http.keepalive_connections) == 078

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

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

Install package locally in test-requirements.txt (#231)Florimond Mancabefore adoptionb46d9ea

lines 80–91why, one step back
80 assert len(http.active_connections) == 2
81 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) == 29091

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

line 92why, one step back
92async def test_close_connections(server, backend):

Install package locally in test-requirements.txt (#231)Florimond Mancabefore adoptionb46d9ea

lines 93–96why, one step back
93 """
94 Using a `Connection: close` header should close the connection.95 """96 headers = [(b"connection", b"close")]

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

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

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

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

Install package locally in test-requirements.txt (#231)Florimond Mancabefore adoptionb46d9ea

lines 99–103why, one step back
99 await response.read()
100 assert len(http.active_connections) == 0101 assert len(http.keepalive_connections) == 0102103

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

line 104why, one step back
104async def test_standard_response_close(server, backend):

Install package locally in test-requirements.txt (#231)Florimond Mancabefore adoptionb46d9ea

lines 105–107why, one step back
105 """
106 A standard close should keep the connection open.107 """

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

line 108why, one step back
108 async with ConnectionPool(backend=backend) as http:

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

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

Install package locally in test-requirements.txt (#231)Florimond Mancabefore adoptionb46d9ea

lines 110–115why, one step back
110 await response.read()
111 await response.close()112 assert len(http.active_connections) == 0113 assert len(http.keepalive_connections) == 1114115

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

line 116why, one step back
116async def test_premature_response_close(server, backend):

Install package locally in test-requirements.txt (#231)Florimond Mancabefore adoptionb46d9ea

lines 117–119why, one step back
117 """
118 A premature close should close the connection.119 """

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

line 120why, one step back
120 async with ConnectionPool(backend=backend) as http:

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

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

Install package locally in test-requirements.txt (#231)Florimond Mancabefore adoptionb46d9ea

lines 122–126why, one step back
122 await response.close()
123 assert len(http.active_connections) == 0124 assert len(http.keepalive_connections) == 0125126

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

lines 127–129why, one step back
127async def test_keepalive_connection_closed_by_server_is_reestablished(
128 server, restart, backend129):

Install package locally in test-requirements.txt (#231)Florimond Mancabefore adoptionb46d9ea

lines 130–133why, one step back
130 """
131 Upon keep-alive connection closed by remote a new connection132 should be reestablished.133 """

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

line 134why, one step back
134 async with ConnectionPool(backend=backend) as http:

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

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

Install package locally in test-requirements.txt (#231)Florimond Mancabefore adoptionb46d9ea

lines 136–137why, one step back
136 await response.read()
137

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

line 138why, one step back
138 # Shutdown the server to close the keep-alive connection

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

line 139why, one step back
139 await restart(server)

Install package locally in test-requirements.txt (#231)Florimond Mancabefore adoptionb46d9ea

line 140why, one step back
140

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

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

Install package locally in test-requirements.txt (#231)Florimond Mancabefore adoptionb46d9ea

lines 142–146why, one step back
142 await response.read()
143 assert len(http.active_connections) == 0144 assert len(http.keepalive_connections) == 1145146

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

lines 147–149why, one step back
147async def test_keepalive_http2_connection_closed_by_server_is_reestablished(
148 server, restart, backend149):

Install package locally in test-requirements.txt (#231)Florimond Mancabefore adoptionb46d9ea

lines 150–153why, one step back
150 """
151 Upon keep-alive connection closed by remote a new connection152 should be reestablished.153 """

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

line 154why, one step back
154 async with ConnectionPool(backend=backend) as http:

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

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

Install package locally in test-requirements.txt (#231)Florimond Mancabefore adoptionb46d9ea

lines 156–157why, one step back
156 await response.read()
157

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

line 158why, one step back
158 # Shutdown the server to close the keep-alive connection

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

line 159why, one step back
159 await restart(server)

Install package locally in test-requirements.txt (#231)Florimond Mancabefore adoptionb46d9ea

line 160why, one step back
160

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

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

Install package locally in test-requirements.txt (#231)Florimond Mancabefore adoptionb46d9ea

lines 162–164why, one step back
162 await response.read()
163 assert len(http.active_connections) == 0164 assert len(http.keepalive_connections) == 1

Release semaphore if connection is dropped (#230)Ordanis Sanchezbefore adoptiona960a2a

lines 165–166why, one step back
165
166

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

line 167why, one step back
167async def test_connection_closed_free_semaphore_on_acquire(server, restart, backend):

Release semaphore if connection is dropped (#230)Ordanis Sanchezbefore adoptiona960a2a

lines 168–171why, one step back
168 """
169 Verify that max_connections semaphore is released170 properly on a disconnected connection.171 """

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

line 172why, one step back
172 async with ConnectionPool(

Add trio concurrency backend (#276)Florimond Mancabefore adoption08355c6

lines 173–174why, one step back
173 pool_limits=httpx.PoolLimits(hard_limit=1), backend=backend
174 ) as http:

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

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

Release semaphore if connection is dropped (#230)Ordanis Sanchezbefore adoptiona960a2a

lines 176–178why, one step back
176 await response.read()
177178 # Close the connection so we're forced to recycle it

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

line 179why, one step back
179 await restart(server)

Release semaphore if connection is dropped (#230)Ordanis Sanchezbefore adoptiona960a2a

line 180why, one step back
180

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

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

Release semaphore if connection is dropped (#230)Ordanis Sanchezbefore adoptiona960a2a

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