<s> """<STR_LIT>""" <EOL> import os <EOL> import re <EOL> import random <EOL> import sys <EOL> import time <EOL> import unittest <EOL> from google . appengine . api import apiproxy_stub_map <EOL> from google . appengine . api import datastore_file_stub <EOL> from google . appengine . datastore import datastore_rpc <EOL> from ndb import eventloop <EOL> from ndb import model <EOL> from ndb import test_utils <EOL> from ndb import tasklets <EOL> from ndb . tasklets import Future , tasklet <EOL> class TaskletTests ( test_utils . DatastoreTest ) : <EOL> def setUp ( self ) : <EOL> super ( TaskletTests , self ) . setUp ( ) <EOL> if eventloop . _EVENT_LOOP_KEY in os . environ : <EOL> del os . environ [ eventloop . _EVENT_LOOP_KEY ] <EOL> if tasklets . _CONTEXT_KEY in os . environ : <EOL> del os . environ [ tasklets . _CONTEXT_KEY ] <EOL> self . ev = eventloop . get_event_loop ( ) <EOL> self . log = [ ] <EOL> def universal_callback ( self , * args ) : <EOL> self . log . append ( args ) <EOL> def testFuture_Constructor ( self ) : <EOL> f = tasklets . Future ( ) <EOL> self . assertEqual ( f . _result , None ) <EOL> self . assertEqual ( f . _exception , None ) <EOL> self . assertEqual ( f . _callbacks , [ ] ) <EOL> def testFuture_Repr ( self ) : <EOL> f = tasklets . Future ( ) <EOL> prefix = ( r'<STR_LIT>' <EOL> r'<STR_LIT>' ) <EOL> self . assertTrue ( re . match ( prefix + r'<STR_LIT>' , repr ( f ) ) , repr ( f ) ) <EOL> f . set_result ( '<STR_LIT:abc>' ) <EOL> self . assertTrue ( re . match ( prefix + r'<STR_LIT>' , repr ( f ) ) , repr ( f ) ) <EOL> f = tasklets . Future ( ) <EOL> f . set_exception ( RuntimeError ( '<STR_LIT:abc>' ) ) <EOL> self . assertTrue ( re . match ( prefix + r'<STR_LIT>' , <EOL> repr ( f ) ) , <EOL> repr ( f ) ) <EOL> def testFuture_Done_State ( self ) : <EOL> f = tasklets . Future ( ) <EOL> self . assertFalse ( f . done ( ) ) <EOL> self . assertEqual ( f . state , f . RUNNING ) <EOL> f . set_result ( <NUM_LIT> ) <EOL> self . assertTrue ( f . done ( ) ) <EOL> self . assertEqual ( f . state , f . FINISHING ) <EOL> def testFuture_SetResult ( self ) : <EOL> f = tasklets . Future ( ) <EOL> f . set_result ( <NUM_LIT> ) <EOL> self . assertEqual ( f . _result , <NUM_LIT> ) <EOL> self . assertEqual ( f . _exception , None ) <EOL> self . assertEqual ( f . get_result ( ) , <NUM_LIT> ) <EOL> def testFuture_SetException ( self ) : <EOL> f = tasklets . Future ( ) <EOL> err = RuntimeError ( <NUM_LIT> ) <EOL> f . set_exception ( err ) <EOL> self . assertEqual ( f . done ( ) , True ) <EOL> self . assertEqual ( f . _exception , err ) <EOL> self . assertEqual ( f . _result , None ) <EOL> self . assertEqual ( f . get_exception ( ) , err ) <EOL> self . assertRaises ( RuntimeError , f . get_result ) <EOL> def testFuture_AddDoneCallback_SetResult ( self ) : <EOL> f = tasklets . Future ( ) <EOL> f . add_callback ( self . universal_callback , f ) <EOL> self . assertEqual ( self . log , [ ] ) <EOL> f . set_result ( <NUM_LIT> ) <EOL> eventloop . run ( ) <EOL> self . assertEqual ( self . log , [ ( f , ) ] ) <EOL> def testFuture_SetResult_AddDoneCallback ( self ) : <EOL> f = tasklets . Future ( ) <EOL> f . set_result ( <NUM_LIT> ) <EOL> self . assertEqual ( f . get_result ( ) , <NUM_LIT> ) <EOL> f . add_callback ( self . universal_callback , f ) <EOL> eventloop . run ( ) <EOL> self . assertEqual ( self . log , [ ( f , ) ] ) <EOL> def testFuture_AddDoneCallback_SetException ( self ) : <EOL> f = tasklets . Future ( ) <EOL> f . add_callback ( self . universal_callback , f ) <EOL> f . set_exception ( RuntimeError ( <NUM_LIT> ) ) <EOL> eventloop . run ( ) <EOL> self . assertEqual ( self . log , [ ( f , ) ] ) <EOL> self . assertEqual ( f . done ( ) , True ) <EOL> def create_futures ( self ) : <EOL> self . futs = [ ] <EOL> for i in range ( <NUM_LIT:5> ) : <EOL> f = tasklets . Future ( ) <EOL> f . add_callback ( self . universal_callback , f ) <EOL> def wake ( fut , result ) : <EOL> fut . set_result ( result ) <EOL> self . ev . queue_call ( i * <NUM_LIT> , wake , f , i ) <EOL> self . futs . append ( f ) <EOL> return set ( self . futs ) <EOL> def testFuture_WaitAny ( self ) : <EOL> self . assertEqual ( tasklets . Future . wait_any ( [ ] ) , None ) <EOL> todo = self . create_futures ( ) <EOL> while todo : <EOL> f = tasklets . Future . wait_any ( todo ) <EOL> todo . remove ( f ) <EOL> eventloop . run ( ) <EOL> self . assertEqual ( self . log , [ ( f , ) for f in self . futs ] ) <EOL> def testFuture_WaitAll ( self ) : <EOL> todo = self . create_futures ( ) <EOL> tasklets . Future . wait_all ( todo ) <EOL> self . assertEqual ( self . log , [ ( f , ) for f in self . futs ] ) <EOL> def testSleep ( self ) : <EOL> log = [ ] <EOL> @ tasklets . tasklet <EOL> def foo ( ) : <EOL> log . append ( time . time ( ) ) <EOL> yield tasklets . sleep ( <NUM_LIT:0.1> ) <EOL> log . append ( time . time ( ) ) <EOL> foo ( ) <EOL> eventloop . run ( ) <EOL> t0 , t1 = log <EOL> dt = t1 - t0 <EOL> self . assertAlmostEqual ( dt , <NUM_LIT:0.1> , places = <NUM_LIT:2> ) <EOL> def testMultiFuture ( self ) : <EOL> @ tasklets . tasklet <EOL> def foo ( dt ) : <EOL> yield tasklets . sleep ( dt ) <EOL> raise tasklets . Return ( '<STR_LIT>' % dt ) <EOL> @ tasklets . tasklet <EOL> def bar ( n ) : <EOL> for i in range ( n ) : <EOL> yield tasklets . sleep ( <NUM_LIT> ) <EOL> raise tasklets . Return ( '<STR_LIT>' % n ) <EOL> bar5 = bar ( <NUM_LIT:5> ) <EOL> futs = [ foo ( <NUM_LIT> ) , foo ( <NUM_LIT> ) , foo ( <NUM_LIT> ) , bar ( <NUM_LIT:3> ) , bar5 , bar5 ] <EOL> mfut = tasklets . MultiFuture ( ) <EOL> for fut in futs : <EOL> mfut . add_dependent ( fut ) <EOL> mfut . complete ( ) <EOL> results = mfut . get_result ( ) <EOL> self . assertEqual ( set ( results ) , <EOL> set ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] ) ) <EOL> def testMultiFuture_PreCompleted ( self ) : <EOL> @ tasklets . tasklet <EOL> def foo ( ) : <EOL> yield tasklets . sleep ( <NUM_LIT> ) <EOL> raise tasklets . Return ( <NUM_LIT> ) <EOL> mfut = tasklets . MultiFuture ( ) <EOL> dep = foo ( ) <EOL> dep . wait ( ) <EOL> mfut . add_dependent ( dep ) <EOL> mfut . complete ( ) <EOL> eventloop . run ( ) <EOL> self . assertTrue ( mfut . done ( ) ) <EOL> self . assertEqual ( mfut . get_result ( ) , [ <NUM_LIT> ] ) <EOL> def testMultiFuture_SetException ( self ) : <EOL> mf = tasklets . MultiFuture ( ) <EOL> f1 = Future ( ) <EOL> f2 = Future ( ) <EOL> f3 = Future ( ) <EOL> f2 . set_result ( <NUM_LIT:2> ) <EOL> mf . putq ( f1 ) <EOL> f1 . set_result ( <NUM_LIT:1> ) <EOL> mf . putq ( f2 ) <EOL> mf . putq ( f3 ) <EOL> mf . putq ( <NUM_LIT:4> ) <EOL> self . ev . run ( ) <EOL> mf . set_exception ( ZeroDivisionError ( ) ) <EOL> f3 . set_result ( <NUM_LIT:3> ) <EOL> self . ev . run ( ) <EOL> self . assertRaises ( ZeroDivisionError , mf . get_result ) <EOL> def testMultiFuture_ItemException ( self ) : <EOL> mf = tasklets . MultiFuture ( ) <EOL> f1 = Future ( ) <EOL> f2 = Future ( ) <EOL> f3 = Future ( ) <EOL> f2 . set_result ( <NUM_LIT:2> ) <EOL> mf . putq ( f1 ) <EOL> f1 . set_exception ( ZeroDivisionError ( ) ) <EOL> mf . putq ( f2 ) <EOL> mf . putq ( f3 ) <EOL> f3 . set_result ( <NUM_LIT:3> ) <EOL> self . ev . run ( ) <EOL> mf . complete ( ) <EOL> self . assertRaises ( ZeroDivisionError , mf . get_result ) <EOL> def testMultiFuture_Repr ( self ) : <EOL> mf = tasklets . MultiFuture ( '<STR_LIT:info>' ) <EOL> r1 = repr ( mf ) <EOL> mf . putq ( <NUM_LIT:1> ) <EOL> r2 = repr ( mf ) <EOL> f2 = Future ( ) <EOL> f2 . set_result ( <NUM_LIT:2> ) <EOL> mf . putq ( <NUM_LIT:2> ) <EOL> r3 = repr ( mf ) <EOL> self . ev . run ( ) <EOL> r4 = repr ( mf ) <EOL> f3 = Future ( ) <EOL> mf . putq ( f3 ) <EOL> r5 = repr ( mf ) <EOL> mf . complete ( ) <EOL> r6 = repr ( mf ) <EOL> f3 . set_result ( <NUM_LIT:3> ) <EOL> self . ev . run ( ) <EOL> r7 = repr ( mf ) <EOL> for r in r1 , r2 , r3 , r4 , r5 , r6 , r7 : <EOL> self . assertTrue ( <EOL> re . match ( <EOL> r'<STR_LIT>' <EOL> r'<STR_LIT>' , <EOL> r ) ) <EOL> if r is r7 : <EOL> self . assertTrue ( '<STR_LIT:result>' in r ) <EOL> else : <EOL> self . assertTrue ( '<STR_LIT>' in r ) <EOL> def testQueueFuture ( self ) : <EOL> q = tasklets . QueueFuture ( ) <EOL> @ tasklets . tasklet <EOL> def produce_one ( i ) : <EOL> yield tasklets . sleep ( i * <NUM_LIT> ) <EOL> raise tasklets . Return ( i ) <EOL> @ tasklets . tasklet <EOL> def producer ( ) : <EOL> q . putq ( <NUM_LIT:0> ) <EOL> for i in range ( <NUM_LIT:1> , <NUM_LIT:10> ) : <EOL> q . add_dependent ( produce_one ( i ) ) <EOL> q . complete ( ) <EOL> @ tasklets . tasklet <EOL> def consumer ( ) : <EOL> for i in range ( <NUM_LIT:10> ) : <EOL> val = yield q . getq ( ) <EOL> self . assertEqual ( val , i ) <EOL> yield q <EOL> self . assertRaises ( EOFError , q . getq ( ) . get_result ) <EOL> @ tasklets . tasklet <EOL> def foo ( ) : <EOL> yield producer ( ) , consumer ( ) <EOL> foo ( ) . get_result ( ) <EOL> def testQueueFuture_Complete ( self ) : <EOL> qf = tasklets . QueueFuture ( ) <EOL> qf . putq ( <NUM_LIT:1> ) <EOL> f2 = Future ( ) <EOL> qf . putq ( f2 ) <EOL> self . ev . run ( ) <EOL> g1 = qf . getq ( ) <EOL> g2 = qf . getq ( ) <EOL> g3 = qf . getq ( ) <EOL> f2 . set_result ( <NUM_LIT:2> ) <EOL> self . ev . run ( ) <EOL> qf . complete ( ) <EOL> self . ev . run ( ) <EOL> self . assertEqual ( g1 . get_result ( ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( g2 . get_result ( ) , <NUM_LIT:2> ) <EOL> self . assertRaises ( EOFError , g3 . get_result ) <EOL> self . assertRaises ( EOFError , qf . getq ( ) . get_result ) <EOL> def testQueueFuture_SetException ( self ) : <EOL> qf = tasklets . QueueFuture ( ) <EOL> f1 = Future ( ) <EOL> f1 . set_result ( <NUM_LIT:1> ) <EOL> qf . putq ( f1 ) <EOL> qf . putq ( f1 ) <EOL> self . ev . run ( ) <EOL> qf . putq ( <NUM_LIT:2> ) <EOL> self . ev . run ( ) <EOL> f3 = Future ( ) <EOL> f3 . set_exception ( ZeroDivisionError ( ) ) <EOL> qf . putq ( f3 ) <EOL> self . ev . run ( ) <EOL> f4 = Future ( ) <EOL> qf . putq ( f4 ) <EOL> self . ev . run ( ) <EOL> qf . set_exception ( KeyError ( ) ) <EOL> f4 . set_result ( <NUM_LIT:4> ) <EOL> self . ev . run ( ) <EOL> self . assertRaises ( KeyError , qf . get_result ) <EOL> self . assertEqual ( qf . getq ( ) . get_result ( ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( qf . getq ( ) . get_result ( ) , <NUM_LIT:2> ) <EOL> self . assertRaises ( ZeroDivisionError , qf . getq ( ) . get_result ) <EOL> self . assertEqual ( qf . getq ( ) . get_result ( ) , <NUM_LIT:4> ) <EOL> self . assertRaises ( KeyError , qf . getq ( ) . get_result ) <EOL> self . assertRaises ( KeyError , qf . getq ( ) . get_result ) <EOL> def testQueueFuture_SetExceptionAlternative ( self ) : <EOL> qf = tasklets . QueueFuture ( ) <EOL> g1 = qf . getq ( ) <EOL> qf . set_exception ( KeyError ( ) ) <EOL> self . ev . run ( ) <EOL> self . assertRaises ( KeyError , g1 . get_result ) <EOL> def testQueueFuture_ItemException ( self ) : <EOL> qf = tasklets . QueueFuture ( ) <EOL> qf . putq ( <NUM_LIT:1> ) <EOL> f2 = Future ( ) <EOL> qf . putq ( f2 ) <EOL> f3 = Future ( ) <EOL> f3 . set_result ( <NUM_LIT:3> ) <EOL> self . ev . run ( ) <EOL> qf . putq ( f3 ) <EOL> self . ev . run ( ) <EOL> f4 = Future ( ) <EOL> f4 . set_exception ( ZeroDivisionError ( ) ) <EOL> self . ev . run ( ) <EOL> qf . putq ( f4 ) <EOL> f5 = Future ( ) <EOL> qf . putq ( f5 ) <EOL> self . ev . run ( ) <EOL> qf . complete ( ) <EOL> self . ev . run ( ) <EOL> f2 . set_result ( <NUM_LIT:2> ) <EOL> self . ev . run ( ) <EOL> f5 . set_exception ( KeyError ( ) ) <EOL> self . ev . run ( ) <EOL> self . assertEqual ( qf . getq ( ) . get_result ( ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( qf . getq ( ) . get_result ( ) , <NUM_LIT:3> ) <EOL> self . assertRaises ( ZeroDivisionError , qf . getq ( ) . get_result ) <EOL> self . assertEqual ( qf . getq ( ) . get_result ( ) , <NUM_LIT:2> ) <EOL> self . assertRaises ( KeyError , qf . getq ( ) . get_result ) <EOL> self . assertRaises ( EOFError , qf . getq ( ) . get_result ) <EOL> self . assertRaises ( EOFError , qf . getq ( ) . get_result ) <EOL> def testSerialQueueFuture ( self ) : <EOL> q = tasklets . SerialQueueFuture ( ) <EOL> @ tasklets . tasklet <EOL> def produce_one ( i ) : <EOL> yield tasklets . sleep ( random . randrange ( <NUM_LIT:10> ) * <NUM_LIT> ) <EOL> raise tasklets . Return ( i ) <EOL> @ tasklets . tasklet <EOL> def producer ( ) : <EOL> for i in range ( <NUM_LIT:10> ) : <EOL> q . add_dependent ( produce_one ( i ) ) <EOL> q . complete ( ) <EOL> @ tasklets . tasklet <EOL> def consumer ( ) : <EOL> for i in range ( <NUM_LIT:10> ) : <EOL> val = yield q . getq ( ) <EOL> self . assertEqual ( val , i ) <EOL> yield q <EOL> self . assertRaises ( EOFError , q . getq ( ) . get_result ) <EOL> yield q <EOL> @ tasklets . synctasklet <EOL> def foo ( ) : <EOL> yield producer ( ) , consumer ( ) <EOL> foo ( ) <EOL> def testSerialQueueFuture_Complete ( self ) : <EOL> sqf = tasklets . SerialQueueFuture ( ) <EOL> g1 = sqf . getq ( ) <EOL> sqf . complete ( ) <EOL> self . assertRaises ( EOFError , g1 . get_result ) <EOL> def testSerialQueueFuture_SetException ( self ) : <EOL> sqf = tasklets . SerialQueueFuture ( ) <EOL> g1 = sqf . getq ( ) <EOL> sqf . set_exception ( KeyError ( ) ) <EOL> self . assertRaises ( KeyError , g1 . get_result ) <EOL> def testSerialQueueFuture_ItemException ( self ) : <EOL> sqf = tasklets . SerialQueueFuture ( ) <EOL> g1 = sqf . getq ( ) <EOL> f1 = Future ( ) <EOL> sqf . putq ( f1 ) <EOL> sqf . complete ( ) <EOL> f1 . set_exception ( ZeroDivisionError ( ) ) <EOL> self . assertRaises ( ZeroDivisionError , g1 . get_result ) <EOL> def testSerialQueueFuture_PutQ_1 ( self ) : <EOL> sqf = tasklets . SerialQueueFuture ( ) <EOL> f1 = Future ( ) <EOL> sqf . putq ( f1 ) <EOL> sqf . complete ( ) <EOL> f1 . set_result ( <NUM_LIT:1> ) <EOL> self . assertEqual ( sqf . getq ( ) . get_result ( ) , <NUM_LIT:1> ) <EOL> def testSerialQueueFuture_PutQ_2 ( self ) : <EOL> sqf = tasklets . SerialQueueFuture ( ) <EOL> sqf . putq ( <NUM_LIT:1> ) <EOL> sqf . complete ( ) <EOL> self . assertEqual ( sqf . getq ( ) . get_result ( ) , <NUM_LIT:1> ) <EOL> def testSerialQueueFuture_PutQ_3 ( self ) : <EOL> sqf = tasklets . SerialQueueFuture ( ) <EOL> g1 = sqf . getq ( ) <EOL> sqf . putq ( <NUM_LIT:1> ) <EOL> sqf . complete ( ) <EOL> self . assertEqual ( g1 . get_result ( ) , <NUM_LIT:1> ) <EOL> def testSerialQueueFuture_PutQ_4 ( self ) : <EOL> sqf = tasklets . SerialQueueFuture ( ) <EOL> g1 = sqf . getq ( ) <EOL> f1 = Future ( ) <EOL> sqf . putq ( f1 ) <EOL> sqf . complete ( ) <EOL> f1 . set_result ( <NUM_LIT:1> ) <EOL> self . assertEqual ( g1 . get_result ( ) , <NUM_LIT:1> ) <EOL> def testSerialQueueFuture_GetQ ( self ) : <EOL> sqf = tasklets . SerialQueueFuture ( ) <EOL> sqf . set_exception ( KeyError ( ) ) <EOL> self . assertRaises ( KeyError , sqf . getq ( ) . get_result ) <EOL> def testReducingFuture ( self ) : <EOL> def reducer ( arg ) : <EOL> return sum ( arg ) <EOL> rf = tasklets . ReducingFuture ( reducer , batch_size = <NUM_LIT:10> ) <EOL> for i in range ( <NUM_LIT:10> ) : <EOL> rf . putq ( i ) <EOL> for i in range ( <NUM_LIT:10> , <NUM_LIT:20> ) : <EOL> f = Future ( ) <EOL> rf . putq ( f ) <EOL> f . set_result ( i ) <EOL> rf . complete ( ) <EOL> self . assertEqual ( rf . get_result ( ) , sum ( range ( <NUM_LIT:20> ) ) ) <EOL> def testReducingFuture_Empty ( self ) : <EOL> def reducer ( arg ) : <EOL> self . fail ( ) <EOL> rf = tasklets . ReducingFuture ( reducer ) <EOL> rf . complete ( ) <EOL> self . assertEqual ( rf . get_result ( ) , None ) <EOL> def testReducingFuture_OneItem ( self ) : <EOL> def reducer ( arg ) : <EOL> self . fail ( ) <EOL> rf = tasklets . ReducingFuture ( reducer ) <EOL> rf . putq ( <NUM_LIT:1> ) <EOL> rf . complete ( ) <EOL> self . assertEqual ( rf . get_result ( ) , <NUM_LIT:1> ) <EOL> def testReducingFuture_ItemException ( self ) : <EOL> def reducer ( arg ) : <EOL> return sum ( arg ) <EOL> rf = tasklets . ReducingFuture ( reducer ) <EOL> f1 = Future ( ) <EOL> f1 . set_exception ( ZeroDivisionError ( ) ) <EOL> rf . putq ( f1 ) <EOL> rf . complete ( ) <EOL> self . assertRaises ( ZeroDivisionError , rf . get_result ) <EOL> def testReducingFuture_ReducerException_1 ( self ) : <EOL> def reducer ( arg ) : <EOL> raise ZeroDivisionError <EOL> rf = tasklets . ReducingFuture ( reducer ) <EOL> rf . putq ( <NUM_LIT:1> ) <EOL> rf . putq ( <NUM_LIT:1> ) <EOL> rf . complete ( ) <EOL> self . assertRaises ( ZeroDivisionError , rf . get_result ) <EOL> def testReducingFuture_ReducerException_2 ( self ) : <EOL> def reducer ( arg ) : <EOL> raise ZeroDivisionError <EOL> rf = tasklets . ReducingFuture ( reducer , batch_size = <NUM_LIT:2> ) <EOL> rf . putq ( <NUM_LIT:1> ) <EOL> rf . putq ( <NUM_LIT:1> ) <EOL> rf . putq ( <NUM_LIT:1> ) <EOL> rf . complete ( ) <EOL> self . assertRaises ( ZeroDivisionError , rf . get_result ) <EOL> def testReducingFuture_ReducerFuture_1 ( self ) : <EOL> def reducer ( arg ) : <EOL> f = Future ( ) <EOL> f . set_result ( sum ( arg ) ) <EOL> return f <EOL> rf = tasklets . ReducingFuture ( reducer , batch_size = <NUM_LIT:2> ) <EOL> rf . putq ( <NUM_LIT:1> ) <EOL> rf . putq ( <NUM_LIT:1> ) <EOL> rf . complete ( ) <EOL> self . assertEqual ( rf . get_result ( ) , <NUM_LIT:2> ) <EOL> def testReducingFuture_ReducerFuture_2 ( self ) : <EOL> def reducer ( arg ) : <EOL> res = sum ( arg ) <EOL> if len ( arg ) < <NUM_LIT:3> : <EOL> f = Future ( ) <EOL> f . set_result ( res ) <EOL> res = f <EOL> return res <EOL> rf = tasklets . ReducingFuture ( reducer , batch_size = <NUM_LIT:3> ) <EOL> rf . putq ( <NUM_LIT:1> ) <EOL> rf . putq ( <NUM_LIT:1> ) <EOL> rf . putq ( <NUM_LIT:1> ) <EOL> rf . putq ( <NUM_LIT:1> ) <EOL> rf . complete ( ) <EOL> self . assertEqual ( rf . get_result ( ) , <NUM_LIT:4> ) <EOL> def testGetReturnValue ( self ) : <EOL> r0 = tasklets . Return ( ) <EOL> r1 = tasklets . Return ( <NUM_LIT> ) <EOL> r2 = tasklets . Return ( <NUM_LIT> , '<STR_LIT:hello>' ) <EOL> r3 = tasklets . Return ( ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ) ) <EOL> self . assertEqual ( tasklets . get_return_value ( r0 ) , None ) <EOL> self . assertEqual ( tasklets . get_return_value ( r1 ) , <NUM_LIT> ) <EOL> self . assertEqual ( tasklets . get_return_value ( r2 ) , ( <NUM_LIT> , '<STR_LIT:hello>' ) ) <EOL> self . assertEqual ( tasklets . get_return_value ( r3 ) , ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ) ) <EOL> def testTasklets_Basic ( self ) : <EOL> @ tasklets . tasklet <EOL> def t1 ( ) : <EOL> a = yield t2 ( <NUM_LIT:3> ) <EOL> b = yield t3 ( <NUM_LIT:2> ) <EOL> raise tasklets . Return ( a + b ) <EOL> @ tasklets . tasklet <EOL> def t2 ( n ) : <EOL> raise tasklets . Return ( n ) <EOL> @ tasklets . tasklet <EOL> def t3 ( n ) : <EOL> return n <EOL> x = t1 ( ) <EOL> self . assertTrue ( isinstance ( x , tasklets . Future ) ) <EOL> y = x . get_result ( ) <EOL> self . assertEqual ( y , <NUM_LIT:5> ) <EOL> def testTasklets_Raising ( self ) : <EOL> @ tasklets . tasklet <EOL> def t1 ( ) : <EOL> f = t2 ( True ) <EOL> try : <EOL> a = yield f <EOL> except RuntimeError , err : <EOL> self . assertEqual ( f . get_exception ( ) , err ) <EOL> raise tasklets . Return ( str ( err ) ) <EOL> @ tasklets . tasklet <EOL> def t2 ( error ) : <EOL> if error : <EOL> raise RuntimeError ( '<STR_LIT:hello>' ) <EOL> else : <EOL> yield tasklets . Future ( ) <EOL> x = t1 ( ) <EOL> y = x . get_result ( ) <EOL> self . assertEqual ( y , '<STR_LIT:hello>' ) <EOL> def testTasklets_YieldRpcs ( self ) : <EOL> @ tasklets . tasklet <EOL> def main_tasklet ( ) : <EOL> rpc1 = self . conn . async_get ( None , [ ] ) <EOL> rpc2 = self . conn . async_put ( None , [ ] ) <EOL> res1 = yield rpc1 <EOL> res2 = yield rpc2 <EOL> raise tasklets . Return ( res1 , res2 ) <EOL> f = main_tasklet ( ) <EOL> result = f . get_result ( ) <EOL> self . assertEqual ( result , ( [ ] , [ ] ) ) <EOL> def testTasklet_YieldTuple ( self ) : <EOL> @ tasklets . tasklet <EOL> def fib ( n ) : <EOL> if n <= <NUM_LIT:1> : <EOL> raise tasklets . Return ( n ) <EOL> a , b = yield fib ( n - <NUM_LIT:1> ) , fib ( n - <NUM_LIT:2> ) <EOL> self . assertTrue ( a >= b , ( a , b ) ) <EOL> raise tasklets . Return ( a + b ) <EOL> fut = fib ( <NUM_LIT:10> ) <EOL> val = fut . get_result ( ) <EOL> self . assertEqual ( val , <NUM_LIT> ) <EOL> def testTasklet_YieldTupleError ( self ) : <EOL> @ tasklets . tasklet <EOL> def good ( ) : <EOL> yield tasklets . sleep ( <NUM_LIT:0> ) <EOL> @ tasklets . tasklet <EOL> def bad ( ) : <EOL> <NUM_LIT:1> / <NUM_LIT:0> <EOL> yield tasklets . sleep ( <NUM_LIT:0> ) <EOL> @ tasklets . tasklet <EOL> def foo ( ) : <EOL> try : <EOL> yield good ( ) , bad ( ) , good ( ) <EOL> self . assertFalse ( '<STR_LIT>' ) <EOL> except ZeroDivisionError : <EOL> pass <EOL> foo ( ) . check_success ( ) <EOL> def testTasklet_YieldTupleTypeError ( self ) : <EOL> @ tasklets . tasklet <EOL> def good ( ) : <EOL> yield tasklets . sleep ( <NUM_LIT:0> ) <EOL> @ tasklets . tasklet <EOL> def bad ( ) : <EOL> <NUM_LIT:1> / <NUM_LIT:0> <EOL> yield tasklets . sleep ( <NUM_LIT:0> ) <EOL> @ tasklets . tasklet <EOL> def foo ( ) : <EOL> try : <EOL> yield good ( ) , bad ( ) , <NUM_LIT> <EOL> except AssertionError : <EOL> pass <EOL> else : <EOL> self . assertFalse ( '<STR_LIT>' ) <EOL> foo ( ) . check_success ( ) <EOL> class TracebackTests ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def testBasicError ( self ) : <EOL> frames = [ sys . _getframe ( ) ] <EOL> @ tasklets . tasklet <EOL> def level3 ( ) : <EOL> frames . append ( sys . _getframe ( ) ) <EOL> raise RuntimeError ( '<STR_LIT:hello>' ) <EOL> yield <EOL> @ tasklets . tasklet <EOL> def level2 ( ) : <EOL> frames . append ( sys . _getframe ( ) ) <EOL> yield level3 ( ) <EOL> @ tasklets . tasklet <EOL> def level1 ( ) : <EOL> frames . append ( sys . _getframe ( ) ) <EOL> yield level2 ( ) <EOL> @ tasklets . tasklet <EOL> def level0 ( ) : <EOL> frames . append ( sys . _getframe ( ) ) <EOL> yield level1 ( ) <EOL> fut = level0 ( ) <EOL> try : <EOL> fut . check_success ( ) <EOL> except RuntimeError , err : <EOL> _ , _ , tb = sys . exc_info ( ) <EOL> self . assertEqual ( str ( err ) , '<STR_LIT:hello>' ) <EOL> tbframes = [ ] <EOL> while tb is not None : <EOL> if tb . tb_frame . f_code . co_name != '<STR_LIT>' : <EOL> tbframes . append ( tb . tb_frame ) <EOL> tb = tb . tb_next <EOL> self . assertEqual ( frames , tbframes ) <EOL> else : <EOL> self . fail ( '<STR_LIT>' ) <EOL> def main ( ) : <EOL> unittest . main ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
