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 7b536b7 commit 0a488ee
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 8 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,3 @@ deploy:
tags: true
all_branches: true
python: 3.5
python: 3.6

This comment has been minimized.

Copy link
@kxepal

kxepal Feb 16, 2017

Member

Was that an accident?

This comment has been minimized.

Copy link
@fafhrd91

fafhrd91 Feb 16, 2017

Member

not really, doesnt matter

This comment has been minimized.

Copy link
@kxepal

kxepal Feb 16, 2017

Member

¯\_(ツ)_/¯

4 changes: 3 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
CHANGES
=======

1.3.2 (2017-02-xx)
1.3.2 (2017-02-16)
------------------

- Awaiting on WebSocketResponse.send_* does not work #1645

- Fix multiple calls to client ws_connect when using a shared header dict #1643

- Make CookieJar.filter_cookies() accept plain string parameter. #1636
Expand Down
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 @@ -12,6 +12,7 @@
from struct import Struct

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

__all__ = ('WebSocketParser', 'WebSocketWriter', 'do_handshake',
Expand Down Expand Up @@ -353,7 +354,7 @@ def _send_frame(self, message, opcode):
self._output_size = 0
return self.writer.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 @@ -37,12 +37,19 @@


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


sentinel = object()
Timeout = timeout

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
2 changes: 1 addition & 1 deletion build-wheels.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/bash
PYTHON_VERSIONS="cp34-cp34m cp35-cp35m"
PYTHON_VERSIONS="cp34-cp34m cp35-cp35m cp36-cp36m"

echo "Compile wheels"
for PYTHON in ${PYTHON_VERSIONS}; do
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 0a488ee

Please sign in to comment.