-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Python 3.5 “async for” compat to websocket
Instead of while True: msg = await ws.receive() ... elif msg.tp == web.MsgType.close: break do: async for msg in ws: ...
- Loading branch information
1 parent
04191d9
commit 48ae89e
Showing
4 changed files
with
71 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
""" | ||
Python 3.5 test module, testing new native async stuff. | ||
This file allows files in here to be called the same as other test files. | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import pytest | ||
|
||
import asyncio | ||
|
||
from aiohttp import web, websocket | ||
from aiohttp.websocket_client import MsgType, ws_connect | ||
|
||
|
||
async def create_server(loop, port, method, path, route_handler): | ||
app = web.Application(loop=loop) | ||
app.router.add_route(method, path, route_handler) | ||
handler = app.make_handler(keep_alive_on=False) | ||
return await loop.create_server(handler, '127.0.0.1', port) | ||
|
||
|
||
@pytest.mark.run_loop | ||
async def test_await(loop, unused_port): | ||
closed = asyncio.Future(loop=loop) | ||
|
||
async def handler(request): | ||
ws = web.WebSocketResponse() | ||
await ws.prepare(request) | ||
async for msg in ws: | ||
assert msg.tp == MsgType.text | ||
s = msg.data | ||
ws.send_str(s + '/answer') | ||
await ws.close() | ||
closed.set_result(1) | ||
return ws | ||
|
||
port = unused_port() | ||
await create_server(loop, port, 'GET', '/', handler) # returns server | ||
resp = await ws_connect('ws://127.0.0.1:{p}'.format(p=port), loop=loop) | ||
|
||
items = ['q1', 'q2', 'q3'] | ||
for item in items: | ||
resp._writer.send(item) | ||
msg = await resp._reader.read() | ||
assert msg.tp == websocket.MSG_TEXT | ||
assert item + '/answer' == msg.data | ||
|
||
msg = await resp._reader.read() | ||
assert msg.tp == websocket.MSG_CLOSE | ||
assert msg.data == 1000 | ||
assert msg.extra == '' | ||
|
||
resp._writer.close() | ||
|
||
await closed | ||
resp.close() |