Skip to content

Commit 4fda47a

Browse files
committed
Add failing test for connection reuse for #10325
1 parent 9482755 commit 4fda47a

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

tests/test_client_functional.py

+45
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,51 @@ async def on_reuseconn(session: object, ctx: object, params: object) -> None:
186186
assert cnt_conn_reuse == 1
187187

188188

189+
@pytest.mark.xfail(reason="second client is not reusing the connection")
190+
async def test_keepalive_post_empty_bytes(aiohttp_client: AiohttpClient) -> None:
191+
async def handler(request: web.Request) -> web.Response:
192+
return web.Response(body=b"")
193+
194+
app = web.Application()
195+
app.router.add_route("POST", "/", handler)
196+
197+
cnt_conn_reuse = 0
198+
199+
async def on_reuseconn(session: object, ctx: object, params: object) -> None:
200+
nonlocal cnt_conn_reuse
201+
cnt_conn_reuse += 1
202+
203+
trace_config1 = aiohttp.TraceConfig()
204+
trace_config1._on_connection_reuseconn.append(on_reuseconn)
205+
206+
connector1 = aiohttp.TCPConnector(limit=1)
207+
client1 = await aiohttp_client(
208+
app, connector=connector1, trace_configs=[trace_config1]
209+
)
210+
211+
resp1 = await client1.post("/", data=io.BytesIO(), headers={"Content-Length": "0"})
212+
await resp1.read()
213+
resp2 = await client1.post("/", data=io.BytesIO(), headers={"Content-Length": "0"})
214+
await resp2.read()
215+
216+
assert cnt_conn_reuse == 1
217+
218+
trace_config2 = aiohttp.TraceConfig()
219+
trace_config2._on_connection_reuseconn.append(on_reuseconn)
220+
221+
connector2 = aiohttp.TCPConnector(limit=1)
222+
client2 = await aiohttp_client(
223+
app, connector=connector2, trace_configs=[trace_config2]
224+
)
225+
226+
resp3 = await client2.post("/", data=io.BytesIO(), headers={"Content-Length": "0"})
227+
await resp3.read()
228+
resp4 = await client2.post("/", data=io.BytesIO(), headers={"Content-Length": "0"})
229+
await resp4.read()
230+
231+
assert cnt_conn_reuse == 2
232+
233+
189234
async def test_keepalive_response_released(aiohttp_client: AiohttpClient) -> None:
190235
async def handler(request: web.Request) -> web.Response:
191236
body = await request.read()

0 commit comments

Comments
 (0)