<s> from __future__ import absolute_import , division , print_function , with_statement <EOL> import logging <EOL> import re <EOL> import socket <EOL> import sys <EOL> import traceback <EOL> from tornado . concurrent import Future , return_future , ReturnValueIgnoredError <EOL> from tornado . escape import utf8 , to_unicode <EOL> from tornado import gen <EOL> from tornado . iostream import IOStream <EOL> from tornado import stack_context <EOL> from tornado . tcpserver import TCPServer <EOL> from tornado . testing import AsyncTestCase , LogTrapTestCase , bind_unused_port , gen_test <EOL> class ReturnFutureTest ( AsyncTestCase ) : <EOL> @ return_future <EOL> def sync_future ( self , callback ) : <EOL> callback ( <NUM_LIT> ) <EOL> @ return_future <EOL> def async_future ( self , callback ) : <EOL> self . io_loop . add_callback ( callback , <NUM_LIT> ) <EOL> @ return_future <EOL> def immediate_failure ( self , callback ) : <EOL> <NUM_LIT:1> / <NUM_LIT:0> <EOL> @ return_future <EOL> def delayed_failure ( self , callback ) : <EOL> self . io_loop . add_callback ( lambda : <NUM_LIT:1> / <NUM_LIT:0> ) <EOL> @ return_future <EOL> def return_value ( self , callback ) : <EOL> return <NUM_LIT> <EOL> @ return_future <EOL> def no_result_future ( self , callback ) : <EOL> callback ( ) <EOL> def test_immediate_failure ( self ) : <EOL> with self . assertRaises ( ZeroDivisionError ) : <EOL> self . immediate_failure ( callback = self . stop ) <EOL> self . io_loop . add_timeout ( self . io_loop . time ( ) + <NUM_LIT> , self . stop ) <EOL> result = self . wait ( ) <EOL> self . assertIs ( result , None ) <EOL> def test_return_value ( self ) : <EOL> with self . assertRaises ( ReturnValueIgnoredError ) : <EOL> self . return_value ( callback = self . stop ) <EOL> def test_callback_kw ( self ) : <EOL> future = self . sync_future ( callback = self . stop ) <EOL> result = self . wait ( ) <EOL> self . assertEqual ( result , <NUM_LIT> ) <EOL> self . assertEqual ( future . result ( ) , <NUM_LIT> ) <EOL> def test_callback_positional ( self ) : <EOL> future = self . sync_future ( self . stop ) <EOL> result = self . wait ( ) <EOL> self . assertEqual ( result , <NUM_LIT> ) <EOL> self . assertEqual ( future . result ( ) , <NUM_LIT> ) <EOL> def test_no_callback ( self ) : <EOL> future = self . sync_future ( ) <EOL> self . assertEqual ( future . result ( ) , <NUM_LIT> ) <EOL> def test_none_callback_kw ( self ) : <EOL> future = self . sync_future ( callback = None ) <EOL> self . assertEqual ( future . result ( ) , <NUM_LIT> ) <EOL> def test_none_callback_pos ( self ) : <EOL> future = self . sync_future ( None ) <EOL> self . assertEqual ( future . result ( ) , <NUM_LIT> ) <EOL> def test_async_future ( self ) : <EOL> future = self . async_future ( ) <EOL> self . assertFalse ( future . done ( ) ) <EOL> self . io_loop . add_future ( future , self . stop ) <EOL> future2 = self . wait ( ) <EOL> self . assertIs ( future , future2 ) <EOL> self . assertEqual ( future . result ( ) , <NUM_LIT> ) <EOL> @ gen_test <EOL> def test_async_future_gen ( self ) : <EOL> result = yield self . async_future ( ) <EOL> self . assertEqual ( result , <NUM_LIT> ) <EOL> def test_delayed_failure ( self ) : <EOL> future = self . delayed_failure ( ) <EOL> self . io_loop . add_future ( future , self . stop ) <EOL> future2 = self . wait ( ) <EOL> self . assertIs ( future , future2 ) <EOL> with self . assertRaises ( ZeroDivisionError ) : <EOL> future . result ( ) <EOL> def test_kw_only_callback ( self ) : <EOL> @ return_future <EOL> def f ( ** kwargs ) : <EOL> kwargs [ '<STR_LIT>' ] ( <NUM_LIT> ) <EOL> future = f ( ) <EOL> self . assertEqual ( future . result ( ) , <NUM_LIT> ) <EOL> def test_error_in_callback ( self ) : <EOL> self . sync_future ( callback = lambda future : <NUM_LIT:1> / <NUM_LIT:0> ) <EOL> self . assertRaises ( ZeroDivisionError , self . wait ) <EOL> def test_no_result_future ( self ) : <EOL> future = self . no_result_future ( self . stop ) <EOL> result = self . wait ( ) <EOL> self . assertIs ( result , None ) <EOL> future . result ( ) <EOL> def test_no_result_future_callback ( self ) : <EOL> future = self . no_result_future ( callback = lambda : self . stop ( ) ) <EOL> result = self . wait ( ) <EOL> self . assertIs ( result , None ) <EOL> future . result ( ) <EOL> @ gen_test <EOL> def test_future_traceback ( self ) : <EOL> @ return_future <EOL> @ gen . engine <EOL> def f ( callback ) : <EOL> yield gen . Task ( self . io_loop . add_callback ) <EOL> try : <EOL> <NUM_LIT:1> / <NUM_LIT:0> <EOL> except ZeroDivisionError : <EOL> self . expected_frame = traceback . extract_tb ( <EOL> sys . exc_info ( ) [ <NUM_LIT:2> ] , limit = <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> raise <EOL> try : <EOL> yield f ( ) <EOL> self . fail ( "<STR_LIT>" ) <EOL> except ZeroDivisionError : <EOL> tb = traceback . extract_tb ( sys . exc_info ( ) [ <NUM_LIT:2> ] ) <EOL> self . assertIn ( self . expected_frame , tb ) <EOL> class CapServer ( TCPServer ) : <EOL> def handle_stream ( self , stream , address ) : <EOL> logging . info ( "<STR_LIT>" ) <EOL> self . stream = stream <EOL> self . stream . read_until ( b"<STR_LIT:\n>" , self . handle_read ) <EOL> def handle_read ( self , data ) : <EOL> logging . info ( "<STR_LIT>" ) <EOL> data = to_unicode ( data ) <EOL> if data == data . upper ( ) : <EOL> self . stream . write ( b"<STR_LIT>" ) <EOL> else : <EOL> self . stream . write ( utf8 ( "<STR_LIT>" % data . upper ( ) ) ) <EOL> self . stream . close ( ) <EOL> class CapError ( Exception ) : <EOL> pass <EOL> class BaseCapClient ( object ) : <EOL> def __init__ ( self , port , io_loop ) : <EOL> self . port = port <EOL> self . io_loop = io_loop <EOL> def process_response ( self , data ) : <EOL> status , message = re . match ( '<STR_LIT>' , to_unicode ( data ) ) . groups ( ) <EOL> if status == '<STR_LIT>' : <EOL> return message <EOL> else : <EOL> raise CapError ( message ) <EOL> class ManualCapClient ( BaseCapClient ) : <EOL> def capitalize ( self , request_data , callback = None ) : <EOL> logging . info ( "<STR_LIT>" ) <EOL> self . request_data = request_data <EOL> self . stream = IOStream ( socket . socket ( ) , io_loop = self . io_loop ) <EOL> self . stream . connect ( ( '<STR_LIT:127.0.0.1>' , self . port ) , <EOL> callback = self . handle_connect ) <EOL> self . future = Future ( ) <EOL> if callback is not None : <EOL> self . future . add_done_callback ( <EOL> stack_context . wrap ( lambda future : callback ( future . result ( ) ) ) ) <EOL> return self . future <EOL> def handle_connect ( self ) : <EOL> logging . info ( "<STR_LIT>" ) <EOL> self . stream . write ( utf8 ( self . request_data + "<STR_LIT:\n>" ) ) <EOL> self . stream . read_until ( b'<STR_LIT:\n>' , callback = self . handle_read ) <EOL> def handle_read ( self , data ) : <EOL> logging . info ( "<STR_LIT>" ) <EOL> self . stream . close ( ) <EOL> try : <EOL> self . future . set_result ( self . process_response ( data ) ) <EOL> except CapError as e : <EOL> self . future . set_exception ( e ) <EOL> class DecoratorCapClient ( BaseCapClient ) : <EOL> @ return_future <EOL> def capitalize ( self , request_data , callback ) : <EOL> logging . info ( "<STR_LIT>" ) <EOL> self . request_data = request_data <EOL> self . stream = IOStream ( socket . socket ( ) , io_loop = self . io_loop ) <EOL> self . stream . connect ( ( '<STR_LIT:127.0.0.1>' , self . port ) , <EOL> callback = self . handle_connect ) <EOL> self . callback = callback <EOL> def handle_connect ( self ) : <EOL> logging . info ( "<STR_LIT>" ) <EOL> self . stream . write ( utf8 ( self . request_data + "<STR_LIT:\n>" ) ) <EOL> self . stream . read_until ( b'<STR_LIT:\n>' , callback = self . handle_read ) <EOL> def handle_read ( self , data ) : <EOL> logging . info ( "<STR_LIT>" ) <EOL> self . stream . close ( ) <EOL> self . callback ( self . process_response ( data ) ) <EOL> class GeneratorCapClient ( BaseCapClient ) : <EOL> @ return_future <EOL> @ gen . engine <EOL> def capitalize ( self , request_data , callback ) : <EOL> logging . info ( '<STR_LIT>' ) <EOL> stream = IOStream ( socket . socket ( ) , io_loop = self . io_loop ) <EOL> logging . info ( '<STR_LIT>' ) <EOL> yield gen . Task ( stream . connect , ( '<STR_LIT:127.0.0.1>' , self . port ) ) <EOL> stream . write ( utf8 ( request_data + '<STR_LIT:\n>' ) ) <EOL> logging . info ( '<STR_LIT>' ) <EOL> data = yield gen . Task ( stream . read_until , b'<STR_LIT:\n>' ) <EOL> logging . info ( '<STR_LIT>' ) <EOL> stream . close ( ) <EOL> callback ( self . process_response ( data ) ) <EOL> class ClientTestMixin ( object ) : <EOL> def setUp ( self ) : <EOL> super ( ClientTestMixin , self ) . setUp ( ) <EOL> self . server = CapServer ( io_loop = self . io_loop ) <EOL> sock , port = bind_unused_port ( ) <EOL> self . server . add_sockets ( [ sock ] ) <EOL> self . client = self . client_class ( io_loop = self . io_loop , port = port ) <EOL> def tearDown ( self ) : <EOL> self . server . stop ( ) <EOL> super ( ClientTestMixin , self ) . tearDown ( ) <EOL> def test_callback ( self ) : <EOL> self . client . capitalize ( "<STR_LIT:hello>" , callback = self . stop ) <EOL> result = self . wait ( ) <EOL> self . assertEqual ( result , "<STR_LIT>" ) <EOL> def test_callback_error ( self ) : <EOL> self . client . capitalize ( "<STR_LIT>" , callback = self . stop ) <EOL> self . assertRaisesRegexp ( CapError , "<STR_LIT>" , self . wait ) <EOL> def test_future ( self ) : <EOL> future = self . client . capitalize ( "<STR_LIT:hello>" ) <EOL> self . io_loop . add_future ( future , self . stop ) <EOL> self . wait ( ) <EOL> self . assertEqual ( future . result ( ) , "<STR_LIT>" ) <EOL> def test_future_error ( self ) : <EOL> future = self . client . capitalize ( "<STR_LIT>" ) <EOL> self . io_loop . add_future ( future , self . stop ) <EOL> self . wait ( ) <EOL> self . assertRaisesRegexp ( CapError , "<STR_LIT>" , future . result ) <EOL> def test_generator ( self ) : <EOL> @ gen . engine <EOL> def f ( ) : <EOL> result = yield self . client . capitalize ( "<STR_LIT:hello>" ) <EOL> self . assertEqual ( result , "<STR_LIT>" ) <EOL> self . stop ( ) <EOL> f ( ) <EOL> self . wait ( ) <EOL> def test_generator_error ( self ) : <EOL> @ gen . engine <EOL> def f ( ) : <EOL> with self . assertRaisesRegexp ( CapError , "<STR_LIT>" ) : <EOL> yield self . client . capitalize ( "<STR_LIT>" ) <EOL> self . stop ( ) <EOL> f ( ) <EOL> self . wait ( ) <EOL> class ManualClientTest ( ClientTestMixin , AsyncTestCase , LogTrapTestCase ) : <EOL> client_class = ManualCapClient <EOL> class DecoratorClientTest ( ClientTestMixin , AsyncTestCase , LogTrapTestCase ) : <EOL> client_class = DecoratorCapClient <EOL> class GeneratorClientTest ( ClientTestMixin , AsyncTestCase , LogTrapTestCase ) : <EOL> client_class = GeneratorCapClient </s>
