<s> import itertools <EOL> from twisted . internet import reactor , defer <EOL> from twisted . trial import unittest <EOL> from piped import util , dependencies , processing , exceptions <EOL> class DependencyManagerTest ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . runtime_environment = processing . RuntimeEnvironment ( ) <EOL> self . runtime_environment . configure ( ) <EOL> self . resource_manager = self . runtime_environment . resource_manager <EOL> self . dependency_manager = self . runtime_environment . dependency_manager <EOL> def test_accessing_unprovided_resources_result_in_an_exception ( self ) : <EOL> consumer = object ( ) <EOL> resource_dependency = dependencies . ResourceDependency ( provider = '<STR_LIT>' , manager = self . dependency_manager ) <EOL> dependency_map = self . dependency_manager . create_dependency_map ( consumer , should_be_unavailable = resource_dependency ) <EOL> self . assertRaises ( exceptions . UnsatisfiedDependencyError , getattr , dependency_map , '<STR_LIT>' ) <EOL> self . assertRaises ( AttributeError , getattr , dependency_map , '<STR_LIT>' ) <EOL> def test_dependency_map_in ( self ) : <EOL> consumer = object ( ) <EOL> resource_dependency = dependencies . ResourceDependency ( provider = '<STR_LIT>' , manager = self . dependency_manager ) <EOL> dependency_map = self . dependency_manager . create_dependency_map ( consumer , should_be_unavailable = resource_dependency ) <EOL> self . assertIn ( '<STR_LIT>' , dependency_map ) <EOL> self . assertNotIn ( '<STR_LIT>' , dependency_map ) <EOL> def test_accessing_provided_dependency ( self ) : <EOL> consumer = object ( ) <EOL> fake_resource = object ( ) <EOL> resource_path = '<STR_LIT>' <EOL> resource_dependency = dependencies . ResourceDependency ( provider = resource_path , manager = self . dependency_manager ) <EOL> dependency_map = self . dependency_manager . create_dependency_map ( consumer , directly_provided = resource_dependency ) <EOL> consumers = list ( ) <EOL> provider = util . AttributeDict ( add_consumer = consumers . append ) <EOL> self . resource_manager . register ( resource_path , provider ) <EOL> self . dependency_manager . resolve_initial_states ( ) <EOL> resource_dependency . on_resource_ready ( fake_resource ) <EOL> self . assertEquals ( dependency_map . directly_provided , fake_resource ) <EOL> self . assertEquals ( consumers , [ resource_dependency ] ) <EOL> def test_getting_dependencies ( self ) : <EOL> foo = object ( ) <EOL> bar = object ( ) <EOL> baz = object ( ) <EOL> zip = object ( ) <EOL> self . dependency_manager . add_dependency ( foo , bar ) <EOL> self . dependency_manager . add_dependency ( baz , bar ) <EOL> self . dependency_manager . add_dependency ( bar , zip ) <EOL> self . assertEquals ( self . dependency_manager . get_dependencies_of ( foo ) , [ self . dependency_manager . as_dependency ( bar ) ] ) <EOL> self . assertEquals ( self . dependency_manager . get_dependencies_of ( zip ) , [ ] ) <EOL> def test_bubbling_availability_for_trivial_dependencies ( self ) : <EOL> """<STR_LIT>""" <EOL> dependencies = dict ( <EOL> A = self . dependency_manager . create_dependency_map ( '<STR_LIT:A>' , B = '<STR_LIT:B>' , C = '<STR_LIT:C>' ) , <EOL> C = self . dependency_manager . create_dependency_map ( '<STR_LIT:C>' , D = '<STR_LIT:D>' , E = '<STR_LIT:E>' ) <EOL> ) <EOL> is_ready = dict ( A = False , B = False , C = False , D = False , E = False ) <EOL> for char in is_ready . keys ( ) : <EOL> def enable ( dependency , char = char ) : <EOL> is_ready [ char ] = True <EOL> self . dependency_manager . as_dependency ( char ) . on_ready += enable <EOL> self . dependency_manager . resolve_initial_states ( ) <EOL> self . assertEquals ( is_ready , dict ( A = True , B = True , C = True , D = True , E = True ) ) <EOL> self . assertEquals ( dependencies [ '<STR_LIT:A>' ] . B , '<STR_LIT:B>' ) <EOL> self . assertEquals ( dependencies [ '<STR_LIT:A>' ] . C , '<STR_LIT:C>' ) <EOL> self . assertEquals ( dependencies [ '<STR_LIT:C>' ] . D , '<STR_LIT:D>' ) <EOL> self . assertEquals ( dependencies [ '<STR_LIT:C>' ] . E , '<STR_LIT:E>' ) <EOL> def test_not_bubbling_when_not_supposed_to ( self ) : <EOL> """<STR_LIT>""" <EOL> dependencies = dict ( <EOL> A = self . dependency_manager . create_dependency_map ( '<STR_LIT:A>' , B = '<STR_LIT:B>' , C = '<STR_LIT:C>' ) , <EOL> C = self . dependency_manager . create_dependency_map ( '<STR_LIT:C>' , D = '<STR_LIT:D>' , E = '<STR_LIT:E>' ) <EOL> ) <EOL> c_dependency = self . dependency_manager . as_dependency ( '<STR_LIT:C>' ) <EOL> c_dependency . cascade_ready = False <EOL> dependencies_are_available = dict ( A = False , C = False ) <EOL> is_ready = dict ( A = False , B = False , C = False , D = False , E = False ) <EOL> for char in is_ready . keys ( ) : <EOL> def update_availability ( readied_dependency , char = char ) : <EOL> char_dependency = self . dependency_manager . as_dependency ( char ) <EOL> if self . dependency_manager . has_all_dependencies_provided ( char_dependency ) : <EOL> dependencies_are_available [ char ] = True <EOL> def enable_ready ( dependency , char = char ) : <EOL> is_ready [ char ] = True <EOL> self . dependency_manager . as_dependency ( char ) . on_dependency_ready += update_availability <EOL> self . dependency_manager . as_dependency ( char ) . on_ready += enable_ready <EOL> self . dependency_manager . resolve_initial_states ( ) <EOL> self . assertEquals ( dependencies_are_available , dict ( A = False , C = True ) ) <EOL> self . assertEquals ( is_ready , dict ( A = False , B = True , C = False , D = True , E = True ) ) <EOL> self . assertEquals ( dependencies [ '<STR_LIT:A>' ] . B , '<STR_LIT:B>' ) <EOL> self . assertRaises ( exceptions . UnsatisfiedDependencyError , getattr , dependencies [ '<STR_LIT:A>' ] , '<STR_LIT:C>' ) <EOL> self . assertEquals ( dependencies [ '<STR_LIT:C>' ] . D , '<STR_LIT:D>' ) <EOL> self . assertEquals ( dependencies [ '<STR_LIT:C>' ] . E , '<STR_LIT:E>' ) <EOL> c_dependency . fire_on_ready ( ) <EOL> self . assertEquals ( dependencies_are_available , dict ( A = True , C = True ) ) <EOL> self . assertEquals ( is_ready , dict ( A = True , B = True , C = True , D = True , E = True ) ) <EOL> self . assertEquals ( dependencies [ '<STR_LIT:A>' ] . C , '<STR_LIT:C>' ) <EOL> def test_bubbling_unavailability ( self ) : <EOL> """<STR_LIT>""" <EOL> dependencies = dict ( <EOL> A = self . dependency_manager . create_dependency_map ( '<STR_LIT:A>' , B = '<STR_LIT:B>' , C = '<STR_LIT:C>' ) , <EOL> C = self . dependency_manager . create_dependency_map ( '<STR_LIT:C>' , D = '<STR_LIT:D>' , E = '<STR_LIT:E>' ) , <EOL> F = self . dependency_manager . create_dependency_map ( '<STR_LIT:F>' , E = '<STR_LIT:E>' ) <EOL> ) <EOL> is_ready = dict ( A = False , B = False , C = False , D = False , E = False , F = False ) <EOL> for char in is_ready . keys ( ) : <EOL> def enable ( dependency , char = char ) : <EOL> is_ready [ char ] = True <EOL> def disable ( dependency , failure = None , char = char ) : <EOL> is_ready [ char ] = False <EOL> self . dependency_manager . as_dependency ( char ) . on_ready += enable <EOL> self . dependency_manager . as_dependency ( char ) . on_lost += disable <EOL> self . dependency_manager . resolve_initial_states ( ) <EOL> self . assertEquals ( is_ready , dict ( A = True , B = True , C = True , D = True , E = True , F = True ) ) <EOL> e_dependency = self . dependency_manager . as_dependency ( '<STR_LIT:E>' ) <EOL> e_dependency . fire_on_lost ( '<STR_LIT>' ) <EOL> self . assertEquals ( is_ready , dict ( A = False , B = True , C = False , D = True , E = False , F = False ) ) <EOL> self . assertRaises ( exceptions . UnsatisfiedDependencyError , getattr , dependencies [ '<STR_LIT:A>' ] , '<STR_LIT:C>' ) <EOL> self . assertRaises ( exceptions . UnsatisfiedDependencyError , getattr , dependencies [ '<STR_LIT:C>' ] , '<STR_LIT:E>' ) <EOL> self . assertRaises ( exceptions . UnsatisfiedDependencyError , getattr , dependencies [ '<STR_LIT:F>' ] , '<STR_LIT:E>' ) <EOL> self . assertEquals ( dependencies [ '<STR_LIT:A>' ] . B , '<STR_LIT:B>' ) <EOL> self . assertEquals ( dependencies [ '<STR_LIT:C>' ] . D , '<STR_LIT:D>' ) <EOL> e_dependency . fire_on_ready ( ) <EOL> self . assertEquals ( is_ready , dict ( A = True , B = True , C = True , D = True , E = True , F = True ) ) <EOL> self . assertEquals ( dependencies [ '<STR_LIT:A>' ] . C , '<STR_LIT:C>' ) <EOL> self . assertEquals ( dependencies [ '<STR_LIT:C>' ] . E , '<STR_LIT:E>' ) <EOL> self . assertEquals ( dependencies [ '<STR_LIT:F>' ] . E , '<STR_LIT:E>' ) <EOL> b_dependency = self . dependency_manager . as_dependency ( '<STR_LIT:B>' ) <EOL> b_dependency . fire_on_lost ( '<STR_LIT>' ) <EOL> self . assertEquals ( is_ready , dict ( A = False , B = False , C = True , D = True , E = True , F = True ) ) <EOL> self . assertRaises ( exceptions . UnsatisfiedDependencyError , getattr , dependencies [ '<STR_LIT:A>' ] , '<STR_LIT:B>' ) <EOL> b_dependency . fire_on_ready ( ) <EOL> self . assertEquals ( is_ready , dict ( A = True , B = True , C = True , D = True , E = True , F = True ) ) <EOL> self . assertEquals ( dependencies [ '<STR_LIT:A>' ] . B , '<STR_LIT:B>' ) <EOL> self . assertEquals ( dependencies [ '<STR_LIT:A>' ] . C , '<STR_LIT:C>' ) <EOL> self . assertEquals ( dependencies [ '<STR_LIT:C>' ] . D , '<STR_LIT:D>' ) <EOL> self . assertEquals ( dependencies [ '<STR_LIT:C>' ] . E , '<STR_LIT:E>' ) <EOL> self . assertEquals ( dependencies [ '<STR_LIT:F>' ] . E , '<STR_LIT:E>' ) <EOL> def test_deferred_dependencies ( self ) : <EOL> """<STR_LIT>""" <EOL> class B : <EOL> def configure ( self , runtime_environment ) : <EOL> self . runtime_environment = runtime_environment <EOL> self . dependency_manager = self . runtime_environment . dependency_manager <EOL> self . runtime_environment . resource_manager . register ( '<STR_LIT:B>' , provider = self ) <EOL> def add_consumer ( self , consumer ) : <EOL> self . dependency_manager . add_dependency ( consumer , self . dependency_manager . as_dependency ( '<STR_LIT:C>' ) ) <EOL> b_dependency = dependencies . ResourceDependency ( provider = '<STR_LIT:B>' , manager = self . dependency_manager ) <EOL> self . dependency_manager . add_dependency ( '<STR_LIT:A>' , b_dependency ) <EOL> a_dependency = self . dependency_manager . as_dependency ( '<STR_LIT:A>' ) <EOL> c_dependency = self . dependency_manager . as_dependency ( '<STR_LIT:C>' ) <EOL> B ( ) . configure ( self . runtime_environment ) <EOL> self . dependency_manager . resolve_initial_states ( ) <EOL> self . assertEquals ( set ( self . dependency_manager . _dependency_graph . edges ( ) ) , set ( [ ( c_dependency , b_dependency ) , ( b_dependency , a_dependency ) ] ) ) <EOL> def test_duplicate_named_dependencies ( self ) : <EOL> """<STR_LIT>""" <EOL> class C : <EOL> def configure ( self , runtime_environment ) : <EOL> self . runtime_environment = runtime_environment <EOL> self . runtime_environment . resource_manager . register ( '<STR_LIT:C>' , provider = self ) <EOL> def add_consumer ( self , resource_dependency ) : <EOL> resource = '<STR_LIT>' , object ( ) <EOL> resource_dependency . on_resource_ready ( resource ) <EOL> class F : <EOL> resource = '<STR_LIT>' <EOL> def configure ( self , runtime_environment ) : <EOL> self . runtime_environment = runtime_environment <EOL> self . runtime_environment . resource_manager . register ( '<STR_LIT:F>' , provider = self ) <EOL> def add_consumer ( self , resource_dependency ) : <EOL> resource_dependency . on_resource_ready ( self . resource ) <EOL> dep_a = dependencies . ResourceDependency ( provider = '<STR_LIT:C>' , manager = self . dependency_manager ) <EOL> dep_b = dependencies . ResourceDependency ( provider = '<STR_LIT:C>' , manager = self . dependency_manager ) <EOL> dep_d = dependencies . ResourceDependency ( provider = '<STR_LIT:F>' , manager = self . dependency_manager ) <EOL> dep_e = dependencies . ResourceDependency ( provider = '<STR_LIT:F>' , manager = self . dependency_manager ) <EOL> self . dependency_manager . create_dependency_map ( self , a = dep_a , b = dep_b , d = dep_d , e = dep_e ) <EOL> C ( ) . configure ( self . runtime_environment ) <EOL> F ( ) . configure ( self . runtime_environment ) <EOL> self . dependency_manager . resolve_initial_states ( ) <EOL> self . assertTrue ( dep_a . get_resource ( ) != dep_b . get_resource ( ) ) <EOL> self . assertEquals ( dep_a . get_resource ( ) [ <NUM_LIT:0> ] , '<STR_LIT>' ) <EOL> self . assertEquals ( dep_b . get_resource ( ) [ <NUM_LIT:0> ] , '<STR_LIT>' ) <EOL> self . assertEquals ( dep_d . get_resource ( ) , '<STR_LIT>' ) <EOL> self . assertEquals ( dep_e . get_resource ( ) , '<STR_LIT>' ) <EOL> def test_bubbling_availability_for_duplicate_named_dependencies ( self ) : <EOL> """<STR_LIT>""" <EOL> g_provider_consumers = list ( ) <EOL> class GProvider : <EOL> def configure ( self , runtime_environment ) : <EOL> self . runtime_environment = runtime_environment <EOL> self . runtime_environment . resource_manager . register ( '<STR_LIT>' , provider = self ) <EOL> def add_consumer ( self , resource_dependency ) : <EOL> g_provider_consumers . append ( resource_dependency ) <EOL> GProvider ( ) . configure ( self . runtime_environment ) <EOL> g_dependency = dependencies . ResourceDependency ( '<STR_LIT>' , manager = self . dependency_manager ) <EOL> deps = dict ( <EOL> A = self . dependency_manager . create_dependency_map ( '<STR_LIT:A>' , foo = '<STR_LIT:B>' , bar = '<STR_LIT:C>' ) , <EOL> C = self . dependency_manager . create_dependency_map ( '<STR_LIT:C>' , foo = '<STR_LIT:D>' , bar = '<STR_LIT:E>' ) , <EOL> F = self . dependency_manager . create_dependency_map ( '<STR_LIT:F>' , foo = '<STR_LIT:E>' , bar = g_dependency ) , <EOL> ) <EOL> is_ready = dict ( A = False , B = False , C = False , D = False , E = False , F = False , G = False ) <EOL> for char in is_ready . keys ( ) : <EOL> def enable ( dependency , char = char ) : <EOL> is_ready [ char ] = True <EOL> self . dependency_manager . as_dependency ( char ) . on_ready += enable <EOL> self . dependency_manager . resolve_initial_states ( ) <EOL> self . assertEquals ( is_ready , dict ( A = True , B = True , C = True , D = True , E = True , F = False , G = False ) ) <EOL> self . assertEquals ( deps [ '<STR_LIT:A>' ] . foo , '<STR_LIT:B>' ) <EOL> self . assertEquals ( deps [ '<STR_LIT:A>' ] . bar , '<STR_LIT:C>' ) <EOL> self . assertEquals ( deps [ '<STR_LIT:C>' ] . foo , '<STR_LIT:D>' ) <EOL> self . assertEquals ( deps [ '<STR_LIT:C>' ] . bar , '<STR_LIT:E>' ) <EOL> self . assertEquals ( deps [ '<STR_LIT:F>' ] . foo , '<STR_LIT:E>' ) <EOL> self . assertRaises ( exceptions . UnsatisfiedDependencyError , getattr , deps [ '<STR_LIT:F>' ] , '<STR_LIT:bar>' ) <EOL> self . assertEquals ( g_provider_consumers , [ g_dependency ] ) <EOL> def test_multiple_resolves ( self ) : <EOL> """<STR_LIT>""" <EOL> is_ready = dict ( ) <EOL> for char in '<STR_LIT:A>' , '<STR_LIT:B>' , '<STR_LIT:C>' , '<STR_LIT:D>' , '<STR_LIT:E>' , '<STR_LIT:X>' , '<STR_LIT:Y>' : <EOL> def enable ( dependency , char = char ) : <EOL> is_ready [ char ] = True <EOL> def disable ( dependency , reason , char = char ) : <EOL> is_ready [ char ] = False <EOL> self . dependency_manager . as_dependency ( char ) . on_ready += enable <EOL> self . dependency_manager . as_dependency ( char ) . on_lost += disable <EOL> dependency_map = self . dependency_manager . create_dependency_map ( '<STR_LIT:A>' , B = '<STR_LIT:B>' , C = '<STR_LIT:C>' ) <EOL> self . dependency_manager . resolve_initial_states ( ) <EOL> for instance in ( '<STR_LIT:B>' , '<STR_LIT:C>' ) : <EOL> self . assertEquals ( dependency_map [ instance ] , instance ) <EOL> self . assertEquals ( is_ready , dict ( A = True , B = True , C = True ) ) <EOL> c_dependency = self . dependency_manager . as_dependency ( '<STR_LIT:C>' ) <EOL> d_dependency = self . dependency_manager . as_dependency ( '<STR_LIT:D>' ) <EOL> e_dependency = self . dependency_manager . as_dependency ( '<STR_LIT:E>' ) <EOL> self . dependency_manager . add_dependency ( c_dependency , d_dependency ) <EOL> self . dependency_manager . add_dependency ( c_dependency , e_dependency ) <EOL> self . assertEquals ( is_ready , dict ( A = True , B = True , C = True ) ) <EOL> self . dependency_manager . resolve_initial_states ( ) <EOL> self . assertEquals ( is_ready , dict ( A = True , B = True , C = True , D = True , E = True ) ) <EOL> for instance in ( '<STR_LIT:B>' , '<STR_LIT:C>' ) : <EOL> self . assertEquals ( dependency_map [ instance ] , instance ) <EOL> e_dependency . fire_on_lost ( '<STR_LIT>' ) <EOL> self . assertEquals ( is_ready , dict ( A = False , B = True , C = False , D = True , E = False ) ) <EOL> self . assertEquals ( dependency_map [ '<STR_LIT:B>' ] , '<STR_LIT:B>' ) <EOL> self . assertRaises ( exceptions . UnsatisfiedDependencyError , getattr , dependency_map , '<STR_LIT:C>' ) <EOL> e_dependency . fire_on_ready ( ) <EOL> self . dependency_manager . create_dependency_map ( '<STR_LIT:X>' , A = '<STR_LIT:A>' , C = '<STR_LIT:C>' , E = '<STR_LIT:E>' ) <EOL> self . assertEquals ( is_ready , dict ( A = True , B = True , C = True , D = True , E = True ) ) <EOL> self . dependency_manager . resolve_initial_states ( ) <EOL> self . assertEquals ( is_ready , dict ( A = True , B = True , C = True , D = True , E = True , X = True ) ) <EOL> d_dependency . fire_on_lost ( '<STR_LIT>' ) <EOL> self . assertEquals ( is_ready , dict ( A = False , B = True , C = False , D = False , E = True , X = False ) ) <EOL> self . dependency_manager . add_dependency ( '<STR_LIT:Y>' , '<STR_LIT:X>' ) <EOL> self . assertEquals ( is_ready , dict ( A = False , B = True , C = False , D = False , E = True , X = False ) ) <EOL> self . dependency_manager . resolve_initial_states ( ) <EOL> self . assertEquals ( is_ready , dict ( A = False , B = True , C = False , D = False , E = True , X = False , Y = False ) ) <EOL> d_dependency . fire_on_ready ( ) <EOL> self . assertEquals ( is_ready , dict ( A = True , B = True , C = True , D = True , E = True , X = True , Y = True ) ) <EOL> @ defer . inlineCallbacks <EOL> def test_multiple_resolves_with_resource_dependency ( self ) : <EOL> """<STR_LIT>""" <EOL> is_ready = dict ( ) <EOL> for char in '<STR_LIT:A>' , '<STR_LIT:B>' , '<STR_LIT:D>' , '<STR_LIT:E>' , '<STR_LIT:X>' , '<STR_LIT:Y>' : <EOL> self . dependency_manager . as_dependency ( char ) . on_ready += lambda dep , char = char : is_ready . __setitem__ ( char , True ) <EOL> self . dependency_manager . as_dependency ( char ) . on_lost += lambda dep , reason , char = char : is_ready . __setitem__ ( char , False ) <EOL> class CProvider ( object ) : <EOL> def configure ( self , runtime_environment ) : <EOL> self . runtime_environment = runtime_environment <EOL> self . runtime_environment . resource_manager . register ( '<STR_LIT>' , provider = self ) <EOL> def add_consumer ( self , resource_dependency ) : <EOL> reactor . callLater ( <NUM_LIT:0> , resource_dependency . on_resource_ready , object ( ) ) <EOL> CProvider ( ) . configure ( self . runtime_environment ) <EOL> c_dependency = dependencies . ResourceDependency ( '<STR_LIT>' , manager = self . dependency_manager ) <EOL> c_dependency . on_ready += lambda dep : is_ready . __setitem__ ( '<STR_LIT:C>' , True ) <EOL> c_dependency . on_lost += lambda dep , reason : is_ready . __setitem__ ( '<STR_LIT:C>' , False ) <EOL> self . dependency_manager . create_dependency_map ( '<STR_LIT:A>' , B = '<STR_LIT:B>' , C = c_dependency ) <EOL> self . dependency_manager . resolve_initial_states ( ) <EOL> self . assertEquals ( is_ready , dict ( A = False , B = True , C = False ) ) <EOL> yield util . wait ( <NUM_LIT:0> ) <EOL> self . assertEquals ( is_ready , dict ( A = True , B = True , C = True ) ) <EOL> d_dependency = self . dependency_manager . as_dependency ( '<STR_LIT:D>' ) <EOL> e_dependency = self . dependency_manager . as_dependency ( '<STR_LIT:E>' ) <EOL> self . dependency_manager . add_dependency ( c_dependency , d_dependency ) <EOL> self . dependency_manager . add_dependency ( c_dependency , e_dependency ) <EOL> self . assertEquals ( is_ready , dict ( A = True , B = True , C = True ) ) <EOL> self . dependency_manager . resolve_initial_states ( ) <EOL> self . assertEquals ( is_ready , dict ( A = True , B = True , C = True , D = True , E = True ) ) <EOL> e_dependency . fire_on_lost ( '<STR_LIT>' ) <EOL> self . assertEquals ( is_ready , dict ( A = False , B = True , C = False , D = True , E = False ) ) <EOL> e_dependency . fire_on_ready ( ) <EOL> self . dependency_manager . create_dependency_map ( '<STR_LIT:X>' , A = '<STR_LIT:A>' , C = '<STR_LIT:C>' , E = '<STR_LIT:E>' ) <EOL> self . assertEquals ( is_ready , dict ( A = True , B = True , C = True , D = True , E = True ) ) <EOL> self . dependency_manager . resolve_initial_states ( ) <EOL> self . assertEquals ( is_ready , dict ( A = True , B = True , C = True , D = True , E = True , X = True ) ) <EOL> d_dependency . fire_on_lost ( '<STR_LIT>' ) <EOL> self . assertEquals ( is_ready , dict ( A = False , B = True , C = False , D = False , E = True , X = False ) ) <EOL> self . dependency_manager . create_dependency_map ( '<STR_LIT:Y>' , X = '<STR_LIT:X>' ) <EOL> self . assertEquals ( is_ready , dict ( A = False , B = True , C = False , D = False , E = True , X = False ) ) <EOL> self . dependency_manager . resolve_initial_states ( ) <EOL> self . assertEquals ( is_ready , dict ( A = False , B = True , C = False , D = False , E = True , X = False , Y = False ) ) <EOL> d_dependency . fire_on_ready ( ) <EOL> self . assertEquals ( is_ready , dict ( A = True , B = True , C = True , D = True , E = True , X = True , Y = True ) ) <EOL> def test_only_fires_on_changes ( self ) : <EOL> """<STR_LIT>""" <EOL> counter = [ <NUM_LIT:0> ] <EOL> a_ready_calls = list ( ) <EOL> a_lost_calls = list ( ) <EOL> def reset_calls ( ) : <EOL> counter [ <NUM_LIT:0> ] = <NUM_LIT:0> <EOL> a_ready_calls [ : ] = list ( ) <EOL> a_lost_calls [ : ] = list ( ) <EOL> def assertCalls ( ready = list ( ) , lost = list ( ) , msg = '<STR_LIT>' ) : <EOL> self . assertEquals ( a_ready_calls , ready , msg ) <EOL> self . assertEquals ( a_lost_calls , lost , msg = '<STR_LIT>' ) <EOL> def add_ready_call ( dependency ) : <EOL> counter [ <NUM_LIT:0> ] += <NUM_LIT:1> <EOL> self . assertEquals ( dependency , a_dependency ) <EOL> a_ready_calls . append ( counter [ <NUM_LIT:0> ] ) <EOL> def add_lost_call ( dependency , reason ) : <EOL> counter [ <NUM_LIT:0> ] += <NUM_LIT:1> <EOL> self . assertEquals ( dependency , a_dependency ) <EOL> a_lost_calls . append ( counter [ <NUM_LIT:0> ] ) <EOL> a_dependency = self . dependency_manager . as_dependency ( '<STR_LIT:A>' ) <EOL> a_dependency . on_ready += add_ready_call <EOL> a_dependency . on_lost += add_lost_call <EOL> b_dependency = self . dependency_manager . as_dependency ( '<STR_LIT:B>' ) <EOL> c_dependency = self . dependency_manager . as_dependency ( '<STR_LIT:C>' ) <EOL> d_dependency = self . dependency_manager . as_dependency ( '<STR_LIT:D>' ) <EOL> e_dependency = self . dependency_manager . as_dependency ( '<STR_LIT:E>' ) <EOL> self . dependency_manager . add_dependency ( a_dependency , b_dependency ) <EOL> self . dependency_manager . add_dependency ( a_dependency , c_dependency ) <EOL> self . dependency_manager . add_dependency ( b_dependency , d_dependency ) <EOL> self . dependency_manager . add_dependency ( c_dependency , d_dependency ) <EOL> self . dependency_manager . add_dependency ( c_dependency , e_dependency ) <EOL> assertCalls ( ) <EOL> self . dependency_manager . resolve_initial_states ( ) <EOL> assertCalls ( [ <NUM_LIT:1> ] ) <EOL> reset_calls ( ) <EOL> for dependency in b_dependency , c_dependency , d_dependency , e_dependency : <EOL> dependency . fire_on_lost ( '<STR_LIT>' ) <EOL> assertCalls ( lost = [ <NUM_LIT:1> ] ) <EOL> reset_calls ( ) <EOL> dependency . fire_on_ready ( ) <EOL> assertCalls ( ready = [ <NUM_LIT:1> ] ) <EOL> reset_calls ( ) <EOL> for dependencies in itertools . permutations ( ( b_dependency , c_dependency , d_dependency , e_dependency ) ) : <EOL> for dependency in dependencies : <EOL> dependency . fire_on_lost ( '<STR_LIT>' ) <EOL> if dependency == dependencies [ <NUM_LIT:0> ] : <EOL> assertCalls ( lost = [ <NUM_LIT:1> ] ) <EOL> for dependency in dependencies : <EOL> dependency . fire_on_ready ( ) <EOL> assertCalls ( lost = [ <NUM_LIT:1> ] , ready = [ <NUM_LIT:2> ] , msg = '<STR_LIT:U+002C>' . join ( [ repr ( dep ) for dep in dependencies ] ) ) <EOL> reset_calls ( ) <EOL> def test_on_dependency_only_fires_on_changes ( self ) : <EOL> """<STR_LIT>""" <EOL> a_ready_calls = list ( ) <EOL> a_lost_calls = list ( ) <EOL> def reset_calls ( ) : <EOL> a_ready_calls [ : ] = list ( ) <EOL> a_lost_calls [ : ] = list ( ) <EOL> def assertCalls ( ready = list ( ) , lost = list ( ) , msg = '<STR_LIT>' ) : <EOL> self . assertEquals ( sorted ( a_ready_calls ) , sorted ( ready ) , msg = msg ) <EOL> self . assertEquals ( sorted ( a_lost_calls ) , sorted ( lost ) , msg = msg ) <EOL> def add_ready_call ( dependency ) : <EOL> a_ready_calls . append ( dependency ) <EOL> def add_lost_call ( dependency , reason ) : <EOL> a_lost_calls . append ( dependency ) <EOL> a_dependency = self . dependency_manager . as_dependency ( '<STR_LIT:A>' ) <EOL> a_dependency . on_dependency_ready += add_ready_call <EOL> a_dependency . on_dependency_lost += add_lost_call <EOL> b_dependency = self . dependency_manager . as_dependency ( '<STR_LIT:B>' ) <EOL> c_dependency = self . dependency_manager . as_dependency ( '<STR_LIT:C>' ) <EOL> d_dependency = self . dependency_manager . as_dependency ( '<STR_LIT:D>' ) <EOL> e_dependency = self . dependency_manager . as_dependency ( '<STR_LIT:E>' ) <EOL> self . dependency_manager . add_dependency ( a_dependency , b_dependency ) <EOL> self . dependency_manager . add_dependency ( a_dependency , c_dependency ) <EOL> self . dependency_manager . add_dependency ( b_dependency , d_dependency ) <EOL> self . dependency_manager . add_dependency ( c_dependency , d_dependency ) <EOL> self . dependency_manager . add_dependency ( c_dependency , e_dependency ) <EOL> assertCalls ( ) <EOL> self . dependency_manager . resolve_initial_states ( ) <EOL> assertCalls ( [ b_dependency , c_dependency ] ) <EOL> reset_calls ( ) <EOL> for direct_dependency in b_dependency , c_dependency : <EOL> direct_dependency . fire_on_lost ( '<STR_LIT>' ) <EOL> assertCalls ( lost = [ direct_dependency ] ) <EOL> reset_calls ( ) <EOL> direct_dependency . fire_on_ready ( ) <EOL> assertCalls ( ready = [ direct_dependency ] ) <EOL> reset_calls ( ) <EOL> d_dependency . fire_on_lost ( '<STR_LIT>' ) <EOL> assertCalls ( lost = [ b_dependency , c_dependency ] ) <EOL> reset_calls ( ) <EOL> e_dependency . fire_on_lost ( '<STR_LIT>' ) <EOL> e_dependency . fire_on_ready ( ) <EOL> assertCalls ( ) <EOL> d_dependency . fire_on_ready ( ) <EOL> assertCalls ( ready = [ b_dependency , c_dependency ] ) <EOL> reset_calls ( ) <EOL> for dependencies in itertools . permutations ( ( b_dependency , c_dependency , d_dependency , e_dependency ) ) : <EOL> for dependency in dependencies : <EOL> if dependency . is_ready : <EOL> dependency . fire_on_lost ( '<STR_LIT>' ) <EOL> if dependency == dependencies [ <NUM_LIT:0> ] : <EOL> self . assertTrue ( len ( a_lost_calls ) >= <NUM_LIT:1> ) <EOL> for dependency in dependencies : <EOL> if not dependency . is_ready : <EOL> dependency . fire_on_ready ( ) <EOL> assertCalls ( lost = [ b_dependency , c_dependency ] , ready = [ b_dependency , c_dependency ] , msg = '<STR_LIT:U+002C>' . join ( [ repr ( dep ) for dep in dependencies ] ) ) <EOL> reset_calls ( ) <EOL> def test_rewriting_dependencies ( self ) : <EOL> ensure = self . dependency_manager . as_dependency <EOL> self . assertIdentical ( ensure ( '<STR_LIT:A>' ) , ensure ( '<STR_LIT:A>' ) ) <EOL> self . assertIsInstance ( ensure ( '<STR_LIT:A>' ) , dependencies . InstanceDependency ) <EOL> self . assertIsInstance ( ensure ( dict ( provider = '<STR_LIT>' ) ) , dependencies . ResourceDependency ) <EOL> self . assertNotEquals ( ensure ( dict ( provider = '<STR_LIT:A>' ) ) , ensure ( dict ( provider = '<STR_LIT:A>' ) ) ) <EOL> self . assertRaises ( exceptions . ResourceError , ensure , dict ( foo = '<STR_LIT:bar>' ) ) <EOL> dependency = dependencies . Dependency ( ) <EOL> self . assertIdentical ( dependency , ensure ( dependency ) ) <EOL> def test_cyclic_raises ( self ) : <EOL> self . dependency_manager . add_dependency ( '<STR_LIT:A>' , '<STR_LIT:B>' ) <EOL> self . dependency_manager . add_dependency ( '<STR_LIT:B>' , '<STR_LIT:A>' ) <EOL> self . assertRaises ( exceptions . CircularDependencyGraph , self . dependency_manager . resolve_initial_states ) <EOL> self . dependency_manager . remove_dependency ( '<STR_LIT:B>' , '<STR_LIT:A>' ) <EOL> self . dependency_manager . add_dependency ( '<STR_LIT:B>' , '<STR_LIT:B>' ) <EOL> self . assertRaises ( exceptions . CircularDependencyGraph , self . dependency_manager . resolve_initial_states ) <EOL> self . dependency_manager . remove_dependency ( '<STR_LIT:B>' , '<STR_LIT:B>' ) <EOL> self . dependency_manager . add_dependency ( '<STR_LIT:A>' , '<STR_LIT:A>' ) <EOL> self . assertRaises ( exceptions . CircularDependencyGraph , self . dependency_manager . resolve_initial_states ) <EOL> self . dependency_manager . remove_dependency ( '<STR_LIT:A>' , '<STR_LIT:A>' ) <EOL> self . dependency_manager . add_dependency ( '<STR_LIT:B>' , '<STR_LIT:C>' ) <EOL> self . dependency_manager . add_dependency ( '<STR_LIT:C>' , '<STR_LIT:A>' ) <EOL> self . assertRaises ( exceptions . CircularDependencyGraph , self . dependency_manager . resolve_initial_states ) <EOL> self . dependency_manager . remove_dependency ( '<STR_LIT:C>' , '<STR_LIT:A>' ) <EOL> self . dependency_manager . add_dependency ( '<STR_LIT:C>' , '<STR_LIT:D>' ) <EOL> self . dependency_manager . add_dependency ( '<STR_LIT:D>' , '<STR_LIT:C>' ) <EOL> self . assertRaises ( exceptions . CircularDependencyGraph , self . dependency_manager . resolve_initial_states ) <EOL> self . dependency_manager . remove_dependency ( '<STR_LIT:D>' , '<STR_LIT:C>' ) <EOL> self . dependency_manager . add_dependency ( '<STR_LIT:D>' , '<STR_LIT:B>' ) <EOL> self . assertRaises ( exceptions . CircularDependencyGraph , self . dependency_manager . resolve_initial_states ) <EOL> self . dependency_manager . remove_dependency ( '<STR_LIT:D>' , '<STR_LIT:B>' ) <EOL> self . dependency_manager . add_dependency ( '<STR_LIT:B>' , '<STR_LIT:A>' ) <EOL> self . dependency_manager . add_dependency ( '<STR_LIT:D>' , '<STR_LIT:A>' ) <EOL> self . assertRaises ( exceptions . CircularDependencyGraph , self . dependency_manager . resolve_initial_states ) <EOL> def test_adding_existing_dependency_raises ( self ) : <EOL> self . dependency_manager . add_dependency ( '<STR_LIT:A>' , '<STR_LIT:B>' ) <EOL> self . assertRaises ( exceptions . AlreadyExistingDependency , self . dependency_manager . add_dependency , '<STR_LIT:A>' , '<STR_LIT:B>' ) <EOL> def test_dependencies_updated ( self ) : <EOL> """<STR_LIT>""" <EOL> is_ready = dict ( ) <EOL> for char in '<STR_LIT:A>' , '<STR_LIT:B>' , '<STR_LIT:C>' , '<STR_LIT:D>' , '<STR_LIT:E>' : <EOL> self . dependency_manager . as_dependency ( char ) . on_ready += lambda dep , char = char : is_ready . __setitem__ ( char , True ) <EOL> self . dependency_manager . as_dependency ( char ) . on_lost += lambda dep , reason , char = char : is_ready . __setitem__ ( char , False ) <EOL> self . dependency_manager . add_dependency ( '<STR_LIT:A>' , '<STR_LIT:B>' ) <EOL> self . dependency_manager . add_dependency ( '<STR_LIT:A>' , '<STR_LIT:C>' ) <EOL> self . dependency_manager . add_dependency ( '<STR_LIT:C>' , '<STR_LIT:D>' ) <EOL> self . assertEquals ( is_ready , dict ( ) ) <EOL> self . dependency_manager . resolve_initial_states ( ) <EOL> self . assertEquals ( is_ready , dict ( A = True , B = True , C = True , D = True ) ) <EOL> self . dependency_manager . add_dependency ( '<STR_LIT:C>' , '<STR_LIT:E>' ) <EOL> self . assertEquals ( is_ready , dict ( A = True , B = True , C = True , D = True ) ) <EOL> self . dependency_manager . resolve_initial_states ( ) <EOL> self . assertEquals ( is_ready , dict ( A = True , B = True , C = True , D = True , E = True ) ) <EOL> e_dependency = self . dependency_manager . as_dependency ( '<STR_LIT:E>' ) <EOL> e_dependency . fire_on_lost ( '<STR_LIT>' ) <EOL> self . assertEquals ( is_ready , dict ( A = False , B = True , C = False , D = True , E = False ) ) <EOL> self . dependency_manager . remove_dependency ( '<STR_LIT:C>' , '<STR_LIT:E>' ) <EOL> self . assertEquals ( is_ready , dict ( A = True , B = True , C = True , D = True , E = False ) ) <EOL> e_dependency . fire_on_ready ( ) <EOL> self . assertEquals ( is_ready , dict ( A = True , B = True , C = True , D = True , E = True ) ) <EOL> class DependencyMapTest ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . runtime_environment = processing . RuntimeEnvironment ( ) <EOL> self . runtime_environment . configure ( ) <EOL> self . resource_manager = self . runtime_environment . resource_manager <EOL> self . dependency_manager = self . runtime_environment . dependency_manager <EOL> def _create_map ( self , consumer = None ) : <EOL> if not consumer : <EOL> consumer = self <EOL> return dependencies . DependencyMap ( consumer , manager = self . dependency_manager ) <EOL> def _create_provider ( self , add_consumer_callback , * resource_paths ) : <EOL> class Provider : <EOL> def add_consumer ( self , resource_dependency ) : <EOL> add_consumer_callback ( resource_dependency ) <EOL> p = Provider ( ) <EOL> for resource_path in resource_paths : <EOL> self . resource_manager . register ( resource_path , p ) <EOL> def test_simple_dependency_map ( self ) : <EOL> self . _create_provider ( lambda dep : dep . on_resource_ready ( '<STR_LIT>' ) , '<STR_LIT:foo>' ) <EOL> map = self . _create_map ( ) <EOL> map [ '<STR_LIT:foo>' ] = dependencies . ResourceDependency ( provider = '<STR_LIT:foo>' ) <EOL> map [ '<STR_LIT:bar>' ] = '<STR_LIT>' <EOL> self . dependency_manager . resolve_initial_states ( ) <EOL> self . assertEquals ( map [ '<STR_LIT:foo>' ] , '<STR_LIT>' ) <EOL> self . assertEquals ( map [ '<STR_LIT:bar>' ] , '<STR_LIT>' ) <EOL> self . assertRaises ( exceptions . KeyAlreadyExists , map . add_dependency , '<STR_LIT:foo>' , '<STR_LIT>' ) <EOL> map . remove_dependency ( '<STR_LIT:foo>' ) <EOL> map . add_dependency ( '<STR_LIT:foo>' , dict ( provider = '<STR_LIT:foo>' ) ) <EOL> self . assertRaises ( exceptions . UnsatisfiedDependencyError , map . __getitem__ , '<STR_LIT:foo>' ) <EOL> self . dependency_manager . resolve_initial_states ( ) <EOL> self . assertEquals ( map [ '<STR_LIT:foo>' ] , '<STR_LIT>' ) <EOL> def test_get_dependency ( self ) : <EOL> self . _create_provider ( lambda dep : dep . on_resource_ready ( '<STR_LIT>' ) , '<STR_LIT:foo>' ) <EOL> map = self . _create_map ( ) <EOL> map [ '<STR_LIT:foo>' ] = foo_dependency = dependencies . ResourceDependency ( provider = '<STR_LIT:foo>' ) <EOL> map [ '<STR_LIT:bar>' ] = '<STR_LIT>' <EOL> self . dependency_manager . resolve_initial_states ( ) <EOL> self . assertEqual ( map . get_dependency ( '<STR_LIT:foo>' ) , foo_dependency ) <EOL> self . assertIsInstance ( map . get_dependency ( '<STR_LIT:bar>' ) , dependencies . InstanceDependency ) <EOL> def test_resource_changes_updates_the_map ( self ) : <EOL> resource_dependencies = list ( ) <EOL> self . _create_provider ( lambda dep : resource_dependencies . append ( dep ) , '<STR_LIT:foo>' ) <EOL> foo_resource = dependencies . ResourceDependency ( provider = '<STR_LIT:foo>' ) <EOL> map = self . _create_map ( ) <EOL> map [ '<STR_LIT:foo>' ] = foo_resource <EOL> self . dependency_manager . resolve_initial_states ( ) <EOL> self . assertEquals ( len ( resource_dependencies ) , <NUM_LIT:1> ) <EOL> self . assertEquals ( resource_dependencies [ <NUM_LIT:0> ] , foo_resource ) <EOL> self . assertRaises ( exceptions . UnsatisfiedDependencyError , map . __getitem__ , '<STR_LIT:foo>' ) <EOL> d = map . wait_for_resource ( '<STR_LIT:foo>' ) <EOL> self . assertEquals ( d . called , False ) <EOL> res = '<STR_LIT>' <EOL> foo_resource . on_resource_ready ( res ) <EOL> self . assertEquals ( map [ '<STR_LIT:foo>' ] , res ) <EOL> self . assertEquals ( d . called , True ) <EOL> self . assertEquals ( d . result , res ) <EOL> d = map . wait_for_resource ( '<STR_LIT:foo>' ) <EOL> self . assertEquals ( d . called , True ) <EOL> self . assertEquals ( d . result , res ) <EOL> foo_resource . on_resource_lost ( '<STR_LIT>' ) <EOL> d = map . wait_for_resource ( '<STR_LIT:foo>' ) <EOL> self . assertRaises ( exceptions . UnsatisfiedDependencyError , map . __getitem__ , '<STR_LIT:foo>' ) <EOL> self . assertEquals ( d . called , False ) <EOL> foo_resource . on_resource_ready ( res ) <EOL> self . assertEquals ( d . called , True ) <EOL> self . assertEquals ( d . result , res ) <EOL> __doctests__ = [ dependencies ] </s>
