]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Fix a StackContext-related bug that was causing exceptions in callbacks
authorBen Darnell <bdarnell@beaker.local>
Fri, 30 Jul 2010 02:08:30 +0000 (19:08 -0700)
committerBen Darnell <bdarnell@beaker.local>
Fri, 30 Jul 2010 02:08:30 +0000 (19:08 -0700)
to result in timeouts instead of making wait() re-throw the exception.
Add a test to verify that this works.

tornado/test/runtests.py
tornado/test/testing_test.py [new file with mode: 0644]
tornado/testing.py

index 79b2cc53e444ba730ad129059f9661a8458ba9d9..36243589f89e21cc9ca89923ae0d85baaa177004 100755 (executable)
@@ -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 (file)
index 0000000..d44dfac
--- /dev/null
@@ -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
index 6b3079359b4f1db7ef035a17c640549b7fe34b38..d8b84c6126078b186cac544af9e3d915bc7ef643 100644 (file)
@@ -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