Skip to content

Commit

Permalink
do not swallow websocket reader exceptions #255
Browse files Browse the repository at this point in the history
  • Loading branch information
fafhrd91 committed Jan 22, 2015
1 parent 40b43ed commit c587340
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
7 changes: 5 additions & 2 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
CHANGES
=======

0.15.0 (Unreleased)
0.14.2 (Unreleased)
-------------------

- Do not swallow websocket reader exceptions #255

- web.Request's read, text, json are memorized #250

0.14.0 (15/01/2014)

0.14.1 (15/01/2014)
-------------------

- HttpMessage._add_default_headers does not overwrite existing headers #216
Expand Down
10 changes: 10 additions & 0 deletions aiohttp/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,16 @@ def wait_closed(self):
raise RuntimeError('Call .start() first')
yield from self._closing_fut

@asyncio.coroutine
def write_eof(self):
if self._eof_sent:
return
if self._resp_impl is None:
raise RuntimeError("Response has not been started")

yield from self.wait_closed()
self._eof_sent = True

@asyncio.coroutine
def receive_msg(self):
if self._reader is None:
Expand Down
56 changes: 56 additions & 0 deletions tests/test_web_websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,3 +257,59 @@ def go():
yield from ws.wait_closed()

self.loop.run_until_complete(go())

def test_write_eof_not_started(self):

@asyncio.coroutine
def go():
ws = WebSocketResponse()
with self.assertRaises(RuntimeError):
yield from ws.write_eof()

self.loop.run_until_complete(go())

def test_write_eof_idempotent(self):
req = self.make_request('GET', '/')
ws = WebSocketResponse()
ws.start(req)
ws._closing_fut.set_result(1)

@asyncio.coroutine
def go():
yield from ws.write_eof()
yield from ws.write_eof()
yield from ws.write_eof()

self.loop.run_until_complete(go())

def test_write_eof_exception(self):
req = self.make_request('GET', '/')
ws = WebSocketResponse()
ws.start(req)
ws._closing_fut.set_exception(ValueError())

@asyncio.coroutine
def go():
with self.assertRaises(ValueError):
yield from ws.write_eof()

self.loop.run_until_complete(go())

def test_receive_msg_exc_in_reader(self):
req = self.make_request('GET', '/')
ws = WebSocketResponse()
ws.start(req)

exc = ValueError()
res = asyncio.Future(loop=self.loop)
res.set_exception(exc)
ws._reader.read.return_value = res

@asyncio.coroutine
def go():
with self.assertRaises(ValueError):
yield from ws.receive_msg()

self.assertIs(ws._closing_fut.exception(), exc)

self.loop.run_until_complete(go())

0 comments on commit c587340

Please sign in to comment.