<s> import time <EOL> from elasticsearch . connection_pool import ConnectionPool , RoundRobinSelector , DummyConnectionPool <EOL> from elasticsearch . exceptions import ImproperlyConfigured <EOL> from . test_cases import TestCase <EOL> class TestConnectionPool ( TestCase ) : <EOL> def test_dummy_cp_raises_exception_on_more_connections ( self ) : <EOL> self . assertRaises ( ImproperlyConfigured , DummyConnectionPool , [ ] ) <EOL> self . assertRaises ( ImproperlyConfigured , DummyConnectionPool , [ object ( ) , object ( ) ] ) <EOL> def test_raises_exception_when_no_connections_defined ( self ) : <EOL> self . assertRaises ( ImproperlyConfigured , ConnectionPool , [ ] ) <EOL> def test_default_round_robin ( self ) : <EOL> pool = ConnectionPool ( [ ( x , { } ) for x in range ( <NUM_LIT:100> ) ] ) <EOL> connections = set ( ) <EOL> for _ in range ( <NUM_LIT:100> ) : <EOL> connections . add ( pool . get_connection ( ) ) <EOL> self . assertEquals ( connections , set ( range ( <NUM_LIT:100> ) ) ) <EOL> def test_disable_shuffling ( self ) : <EOL> pool = ConnectionPool ( [ ( x , { } ) for x in range ( <NUM_LIT:100> ) ] , randomize_hosts = False ) <EOL> connections = [ ] <EOL> for _ in range ( <NUM_LIT:100> ) : <EOL> connections . append ( pool . get_connection ( ) ) <EOL> self . assertEquals ( connections , list ( range ( <NUM_LIT:100> ) ) ) <EOL> def test_selectors_have_access_to_connection_opts ( self ) : <EOL> class MySelector ( RoundRobinSelector ) : <EOL> def select ( self , connections ) : <EOL> return self . connection_opts [ super ( MySelector , self ) . select ( connections ) ] [ "<STR_LIT>" ] <EOL> pool = ConnectionPool ( [ ( x , { "<STR_LIT>" : x * x } ) for x in range ( <NUM_LIT:100> ) ] , selector_class = MySelector , randomize_hosts = False ) <EOL> connections = [ ] <EOL> for _ in range ( <NUM_LIT:100> ) : <EOL> connections . append ( pool . get_connection ( ) ) <EOL> self . assertEquals ( connections , [ x * x for x in range ( <NUM_LIT:100> ) ] ) <EOL> def test_dead_nodes_are_removed_from_active_connections ( self ) : <EOL> pool = ConnectionPool ( [ ( x , { } ) for x in range ( <NUM_LIT:100> ) ] ) <EOL> now = time . time ( ) <EOL> pool . mark_dead ( <NUM_LIT> , now = now ) <EOL> self . assertEquals ( <NUM_LIT> , len ( pool . connections ) ) <EOL> self . assertEquals ( <NUM_LIT:1> , pool . dead . qsize ( ) ) <EOL> self . assertEquals ( ( now + <NUM_LIT> , <NUM_LIT> ) , pool . dead . get ( ) ) <EOL> def test_connection_is_skipped_when_dead ( self ) : <EOL> pool = ConnectionPool ( [ ( x , { } ) for x in range ( <NUM_LIT:2> ) ] ) <EOL> pool . mark_dead ( <NUM_LIT:0> ) <EOL> self . assertEquals ( [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ] , [ pool . get_connection ( ) , pool . get_connection ( ) , pool . get_connection ( ) , ] ) <EOL> def test_connection_is_forcibly_resurrected_when_no_live_ones_are_availible ( self ) : <EOL> pool = ConnectionPool ( [ ( x , { } ) for x in range ( <NUM_LIT:2> ) ] ) <EOL> pool . dead_count [ <NUM_LIT:0> ] = <NUM_LIT:1> <EOL> pool . mark_dead ( <NUM_LIT:0> ) <EOL> pool . mark_dead ( <NUM_LIT:1> ) <EOL> self . assertEquals ( [ ] , pool . connections ) <EOL> self . assertEquals ( <NUM_LIT:1> , pool . get_connection ( ) ) <EOL> self . assertEquals ( [ <NUM_LIT:1> , ] , pool . connections ) <EOL> def test_connection_is_resurrected_after_its_timeout ( self ) : <EOL> pool = ConnectionPool ( [ ( x , { } ) for x in range ( <NUM_LIT:100> ) ] ) <EOL> now = time . time ( ) <EOL> pool . mark_dead ( <NUM_LIT> , now = now - <NUM_LIT> ) <EOL> pool . get_connection ( ) <EOL> self . assertEquals ( <NUM_LIT> , pool . connections [ - <NUM_LIT:1> ] ) <EOL> self . assertEquals ( <NUM_LIT:100> , len ( pool . connections ) ) <EOL> def test_force_resurrect_always_returns_a_connection ( self ) : <EOL> pool = ConnectionPool ( [ ( <NUM_LIT:0> , { } ) ] ) <EOL> pool . connections = [ ] <EOL> self . assertEquals ( <NUM_LIT:0> , pool . get_connection ( ) ) <EOL> self . assertEquals ( [ ] , pool . connections ) <EOL> self . assertTrue ( pool . dead . empty ( ) ) <EOL> def test_already_failed_connection_has_longer_timeout ( self ) : <EOL> pool = ConnectionPool ( [ ( x , { } ) for x in range ( <NUM_LIT:100> ) ] ) <EOL> now = time . time ( ) <EOL> pool . dead_count [ <NUM_LIT> ] = <NUM_LIT:2> <EOL> pool . mark_dead ( <NUM_LIT> , now = now ) <EOL> self . assertEquals ( <NUM_LIT:3> , pool . dead_count [ <NUM_LIT> ] ) <EOL> self . assertEquals ( ( now + <NUM_LIT:4> * <NUM_LIT> , <NUM_LIT> ) , pool . dead . get ( ) ) <EOL> def test_timeout_for_failed_connections_is_limitted ( self ) : <EOL> pool = ConnectionPool ( [ ( x , { } ) for x in range ( <NUM_LIT:100> ) ] ) <EOL> now = time . time ( ) <EOL> pool . dead_count [ <NUM_LIT> ] = <NUM_LIT> <EOL> pool . mark_dead ( <NUM_LIT> , now = now ) <EOL> self . assertEquals ( <NUM_LIT> , pool . dead_count [ <NUM_LIT> ] ) <EOL> self . assertEquals ( ( now + <NUM_LIT:32> * <NUM_LIT> , <NUM_LIT> ) , pool . dead . get ( ) ) <EOL> def test_dead_count_is_wiped_clean_for_connection_if_marked_live ( self ) : <EOL> pool = ConnectionPool ( [ ( x , { } ) for x in range ( <NUM_LIT:100> ) ] ) <EOL> now = time . time ( ) <EOL> pool . dead_count [ <NUM_LIT> ] = <NUM_LIT:2> <EOL> pool . mark_dead ( <NUM_LIT> , now = now ) <EOL> self . assertEquals ( <NUM_LIT:3> , pool . dead_count [ <NUM_LIT> ] ) <EOL> pool . mark_live ( <NUM_LIT> ) <EOL> self . assertNotIn ( <NUM_LIT> , pool . dead_count ) </s>
