Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PR #5637/6fb3efc backport][3.8] Update client/server implementation in the autobahn tests #5762

Merged
merged 4 commits into from
Jun 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES/5606.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +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``.
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ Vladyslav Bondar
W. Trevor King
Wei Lin
Weiwei Wang
Will Fatherley
Will McGugan
Willem de Groot
William Grzybowski
Expand Down
45 changes: 20 additions & 25 deletions tests/autobahn/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,26 @@


async def client(loop, url, name):
ws = await aiohttp.ws_connect(url + "/getCaseCount")
num_tests = int((await ws.receive()).data)
print("running %d cases" % num_tests)
await ws.close()

for i in range(1, num_tests + 1):
print("running test case:", i)
text_url = url + "/runCase?case=%d&agent=%s" % (i, name)
ws = await aiohttp.ws_connect(text_url)
while True:
msg = await ws.receive()

if msg.type == aiohttp.WSMsgType.text:
await ws.send_str(msg.data)
elif msg.type == aiohttp.WSMsgType.binary:
await ws.send_bytes(msg.data)
elif msg.type == aiohttp.WSMsgType.close:
await ws.close()
break
else:
break

url = url + "/updateReports?agent=%s" % name
ws = await aiohttp.ws_connect(url)
await ws.close()
async with aiohttp.ClientSession() as session:
async with session.ws_connect(url + "/getCaseCount") as ws:
num_tests = int((await ws.receive()).data)
print("running %d cases" % num_tests)

for i in range(1, num_tests + 1):
print("running test case:", i)
text_url = url + "/runCase?case=%d&agent=%s" % (i, name)
async with session.ws_connect(text_url) as ws:
async for msg in ws:
if msg.type == aiohttp.WSMsgType.TEXT:
await ws.send_str(msg.data)
elif msg.type == aiohttp.WSMsgType.BINARY:
await ws.send_bytes(msg.data)
else:
break

url = url + "/updateReports?agent=%s" % name
async with session.ws_connect(url) as ws:
print("finally requesting %s" % url)


async def run(loop, url, name):
Expand Down
36 changes: 13 additions & 23 deletions tests/autobahn/server.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#!/usr/bin/env python3

import asyncio
import logging

from aiohttp import web
from aiohttp import WSCloseCode, web


async def wshandler(request):
Expand All @@ -17,11 +16,11 @@ async def wshandler(request):
while True:
msg = await ws.receive()

if msg.type == web.WSMsgType.text:
if msg.type == web.WSMsgType.TEXT:
await ws.send_str(msg.data)
elif msg.type == web.WSMsgType.binary:
elif msg.type == web.WSMsgType.BINARY:
await ws.send_bytes(msg.data)
elif msg.type == web.WSMsgType.close:
elif msg.type == web.WSMsgType.CLOSE:
await ws.close()
break
else:
Expand All @@ -30,29 +29,20 @@ async def wshandler(request):
return ws


async def main(loop):
app = web.Application()
app.router.add_route("GET", "/", wshandler)

handler = app._make_handler()
srv = await loop.create_server(handler, "127.0.0.1", 9001)
print("Server started at http://127.0.0.1:9001")
return app, srv, handler


async def finish(app, srv, handler):
srv.close()
await handler.shutdown()
await srv.wait_closed()
async def on_shutdown(app):
for ws in set(app["websockets"]):
await ws.close(code=WSCloseCode.GOING_AWAY, message="Server shutdown")


if __name__ == "__main__":
loop = asyncio.get_event_loop()
logging.basicConfig(
level=logging.DEBUG, format="%(asctime)s %(levelname)s %(message)s"
)
app, srv, handler = loop.run_until_complete(main(loop))

app = web.Application()
app.router.add_route("GET", "/", wshandler)
app.on_shutdown.append(on_shutdown)
try:
loop.run_forever()
web.run_app(app, host="127.0.0.1", port=9001)
except KeyboardInterrupt:
loop.run_until_complete(finish(app, srv, handler))
print("Server stopped at http://127.0.0.1:9001")