]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Add a test for exception in websocket callbacks.
authorBen Darnell <ben@bendarnell.com>
Sat, 5 Jul 2014 22:49:36 +0000 (18:49 -0400)
committerBen Darnell <ben@bendarnell.com>
Sat, 5 Jul 2014 22:49:36 +0000 (18:49 -0400)
tornado/test/websocket_test.py

index 177abc3bb5daff25f97fe02d0239ee413d484e86..a1f85cf5c17d30c7f0c7bddb46604d012ad48972 100644 (file)
@@ -4,7 +4,7 @@ import traceback
 
 from tornado.concurrent import Future
 from tornado.httpclient import HTTPError, HTTPRequest
-from tornado.log import gen_log
+from tornado.log import gen_log, app_log
 from tornado.testing import AsyncHTTPTestCase, gen_test, bind_unused_port, ExpectLog
 from tornado.test.util import unittest
 from tornado.web import Application, RequestHandler
@@ -46,6 +46,11 @@ class EchoHandler(TestWebSocketHandler):
         self.write_message(message, isinstance(message, bytes))
 
 
+class ErrorInOnMessageHandler(TestWebSocketHandler):
+    def on_message(self, message):
+        1/0
+
+
 class HeaderHandler(TestWebSocketHandler):
     def open(self):
         try:
@@ -77,6 +82,8 @@ class WebSocketTest(AsyncHTTPTestCase):
             ('/header', HeaderHandler, dict(close_future=self.close_future)),
             ('/close_reason', CloseReasonHandler,
              dict(close_future=self.close_future)),
+            ('/error_in_on_message', ErrorInOnMessageHandler,
+             dict(close_future=self.close_future)),
         ])
 
     def test_http_request(self):
@@ -128,6 +135,17 @@ class WebSocketTest(AsyncHTTPTestCase):
         ws.close()
         yield self.close_future
 
+    @gen_test
+    def test_error_in_on_message(self):
+        ws = yield websocket_connect(
+            'ws://localhost:%d/error_in_on_message' % self.get_http_port())
+        ws.write_message('hello')
+        with ExpectLog(app_log, "Uncaught exception"):
+            response = yield ws.read_message()
+        self.assertIs(response, None)
+        ws.close()
+        yield self.close_future
+
     @gen_test
     def test_websocket_http_fail(self):
         with self.assertRaises(HTTPError) as cm: