Skip to content

Commit

Permalink
Awaiting on WebSocketResponse.send_* does not work #1645
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikolay Kim committed Feb 16, 2017
1 parent 9218c26 commit 8a3df7f
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 7 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,4 @@ deploy:
on:
tags: true
all_branches: true
python:
- 3.5
python: 3.5
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ This is simple usage example:
async for msg in ws:
if msg.type == web.MsgType.text:
ws.send_str("Hello, {}".format(msg.data))
await ws.send_str("Hello, {}".format(msg.data))
elif msg.type == web.MsgType.binary:
ws.send_bytes(msg.data)
await ws.send_bytes(msg.data)
elif msg.type == web.MsgType.close:
break
Expand Down
3 changes: 2 additions & 1 deletion aiohttp/_ws_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from struct import Struct

from aiohttp import errors, hdrs, helpers
from aiohttp.helpers import noop
from aiohttp.log import ws_logger

__all__ = ('WebSocketReader', 'WebSocketWriter', 'do_handshake',
Expand Down Expand Up @@ -454,7 +455,7 @@ def _send_frame(self, message, opcode):
self._output_size = 0
return self.stream.drain()

return ()
return noop()

def pong(self, message=b''):
"""Send pong message."""
Expand Down
9 changes: 8 additions & 1 deletion aiohttp/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,20 @@


__all__ = ('BasicAuth', 'create_future', 'FormData', 'parse_mimetype',
'Timeout', 'ensure_future')
'Timeout', 'ensure_future', 'noop')


sentinel = object()
Timeout = timeout
NO_EXTENSIONS = bool(os.environ.get('AIOHTTP_NO_EXTENSIONS'))

if sys.version_info < (3, 5):
noop = tuple
else:
@asyncio.coroutine
def noop(*args, **kwargs):
pass


class BasicAuth(namedtuple('BasicAuth', ['login', 'password', 'encoding'])):
"""Http basic authentication helper.
Expand Down
25 changes: 25 additions & 0 deletions tests/test_py35/test_client_websocket_35.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,31 @@ async def handler(request):
assert ws.closed


async def test_client_ws_async_with_send(loop, test_server):
# send_xxx methods have to return awaitable objects

async def handler(request):
ws = web.WebSocketResponse()
await ws.prepare(request)
msg = await ws.receive()
ws.send_str(msg.data + '/answer')
await ws.close()
return ws

app = web.Application(loop=loop)
app.router.add_route('GET', '/', handler)

server = await test_server(app)

async with aiohttp.ClientSession(loop=loop) as client:
async with client.ws_connect(server.make_url('/')) as ws:
await ws.send_str('request')
msg = await ws.receive()
assert msg.data == 'request/answer'

assert ws.closed


async def test_client_ws_async_with_shortcut(loop, test_server):

async def handler(request):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_py35/test_web_websocket_35.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ async def handler(request):
async for msg in ws:
assert msg.type == aiohttp.MsgType.TEXT
s = msg.data
ws.send_str(s + '/answer')
await ws.send_str(s + '/answer')
await ws.close()
closed.set_result(1)
return ws
Expand Down

0 comments on commit 8a3df7f

Please sign in to comment.