]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Handle multiple exceptions in AsyncTestCase.
authorBen Darnell <ben@bendarnell.com>
Mon, 15 Sep 2014 03:02:32 +0000 (23:02 -0400)
committerBen Darnell <ben@bendarnell.com>
Mon, 15 Sep 2014 03:53:12 +0000 (23:53 -0400)
Previously we would silently swallow all but the last; now we
raise the first and log all the rest.

Closes #1177.

tornado/test/testing_test.py
tornado/testing.py

index 9d05126f8aa6300e116b2a11d81ee66104ad77d4..bcea4d252fcb9540f531a217dba33e3eee9d6562 100644 (file)
@@ -3,7 +3,8 @@
 from __future__ import absolute_import, division, print_function, with_statement
 
 from tornado import gen, ioloop
-from tornado.testing import AsyncTestCase, gen_test
+from tornado.log import app_log
+from tornado.testing import AsyncTestCase, gen_test, ExpectLog
 from tornado.test.util import unittest
 
 import contextlib
@@ -62,6 +63,17 @@ class AsyncTestCaseTest(AsyncTestCase):
         self.io_loop.add_timeout(self.io_loop.time() + 0.03, self.stop)
         self.wait(timeout=0.15)
 
+    def test_multiple_errors(self):
+        def fail(message):
+            raise Exception(message)
+        self.io_loop.add_callback(lambda: fail("error one"))
+        self.io_loop.add_callback(lambda: fail("error two"))
+        # The first error gets raised; the second gets logged.
+        with ExpectLog(app_log, "multiple unhandled exceptions"):
+            with self.assertRaises(Exception) as cm:
+                self.wait()
+        self.assertEqual(str(cm.exception), "error one")
+
 
 class AsyncTestCaseWrapperTest(unittest.TestCase):
     def test_undecorated_generator(self):
index 5efd8334ede14e549d992a47d8d13b8bcae988ab..4d85abe997372611e1bc2d1a23287329f967aafd 100644 (file)
@@ -28,7 +28,7 @@ except ImportError:
     IOLoop = None
     netutil = None
     SimpleAsyncHTTPClient = None
-from tornado.log import gen_log
+from tornado.log import gen_log, app_log
 from tornado.stack_context import ExceptionStackContext
 from tornado.util import raise_exc_info, basestring_type
 import functools
@@ -237,7 +237,11 @@ class AsyncTestCase(unittest.TestCase):
         return IOLoop()
 
     def _handle_exception(self, typ, value, tb):
-        self.__failure = (typ, value, tb)
+        if self.__failure is None:
+            self.__failure = (typ, value, tb)
+        else:
+            app_log.error("multiple unhandled exceptions in test",
+                          exc_info=(typ, value, tb))
         self.stop()
         return True