Skip to content

Commit c587340

Browse files
committed
do not swallow websocket reader exceptions #255
1 parent 40b43ed commit c587340

File tree

3 files changed

+71
-2
lines changed

3 files changed

+71
-2
lines changed

CHANGES.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
CHANGES
22
=======
33

4-
0.15.0 (Unreleased)
4+
0.14.2 (Unreleased)
55
-------------------
66

7+
- Do not swallow websocket reader exceptions #255
8+
79
- web.Request's read, text, json are memorized #250
810

9-
0.14.0 (15/01/2014)
11+
12+
0.14.1 (15/01/2014)
1013
-------------------
1114

1215
- HttpMessage._add_default_headers does not overwrite existing headers #216

aiohttp/web.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,16 @@ def wait_closed(self):
829829
raise RuntimeError('Call .start() first')
830830
yield from self._closing_fut
831831

832+
@asyncio.coroutine
833+
def write_eof(self):
834+
if self._eof_sent:
835+
return
836+
if self._resp_impl is None:
837+
raise RuntimeError("Response has not been started")
838+
839+
yield from self.wait_closed()
840+
self._eof_sent = True
841+
832842
@asyncio.coroutine
833843
def receive_msg(self):
834844
if self._reader is None:

tests/test_web_websocket.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,3 +257,59 @@ def go():
257257
yield from ws.wait_closed()
258258

259259
self.loop.run_until_complete(go())
260+
261+
def test_write_eof_not_started(self):
262+
263+
@asyncio.coroutine
264+
def go():
265+
ws = WebSocketResponse()
266+
with self.assertRaises(RuntimeError):
267+
yield from ws.write_eof()
268+
269+
self.loop.run_until_complete(go())
270+
271+
def test_write_eof_idempotent(self):
272+
req = self.make_request('GET', '/')
273+
ws = WebSocketResponse()
274+
ws.start(req)
275+
ws._closing_fut.set_result(1)
276+
277+
@asyncio.coroutine
278+
def go():
279+
yield from ws.write_eof()
280+
yield from ws.write_eof()
281+
yield from ws.write_eof()
282+
283+
self.loop.run_until_complete(go())
284+
285+
def test_write_eof_exception(self):
286+
req = self.make_request('GET', '/')
287+
ws = WebSocketResponse()
288+
ws.start(req)
289+
ws._closing_fut.set_exception(ValueError())
290+
291+
@asyncio.coroutine
292+
def go():
293+
with self.assertRaises(ValueError):
294+
yield from ws.write_eof()
295+
296+
self.loop.run_until_complete(go())
297+
298+
def test_receive_msg_exc_in_reader(self):
299+
req = self.make_request('GET', '/')
300+
ws = WebSocketResponse()
301+
ws.start(req)
302+
303+
exc = ValueError()
304+
res = asyncio.Future(loop=self.loop)
305+
res.set_exception(exc)
306+
ws._reader.read.return_value = res
307+
308+
@asyncio.coroutine
309+
def go():
310+
with self.assertRaises(ValueError):
311+
yield from ws.receive_msg()
312+
313+
self.assertIs(ws._closing_fut.exception(), exc)
314+
315+
self.loop.run_until_complete(go())

0 commit comments

Comments
 (0)