<s> """<STR_LIT>""" <EOL> import logging <EOL> import os <EOL> import time <EOL> from . google_imports import apiproxy_stub_map <EOL> from . google_imports import datastore_rpc <EOL> from . google_test_imports import unittest <EOL> from . import eventloop <EOL> from . import test_utils <EOL> class EventLoopTests ( test_utils . NDBTest ) : <EOL> def setUp ( self ) : <EOL> super ( EventLoopTests , self ) . setUp ( ) <EOL> if eventloop . _EVENT_LOOP_KEY in os . environ : <EOL> del os . environ [ eventloop . _EVENT_LOOP_KEY ] <EOL> self . ev = eventloop . get_event_loop ( ) <EOL> the_module = eventloop <EOL> def testQueueTasklet ( self ) : <EOL> def f ( unused_number , unused_string , unused_a , unused_b ) : return <NUM_LIT:1> <EOL> def g ( unused_number , unused_string ) : return <NUM_LIT:2> <EOL> def h ( unused_c , unused_d ) : return <NUM_LIT:3> <EOL> t_before = time . time ( ) <EOL> eventloop . queue_call ( <NUM_LIT:1> , f , <NUM_LIT> , '<STR_LIT:hello>' , unused_a = <NUM_LIT:1> , unused_b = <NUM_LIT:2> ) <EOL> eventloop . queue_call ( <NUM_LIT:3> , h , unused_c = <NUM_LIT:3> , unused_d = <NUM_LIT:4> ) <EOL> eventloop . queue_call ( <NUM_LIT:2> , g , <NUM_LIT:100> , '<STR_LIT:abc>' ) <EOL> t_after = time . time ( ) <EOL> self . assertEqual ( len ( self . ev . queue ) , <NUM_LIT:3> ) <EOL> [ ( t1 , f1 , a1 , k1 ) , ( t2 , f2 , a2 , k2 ) , ( t3 , f3 , a3 , k3 ) ] = self . ev . queue <EOL> self . assertTrue ( t1 < t2 ) <EOL> self . assertTrue ( t2 < t3 ) <EOL> self . assertTrue ( abs ( t1 - ( t_before + <NUM_LIT:1> ) ) <= t_after - t_before ) <EOL> self . assertTrue ( abs ( t2 - ( t_before + <NUM_LIT:2> ) ) <= t_after - t_before ) <EOL> self . assertTrue ( abs ( t3 - ( t_before + <NUM_LIT:3> ) ) <= t_after - t_before ) <EOL> self . assertEqual ( f1 , f ) <EOL> self . assertEqual ( f2 , g ) <EOL> self . assertEqual ( f3 , h ) <EOL> self . assertEqual ( a1 , ( <NUM_LIT> , '<STR_LIT:hello>' ) ) <EOL> self . assertEqual ( a2 , ( <NUM_LIT:100> , '<STR_LIT:abc>' ) ) <EOL> self . assertEqual ( a3 , ( ) ) <EOL> self . assertEqual ( k1 , { '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : <NUM_LIT:2> } ) <EOL> self . assertEqual ( k2 , { } ) <EOL> self . assertEqual ( k3 , { '<STR_LIT>' : <NUM_LIT:3> , '<STR_LIT>' : <NUM_LIT:4> } ) <EOL> ev = eventloop . get_event_loop ( ) <EOL> ev . queue = [ ] <EOL> ev . rpcs = { } <EOL> def testFifoOrderForEventsWithDelayNone ( self ) : <EOL> order = [ ] <EOL> def foo ( arg ) : order . append ( arg ) <EOL> eventloop . queue_call ( None , foo , <NUM_LIT:2> ) <EOL> eventloop . queue_call ( None , foo , <NUM_LIT:1> ) <EOL> eventloop . queue_call ( <NUM_LIT:0> , foo , <NUM_LIT:0> ) <EOL> self . assertEqual ( len ( self . ev . current ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( len ( self . ev . queue ) , <NUM_LIT:1> ) <EOL> [ ( _f1 , a1 , _k1 ) , ( _f2 , a2 , _k2 ) ] = self . ev . current <EOL> self . assertEqual ( a1 , ( <NUM_LIT:2> , ) ) <EOL> self . assertEqual ( a2 , ( <NUM_LIT:1> , ) ) <EOL> ( _t , _f , a , _k ) = self . ev . queue [ <NUM_LIT:0> ] <EOL> self . assertEqual ( a , ( <NUM_LIT:0> , ) ) <EOL> eventloop . run ( ) <EOL> self . assertEqual ( order , [ <NUM_LIT:2> , <NUM_LIT:1> , <NUM_LIT:0> ] ) <EOL> def testRun ( self ) : <EOL> record = [ ] <EOL> def foo ( arg ) : <EOL> record . append ( arg ) <EOL> eventloop . queue_call ( <NUM_LIT> , foo , <NUM_LIT> ) <EOL> eventloop . queue_call ( <NUM_LIT:0.1> , foo , arg = '<STR_LIT:hello>' ) <EOL> eventloop . run ( ) <EOL> self . assertEqual ( record , [ '<STR_LIT:hello>' , <NUM_LIT> ] ) <EOL> def testRunWithRpcs ( self ) : <EOL> record = [ ] <EOL> def foo ( arg ) : <EOL> record . append ( arg ) <EOL> eventloop . queue_call ( <NUM_LIT:0.1> , foo , <NUM_LIT> ) <EOL> config = datastore_rpc . Configuration ( on_completion = foo ) <EOL> rpc = self . conn . async_get ( config , [ ] ) <EOL> if not isinstance ( rpc , apiproxy_stub_map . UserRPC ) : <EOL> self . assertEqual ( len ( rpc . rpcs ) , <NUM_LIT:1> ) <EOL> rpc = rpc . rpcs [ <NUM_LIT:0> ] <EOL> eventloop . queue_rpc ( rpc ) <EOL> eventloop . run ( ) <EOL> self . assertEqual ( record , [ rpc , <NUM_LIT> ] ) <EOL> self . assertEqual ( rpc . state , <NUM_LIT:2> ) <EOL> def testIdle ( self ) : <EOL> counters = [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] <EOL> def idler1 ( ) : <EOL> logging . info ( '<STR_LIT>' ) <EOL> counters [ <NUM_LIT:0> ] += <NUM_LIT:1> <EOL> return False <EOL> def idler2 ( a , b = None ) : <EOL> logging . info ( '<STR_LIT>' , a , b ) <EOL> counters [ <NUM_LIT:1> ] += <NUM_LIT:1> <EOL> return False <EOL> def idler3 ( k = None ) : <EOL> logging . info ( '<STR_LIT>' , k ) <EOL> counters [ <NUM_LIT:2> ] += <NUM_LIT:1> <EOL> return None <EOL> self . ev . add_idle ( idler1 ) <EOL> self . ev . add_idle ( idler2 , <NUM_LIT:10> , <NUM_LIT:20> ) <EOL> eventloop . add_idle ( idler3 , k = <NUM_LIT> ) <EOL> self . ev . run ( ) <EOL> self . assertEqual ( counters , [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ] ) <EOL> self . ev . run ( ) <EOL> self . assertEqual ( counters , [ <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:1> ] ) <EOL> def testMultiRpcReadiness ( self ) : <EOL> from . import key <EOL> k1 = key . Key ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> k2 = key . Key ( '<STR_LIT>' , <NUM_LIT:2> ) <EOL> r1 = self . conn . async_get ( None , [ k1 ] ) <EOL> r2 = self . conn . async_get ( None , [ k2 ] ) <EOL> rpc = datastore_rpc . MultiRpc ( [ r1 , r2 ] ) <EOL> r1 . wait ( ) <EOL> r2 . wait ( ) <EOL> calls = [ ] <EOL> def callback ( ) : <EOL> calls . append ( <NUM_LIT:1> ) <EOL> eventloop . queue_rpc ( rpc , callback ) <EOL> eventloop . run ( ) <EOL> self . assertEqual ( calls , [ <NUM_LIT:1> ] ) <EOL> def testCleanUpStaleEvents ( self ) : <EOL> from . import model <EOL> class M ( model . Model ) : <EOL> pass <EOL> M ( ) . put ( ) <EOL> M ( ) . put ( ) <EOL> M ( ) . put ( ) <EOL> M . query ( ) . fetch_page ( <NUM_LIT:2> ) <EOL> ev = eventloop . get_event_loop ( ) <EOL> self . assertEqual ( len ( ev . rpcs ) , <NUM_LIT:1> ) <EOL> del os . environ [ eventloop . _EVENT_LOOP_KEY ] <EOL> ev = eventloop . get_event_loop ( ) <EOL> self . assertEqual ( len ( ev . rpcs ) , <NUM_LIT:0> ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
