-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
126 lines (97 loc) · 3.54 KB
/
main.py
File metadata and controls
126 lines (97 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import asyncio
import io
from types import SimpleNamespace
import aiohttp
import aiohttp.abc
import aiohttp.payload
import aiohttp.web
async def hello(_request: aiohttp.web.Request) -> aiohttp.web.Response:
body = await _request.read()
print(f"server received {len(body)} bytes")
return aiohttp.web.Response(body=b"")
async def monkey_patch_IOBasePayload_write(
self: aiohttp.payload.IOBasePayload, writer: aiohttp.abc.AbstractStreamWriter
) -> None:
try:
chunk = self._value.read(2**16)
while chunk:
await writer.write(chunk)
chunk = self._value.read(2**16)
finally:
self._value.close()
aiohttp.payload.IOBasePayload.write = monkey_patch_IOBasePayload_write # type: ignore
async def main() -> None:
app = aiohttp.web.Application()
app.router.add_post("/hello", hello)
server_task = asyncio.create_task(aiohttp.web._run_app(app, port=3003))
# wait for server to startup
await asyncio.sleep(1)
reused = 0
num_tests = 5
async def on_connection_reuseconn(
__client_session: aiohttp.ClientSession,
__trace_config_ctx: SimpleNamespace,
params: aiohttp.tracing.TraceConnectionReuseconnParams,
) -> None:
nonlocal reused
reused += 1
tracer = aiohttp.TraceConfig()
tracer.on_connection_reuseconn.append(on_connection_reuseconn)
for _ in range(num_tests):
async with aiohttp.ClientSession(trace_configs=[tracer]) as session:
async with session.post(
"http://127.0.0.1:3003/hello",
data=b"test",
) as response:
response.raise_for_status()
assert reused == 0, f"{reused=}"
async with session.post(
"http://127.0.0.1:3003/hello",
data=b"test",
) as response:
response.raise_for_status()
assert reused == 1, f"{reused=}"
reused = 0
reused = 0
test_failed_ctr = 0
for _ in range(num_tests):
async with aiohttp.ClientSession(trace_configs=[tracer]) as session:
async with session.post(
"http://127.0.0.1:3003/hello",
data=io.BytesIO(b"test"),
) as response:
response.raise_for_status()
assert reused == 0, f"{reused=}"
async with session.post(
"http://127.0.0.1:3003/hello",
data=io.BytesIO(b"test"),
) as response:
response.raise_for_status()
if reused != 1:
print("===============TEST FAILED===============")
test_failed_ctr += 1
reused = 0
for _ in range(num_tests):
async with aiohttp.ClientSession(trace_configs=[tracer]) as session:
async with session.post(
"http://127.0.0.1:3003/hello",
data=b"test",
) as response:
response.raise_for_status()
assert reused == 0, f"{reused=}"
async with session.post(
"http://127.0.0.1:3003/hello",
data=b"test",
) as response:
response.raise_for_status()
assert reused == 1, f"{reused=}"
reused = 0
server_task.cancel()
try:
await server_task
except asyncio.CancelledError:
...
assert test_failed_ctr == 0, f"test failed {test_failed_ctr}/{num_tests} times"
print("===============TEST PASSED===============")
if __name__ == "__main__":
asyncio.run(main())