<s> from __future__ import absolute_import , division , print_function , with_statement <EOL> from tornado import gen , ioloop <EOL> from tornado . testing import AsyncTestCase , gen_test <EOL> from tornado . test . util import unittest <EOL> import contextlib <EOL> import os <EOL> @ contextlib . contextmanager <EOL> def set_environ ( name , value ) : <EOL> old_value = os . environ . get ( '<STR_LIT:name>' ) <EOL> os . environ [ name ] = value <EOL> try : <EOL> yield <EOL> finally : <EOL> if old_value is None : <EOL> del os . environ [ name ] <EOL> else : <EOL> os . environ [ name ] = old_value <EOL> class AsyncTestCaseTest ( AsyncTestCase ) : <EOL> def test_exception_in_callback ( self ) : <EOL> self . io_loop . add_callback ( lambda : <NUM_LIT:1> / <NUM_LIT:0> ) <EOL> try : <EOL> self . wait ( ) <EOL> self . fail ( "<STR_LIT>" ) <EOL> except ZeroDivisionError : <EOL> pass <EOL> def test_wait_timeout ( self ) : <EOL> time = self . io_loop . time <EOL> self . io_loop . add_timeout ( time ( ) + <NUM_LIT> , self . stop ) <EOL> self . wait ( ) <EOL> self . io_loop . add_timeout ( time ( ) + <NUM_LIT:1> , self . stop ) <EOL> with self . assertRaises ( self . failureException ) : <EOL> self . wait ( timeout = <NUM_LIT> ) <EOL> self . io_loop . add_timeout ( time ( ) + <NUM_LIT:1> , self . stop ) <EOL> with set_environ ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> with self . assertRaises ( self . failureException ) : <EOL> self . wait ( ) <EOL> def test_subsequent_wait_calls ( self ) : <EOL> """<STR_LIT>""" <EOL> self . io_loop . add_timeout ( self . io_loop . time ( ) + <NUM_LIT> , self . stop ) <EOL> self . wait ( timeout = <NUM_LIT> ) <EOL> self . io_loop . add_timeout ( self . io_loop . time ( ) + <NUM_LIT> , self . stop ) <EOL> self . wait ( timeout = <NUM_LIT> ) <EOL> class SetUpTearDownTest ( unittest . TestCase ) : <EOL> def test_set_up_tear_down ( self ) : <EOL> """<STR_LIT>""" <EOL> events = [ ] <EOL> result = unittest . TestResult ( ) <EOL> class SetUpTearDown ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> events . append ( '<STR_LIT>' ) <EOL> def tearDown ( self ) : <EOL> events . append ( '<STR_LIT>' ) <EOL> class InheritBoth ( AsyncTestCase , SetUpTearDown ) : <EOL> def test ( self ) : <EOL> events . append ( '<STR_LIT:test>' ) <EOL> InheritBoth ( '<STR_LIT:test>' ) . run ( result ) <EOL> expected = [ '<STR_LIT>' , '<STR_LIT:test>' , '<STR_LIT>' ] <EOL> self . assertEqual ( expected , events ) <EOL> class GenTest ( AsyncTestCase ) : <EOL> def setUp ( self ) : <EOL> super ( GenTest , self ) . setUp ( ) <EOL> self . finished = False <EOL> def tearDown ( self ) : <EOL> self . assertTrue ( self . finished ) <EOL> super ( GenTest , self ) . tearDown ( ) <EOL> @ gen_test <EOL> def test_sync ( self ) : <EOL> self . finished = True <EOL> @ gen_test <EOL> def test_async ( self ) : <EOL> yield gen . Task ( self . io_loop . add_callback ) <EOL> self . finished = True <EOL> def test_timeout ( self ) : <EOL> @ gen_test ( timeout = <NUM_LIT:0.1> ) <EOL> def test ( self ) : <EOL> yield gen . Task ( self . io_loop . add_timeout , self . io_loop . time ( ) + <NUM_LIT:1> ) <EOL> with self . assertRaises ( ioloop . TimeoutError ) : <EOL> test ( self ) <EOL> self . finished = True <EOL> def test_no_timeout ( self ) : <EOL> @ gen_test ( timeout = <NUM_LIT:1> ) <EOL> def test ( self ) : <EOL> time = self . io_loop . time <EOL> yield gen . Task ( self . io_loop . add_timeout , time ( ) + <NUM_LIT:0.1> ) <EOL> test ( self ) <EOL> self . finished = True <EOL> def test_timeout_environment_variable ( self ) : <EOL> @ gen_test ( timeout = <NUM_LIT:0.5> ) <EOL> def test_long_timeout ( self ) : <EOL> time = self . io_loop . time <EOL> yield gen . Task ( self . io_loop . add_timeout , time ( ) + <NUM_LIT> ) <EOL> with set_environ ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> test_long_timeout ( self ) <EOL> self . finished = True <EOL> def test_no_timeout_environment_variable ( self ) : <EOL> @ gen_test ( timeout = <NUM_LIT> ) <EOL> def test_short_timeout ( self ) : <EOL> time = self . io_loop . time <EOL> yield gen . Task ( self . io_loop . add_timeout , time ( ) + <NUM_LIT:1> ) <EOL> with set_environ ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> with self . assertRaises ( ioloop . TimeoutError ) : <EOL> test_short_timeout ( self ) <EOL> self . finished = True <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
