# Path: tornado/testing.py
# class AsyncTestCase(unittest.TestCase):
#     """TestCase subclass for testing IOLoop-based asynchronous code.
# 
#     The unittest framework is synchronous, so the test must be complete
#     by the time the test method returns.  This method provides the stop()
#     and wait() methods for this purpose.  The test method itself must call
#     self.wait(), and asynchronous callbacks should call self.stop() to signal
#     completion.
# 
#     By default, a new IOLoop is constructed for each test and is available
#     as self.io_loop.  This IOLoop should be used in the construction of
#     HTTP clients/servers, etc.  If the code being tested requires a
#     global IOLoop, subclasses should override get_new_ioloop to return it.
# 
#     The IOLoop's start and stop methods should not be called directly.
#     Instead, use self.stop self.wait.  Arguments passed to self.stop are
#     returned from self.wait.  It is possible to have multiple
#     wait/stop cycles in the same test.
# 
#     Example::
# 
#         # This test uses an asynchronous style similar to most async
#         # application code.
#         class MyTestCase(AsyncTestCase):
#             def test_http_fetch(self):
#                 client = AsyncHTTPClient(self.io_loop)
#                 client.fetch("http://www.tornadoweb.org/", self.handle_fetch)
#                 self.wait()
# 
#             def handle_fetch(self, response):
#                 # Test contents of response (failures and exceptions here
#                 # will cause self.wait() to throw an exception and end the
#                 # test).
#                 # Exceptions thrown here are magically propagated to
#                 # self.wait() in test_http_fetch() via stack_context.
#                 self.assertIn("FriendFeed", response.body)
#                 self.stop()
# 
#         # This test uses the argument passing between self.stop and self.wait
#         # for a simpler, more synchronous style.
#         # This style is recommended over the preceding example because it
#         # keeps the assertions in the test method itself, and is therefore
#         # less sensitive to the subtleties of stack_context.
#         class MyTestCase2(AsyncTestCase):
#             def test_http_fetch(self):
#                 client = AsyncHTTPClient(self.io_loop)
#                 client.fetch("http://www.tornadoweb.org/", self.stop)
#                 response = self.wait()
#                 # Test contents of response
#                 self.assertIn("FriendFeed", response.body)
#     """
#     def __init__(self, *args, **kwargs):
#         super(AsyncTestCase, self).__init__(*args, **kwargs)
#         self.__stopped = False
#         self.__running = False
#         self.__failure = None
#         self.__stop_args = None
#         self.__timeout = None
# 
#     def setUp(self):
#         super(AsyncTestCase, self).setUp()
#         self.io_loop = self.get_new_ioloop()
#         self.io_loop.make_current()
# 
#     def tearDown(self):
#         self.io_loop.clear_current()
#         if (not IOLoop.initialized() or
#             self.io_loop is not IOLoop.instance()):
#             # Try to clean up any file descriptors left open in the ioloop.
#             # This avoids leaks, especially when tests are run repeatedly
#             # in the same process with autoreload (because curl does not
#             # set FD_CLOEXEC on its file descriptors)
#             self.io_loop.close(all_fds=True)
#         super(AsyncTestCase, self).tearDown()
# 
#     def get_new_ioloop(self):
#         '''Creates a new IOLoop for this test.  May be overridden in
#         subclasses for tests that require a specific IOLoop (usually
#         the singleton).
#         '''
#         return IOLoop()
# 
#     def _handle_exception(self, typ, value, tb):
#         self.__failure = sys.exc_info()
#         self.stop()
#         return True
# 
#     def __rethrow(self):
#         if self.__failure is not None:
#             failure = self.__failure
#             self.__failure = None
#             raise_exc_info(failure)
# 
#     def run(self, result=None):
#         with ExceptionStackContext(self._handle_exception):
#             super(AsyncTestCase, self).run(result)
#         # In case an exception escaped super.run or the StackContext caught
#         # an exception when there wasn't a wait() to re-raise it, do so here.
#         self.__rethrow()
# 
#     def stop(self, _arg=None, **kwargs):
#         '''Stops the ioloop, causing one pending (or future) call to wait()
#         to return.
# 
#         Keyword arguments or a single positional argument passed to stop() are
#         saved and will be returned by wait().
#         '''
#         assert _arg is None or not kwargs
#         self.__stop_args = kwargs or _arg
#         if self.__running:
#             self.io_loop.stop()
#             self.__running = False
#         self.__stopped = True
# 
#     def wait(self, condition=None, timeout=5):
#         """Runs the IOLoop until stop is called or timeout has passed.
# 
#         In the event of a timeout, an exception will be thrown.
# 
#         If condition is not None, the IOLoop will be restarted after stop()
#         until condition() returns true.
#         """
#         if not self.__stopped:
#             if timeout:
#                 def timeout_func():
#                     try:
#                         raise self.failureException(
#                           'Async operation timed out after %s seconds' %
#                           timeout)
#                     except Exception:
#                         self.__failure = sys.exc_info()
#                     self.stop()
#                 if self.__timeout is not None:
#                     self.io_loop.remove_timeout(self.__timeout)
#                 self.__timeout = self.io_loop.add_timeout(self.io_loop.time() + timeout, timeout_func)
#             while True:
#                 self.__running = True
#                 self.io_loop.start()
#                 if (self.__failure is not None or
#                     condition is None or condition()):
#                     break
#         assert self.__stopped
#         self.__stopped = False
#         self.__rethrow()
#         result = self.__stop_args
#         self.__stop_args = None
#         return result
# 
# Path: tornado/test/util.py



import time
from tornado.testing import AsyncTestCase
from tornado.test.util import unittest
#!/usr/bin/env python

from __future__ import absolute_import, division, with_statement


class AsyncTestCaseTest(AsyncTestCase):
    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


# Path: tornado/testing.py
# class AsyncTestCase(unittest.TestCase):
#     """TestCase subclass for testing IOLoop-based asynchronous code.
# 
#     The unittest framework is synchronous, so the test must be complete
#     by the time the test method returns.  This method provides the stop()
#     and wait() methods for this purpose.  The test method itself must call
#     self.wait(), and asynchronous callbacks should call self.stop() to signal
#     completion.
# 
#     By default, a new IOLoop is constructed for each test and is available
#     as self.io_loop.  This IOLoop should be used in the construction of
#     HTTP clients/servers, etc.  If the code being tested requires a
#     global IOLoop, subclasses should override get_new_ioloop to return it.
# 
#     The IOLoop's start and stop methods should not be called directly.
#     Instead, use self.stop self.wait.  Arguments passed to self.stop are
#     returned from self.wait.  It is possible to have multiple
#     wait/stop cycles in the same test.
# 
#     Example::
# 
#         # This test uses an asynchronous style similar to most async
#         # application code.
#         class MyTestCase(AsyncTestCase):
#             def test_http_fetch(self):
#                 client = AsyncHTTPClient(self.io_loop)
#                 client.fetch("http://www.tornadoweb.org/", self.handle_fetch)
#                 self.wait()
# 
#             def handle_fetch(self, response):
#                 # Test contents of response (failures and exceptions here
#                 # will cause self.wait() to throw an exception and end the
#                 # test).
#                 # Exceptions thrown here are magically propagated to
#                 # self.wait() in test_http_fetch() via stack_context.
#                 self.assertIn("FriendFeed", response.body)
#                 self.stop()
# 
#         # This test uses the argument passing between self.stop and self.wait
#         # for a simpler, more synchronous style.
#         # This style is recommended over the preceding example because it
#         # keeps the assertions in the test method itself, and is therefore
#         # less sensitive to the subtleties of stack_context.
#         class MyTestCase2(AsyncTestCase):
#             def test_http_fetch(self):
#                 client = AsyncHTTPClient(self.io_loop)
#                 client.fetch("http://www.tornadoweb.org/", self.stop)
#                 response = self.wait()
#                 # Test contents of response
#                 self.assertIn("FriendFeed", response.body)
#     """
#     def __init__(self, *args, **kwargs):
#         super(AsyncTestCase, self).__init__(*args, **kwargs)
#         self.__stopped = False
#         self.__running = False
#         self.__failure = None
#         self.__stop_args = None
#         self.__timeout = None
# 
#     def setUp(self):
#         super(AsyncTestCase, self).setUp()
#         self.io_loop = self.get_new_ioloop()
#         self.io_loop.make_current()
# 
#     def tearDown(self):
#         self.io_loop.clear_current()
#         if (not IOLoop.initialized() or
#             self.io_loop is not IOLoop.instance()):
#             # Try to clean up any file descriptors left open in the ioloop.
#             # This avoids leaks, especially when tests are run repeatedly
#             # in the same process with autoreload (because curl does not
#             # set FD_CLOEXEC on its file descriptors)
#             self.io_loop.close(all_fds=True)
#         super(AsyncTestCase, self).tearDown()
# 
#     def get_new_ioloop(self):
#         '''Creates a new IOLoop for this test.  May be overridden in
#         subclasses for tests that require a specific IOLoop (usually
#         the singleton).
#         '''
#         return IOLoop()
# 
#     def _handle_exception(self, typ, value, tb):
#         self.__failure = sys.exc_info()
#         self.stop()
#         return True
# 
#     def __rethrow(self):
#         if self.__failure is not None:
#             failure = self.__failure
#             self.__failure = None
#             raise_exc_info(failure)
# 
#     def run(self, result=None):
#         with ExceptionStackContext(self._handle_exception):
#             super(AsyncTestCase, self).run(result)
#         # In case an exception escaped super.run or the StackContext caught
#         # an exception when there wasn't a wait() to re-raise it, do so here.
#         self.__rethrow()
# 
#     def stop(self, _arg=None, **kwargs):
#         '''Stops the ioloop, causing one pending (or future) call to wait()
#         to return.
# 
#         Keyword arguments or a single positional argument passed to stop() are
#         saved and will be returned by wait().
#         '''
#         assert _arg is None or not kwargs
#         self.__stop_args = kwargs or _arg
#         if self.__running:
#             self.io_loop.stop()
#             self.__running = False
#         self.__stopped = True
# 
#     def wait(self, condition=None, timeout=5):
#         """Runs the IOLoop until stop is called or timeout has passed.
# 
#         In the event of a timeout, an exception will be thrown.
# 
#         If condition is not None, the IOLoop will be restarted after stop()
#         until condition() returns true.
#         """
#         if not self.__stopped:
#             if timeout:
#                 def timeout_func():
#                     try:
#                         raise self.failureException(
#                           'Async operation timed out after %s seconds' %
#                           timeout)
#                     except Exception:
#                         self.__failure = sys.exc_info()
#                     self.stop()
#                 if self.__timeout is not None:
#                     self.io_loop.remove_timeout(self.__timeout)
#                 self.__timeout = self.io_loop.add_timeout(self.io_loop.time() + timeout, timeout_func)
#             while True:
#                 self.__running = True
#                 self.io_loop.start()
#                 if (self.__failure is not None or
#                     condition is None or condition()):
#                     break
#         assert self.__stopped
#         self.__stopped = False
#         self.__rethrow()
#         result = self.__stop_args
#         self.__stop_args = None
#         return result
# 
# Path: tornado/test/util.py



# Path: tornado/test/testing_test.py
import time
from tornado.testing import AsyncTestCase
from tornado.test.util import unittest

#!/usr/bin/env python

from __future__ import absolute_import, division, with_statement


class AsyncTestCaseTest(AsyncTestCase):
    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


    def test_subsequent_wait_calls(self):