From: Ben Darnell Date: Fri, 30 Jul 2010 02:08:30 +0000 (-0700) Subject: Fix a StackContext-related bug that was causing exceptions in callbacks X-Git-Tag: v1.1.0~58 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6df410aa53261f56d6ec0aa336c9578c246277f8;p=thirdparty%2Ftornado.git Fix a StackContext-related bug that was causing exceptions in callbacks to result in timeouts instead of making wait() re-throw the exception. Add a test to verify that this works. --- diff --git a/tornado/test/runtests.py b/tornado/test/runtests.py index 79b2cc53e..36243589f 100755 --- a/tornado/test/runtests.py +++ b/tornado/test/runtests.py @@ -3,8 +3,9 @@ import unittest TEST_MODULES = [ 'tornado.httputil.doctests', - 'tornado.test.stack_context_test', 'tornado.test.ioloop_test', + 'tornado.test.stack_context_test', + 'tornado.test.testing_test', ] def all(): diff --git a/tornado/test/testing_test.py b/tornado/test/testing_test.py new file mode 100644 index 000000000..d44dfac40 --- /dev/null +++ b/tornado/test/testing_test.py @@ -0,0 +1,15 @@ +#!/usr/bin/env python +import unittest +from tornado.testing import AsyncTestCase, LogTrapTestCase + +class AsyncTestCaseTest(AsyncTestCase, LogTrapTestCase): + def test_exception_in_callback(self): + self.io_loop.add_callback(lambda: 1/0) + try: + self.wait() + self.fail("did not get expected exception") + except ZeroDivisionError: + pass + +if __name__ == '__main__': + unittest.main diff --git a/tornado/testing.py b/tornado/testing.py index 6b3079359..d8b84c612 100644 --- a/tornado/testing.py +++ b/tornado/testing.py @@ -20,7 +20,7 @@ from __future__ import with_statement from cStringIO import StringIO from tornado.httpclient import AsyncHTTPClient from tornado.httpserver import HTTPServer -from tornado.stack_context import StackContext +from tornado.stack_context import StackContext, NullContext import contextlib import functools import logging @@ -147,7 +147,11 @@ class AsyncTestCase(unittest.TestCase): self.io_loop.add_timeout(time.time() + timeout, timeout_func) while True: self.__running = True - self.io_loop.start() + with NullContext(): + # Wipe out the StackContext that was established in + # self.run() so that all callbacks executed inside the + # IOLoop will re-run it. + self.io_loop.start() if (self.__failure is not None or condition is None or condition()): break