Skip to content

Commit

Permalink
Explicitly return TimeoutError on WebSocketResponse.close() (#1084)
Browse files Browse the repository at this point in the history
  • Loading branch information
achimnol committed Aug 15, 2016
1 parent 2269068 commit 6a07366
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
4 changes: 4 additions & 0 deletions aiohttp/web_ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,10 @@ def close(self, *, code=1000, message=b''):
if msg.type == WSMsgType.CLOSE:
self._close_code = msg.data
return True

self._close_code = 1006
self._exception = asyncio.TimeoutError()
return True
else:
return False

Expand Down
25 changes: 15 additions & 10 deletions tests/test_web_websocket_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import aiohttp
from aiohttp import helpers, web
from aiohttp._ws_impl import WSMsgType


@pytest.mark.run_loop
Expand Down Expand Up @@ -220,7 +221,7 @@ def handler(request):

@pytest.mark.run_loop
def test_close_timeout(create_app_and_client, loop):
closed = helpers.create_future(loop)
aborted = helpers.create_future(loop)

@asyncio.coroutine
def handler(request):
Expand All @@ -229,12 +230,14 @@ def handler(request):
assert 'request' == (yield from ws.receive_str())
ws.send_str('reply')
begin = ws._loop.time()
yield from ws.close()
assert True == (yield from ws.close())
elapsed = ws._loop.time() - begin
assert elapsed < 0.201, \
'close() should have returned before ' \
'at most 2x timeout.'
closed.set_result(1)
assert ws.close_code == 1006
assert isinstance(ws.exception(), asyncio.TimeoutError)
aborted.set_result(1)
return ws

app, client = yield from create_app_and_client()
Expand All @@ -247,15 +250,17 @@ def handler(request):
# The server closes here. Then the client sends bogus messages with an
# internval shorter than server-side close timeout, to make the server
# hanging indefinitely.
yield from asyncio.sleep(0.04, loop=loop)
yield from asyncio.sleep(0.08, loop=loop)
msg = yield from ws._reader.read()
assert msg.type == WSMsgType.CLOSE
ws.send_str('hang')
yield from asyncio.sleep(0.04, loop=loop)
yield from asyncio.sleep(0.08, loop=loop)
ws.send_str('hang')
yield from asyncio.sleep(0.04, loop=loop)
yield from asyncio.sleep(0.08, loop=loop)
ws.send_str('hang')
yield from asyncio.sleep(0.04, loop=loop)
yield from asyncio.sleep(0.08, loop=loop)
ws.send_str('hang')
yield from asyncio.sleep(0.04, loop=loop)
# The server should have been closed now.
assert 1 == (yield from closed)
yield from asyncio.sleep(0.08, loop=loop)
assert 1 == (yield from aborted)

yield from ws.close()

0 comments on commit 6a07366

Please sign in to comment.