Skip to content

Commit 6fb3efc

Browse files
author
Will Fatherley
authored
Update client/server implementation in the autobahn tests (#5637)
1 parent b619591 commit 6fb3efc

File tree

4 files changed

+32
-45
lines changed

4 files changed

+32
-45
lines changed

CHANGES/5606.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Replace deprecated app handler design in ``tests/autobahn/server.py`` with call to ``web.run_app``; replace deprecated ``aiohttp.ws_connect`` calls in ``tests/autobahn/client.py`` with ``aiohttp.ClienSession.ws_connect``.

CONTRIBUTORS.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ Vladyslav Bondar
310310
W. Trevor King
311311
Wei Lin
312312
Weiwei Wang
313+
Will Fatherley
313314
Will McGugan
314315
Willem de Groot
315316
William Grzybowski

tests/autobahn/client.py

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,26 @@
66

77

88
async def client(loop, url, name):
9-
ws = await aiohttp.ws_connect(url + "/getCaseCount")
10-
num_tests = int((await ws.receive()).data)
11-
print("running %d cases" % num_tests)
12-
await ws.close()
13-
14-
for i in range(1, num_tests + 1):
15-
print("running test case:", i)
16-
text_url = url + "/runCase?case=%d&agent=%s" % (i, name)
17-
ws = await aiohttp.ws_connect(text_url)
18-
while True:
19-
msg = await ws.receive()
20-
21-
if msg.type == aiohttp.WSMsgType.TEXT:
22-
await ws.send_str(msg.data)
23-
elif msg.type == aiohttp.WSMsgType.BINARY:
24-
await ws.send_bytes(msg.data)
25-
elif msg.type == aiohttp.WSMsgType.CLOSE:
26-
await ws.close()
27-
break
28-
else:
29-
break
30-
31-
url = url + "/updateReports?agent=%s" % name
32-
ws = await aiohttp.ws_connect(url)
33-
await ws.close()
9+
async with aiohttp.ClientSession() as session:
10+
async with session.ws_connect(url + "/getCaseCount") as ws:
11+
num_tests = int((await ws.receive()).data)
12+
print("running %d cases" % num_tests)
13+
14+
for i in range(1, num_tests + 1):
15+
print("running test case:", i)
16+
text_url = url + "/runCase?case=%d&agent=%s" % (i, name)
17+
async with session.ws_connect(text_url) as ws:
18+
async for msg in ws:
19+
if msg.type == aiohttp.WSMsgType.TEXT:
20+
await ws.send_str(msg.data)
21+
elif msg.type == aiohttp.WSMsgType.BINARY:
22+
await ws.send_bytes(msg.data)
23+
else:
24+
break
25+
26+
url = url + "/updateReports?agent=%s" % name
27+
async with session.ws_connect(url) as ws:
28+
print("finally requesting %s" % url)
3429

3530

3631
async def run(loop, url, name):

tests/autobahn/server.py

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
#!/usr/bin/env python3
22

3-
import asyncio
43
import logging
54

6-
from aiohttp import web
5+
from aiohttp import WSCloseCode, web
76

87

98
async def wshandler(request):
@@ -30,29 +29,20 @@ async def wshandler(request):
3029
return ws
3130

3231

33-
async def main(loop):
34-
app = web.Application()
35-
app.router.add_route("GET", "/", wshandler)
36-
37-
handler = app._make_handler()
38-
srv = await loop.create_server(handler, "127.0.0.1", 9001)
39-
print("Server started at http://127.0.0.1:9001")
40-
return app, srv, handler
41-
42-
43-
async def finish(app, srv, handler):
44-
srv.close()
45-
await handler.shutdown()
46-
await srv.wait_closed()
32+
async def on_shutdown(app):
33+
for ws in set(app["websockets"]):
34+
await ws.close(code=WSCloseCode.GOING_AWAY, message="Server shutdown")
4735

4836

4937
if __name__ == "__main__":
50-
loop = asyncio.get_event_loop()
5138
logging.basicConfig(
5239
level=logging.DEBUG, format="%(asctime)s %(levelname)s %(message)s"
5340
)
54-
app, srv, handler = loop.run_until_complete(main(loop))
41+
42+
app = web.Application()
43+
app.router.add_route("GET", "/", wshandler)
44+
app.on_shutdown.append(on_shutdown)
5545
try:
56-
loop.run_forever()
46+
web.run_app(app, host="127.0.0.1", port=9001)
5747
except KeyboardInterrupt:
58-
loop.run_until_complete(finish(app, srv, handler))
48+
print("Server stopped at http://127.0.0.1:9001")

0 commit comments

Comments
 (0)