<s> import os <EOL> import threading <EOL> import time <EOL> import uuid <EOL> from concurrent import futures <EOL> import fixtures <EOL> import mock <EOL> import testscenarios <EOL> from testtools import matchers <EOL> from testtools import testcase <EOL> import tooz . coordination <EOL> from tooz import tests <EOL> def try_to_lock_job ( name , coord , url , member_id ) : <EOL> if not coord : <EOL> coord = tooz . coordination . get_coordinator ( <EOL> url , member_id ) <EOL> coord . start ( ) <EOL> lock2 = coord . get_lock ( name ) <EOL> return lock2 . acquire ( blocking = False ) <EOL> class TestAPI ( testscenarios . TestWithScenarios , <EOL> tests . TestCaseSkipNotImplemented ) : <EOL> scenarios = [ <EOL> ( '<STR_LIT>' , { '<STR_LIT:url>' : os . getenv ( "<STR_LIT>" ) , <EOL> '<STR_LIT>' : '<STR_LIT>' } ) , <EOL> ( '<STR_LIT>' , { '<STR_LIT:url>' : '<STR_LIT>' } ) , <EOL> ( '<STR_LIT>' , { '<STR_LIT:url>' : os . getenv ( "<STR_LIT>" ) , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : True } ) , <EOL> ( '<STR_LIT>' , { '<STR_LIT:url>' : '<STR_LIT>' } ) , <EOL> ( '<STR_LIT:file>' , { '<STR_LIT:url>' : '<STR_LIT>' } ) , <EOL> ( '<STR_LIT>' , { '<STR_LIT:url>' : os . getenv ( "<STR_LIT>" ) , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : True } ) , <EOL> ( '<STR_LIT>' , { '<STR_LIT:url>' : os . getenv ( "<STR_LIT>" ) , <EOL> '<STR_LIT>' : '<STR_LIT>' } ) , <EOL> ( '<STR_LIT>' , { '<STR_LIT:url>' : os . getenv ( "<STR_LIT>" ) , <EOL> '<STR_LIT>' : '<STR_LIT>' } ) , <EOL> ( '<STR_LIT>' , { '<STR_LIT:url>' : os . getenv ( "<STR_LIT>" ) , <EOL> '<STR_LIT>' : '<STR_LIT>' } ) , <EOL> ( '<STR_LIT>' , { '<STR_LIT:url>' : os . getenv ( "<STR_LIT>" ) , <EOL> '<STR_LIT>' : '<STR_LIT>' } ) <EOL> ] <EOL> def assertRaisesAny ( self , exc_classes , callable_obj , * args , ** kwargs ) : <EOL> checkers = [ matchers . MatchesException ( exc_class ) <EOL> for exc_class in exc_classes ] <EOL> matcher = matchers . Raises ( matchers . MatchesAny ( * checkers ) ) <EOL> callable_obj = testcase . Nullary ( callable_obj , * args , ** kwargs ) <EOL> self . assertThat ( callable_obj , matcher ) <EOL> def setUp ( self ) : <EOL> super ( TestAPI , self ) . setUp ( ) <EOL> self . useFixture ( fixtures . NestedTempfile ( ) ) <EOL> if self . url is None : <EOL> self . skipTest ( "<STR_LIT>" ) <EOL> self . group_id = self . _get_random_uuid ( ) <EOL> self . member_id = self . _get_random_uuid ( ) <EOL> self . _coord = tooz . coordination . get_coordinator ( self . url , <EOL> self . member_id ) <EOL> self . _coord . start ( ) <EOL> def tearDown ( self ) : <EOL> self . _coord . stop ( ) <EOL> super ( TestAPI , self ) . tearDown ( ) <EOL> def test_connection_error ( self ) : <EOL> if not hasattr ( self , "<STR_LIT>" ) : <EOL> raise testcase . TestSkipped ( "<STR_LIT>" ) <EOL> coord = tooz . coordination . get_coordinator ( self . bad_url , <EOL> self . member_id ) <EOL> self . assertRaises ( tooz . coordination . ToozConnectionError , <EOL> coord . start ) <EOL> def test_stop_first ( self ) : <EOL> c = tooz . coordination . get_coordinator ( self . url , <EOL> self . member_id ) <EOL> self . assertRaises ( tooz . coordination . ToozError , <EOL> c . stop ) <EOL> def test_create_group ( self ) : <EOL> self . _coord . create_group ( self . group_id ) . get ( ) <EOL> all_group_ids = self . _coord . get_groups ( ) . get ( ) <EOL> self . assertTrue ( self . group_id in all_group_ids ) <EOL> def test_get_lock_release_broken ( self ) : <EOL> name = self . _get_random_uuid ( ) <EOL> memberid2 = self . _get_random_uuid ( ) <EOL> coord2 = tooz . coordination . get_coordinator ( self . url , <EOL> memberid2 ) <EOL> coord2 . start ( ) <EOL> lock1 = self . _coord . get_lock ( name ) <EOL> lock2 = coord2 . get_lock ( name ) <EOL> self . assertTrue ( lock1 . acquire ( blocking = False ) ) <EOL> self . assertFalse ( lock2 . acquire ( blocking = False ) ) <EOL> self . assertTrue ( lock2 . break_ ( ) ) <EOL> self . assertTrue ( lock2 . acquire ( blocking = False ) ) <EOL> self . assertFalse ( lock1 . release ( ) ) <EOL> memberid3 = self . _get_random_uuid ( ) <EOL> coord3 = tooz . coordination . get_coordinator ( self . url , <EOL> memberid3 ) <EOL> coord3 . start ( ) <EOL> lock3 = coord3 . get_lock ( name ) <EOL> self . assertFalse ( lock3 . acquire ( blocking = False ) ) <EOL> def test_create_group_already_exist ( self ) : <EOL> self . _coord . create_group ( self . group_id ) . get ( ) <EOL> create_group = self . _coord . create_group ( self . group_id ) <EOL> self . assertRaises ( tooz . coordination . GroupAlreadyExist , <EOL> create_group . get ) <EOL> def test_get_groups ( self ) : <EOL> groups_ids = [ self . _get_random_uuid ( ) for _ in range ( <NUM_LIT:0> , <NUM_LIT:5> ) ] <EOL> for group_id in groups_ids : <EOL> self . _coord . create_group ( group_id ) . get ( ) <EOL> created_groups = self . _coord . get_groups ( ) . get ( ) <EOL> for group_id in groups_ids : <EOL> self . assertTrue ( group_id in created_groups ) <EOL> def test_delete_group ( self ) : <EOL> self . _coord . create_group ( self . group_id ) . get ( ) <EOL> all_group_ids = self . _coord . get_groups ( ) . get ( ) <EOL> self . assertTrue ( self . group_id in all_group_ids ) <EOL> self . _coord . delete_group ( self . group_id ) . get ( ) <EOL> all_group_ids = self . _coord . get_groups ( ) . get ( ) <EOL> self . assertFalse ( self . group_id in all_group_ids ) <EOL> join_group = self . _coord . join_group ( self . group_id ) <EOL> self . assertRaises ( tooz . coordination . GroupNotCreated , <EOL> join_group . get ) <EOL> def test_delete_group_non_existent ( self ) : <EOL> delete = self . _coord . delete_group ( self . group_id ) <EOL> self . assertRaises ( tooz . coordination . GroupNotCreated , <EOL> delete . get ) <EOL> def test_delete_group_non_empty ( self ) : <EOL> self . _coord . create_group ( self . group_id ) . get ( ) <EOL> self . _coord . join_group ( self . group_id ) . get ( ) <EOL> delete = self . _coord . delete_group ( self . group_id ) <EOL> self . assertRaises ( tooz . coordination . GroupNotEmpty , <EOL> delete . get ) <EOL> self . _coord . leave_group ( self . group_id ) <EOL> self . _coord . delete_group ( self . group_id ) . get ( ) <EOL> def test_join_group ( self ) : <EOL> self . _coord . create_group ( self . group_id ) . get ( ) <EOL> self . _coord . join_group ( self . group_id ) . get ( ) <EOL> member_list = self . _coord . get_members ( self . group_id ) . get ( ) <EOL> self . assertTrue ( self . member_id in member_list ) <EOL> def test_join_nonexistent_group ( self ) : <EOL> join_group = self . _coord . join_group ( self . group_id ) <EOL> self . assertRaises ( tooz . coordination . GroupNotCreated , <EOL> join_group . get ) <EOL> def test_join_group_with_member_id_already_exists ( self ) : <EOL> self . _coord . create_group ( self . group_id ) . get ( ) <EOL> self . _coord . join_group ( self . group_id ) . get ( ) <EOL> client = tooz . coordination . get_coordinator ( self . url , <EOL> self . member_id ) <EOL> client . start ( ) <EOL> join_group = client . join_group ( self . group_id ) <EOL> self . assertRaises ( tooz . coordination . MemberAlreadyExist , <EOL> join_group . get ) <EOL> def test_leave_group ( self ) : <EOL> self . _coord . create_group ( self . group_id ) . get ( ) <EOL> all_group_ids = self . _coord . get_groups ( ) . get ( ) <EOL> self . assertTrue ( self . group_id in all_group_ids ) <EOL> self . _coord . join_group ( self . group_id ) . get ( ) <EOL> member_list = self . _coord . get_members ( self . group_id ) . get ( ) <EOL> self . assertTrue ( self . member_id in member_list ) <EOL> member_ids = self . _coord . get_members ( self . group_id ) . get ( ) <EOL> self . assertTrue ( self . member_id in member_ids ) <EOL> self . _coord . leave_group ( self . group_id ) . get ( ) <EOL> new_member_objects = self . _coord . get_members ( self . group_id ) . get ( ) <EOL> new_member_list = [ member . member_id for member in new_member_objects ] <EOL> self . assertTrue ( self . member_id not in new_member_list ) <EOL> def test_leave_nonexistent_group ( self ) : <EOL> all_group_ids = self . _coord . get_groups ( ) . get ( ) <EOL> self . assertTrue ( self . group_id not in all_group_ids ) <EOL> leave_group = self . _coord . leave_group ( self . group_id ) <EOL> self . assertRaisesAny ( [ tooz . coordination . MemberNotJoined , <EOL> tooz . coordination . GroupNotCreated ] , <EOL> leave_group . get ) <EOL> def test_leave_group_not_joined_by_member ( self ) : <EOL> self . _coord . create_group ( self . group_id ) . get ( ) <EOL> all_group_ids = self . _coord . get_groups ( ) . get ( ) <EOL> self . assertTrue ( self . group_id in all_group_ids ) <EOL> leave_group = self . _coord . leave_group ( self . group_id ) <EOL> self . assertRaises ( tooz . coordination . MemberNotJoined , <EOL> leave_group . get ) <EOL> def test_get_lock_twice_locked_one_released_two ( self ) : <EOL> name = self . _get_random_uuid ( ) <EOL> lock1 = self . _coord . get_lock ( name ) <EOL> lock2 = self . _coord . get_lock ( name ) <EOL> self . assertTrue ( lock1 . acquire ( ) ) <EOL> self . assertFalse ( lock2 . acquire ( blocking = False ) ) <EOL> self . assertFalse ( lock2 . release ( ) ) <EOL> self . assertTrue ( lock1 . release ( ) ) <EOL> self . assertFalse ( lock2 . release ( ) ) <EOL> def test_get_members ( self ) : <EOL> group_id_test2 = self . _get_random_uuid ( ) <EOL> member_id_test2 = self . _get_random_uuid ( ) <EOL> client2 = tooz . coordination . get_coordinator ( self . url , <EOL> member_id_test2 ) <EOL> client2 . start ( ) <EOL> self . _coord . create_group ( group_id_test2 ) . get ( ) <EOL> self . _coord . join_group ( group_id_test2 ) . get ( ) <EOL> client2 . join_group ( group_id_test2 ) . get ( ) <EOL> members_ids = self . _coord . get_members ( group_id_test2 ) . get ( ) <EOL> self . assertTrue ( self . member_id in members_ids ) <EOL> self . assertTrue ( member_id_test2 in members_ids ) <EOL> def test_get_member_capabilities ( self ) : <EOL> self . _coord . create_group ( self . group_id ) . get ( ) <EOL> self . _coord . join_group ( self . group_id , b"<STR_LIT>" ) <EOL> capa = self . _coord . get_member_capabilities ( self . group_id , <EOL> self . member_id ) . get ( ) <EOL> self . assertEqual ( capa , b"<STR_LIT>" ) <EOL> def test_get_member_capabilities_complex ( self ) : <EOL> self . _coord . create_group ( self . group_id ) . get ( ) <EOL> caps = { <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> } <EOL> self . _coord . join_group ( self . group_id , caps ) <EOL> capa = self . _coord . get_member_capabilities ( self . group_id , <EOL> self . member_id ) . get ( ) <EOL> self . assertEqual ( capa , caps ) <EOL> self . assertEqual ( capa [ '<STR_LIT:type>' ] , caps [ '<STR_LIT:type>' ] ) <EOL> def test_get_member_capabilities_nonexistent_group ( self ) : <EOL> capa = self . _coord . get_member_capabilities ( self . group_id , <EOL> self . member_id ) <EOL> self . assertRaisesAny ( [ tooz . coordination . MemberNotJoined , <EOL> tooz . coordination . GroupNotCreated ] , <EOL> capa . get ) <EOL> def test_get_member_capabilities_nonjoined_member ( self ) : <EOL> self . _coord . create_group ( self . group_id ) . get ( ) <EOL> capa = self . _coord . get_member_capabilities ( self . group_id , <EOL> self . member_id ) <EOL> self . assertRaises ( tooz . coordination . MemberNotJoined , <EOL> capa . get ) <EOL> def test_get_member_info ( self ) : <EOL> self . _coord . create_group ( self . group_id ) . get ( ) <EOL> self . _coord . join_group ( self . group_id , b"<STR_LIT>" ) <EOL> member_info = self . _coord . get_member_info ( self . group_id , <EOL> self . member_id ) . get ( ) <EOL> self . assertEqual ( member_info [ '<STR_LIT>' ] , b"<STR_LIT>" ) <EOL> def test_get_member_info_complex ( self ) : <EOL> self . _coord . create_group ( self . group_id ) . get ( ) <EOL> caps = { <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> } <EOL> member_info = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:0>' , <EOL> '<STR_LIT>' : '<STR_LIT:0>' } <EOL> self . _coord . join_group ( self . group_id , caps ) <EOL> member_info = self . _coord . get_member_info ( self . group_id , <EOL> self . member_id ) . get ( ) <EOL> self . assertEqual ( member_info [ '<STR_LIT>' ] , caps ) <EOL> def test_get_member_info_nonexistent_group ( self ) : <EOL> member_info = self . _coord . get_member_info ( self . group_id , <EOL> self . member_id ) <EOL> self . assertRaisesAny ( [ tooz . coordination . MemberNotJoined , <EOL> tooz . coordination . GroupNotCreated ] , <EOL> member_info . get ) <EOL> def test_get_member_info_nonjoined_member ( self ) : <EOL> self . _coord . create_group ( self . group_id ) . get ( ) <EOL> member_id = self . _get_random_uuid ( ) <EOL> member_info = self . _coord . get_member_info ( self . group_id , <EOL> member_id ) <EOL> self . assertRaises ( tooz . coordination . MemberNotJoined , <EOL> member_info . get ) <EOL> def test_update_capabilities ( self ) : <EOL> self . _coord . create_group ( self . group_id ) . get ( ) <EOL> self . _coord . join_group ( self . group_id , b"<STR_LIT>" ) . get ( ) <EOL> capa = self . _coord . get_member_capabilities ( self . group_id , <EOL> self . member_id ) . get ( ) <EOL> self . assertEqual ( capa , b"<STR_LIT>" ) <EOL> self . _coord . update_capabilities ( self . group_id , <EOL> b"<STR_LIT>" ) . get ( ) <EOL> capa2 = self . _coord . get_member_capabilities ( self . group_id , <EOL> self . member_id ) . get ( ) <EOL> self . assertEqual ( capa2 , b"<STR_LIT>" ) <EOL> def test_update_capabilities_with_group_id_nonexistent ( self ) : <EOL> update_cap = self . _coord . update_capabilities ( self . group_id , <EOL> b'<STR_LIT>' ) <EOL> self . assertRaisesAny ( [ tooz . coordination . MemberNotJoined , <EOL> tooz . coordination . GroupNotCreated ] , <EOL> update_cap . get ) <EOL> def test_heartbeat ( self ) : <EOL> self . _coord . heartbeat ( ) <EOL> def test_disconnect_leave_group ( self ) : <EOL> member_id_test2 = self . _get_random_uuid ( ) <EOL> client2 = tooz . coordination . get_coordinator ( self . url , <EOL> member_id_test2 ) <EOL> client2 . start ( ) <EOL> self . _coord . create_group ( self . group_id ) . get ( ) <EOL> self . _coord . join_group ( self . group_id ) . get ( ) <EOL> client2 . join_group ( self . group_id ) . get ( ) <EOL> members_ids = self . _coord . get_members ( self . group_id ) . get ( ) <EOL> self . assertTrue ( self . member_id in members_ids ) <EOL> self . assertTrue ( member_id_test2 in members_ids ) <EOL> client2 . stop ( ) <EOL> members_ids = self . _coord . get_members ( self . group_id ) . get ( ) <EOL> self . assertTrue ( self . member_id in members_ids ) <EOL> self . assertTrue ( member_id_test2 not in members_ids ) <EOL> def test_timeout ( self ) : <EOL> if not getattr ( self , "<STR_LIT>" , False ) : <EOL> self . skipTest ( "<STR_LIT>" ) <EOL> member_id_test2 = self . _get_random_uuid ( ) <EOL> client2 = tooz . coordination . get_coordinator ( self . url , <EOL> member_id_test2 ) <EOL> client2 . start ( ) <EOL> self . _coord . create_group ( self . group_id ) . get ( ) <EOL> self . _coord . join_group ( self . group_id ) . get ( ) <EOL> client2 . join_group ( self . group_id ) . get ( ) <EOL> members_ids = self . _coord . get_members ( self . group_id ) . get ( ) <EOL> self . assertTrue ( self . member_id in members_ids ) <EOL> self . assertTrue ( member_id_test2 in members_ids ) <EOL> self . _coord . watch_leave_group ( self . group_id , self . _set_event ) <EOL> time . sleep ( <NUM_LIT:3> ) <EOL> self . _coord . heartbeat ( ) <EOL> time . sleep ( <NUM_LIT:3> ) <EOL> members_ids = self . _coord . get_members ( self . group_id ) . get ( ) <EOL> while True : <EOL> if self . _coord . run_watchers ( ) : <EOL> break <EOL> self . assertTrue ( self . member_id in members_ids ) <EOL> self . assertTrue ( member_id_test2 not in members_ids ) <EOL> self . assertIsInstance ( self . event , <EOL> tooz . coordination . MemberLeftGroup ) <EOL> self . assertEqual ( member_id_test2 , <EOL> self . event . member_id ) <EOL> self . assertEqual ( self . group_id , <EOL> self . event . group_id ) <EOL> def _set_event ( self , event ) : <EOL> self . event = event <EOL> return <NUM_LIT> <EOL> def test_watch_group_join ( self ) : <EOL> member_id_test2 = self . _get_random_uuid ( ) <EOL> client2 = tooz . coordination . get_coordinator ( self . url , <EOL> member_id_test2 ) <EOL> client2 . start ( ) <EOL> self . _coord . create_group ( self . group_id ) . get ( ) <EOL> self . _coord . watch_join_group ( self . group_id , self . _set_event ) <EOL> client2 . join_group ( self . group_id ) . get ( ) <EOL> members_ids = self . _coord . get_members ( self . group_id ) . get ( ) <EOL> self . assertTrue ( member_id_test2 in members_ids ) <EOL> while True : <EOL> if self . _coord . run_watchers ( ) : <EOL> break <EOL> self . assertIsInstance ( self . event , <EOL> tooz . coordination . MemberJoinedGroup ) <EOL> self . assertEqual ( member_id_test2 , <EOL> self . event . member_id ) <EOL> self . assertEqual ( self . group_id , <EOL> self . event . group_id ) <EOL> self . _coord . unwatch_join_group ( self . group_id , self . _set_event ) <EOL> self . event = None <EOL> client2 . leave_group ( self . group_id ) . get ( ) <EOL> client2 . join_group ( self . group_id ) . get ( ) <EOL> self . _coord . run_watchers ( ) <EOL> self . assertIsNone ( self . event ) <EOL> def test_watch_leave_group ( self ) : <EOL> member_id_test2 = self . _get_random_uuid ( ) <EOL> client2 = tooz . coordination . get_coordinator ( self . url , <EOL> member_id_test2 ) <EOL> client2 . start ( ) <EOL> self . _coord . create_group ( self . group_id ) . get ( ) <EOL> self . _coord . watch_join_group ( self . group_id , lambda children : True ) <EOL> self . _coord . watch_leave_group ( self . group_id , self . _set_event ) <EOL> client2 . join_group ( self . group_id ) . get ( ) <EOL> while True : <EOL> if self . _coord . run_watchers ( ) : <EOL> break <EOL> client2 . leave_group ( self . group_id ) . get ( ) <EOL> while True : <EOL> if self . _coord . run_watchers ( ) : <EOL> break <EOL> self . assertIsInstance ( self . event , <EOL> tooz . coordination . MemberLeftGroup ) <EOL> self . assertEqual ( member_id_test2 , <EOL> self . event . member_id ) <EOL> self . assertEqual ( self . group_id , <EOL> self . event . group_id ) <EOL> self . _coord . unwatch_leave_group ( self . group_id , self . _set_event ) <EOL> self . event = None <EOL> client2 . join_group ( self . group_id ) . get ( ) <EOL> client2 . leave_group ( self . group_id ) . get ( ) <EOL> self . _coord . run_watchers ( ) <EOL> self . assertIsNone ( self . event ) <EOL> def test_watch_join_group_disappear ( self ) : <EOL> if not hasattr ( self . _coord , '<STR_LIT>' ) : <EOL> self . skipTest ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> self . _coord . create_group ( self . group_id ) . get ( ) <EOL> self . _coord . watch_join_group ( self . group_id , self . _set_event ) <EOL> self . _coord . watch_leave_group ( self . group_id , self . _set_event ) <EOL> member_id_test2 = self . _get_random_uuid ( ) <EOL> client2 = tooz . coordination . get_coordinator ( self . url , <EOL> member_id_test2 ) <EOL> client2 . start ( ) <EOL> client2 . join_group ( self . group_id ) . get ( ) <EOL> while True : <EOL> if self . _coord . run_watchers ( ) : <EOL> break <EOL> self . assertIsInstance ( self . event , <EOL> tooz . coordination . MemberJoinedGroup ) <EOL> self . event = None <EOL> self . _coord . _destroy_group ( self . group_id ) <EOL> while True : <EOL> if self . _coord . run_watchers ( ) : <EOL> break <EOL> self . assertIsInstance ( self . event , <EOL> tooz . coordination . MemberLeftGroup ) <EOL> def test_watch_join_group_non_existent ( self ) : <EOL> self . assertRaises ( tooz . coordination . GroupNotCreated , <EOL> self . _coord . watch_join_group , <EOL> self . group_id , <EOL> lambda : None ) <EOL> self . assertEqual ( <NUM_LIT:0> , len ( self . _coord . _hooks_join_group [ self . group_id ] ) ) <EOL> def test_watch_join_group_booted_out ( self ) : <EOL> self . _coord . create_group ( self . group_id ) . get ( ) <EOL> self . _coord . join_group ( self . group_id ) . get ( ) <EOL> self . _coord . watch_join_group ( self . group_id , self . _set_event ) <EOL> self . _coord . watch_leave_group ( self . group_id , self . _set_event ) <EOL> member_id_test2 = self . _get_random_uuid ( ) <EOL> client2 = tooz . coordination . get_coordinator ( self . url , <EOL> member_id_test2 ) <EOL> client2 . start ( ) <EOL> client2 . join_group ( self . group_id ) . get ( ) <EOL> while True : <EOL> if self . _coord . run_watchers ( ) : <EOL> break <EOL> client3 = tooz . coordination . get_coordinator ( self . url , self . member_id ) <EOL> client3 . start ( ) <EOL> client3 . leave_group ( self . group_id ) . get ( ) <EOL> if hasattr ( self . _coord , '<STR_LIT>' ) : <EOL> self . assertIn ( self . group_id , self . _coord . _joined_groups ) <EOL> self . _coord . run_watchers ( ) <EOL> self . assertNotIn ( self . group_id , self . _coord . _joined_groups ) <EOL> def test_watch_leave_group_non_existent ( self ) : <EOL> self . assertRaises ( tooz . coordination . GroupNotCreated , <EOL> self . _coord . watch_leave_group , <EOL> self . group_id , <EOL> lambda : None ) <EOL> self . assertEqual ( <NUM_LIT:0> , len ( self . _coord . _hooks_leave_group [ self . group_id ] ) ) <EOL> def test_run_for_election ( self ) : <EOL> self . _coord . create_group ( self . group_id ) . get ( ) <EOL> self . _coord . watch_elected_as_leader ( self . group_id , self . _set_event ) <EOL> self . _coord . run_watchers ( ) <EOL> self . assertIsInstance ( self . event , <EOL> tooz . coordination . LeaderElected ) <EOL> self . assertEqual ( self . member_id , <EOL> self . event . member_id ) <EOL> self . assertEqual ( self . group_id , <EOL> self . event . group_id ) <EOL> def test_run_for_election_multiple_clients ( self ) : <EOL> self . _coord . create_group ( self . group_id ) . get ( ) <EOL> self . _coord . watch_elected_as_leader ( self . group_id , self . _set_event ) <EOL> self . _coord . run_watchers ( ) <EOL> member_id_test2 = self . _get_random_uuid ( ) <EOL> client2 = tooz . coordination . get_coordinator ( self . url , <EOL> member_id_test2 ) <EOL> client2 . start ( ) <EOL> client2 . watch_elected_as_leader ( self . group_id , self . _set_event ) <EOL> client2 . run_watchers ( ) <EOL> self . assertIsInstance ( self . event , <EOL> tooz . coordination . LeaderElected ) <EOL> self . assertEqual ( self . member_id , <EOL> self . event . member_id ) <EOL> self . assertEqual ( self . group_id , <EOL> self . event . group_id ) <EOL> self . assertEqual ( self . _coord . get_leader ( self . group_id ) . get ( ) , <EOL> self . member_id ) <EOL> self . event = None <EOL> self . _coord . stop ( ) <EOL> client2 . run_watchers ( ) <EOL> self . assertIsInstance ( self . event , <EOL> tooz . coordination . LeaderElected ) <EOL> self . assertEqual ( member_id_test2 , <EOL> self . event . member_id ) <EOL> self . assertEqual ( self . group_id , <EOL> self . event . group_id ) <EOL> self . assertEqual ( client2 . get_leader ( self . group_id ) . get ( ) , <EOL> member_id_test2 ) <EOL> self . _coord . start ( ) <EOL> def test_get_leader ( self ) : <EOL> self . _coord . create_group ( self . group_id ) . get ( ) <EOL> leader = self . _coord . get_leader ( self . group_id ) . get ( ) <EOL> self . assertEqual ( leader , None ) <EOL> self . _coord . join_group ( self . group_id ) . get ( ) <EOL> leader = self . _coord . get_leader ( self . group_id ) . get ( ) <EOL> self . assertEqual ( leader , None ) <EOL> self . _coord . watch_elected_as_leader ( self . group_id , self . _set_event ) <EOL> self . _coord . run_watchers ( ) <EOL> leader = self . _coord . get_leader ( self . group_id ) . get ( ) <EOL> self . assertEqual ( leader , self . member_id ) <EOL> def test_run_for_election_multiple_clients_stand_down ( self ) : <EOL> self . _coord . create_group ( self . group_id ) . get ( ) <EOL> self . _coord . watch_elected_as_leader ( self . group_id , self . _set_event ) <EOL> self . _coord . run_watchers ( ) <EOL> member_id_test2 = self . _get_random_uuid ( ) <EOL> client2 = tooz . coordination . get_coordinator ( self . url , <EOL> member_id_test2 ) <EOL> client2 . start ( ) <EOL> client2 . watch_elected_as_leader ( self . group_id , self . _set_event ) <EOL> client2 . run_watchers ( ) <EOL> self . assertIsInstance ( self . event , <EOL> tooz . coordination . LeaderElected ) <EOL> self . assertEqual ( self . member_id , <EOL> self . event . member_id ) <EOL> self . assertEqual ( self . group_id , <EOL> self . event . group_id ) <EOL> self . event = None <EOL> self . _coord . stand_down_group_leader ( self . group_id ) <EOL> client2 . run_watchers ( ) <EOL> self . assertIsInstance ( self . event , <EOL> tooz . coordination . LeaderElected ) <EOL> self . assertEqual ( member_id_test2 , <EOL> self . event . member_id ) <EOL> self . assertEqual ( self . group_id , <EOL> self . event . group_id ) <EOL> self . event = None <EOL> client2 . stand_down_group_leader ( self . group_id ) <EOL> self . _coord . run_watchers ( ) <EOL> self . assertIsInstance ( self . event , <EOL> tooz . coordination . LeaderElected ) <EOL> self . assertEqual ( self . member_id , <EOL> self . event . member_id ) <EOL> self . assertEqual ( self . group_id , <EOL> self . event . group_id ) <EOL> def test_unwatch_elected_as_leader ( self ) : <EOL> self . _coord . create_group ( self . group_id ) . get ( ) <EOL> self . _coord . watch_elected_as_leader ( self . group_id , self . _set_event ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( self . _coord . _hooks_elected_leader ) ) <EOL> self . _coord . unwatch_elected_as_leader ( self . group_id , self . _set_event ) <EOL> self . assertEqual ( <NUM_LIT:0> , len ( self . _coord . _hooks_elected_leader ) ) <EOL> def test_unwatch_elected_as_leader_callback_not_found ( self ) : <EOL> self . _coord . create_group ( self . group_id ) . get ( ) <EOL> self . assertRaises ( tooz . coordination . WatchCallbackNotFound , <EOL> self . _coord . unwatch_elected_as_leader , <EOL> self . group_id , lambda x : None ) <EOL> def test_unwatch_join_group_callback_not_found ( self ) : <EOL> self . _coord . create_group ( self . group_id ) . get ( ) <EOL> self . assertRaises ( tooz . coordination . WatchCallbackNotFound , <EOL> self . _coord . unwatch_join_group , <EOL> self . group_id , lambda x : None ) <EOL> def test_unwatch_leave_group_callback_not_found ( self ) : <EOL> self . _coord . create_group ( self . group_id ) . get ( ) <EOL> self . assertRaises ( tooz . coordination . WatchCallbackNotFound , <EOL> self . _coord . unwatch_leave_group , <EOL> self . group_id , lambda x : None ) <EOL> def test_get_lock ( self ) : <EOL> lock = self . _coord . get_lock ( self . _get_random_uuid ( ) ) <EOL> self . assertTrue ( lock . acquire ( ) ) <EOL> self . assertTrue ( lock . release ( ) ) <EOL> with lock : <EOL> pass <EOL> def test_get_lock_concurrency_locking_same_lock ( self ) : <EOL> lock = self . _coord . get_lock ( self . _get_random_uuid ( ) ) <EOL> graceful_ending = threading . Event ( ) <EOL> def thread ( ) : <EOL> self . assertTrue ( lock . acquire ( ) ) <EOL> self . assertTrue ( lock . release ( ) ) <EOL> graceful_ending . set ( ) <EOL> t = threading . Thread ( target = thread ) <EOL> t . daemon = True <EOL> with lock : <EOL> t . start ( ) <EOL> time . sleep ( <NUM_LIT> ) <EOL> t . join ( ) <EOL> graceful_ending . wait ( <NUM_LIT> ) <EOL> self . assertTrue ( graceful_ending . is_set ( ) ) <EOL> def _do_test_get_lock_concurrency_locking_two_lock ( self , executor , <EOL> use_same_coord ) : <EOL> name = self . _get_random_uuid ( ) <EOL> lock1 = self . _coord . get_lock ( name ) <EOL> with lock1 : <EOL> with executor ( max_workers = <NUM_LIT:1> ) as e : <EOL> coord = self . _coord if use_same_coord else None <EOL> f = e . submit ( try_to_lock_job , name , coord , self . url , <EOL> self . _get_random_uuid ( ) ) <EOL> self . assertFalse ( f . result ( ) ) <EOL> def test_get_lock_concurrency_locking_two_lock_process ( self ) : <EOL> self . _do_test_get_lock_concurrency_locking_two_lock ( <EOL> futures . ProcessPoolExecutor , False ) <EOL> def test_get_lock_concurrency_locking_two_lock_thread1 ( self ) : <EOL> self . _do_test_get_lock_concurrency_locking_two_lock ( <EOL> futures . ThreadPoolExecutor , False ) <EOL> def test_get_lock_concurrency_locking_two_lock_thread2 ( self ) : <EOL> self . _do_test_get_lock_concurrency_locking_two_lock ( <EOL> futures . ThreadPoolExecutor , True ) <EOL> def test_get_lock_concurrency_locking2 ( self ) : <EOL> lock1 = self . _coord . get_lock ( self . _get_random_uuid ( ) ) <EOL> lock2 = self . _coord . get_lock ( self . _get_random_uuid ( ) ) <EOL> graceful_ending = threading . Event ( ) <EOL> thread_locked = threading . Event ( ) <EOL> def thread ( ) : <EOL> with lock2 : <EOL> try : <EOL> self . assertFalse ( lock1 . acquire ( blocking = False ) ) <EOL> except tooz . NotImplemented : <EOL> pass <EOL> thread_locked . set ( ) <EOL> graceful_ending . set ( ) <EOL> t = threading . Thread ( target = thread ) <EOL> t . daemon = True <EOL> with lock1 : <EOL> t . start ( ) <EOL> thread_locked . wait ( ) <EOL> self . assertTrue ( thread_locked . is_set ( ) ) <EOL> t . join ( ) <EOL> graceful_ending . wait ( ) <EOL> self . assertTrue ( graceful_ending . is_set ( ) ) <EOL> def test_get_lock_twice_locked_twice ( self ) : <EOL> name = self . _get_random_uuid ( ) <EOL> lock1 = self . _coord . get_lock ( name ) <EOL> lock2 = self . _coord . get_lock ( name ) <EOL> with lock1 : <EOL> self . assertFalse ( lock2 . acquire ( blocking = False ) ) <EOL> def test_get_lock_context_fails ( self ) : <EOL> name = self . _get_random_uuid ( ) <EOL> lock1 = self . _coord . get_lock ( name ) <EOL> lock2 = self . _coord . get_lock ( name ) <EOL> with mock . patch . object ( lock2 , '<STR_LIT>' , return_value = False ) : <EOL> with lock1 : <EOL> self . assertRaises ( <EOL> tooz . coordination . LockAcquireFailed , <EOL> lock2 . __enter__ ) <EOL> def test_get_lock_context_check_value ( self ) : <EOL> name = self . _get_random_uuid ( ) <EOL> lock = self . _coord . get_lock ( name ) <EOL> with lock as returned_lock : <EOL> self . assertEqual ( lock , returned_lock ) <EOL> def test_lock_context_manager_acquire_no_argument ( self ) : <EOL> name = self . _get_random_uuid ( ) <EOL> lock1 = self . _coord . get_lock ( name ) <EOL> lock2 = self . _coord . get_lock ( name ) <EOL> with lock1 ( ) : <EOL> self . assertFalse ( lock2 . acquire ( blocking = False ) ) <EOL> def test_lock_context_manager_acquire_argument_return_value ( self ) : <EOL> name = self . _get_random_uuid ( ) <EOL> blocking_value = <NUM_LIT> <EOL> lock = self . _coord . get_lock ( name ) <EOL> with lock ( blocking_value ) as returned_lock : <EOL> self . assertEqual ( lock , returned_lock ) <EOL> def test_lock_context_manager_acquire_argument_release_within ( self ) : <EOL> name = self . _get_random_uuid ( ) <EOL> blocking_value = <NUM_LIT> <EOL> lock = self . _coord . get_lock ( name ) <EOL> with lock ( blocking_value ) as returned_lock : <EOL> self . assertTrue ( returned_lock . release ( ) ) <EOL> def test_lock_context_manager_acquire_argument ( self ) : <EOL> name = self . _get_random_uuid ( ) <EOL> blocking_value = <NUM_LIT> <EOL> lock = self . _coord . get_lock ( name ) <EOL> with mock . patch . object ( lock , '<STR_LIT>' , wraps = True , autospec = True ) as mock_acquire : <EOL> with lock ( blocking_value ) : <EOL> mock_acquire . assert_called_once_with ( blocking_value ) <EOL> def test_lock_context_manager_acquire_argument_timeout ( self ) : <EOL> name = self . _get_random_uuid ( ) <EOL> lock1 = self . _coord . get_lock ( name ) <EOL> lock2 = self . _coord . get_lock ( name ) <EOL> with lock1 : <EOL> try : <EOL> with lock2 ( False ) : <EOL> self . fail ( '<STR_LIT>' ) <EOL> except tooz . coordination . LockAcquireFailed : <EOL> pass <EOL> def test_get_lock_locked_twice ( self ) : <EOL> name = self . _get_random_uuid ( ) <EOL> lock = self . _coord . get_lock ( name ) <EOL> with lock : <EOL> self . assertFalse ( lock . acquire ( blocking = False ) ) <EOL> def test_get_multiple_locks_with_same_coord ( self ) : <EOL> name = self . _get_random_uuid ( ) <EOL> lock1 = self . _coord . get_lock ( name ) <EOL> lock2 = self . _coord . get_lock ( name ) <EOL> self . assertTrue ( lock1 . acquire ( ) ) <EOL> self . assertFalse ( lock2 . acquire ( blocking = False ) ) <EOL> self . assertFalse ( self . _coord . get_lock ( name ) . acquire ( blocking = False ) ) <EOL> self . assertTrue ( lock1 . release ( ) ) <EOL> def test_ensure_acquire_release_return ( self ) : <EOL> name = self . _get_random_uuid ( ) <EOL> lock1 = self . _coord . get_lock ( name ) <EOL> self . assertTrue ( lock1 . acquire ( ) ) <EOL> self . assertTrue ( lock1 . release ( ) ) <EOL> self . assertFalse ( lock1 . release ( ) ) <EOL> def test_get_lock_multiple_coords ( self ) : <EOL> member_id2 = self . _get_random_uuid ( ) <EOL> client2 = tooz . coordination . get_coordinator ( self . url , <EOL> member_id2 ) <EOL> client2 . start ( ) <EOL> lock_name = self . _get_random_uuid ( ) <EOL> lock = self . _coord . get_lock ( lock_name ) <EOL> self . assertTrue ( lock . acquire ( ) ) <EOL> lock2 = client2 . get_lock ( lock_name ) <EOL> self . assertFalse ( lock2 . acquire ( blocking = False ) ) <EOL> self . assertTrue ( lock . release ( ) ) <EOL> self . assertTrue ( lock2 . acquire ( blocking = True ) ) <EOL> self . assertTrue ( lock2 . release ( ) ) <EOL> def test_get_started_status ( self ) : <EOL> self . assertTrue ( self . _coord . is_started ) <EOL> self . _coord . stop ( ) <EOL> self . assertFalse ( self . _coord . is_started ) <EOL> self . _coord . start ( ) <EOL> def do_test_name_property ( self ) : <EOL> name = self . _get_random_uuid ( ) <EOL> lock = self . _coord . get_lock ( name ) <EOL> self . assertEqual ( name , lock . name ) <EOL> @ staticmethod <EOL> def _get_random_uuid ( ) : <EOL> return str ( uuid . uuid4 ( ) ) . encode ( '<STR_LIT:ascii>' ) <EOL> def test_acquire_twice_no_deadlock_releasing ( self ) : <EOL> name = self . _get_random_uuid ( ) <EOL> lock = self . _coord . get_lock ( name ) <EOL> self . assertTrue ( lock . acquire ( blocking = False ) ) <EOL> self . assertFalse ( lock . acquire ( blocking = False ) ) <EOL> self . assertTrue ( lock . release ( ) ) <EOL> class TestHook ( testcase . TestCase ) : <EOL> def setUp ( self ) : <EOL> super ( TestHook , self ) . setUp ( ) <EOL> self . hooks = tooz . coordination . Hooks ( ) <EOL> self . triggered = False <EOL> def _trigger ( self ) : <EOL> self . triggered = True <EOL> def test_register_hook ( self ) : <EOL> self . assertEqual ( self . hooks . run ( ) , [ ] ) <EOL> self . assertFalse ( self . triggered ) <EOL> self . hooks . append ( self . _trigger ) <EOL> self . assertEqual ( self . hooks . run ( ) , [ None ] ) <EOL> self . assertTrue ( self . triggered ) <EOL> def test_unregister_hook ( self ) : <EOL> self . hooks . append ( self . _trigger ) <EOL> self . assertEqual ( self . hooks . run ( ) , [ None ] ) <EOL> self . assertTrue ( self . triggered ) <EOL> self . triggered = False <EOL> self . hooks . remove ( self . _trigger ) <EOL> self . assertEqual ( self . hooks . run ( ) , [ ] ) <EOL> self . assertFalse ( self . triggered ) </s>
