<s> import mock <EOL> import zookeeper <EOL> from mock import patch <EOL> from txzookeeper import client as txclient <EOL> from twisted . internet import defer <EOL> from piped import exceptions , processing , dependencies , util <EOL> from twisted . trial import unittest <EOL> from piped_zookeeper import providers <EOL> class TestClientProvider ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . runtime_environment = processing . RuntimeEnvironment ( ) <EOL> self . runtime_environment . configure ( ) <EOL> def test_client_provided ( self ) : <EOL> dm = self . runtime_environment . dependency_manager <EOL> cm = self . runtime_environment . configuration_manager <EOL> cm . set ( '<STR_LIT>' , <EOL> dict ( <EOL> servers = '<STR_LIT>' , <EOL> ) <EOL> ) <EOL> provider = providers . ZookeeperClientProvider ( ) <EOL> provider . configure ( self . runtime_environment ) <EOL> self . assertEquals ( provider . _client_by_name , dict ( ) ) <EOL> dependency = dm . add_dependency ( self , dict ( provider = '<STR_LIT>' ) ) <EOL> dm . resolve_initial_states ( ) <EOL> internal_client = provider . _client_by_name [ '<STR_LIT>' ] <EOL> internal_client . _create_client = lambda : mock . Mock ( ) <EOL> yield internal_client . startService ( ) <EOL> client = dependency . get_resource ( ) <EOL> self . assertIsInstance ( client , providers . PipedZookeeperClient ) <EOL> another_client_dependency = dm . add_dependency ( self , dict ( provider = '<STR_LIT>' ) ) <EOL> dm . resolve_initial_states ( ) <EOL> self . assertNotEquals ( another_client_dependency , dependency ) <EOL> self . assertIdentical ( client , another_client_dependency . get_resource ( ) ) <EOL> def test_client_created_automatically_if_events ( self ) : <EOL> cm = self . runtime_environment . configuration_manager <EOL> cm . set ( '<STR_LIT>' , <EOL> dict ( <EOL> servers = '<STR_LIT>' , <EOL> events = dict ( <EOL> starting = '<STR_LIT>' <EOL> ) <EOL> ) <EOL> ) <EOL> provider = providers . ZookeeperClientProvider ( ) <EOL> provider . configure ( self . runtime_environment ) <EOL> self . assertNotEquals ( provider . _client_by_name , dict ( ) ) <EOL> class TestClient ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . runtime_environment = processing . RuntimeEnvironment ( ) <EOL> self . runtime_environment . configure ( ) <EOL> def test_invalid_event ( self ) : <EOL> client = providers . PipedZookeeperClient ( <EOL> servers = '<STR_LIT>' , <EOL> events = dict ( foo = '<STR_LIT:bar>' ) <EOL> ) <EOL> self . assertRaises ( exceptions . ConfigurationError , client . configure , self . runtime_environment ) <EOL> def test_default_acls ( self ) : <EOL> client = providers . PipedZookeeperClient ( <EOL> servers = '<STR_LIT>' , <EOL> default_encoded_acls = [ dict ( scheme = '<STR_LIT>' , id = '<STR_LIT>' , perms = [ '<STR_LIT:all>' ] ) ] <EOL> ) <EOL> self . assertIn ( dict ( scheme = '<STR_LIT>' , id = '<STR_LIT>' , perms = <NUM_LIT> ) , client . default_acls ) <EOL> self . assertEqual ( len ( client . default_acls ) , <NUM_LIT:1> ) <EOL> def test_processors_depended_on ( self ) : <EOL> events = dict ( <EOL> zip ( providers . PipedZookeeperClient . possible_events , providers . PipedZookeeperClient . possible_events ) <EOL> ) <EOL> client = providers . PipedZookeeperClient ( <EOL> servers = '<STR_LIT>' , <EOL> events = events <EOL> ) <EOL> client . configure ( self . runtime_environment ) <EOL> dependencies = self . runtime_environment . dependency_manager . get_dependencies_of ( client . dependencies ) <EOL> dependency_by_provider_name = dict ( ) <EOL> for dependency in dependencies : <EOL> dependency_by_provider_name [ dependency . provider ] = dependency <EOL> for event in events . keys ( ) : <EOL> self . assertIn ( event , dependency_by_provider_name ) <EOL> dependency_by_provider_name . pop ( event ) <EOL> self . assertEquals ( dependency_by_provider_name , dict ( ) ) <EOL> def _create_cacheable ( self ) : <EOL> pending = list ( ) <EOL> def call ( ** kwargs ) : <EOL> d = defer . Deferred ( ) <EOL> watcher = defer . Deferred ( ) <EOL> pending . append ( ( d , kwargs , watcher ) ) <EOL> return d , watcher <EOL> return pending , call <EOL> def test_caching_two_calls_with_same_arguments ( self ) : <EOL> client = providers . PipedZookeeperClient ( <EOL> servers = '<STR_LIT>' <EOL> ) <EOL> pending , cacheable = self . _create_cacheable ( ) <EOL> cached_call = client . _cached ( cacheable ) <EOL> foo = cached_call ( foo = True ) <EOL> foo_2 = cached_call ( foo = True ) <EOL> self . assertEquals ( len ( pending ) , <NUM_LIT:1> ) <EOL> pending . pop ( ) [ <NUM_LIT:0> ] . callback ( '<STR_LIT>' ) <EOL> self . assertEquals ( foo . result , '<STR_LIT>' ) <EOL> self . assertEquals ( foo_2 . result , '<STR_LIT>' ) <EOL> foo_3 = cached_call ( foo = True ) <EOL> self . assertEquals ( len ( pending ) , <NUM_LIT:0> ) <EOL> self . assertEquals ( foo_3 . result , '<STR_LIT>' ) <EOL> def test_exceptions_should_not_be_cached ( self ) : <EOL> client = providers . PipedZookeeperClient ( <EOL> servers = '<STR_LIT>' <EOL> ) <EOL> pending , cacheable = self . _create_cacheable ( ) <EOL> cached_call = client . _cached ( cacheable ) <EOL> foo = cached_call ( foo = True ) <EOL> foo_2 = cached_call ( foo = True ) <EOL> self . assertEquals ( len ( pending ) , <NUM_LIT:1> ) <EOL> exception = Exception ( '<STR_LIT>' ) <EOL> pending . pop ( ) [ <NUM_LIT:0> ] . errback ( exception ) <EOL> foo . addCallback ( self . fail ) <EOL> foo_2 . addCallback ( self . fail ) <EOL> foo . addErrback ( lambda reason : self . assertEquals ( reason . value , exception ) ) <EOL> foo_2 . addErrback ( lambda reason : self . assertEquals ( reason . value , exception ) ) <EOL> self . assertEquals ( len ( pending ) , <NUM_LIT:0> ) <EOL> foo_3 = cached_call ( foo = True ) <EOL> self . assertEquals ( len ( pending ) , <NUM_LIT:1> ) <EOL> def test_cache_cleared_when_watch_fired ( self ) : <EOL> client = providers . PipedZookeeperClient ( <EOL> servers = '<STR_LIT>' <EOL> ) <EOL> pending , cacheable = self . _create_cacheable ( ) <EOL> cached_call = client . _cached ( cacheable ) <EOL> foo = cached_call ( foo = True ) <EOL> self . assertEquals ( len ( pending ) , <NUM_LIT:1> ) <EOL> d , kwargs , watcher = pending . pop ( ) <EOL> d . callback ( <NUM_LIT> ) <EOL> self . assertEquals ( foo . result , <NUM_LIT> ) <EOL> foo_2 = cached_call ( foo = True ) <EOL> self . assertEquals ( len ( pending ) , <NUM_LIT:0> ) <EOL> self . assertEquals ( foo_2 . result , <NUM_LIT> ) <EOL> watcher . callback ( None ) <EOL> foo_3 = cached_call ( foo = True ) <EOL> self . assertEquals ( len ( pending ) , <NUM_LIT:1> ) <EOL> def test_cache_cleared_when_client_disconnects ( self ) : <EOL> client = providers . PipedZookeeperClient ( <EOL> servers = '<STR_LIT>' <EOL> ) <EOL> pending , cacheable = self . _create_cacheable ( ) <EOL> cached_call = client . _cached ( cacheable ) <EOL> foo = cached_call ( foo = True ) <EOL> self . assertEquals ( len ( pending ) , <NUM_LIT:1> ) <EOL> d , kwargs , watcher = pending . pop ( ) <EOL> d . callback ( <NUM_LIT> ) <EOL> self . assertEquals ( foo . result , <NUM_LIT> ) <EOL> foo_2 = cached_call ( foo = True ) <EOL> self . assertEquals ( len ( pending ) , <NUM_LIT:0> ) <EOL> self . assertEquals ( foo_2 . result , <NUM_LIT> ) <EOL> client . on_disconnected ( '<STR_LIT>' ) <EOL> foo_3 = cached_call ( foo = True ) <EOL> self . assertEquals ( len ( pending ) , <NUM_LIT:1> ) <EOL> def test_events ( self ) : <EOL> events = dict ( <EOL> zip ( providers . PipedZookeeperClient . possible_events , providers . PipedZookeeperClient . possible_events ) <EOL> ) <EOL> client = providers . PipedZookeeperClient ( <EOL> servers = '<STR_LIT>' , <EOL> events = events <EOL> ) <EOL> client . configure ( self . runtime_environment ) <EOL> with patch . object ( client , '<STR_LIT>' ) as mocked_connect : <EOL> connecting = defer . Deferred ( ) <EOL> mocked_connect . return_value = connecting <EOL> with patch . object ( client , '<STR_LIT>' ) as mocked_dependencies : <EOL> client . startService ( ) <EOL> mocked_dependencies . wait_for_resource . assert_called_with ( '<STR_LIT>' ) <EOL> connecting . callback ( True ) <EOL> mocked_dependencies . wait_for_resource . assert_called_with ( '<STR_LIT>' ) <EOL> connecting_event = txclient . ClientEvent ( type = zookeeper . SESSION_EVENT , connection_state = zookeeper . CONNECTING_STATE , path = None ) <EOL> client . _watch_connection ( client , connecting_event ) <EOL> mocked_dependencies . wait_for_resource . assert_called_with ( '<STR_LIT>' ) <EOL> connected_event = txclient . ClientEvent ( type = zookeeper . SESSION_EVENT , connection_state = zookeeper . CONNECTED_STATE , path = None ) <EOL> client . _watch_connection ( client , connected_event ) <EOL> mocked_dependencies . wait_for_resource . assert_called_with ( '<STR_LIT>' ) <EOL> client . stopService ( ) <EOL> mocked_dependencies . wait_for_resource . assert_called_with ( '<STR_LIT>' ) <EOL> client . startService ( ) <EOL> mocked_dependencies . reset_mock ( ) <EOL> expired_event = txclient . ClientEvent ( type = zookeeper . SESSION_EVENT , connection_state = zookeeper . EXPIRED_SESSION_STATE , path = None ) <EOL> client . _watch_connection ( client , expired_event ) <EOL> yield util . wait ( <NUM_LIT:0> ) <EOL> fired_events = [ args [ <NUM_LIT:0> ] for args , kwargs in mocked_dependencies . wait_for_resource . call_args_list ] <EOL> self . assertEquals ( fired_events , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> with patch . object ( providers . log , '<STR_LIT>' ) as mocked_warn : <EOL> auth_event = txclient . ClientEvent ( type = zookeeper . SESSION_EVENT , connection_state = zookeeper . AUTH_FAILED_STATE , path = None ) <EOL> client . _watch_connection ( client , auth_event ) <EOL> self . assertEquals ( mocked_warn . call_count , <NUM_LIT:1> ) <EOL> def test_parse_servers ( self ) : <EOL> client = providers . PipedZookeeperClient ( servers = '<STR_LIT>' ) <EOL> self . assertEqual ( client . servers , [ '<STR_LIT:foo>' , '<STR_LIT:bar>' , '<STR_LIT>' ] ) <EOL> self . assertEqual ( client . chroot , '<STR_LIT>' ) <EOL> client = providers . PipedZookeeperClient ( servers = '<STR_LIT>' ) <EOL> self . assertEqual ( client . servers , [ '<STR_LIT:foo>' , '<STR_LIT:bar>' , '<STR_LIT>' ] ) <EOL> self . assertEqual ( client . chroot , '<STR_LIT>' ) <EOL> client = providers . PipedZookeeperClient ( servers = '<STR_LIT>' ) <EOL> self . assertEqual ( client . servers , [ '<STR_LIT:foo>' , '<STR_LIT:bar>' , '<STR_LIT>' ] ) <EOL> self . assertEqual ( client . chroot , '<STR_LIT>' ) <EOL> try : <EOL> client = providers . PipedZookeeperClient ( servers = '<STR_LIT>' ) <EOL> self . fail ( '<STR_LIT>' ) <EOL> except exceptions . ConfigurationError as ce : <EOL> self . assertIn ( ce . message , '<STR_LIT>' ) <EOL> self . assertIn ( ce . message , '<STR_LIT>' ) <EOL> self . assertIn ( ce . message , '<STR_LIT>' ) </s>
