<s> from __future__ import absolute_import , division , print_function , with_statement <EOL> import contextlib <EOL> import datetime <EOL> import functools <EOL> import socket <EOL> import sys <EOL> import threading <EOL> import time <EOL> from tornado import gen <EOL> from tornado . ioloop import IOLoop , TimeoutError <EOL> from tornado . stack_context import ExceptionStackContext , StackContext , wrap , NullContext <EOL> from tornado . testing import AsyncTestCase , bind_unused_port <EOL> from tornado . test . util import unittest , skipIfNonUnix , skipOnTravis <EOL> try : <EOL> from concurrent import futures <EOL> except ImportError : <EOL> futures = None <EOL> class TestIOLoop ( AsyncTestCase ) : <EOL> @ skipOnTravis <EOL> def test_add_callback_wakeup ( self ) : <EOL> def callback ( ) : <EOL> self . called = True <EOL> self . stop ( ) <EOL> def schedule_callback ( ) : <EOL> self . called = False <EOL> self . io_loop . add_callback ( callback ) <EOL> self . start_time = time . time ( ) <EOL> self . io_loop . add_timeout ( self . io_loop . time ( ) , schedule_callback ) <EOL> self . wait ( ) <EOL> self . assertAlmostEqual ( time . time ( ) , self . start_time , places = <NUM_LIT:2> ) <EOL> self . assertTrue ( self . called ) <EOL> @ skipOnTravis <EOL> def test_add_callback_wakeup_other_thread ( self ) : <EOL> def target ( ) : <EOL> time . sleep ( <NUM_LIT> ) <EOL> self . stop_time = time . time ( ) <EOL> self . io_loop . add_callback ( self . stop ) <EOL> thread = threading . Thread ( target = target ) <EOL> self . io_loop . add_callback ( thread . start ) <EOL> self . wait ( ) <EOL> self . assertAlmostEqual ( time . time ( ) , self . stop_time , places = <NUM_LIT:2> ) <EOL> thread . join ( ) <EOL> def test_add_timeout_timedelta ( self ) : <EOL> self . io_loop . add_timeout ( datetime . timedelta ( microseconds = <NUM_LIT:1> ) , self . stop ) <EOL> self . wait ( ) <EOL> def test_multiple_add ( self ) : <EOL> sock , port = bind_unused_port ( ) <EOL> try : <EOL> self . io_loop . add_handler ( sock . fileno ( ) , lambda fd , events : None , <EOL> IOLoop . READ ) <EOL> self . assertRaises ( Exception , self . io_loop . add_handler , <EOL> sock . fileno ( ) , lambda fd , events : None , <EOL> IOLoop . READ ) <EOL> finally : <EOL> self . io_loop . remove_handler ( sock . fileno ( ) ) <EOL> sock . close ( ) <EOL> def test_remove_without_add ( self ) : <EOL> sock , port = bind_unused_port ( ) <EOL> try : <EOL> self . io_loop . remove_handler ( sock . fileno ( ) ) <EOL> finally : <EOL> sock . close ( ) <EOL> def test_add_callback_from_signal ( self ) : <EOL> self . io_loop . add_callback_from_signal ( self . stop ) <EOL> self . wait ( ) <EOL> def test_add_callback_from_signal_other_thread ( self ) : <EOL> other_ioloop = IOLoop ( ) <EOL> thread = threading . Thread ( target = other_ioloop . start ) <EOL> thread . start ( ) <EOL> other_ioloop . add_callback_from_signal ( other_ioloop . stop ) <EOL> thread . join ( ) <EOL> other_ioloop . close ( ) <EOL> def test_add_callback_while_closing ( self ) : <EOL> closing = threading . Event ( ) <EOL> def target ( ) : <EOL> other_ioloop . add_callback ( other_ioloop . stop ) <EOL> other_ioloop . start ( ) <EOL> closing . set ( ) <EOL> other_ioloop . close ( all_fds = True ) <EOL> other_ioloop = IOLoop ( ) <EOL> thread = threading . Thread ( target = target ) <EOL> thread . start ( ) <EOL> closing . wait ( ) <EOL> for i in range ( <NUM_LIT:1000> ) : <EOL> try : <EOL> other_ioloop . add_callback ( lambda : None ) <EOL> except RuntimeError as e : <EOL> self . assertEqual ( "<STR_LIT>" , str ( e ) ) <EOL> break <EOL> def test_handle_callback_exception ( self ) : <EOL> def handle_callback_exception ( callback ) : <EOL> self . assertIs ( sys . exc_info ( ) [ <NUM_LIT:0> ] , ZeroDivisionError ) <EOL> self . stop ( ) <EOL> self . io_loop . handle_callback_exception = handle_callback_exception <EOL> with NullContext ( ) : <EOL> self . io_loop . add_callback ( lambda : <NUM_LIT:1> / <NUM_LIT:0> ) <EOL> self . wait ( ) <EOL> @ skipIfNonUnix <EOL> def test_read_while_writeable ( self ) : <EOL> client , server = socket . socketpair ( ) <EOL> try : <EOL> def handler ( fd , events ) : <EOL> self . assertEqual ( events , IOLoop . READ ) <EOL> self . stop ( ) <EOL> self . io_loop . add_handler ( client . fileno ( ) , handler , IOLoop . READ ) <EOL> self . io_loop . add_timeout ( self . io_loop . time ( ) + <NUM_LIT> , <EOL> functools . partial ( server . send , b'<STR_LIT>' ) ) <EOL> self . wait ( ) <EOL> self . io_loop . remove_handler ( client . fileno ( ) ) <EOL> finally : <EOL> client . close ( ) <EOL> server . close ( ) <EOL> def test_remove_timeout_after_fire ( self ) : <EOL> handle = self . io_loop . add_timeout ( self . io_loop . time ( ) , self . stop ( ) ) <EOL> self . wait ( ) <EOL> self . io_loop . remove_timeout ( handle ) <EOL> def test_remove_timeout_cleanup ( self ) : <EOL> for i in range ( <NUM_LIT> ) : <EOL> timeout = self . io_loop . add_timeout ( self . io_loop . time ( ) + <NUM_LIT> , <EOL> lambda : None ) <EOL> self . io_loop . remove_timeout ( timeout ) <EOL> self . io_loop . add_callback ( lambda : self . io_loop . add_callback ( self . stop ) ) <EOL> self . wait ( ) <EOL> class TestIOLoopCurrent ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . io_loop = IOLoop ( ) <EOL> def tearDown ( self ) : <EOL> self . io_loop . close ( ) <EOL> def test_current ( self ) : <EOL> def f ( ) : <EOL> self . current_io_loop = IOLoop . current ( ) <EOL> self . io_loop . stop ( ) <EOL> self . io_loop . add_callback ( f ) <EOL> self . io_loop . start ( ) <EOL> self . assertIs ( self . current_io_loop , self . io_loop ) <EOL> class TestIOLoopAddCallback ( AsyncTestCase ) : <EOL> def setUp ( self ) : <EOL> super ( TestIOLoopAddCallback , self ) . setUp ( ) <EOL> self . active_contexts = [ ] <EOL> def add_callback ( self , callback , * args , ** kwargs ) : <EOL> self . io_loop . add_callback ( callback , * args , ** kwargs ) <EOL> @ contextlib . contextmanager <EOL> def context ( self , name ) : <EOL> self . active_contexts . append ( name ) <EOL> yield <EOL> self . assertEqual ( self . active_contexts . pop ( ) , name ) <EOL> def test_pre_wrap ( self ) : <EOL> def f1 ( ) : <EOL> self . assertIn ( '<STR_LIT>' , self . active_contexts ) <EOL> self . assertNotIn ( '<STR_LIT>' , self . active_contexts ) <EOL> self . stop ( ) <EOL> with StackContext ( functools . partial ( self . context , '<STR_LIT>' ) ) : <EOL> wrapped = wrap ( f1 ) <EOL> with StackContext ( functools . partial ( self . context , '<STR_LIT>' ) ) : <EOL> self . add_callback ( wrapped ) <EOL> self . wait ( ) <EOL> def test_pre_wrap_with_args ( self ) : <EOL> def f1 ( foo , bar ) : <EOL> self . assertIn ( '<STR_LIT>' , self . active_contexts ) <EOL> self . assertNotIn ( '<STR_LIT>' , self . active_contexts ) <EOL> self . stop ( ( foo , bar ) ) <EOL> with StackContext ( functools . partial ( self . context , '<STR_LIT>' ) ) : <EOL> wrapped = wrap ( f1 ) <EOL> with StackContext ( functools . partial ( self . context , '<STR_LIT>' ) ) : <EOL> self . add_callback ( wrapped , <NUM_LIT:1> , bar = <NUM_LIT:2> ) <EOL> result = self . wait ( ) <EOL> self . assertEqual ( result , ( <NUM_LIT:1> , <NUM_LIT:2> ) ) <EOL> class TestIOLoopAddCallbackFromSignal ( TestIOLoopAddCallback ) : <EOL> def add_callback ( self , callback , * args , ** kwargs ) : <EOL> self . io_loop . add_callback_from_signal ( callback , * args , ** kwargs ) <EOL> @ unittest . skipIf ( futures is None , "<STR_LIT>" ) <EOL> class TestIOLoopFutures ( AsyncTestCase ) : <EOL> def test_add_future_threads ( self ) : <EOL> with futures . ThreadPoolExecutor ( <NUM_LIT:1> ) as pool : <EOL> self . io_loop . add_future ( pool . submit ( lambda : None ) , <EOL> lambda future : self . stop ( future ) ) <EOL> future = self . wait ( ) <EOL> self . assertTrue ( future . done ( ) ) <EOL> self . assertTrue ( future . result ( ) is None ) <EOL> def test_add_future_stack_context ( self ) : <EOL> ready = threading . Event ( ) <EOL> def task ( ) : <EOL> ready . wait ( <NUM_LIT:1> ) <EOL> assert ready . isSet ( ) , "<STR_LIT>" <EOL> raise Exception ( "<STR_LIT>" ) <EOL> def callback ( future ) : <EOL> self . future = future <EOL> raise Exception ( "<STR_LIT>" ) <EOL> def handle_exception ( typ , value , traceback ) : <EOL> self . exception = value <EOL> self . stop ( ) <EOL> return True <EOL> with futures . ThreadPoolExecutor ( <NUM_LIT:1> ) as pool : <EOL> with ExceptionStackContext ( handle_exception ) : <EOL> self . io_loop . add_future ( pool . submit ( task ) , callback ) <EOL> ready . set ( ) <EOL> self . wait ( ) <EOL> self . assertEqual ( self . exception . args [ <NUM_LIT:0> ] , "<STR_LIT>" ) <EOL> self . assertEqual ( self . future . exception ( ) . args [ <NUM_LIT:0> ] , "<STR_LIT>" ) <EOL> class TestIOLoopRunSync ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . io_loop = IOLoop ( ) <EOL> def tearDown ( self ) : <EOL> self . io_loop . close ( ) <EOL> def test_sync_result ( self ) : <EOL> self . assertEqual ( self . io_loop . run_sync ( lambda : <NUM_LIT> ) , <NUM_LIT> ) <EOL> def test_sync_exception ( self ) : <EOL> with self . assertRaises ( ZeroDivisionError ) : <EOL> self . io_loop . run_sync ( lambda : <NUM_LIT:1> / <NUM_LIT:0> ) <EOL> def test_async_result ( self ) : <EOL> @ gen . coroutine <EOL> def f ( ) : <EOL> yield gen . Task ( self . io_loop . add_callback ) <EOL> raise gen . Return ( <NUM_LIT> ) <EOL> self . assertEqual ( self . io_loop . run_sync ( f ) , <NUM_LIT> ) <EOL> def test_async_exception ( self ) : <EOL> @ gen . coroutine <EOL> def f ( ) : <EOL> yield gen . Task ( self . io_loop . add_callback ) <EOL> <NUM_LIT:1> / <NUM_LIT:0> <EOL> with self . assertRaises ( ZeroDivisionError ) : <EOL> self . io_loop . run_sync ( f ) <EOL> def test_current ( self ) : <EOL> def f ( ) : <EOL> self . assertIs ( IOLoop . current ( ) , self . io_loop ) <EOL> self . io_loop . run_sync ( f ) <EOL> def test_timeout ( self ) : <EOL> @ gen . coroutine <EOL> def f ( ) : <EOL> yield gen . Task ( self . io_loop . add_timeout , self . io_loop . time ( ) + <NUM_LIT:1> ) <EOL> self . assertRaises ( TimeoutError , self . io_loop . run_sync , f , timeout = <NUM_LIT> ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> unittest . main ( ) </s>
