<s> import sys <EOL> import traceback <EOL> import unittest <EOL> from threading import Thread , Event <EOL> from hazelcast . future import Future , ImmediateFuture , combine_futures , make_blocking <EOL> class FutureTest ( unittest . TestCase ) : <EOL> def test_set_result ( self ) : <EOL> f = Future ( ) <EOL> def set_result ( ) : <EOL> f . set_result ( "<STR_LIT>" ) <EOL> Thread ( target = set_result ) . start ( ) <EOL> self . assertEqual ( f . result ( ) , "<STR_LIT>" ) <EOL> def test_set_exception ( self ) : <EOL> f = Future ( ) <EOL> exc = [ ] <EOL> def set_exception ( ) : <EOL> try : <EOL> { } [ "<STR_LIT>" ] <EOL> except KeyError as e : <EOL> exc . append ( sys . exc_info ( ) ) <EOL> f . set_exception ( e , sys . exc_info ( ) [ <NUM_LIT:2> ] ) <EOL> Thread ( target = set_exception ) . start ( ) <EOL> exception = f . exception ( ) <EOL> traceback = f . traceback ( ) <EOL> exc = exc [ <NUM_LIT:0> ] <EOL> self . assertEqual ( exc [ <NUM_LIT:1> ] , exception ) <EOL> self . assertEqual ( exc [ <NUM_LIT:2> ] , traceback ) <EOL> def test_result_raises_exception_with_traceback ( self ) : <EOL> f = Future ( ) <EOL> exc_info = None <EOL> try : <EOL> { } [ "<STR_LIT>" ] <EOL> except KeyError as e : <EOL> exc_info = sys . exc_info ( ) <EOL> f . set_exception ( e , sys . exc_info ( ) [ <NUM_LIT:2> ] ) <EOL> try : <EOL> f . result ( ) <EOL> self . fail ( "<STR_LIT>" ) <EOL> except : <EOL> info = sys . exc_info ( ) <EOL> self . assertEqual ( info [ <NUM_LIT:1> ] , exc_info [ <NUM_LIT:1> ] ) <EOL> original_tb = traceback . extract_tb ( exc_info [ <NUM_LIT:2> ] ) <EOL> actual_tb = traceback . extract_tb ( info [ <NUM_LIT:2> ] ) [ <NUM_LIT:1> : ] <EOL> self . assertEqual ( original_tb , actual_tb ) <EOL> def test_add_callback_with_success ( self ) : <EOL> f = Future ( ) <EOL> e = Event ( ) <EOL> def set_result ( ) : <EOL> f . set_result ( "<STR_LIT>" ) <EOL> def callback ( future ) : <EOL> self . assertEqual ( future . result ( ) , "<STR_LIT>" ) <EOL> e . set ( ) <EOL> f . add_done_callback ( callback ) <EOL> Thread ( target = set_result ) . start ( ) <EOL> self . assertTrue ( e . wait ( timeout = <NUM_LIT:5> ) ) <EOL> def test_add_callback_with_failure ( self ) : <EOL> f = Future ( ) <EOL> e = Event ( ) <EOL> exc = [ ] <EOL> def set_exception ( ) : <EOL> map = { } <EOL> try : <EOL> a = map [ "<STR_LIT>" ] <EOL> except KeyError as e : <EOL> exc . append ( sys . exc_info ( ) ) <EOL> f . set_exception ( e , sys . exc_info ( ) [ <NUM_LIT:2> ] ) <EOL> def callback ( future ) : <EOL> exc_info = exc [ <NUM_LIT:0> ] <EOL> self . assertEqual ( exc_info [ <NUM_LIT:1> ] , future . exception ( ) ) <EOL> self . assertEqual ( exc_info [ <NUM_LIT:2> ] , future . traceback ( ) ) <EOL> e . set ( ) <EOL> f . add_done_callback ( callback ) <EOL> Thread ( target = set_exception ) . start ( ) <EOL> self . assertTrue ( e . wait ( timeout = <NUM_LIT:5> ) ) <EOL> def test_add_callback_after_completion_with_success ( self ) : <EOL> f = Future ( ) <EOL> f . set_result ( "<STR_LIT>" ) <EOL> counter = [ <NUM_LIT:0> ] <EOL> def callback ( future ) : <EOL> counter [ <NUM_LIT:0> ] += <NUM_LIT:1> <EOL> self . assertEqual ( future . result ( ) , "<STR_LIT>" ) <EOL> f . add_done_callback ( callback ) <EOL> self . assertEqual ( counter [ <NUM_LIT:0> ] , <NUM_LIT:1> ) <EOL> def test_add_callback_after_completion_with_error ( self ) : <EOL> f = Future ( ) <EOL> error = RuntimeError ( "<STR_LIT:error>" ) <EOL> f . set_exception ( error , None ) <EOL> counter = [ <NUM_LIT:0> ] <EOL> def callback ( future ) : <EOL> counter [ <NUM_LIT:0> ] += <NUM_LIT:1> <EOL> self . assertEqual ( future . exception ( ) , error ) <EOL> f . add_done_callback ( callback ) <EOL> self . assertEqual ( counter [ <NUM_LIT:0> ] , <NUM_LIT:1> ) <EOL> def test_get_result_from_reactor_thread ( self ) : <EOL> f = Future ( ) <EOL> e = Event ( ) <EOL> def get_result ( ) : <EOL> f . _threading_locals . is_reactor_thread = True <EOL> try : <EOL> f . result ( ) <EOL> except RuntimeError : <EOL> e . set ( ) <EOL> Thread ( target = get_result ) . start ( ) <EOL> self . assertTrue ( e . wait ( <NUM_LIT:5> ) , "<STR_LIT>" ) <EOL> def test_get_exception_from_reactor_thread ( self ) : <EOL> f = Future ( ) <EOL> e = Event ( ) <EOL> def get_result ( ) : <EOL> f . _threading_locals . is_reactor_thread = True <EOL> try : <EOL> f . exception ( ) <EOL> except RuntimeError : <EOL> e . set ( ) <EOL> Thread ( target = get_result ) . start ( ) <EOL> self . assertTrue ( e . wait ( <NUM_LIT:5> ) , "<STR_LIT>" ) <EOL> def test_continue_with_on_success ( self ) : <EOL> f = Future ( ) <EOL> f . set_result ( <NUM_LIT:1> ) <EOL> def continuation ( future ) : <EOL> return future . result ( ) + <NUM_LIT:1> <EOL> result = f . continue_with ( continuation ) . result ( ) <EOL> self . assertEqual ( <NUM_LIT:2> , result ) <EOL> def test_continue_with_on_failure ( self ) : <EOL> f = Future ( ) <EOL> f . set_exception ( RuntimeError ( "<STR_LIT:error>" ) ) <EOL> def continuation ( future ) : <EOL> if future . is_success ( ) : <EOL> return <NUM_LIT:0> <EOL> else : <EOL> return <NUM_LIT:1> <EOL> result = f . continue_with ( continuation ) . result ( ) <EOL> self . assertEqual ( <NUM_LIT:1> , result ) <EOL> def test_callback_called_exactly_once ( self ) : <EOL> for _ in xrange ( <NUM_LIT:0> , <NUM_LIT> ) : <EOL> f = Future ( ) <EOL> def set_result ( ) : <EOL> f . set_result ( "<STR_LIT>" ) <EOL> t = Thread ( target = set_result ) <EOL> t . start ( ) <EOL> i = [ <NUM_LIT:0> ] <EOL> def callback ( c ) : <EOL> i [ <NUM_LIT:0> ] += <NUM_LIT:1> <EOL> f . add_done_callback ( callback ) <EOL> t . join ( ) <EOL> self . assertEqual ( i [ <NUM_LIT:0> ] , <NUM_LIT:1> ) <EOL> def test_callback_called_exactly_once_when_exception ( self ) : <EOL> for _ in xrange ( <NUM_LIT:0> , <NUM_LIT> ) : <EOL> f = Future ( ) <EOL> def set_exception ( ) : <EOL> f . set_exception ( RuntimeError ( "<STR_LIT:error>" ) ) <EOL> t = Thread ( target = set_exception ) <EOL> t . start ( ) <EOL> i = [ <NUM_LIT:0> ] <EOL> def callback ( c ) : <EOL> i [ <NUM_LIT:0> ] += <NUM_LIT:1> <EOL> f . add_done_callback ( callback ) <EOL> t . join ( ) <EOL> self . assertEqual ( i [ <NUM_LIT:0> ] , <NUM_LIT:1> ) <EOL> def test_done ( self ) : <EOL> f = Future ( ) <EOL> self . assertFalse ( f . done ( ) ) <EOL> f . set_result ( "<STR_LIT>" ) <EOL> self . assertTrue ( f . done ( ) ) <EOL> def test_running ( self ) : <EOL> f = Future ( ) <EOL> self . assertTrue ( f . running ( ) ) <EOL> f . set_result ( "<STR_LIT>" ) <EOL> self . assertFalse ( f . running ( ) ) <EOL> def test_set_exception_with_non_exception ( self ) : <EOL> f = Future ( ) <EOL> with self . assertRaises ( RuntimeError ) : <EOL> f . set_exception ( "<STR_LIT>" ) <EOL> def test_callback_throws_exception ( self ) : <EOL> f = Future ( ) <EOL> def invalid_func ( ) : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> f . add_done_callback ( invalid_func ) <EOL> f . set_result ( "<STR_LIT>" ) <EOL> def test_continue_with_throws_exception ( self ) : <EOL> f = Future ( ) <EOL> e = RuntimeError ( "<STR_LIT:error>" ) <EOL> def continue_func ( f ) : <EOL> raise e <EOL> n = f . continue_with ( continue_func ) <EOL> f . set_result ( "<STR_LIT>" ) <EOL> self . assertFalse ( n . is_success ( ) ) <EOL> self . assertEqual ( n . exception ( ) , e ) <EOL> class ImmediateFutureTest ( unittest . TestCase ) : <EOL> f = None <EOL> def setUp ( self ) : <EOL> self . f = ImmediateFuture ( "<STR_LIT>" ) <EOL> def test_result ( self ) : <EOL> self . assertEqual ( "<STR_LIT>" , self . f . result ( ) ) <EOL> def test_exception ( self ) : <EOL> self . assertIsNone ( self . f . exception ( ) ) <EOL> def test_set_result ( self ) : <EOL> with self . assertRaises ( NotImplementedError ) : <EOL> self . f . set_result ( "<STR_LIT>" ) <EOL> def test_set_exception ( self ) : <EOL> with self . assertRaises ( NotImplementedError ) : <EOL> self . f . set_exception ( RuntimeError ( ) ) <EOL> def test_is_succcess ( self ) : <EOL> self . assertTrue ( self . f . is_success ( ) ) <EOL> def test_is_done ( self ) : <EOL> self . assertTrue ( self . f . done ( ) ) <EOL> def test_is_not_running ( self ) : <EOL> self . assertFalse ( self . f . running ( ) ) <EOL> def test_callback ( self ) : <EOL> n = [ <NUM_LIT:0> ] <EOL> def callback ( f ) : <EOL> n [ <NUM_LIT:0> ] += <NUM_LIT:1> <EOL> self . f . add_done_callback ( callback ) <EOL> self . assertEqual ( n [ <NUM_LIT:0> ] , <NUM_LIT:1> ) <EOL> class CombineFutureTest ( unittest . TestCase ) : <EOL> def test_combine_futures ( self ) : <EOL> f1 , f2 , f3 = Future ( ) , Future ( ) , Future ( ) <EOL> combined = combine_futures ( f1 , f2 , f3 ) <EOL> f1 . set_result ( "<STR_LIT>" ) <EOL> self . assertFalse ( combined . done ( ) ) <EOL> f2 . set_result ( "<STR_LIT>" ) <EOL> self . assertFalse ( combined . done ( ) ) <EOL> f3 . set_result ( "<STR_LIT>" ) <EOL> self . assertEqual ( combined . result ( ) , [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> def test_combine_futures_exception ( self ) : <EOL> f1 , f2 , f3 = Future ( ) , Future ( ) , Future ( ) <EOL> combined = combine_futures ( f1 , f2 , f3 ) <EOL> e = RuntimeError ( "<STR_LIT:error>" ) <EOL> f1 . set_result ( "<STR_LIT>" ) <EOL> f2 . set_result ( "<STR_LIT>" ) <EOL> f3 . set_exception ( e ) <EOL> self . assertEqual ( e , combined . exception ( ) ) <EOL> class MakeBlockingTest ( unittest . TestCase ) : <EOL> class Calculator ( object ) : <EOL> def __init__ ( self ) : <EOL> self . name = "<STR_LIT>" <EOL> def add_one ( self , x ) : <EOL> f = Future ( ) <EOL> f . set_result ( x + <NUM_LIT:1> ) <EOL> return f <EOL> def multiply ( self , x , y ) : <EOL> f = Future ( ) <EOL> f . set_result ( x * y ) <EOL> return f <EOL> def multiply_sync ( self , x , y ) : <EOL> return x * y <EOL> def setUp ( self ) : <EOL> self . calculator = make_blocking ( MakeBlockingTest . Calculator ( ) ) <EOL> def test_args ( self ) : <EOL> self . assertEqual ( self . calculator . add_one ( <NUM_LIT:1> ) , <NUM_LIT:2> ) <EOL> def test_kwargs ( self ) : <EOL> self . assertEqual ( self . calculator . multiply ( x = <NUM_LIT:4> , y = <NUM_LIT:5> ) , <NUM_LIT:20> ) <EOL> def test_blocking_method ( self ) : <EOL> self . assertEqual ( self . calculator . multiply_sync ( x = <NUM_LIT:4> , y = <NUM_LIT:5> ) , <NUM_LIT:20> ) <EOL> def test_missing_method ( self ) : <EOL> with self . assertRaises ( AttributeError ) : <EOL> self . calculator . missing_method ( ) <EOL> def test_attribute ( self ) : <EOL> self . assertEqual ( self . calculator . name , "<STR_LIT>" ) </s>
