From: Ben Darnell Date: Mon, 15 Sep 2014 03:02:32 +0000 (-0400) Subject: Handle multiple exceptions in AsyncTestCase. X-Git-Tag: v4.1.0b1~88 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=09bc9827e757efb2b28fd4ff50b1ad871344a0e2;p=thirdparty%2Ftornado.git Handle multiple exceptions in AsyncTestCase. Previously we would silently swallow all but the last; now we raise the first and log all the rest. Closes #1177. --- diff --git a/tornado/test/testing_test.py b/tornado/test/testing_test.py index 9d05126f8..bcea4d252 100644 --- a/tornado/test/testing_test.py +++ b/tornado/test/testing_test.py @@ -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): diff --git a/tornado/testing.py b/tornado/testing.py index 5efd8334e..4d85abe99 100644 --- a/tornado/testing.py +++ b/tornado/testing.py @@ -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