<s> """<STR_LIT>""" <EOL> import sys <EOL> import threading <EOL> sys . path [ <NUM_LIT:0> : <NUM_LIT:0> ] = [ "<STR_LIT>" ] <EOL> from pymongo . errors import AutoReconnect , InvalidOperation <EOL> from pymongo . server_selectors import writable_server_selector <EOL> from pymongo . topology_description import TOPOLOGY_TYPE <EOL> from test import unittest , client_context , MockClientTest <EOL> from test . pymongo_mocks import MockClient <EOL> from test . utils import connected , wait_until <EOL> @ client_context . require_connection <EOL> def setUpModule ( ) : <EOL> pass <EOL> class SimpleOp ( threading . Thread ) : <EOL> def __init__ ( self , client ) : <EOL> super ( SimpleOp , self ) . __init__ ( ) <EOL> self . client = client <EOL> self . passed = False <EOL> def run ( self ) : <EOL> self . client . db . command ( '<STR_LIT>' ) <EOL> self . passed = True <EOL> def do_simple_op ( client , nthreads ) : <EOL> threads = [ SimpleOp ( client ) for _ in range ( nthreads ) ] <EOL> for t in threads : <EOL> t . start ( ) <EOL> for t in threads : <EOL> t . join ( ) <EOL> for t in threads : <EOL> assert t . passed <EOL> def writable_addresses ( topology ) : <EOL> return set ( server . description . address for server in <EOL> topology . select_servers ( writable_server_selector ) ) <EOL> class TestMongosLoadBalancing ( MockClientTest ) : <EOL> def mock_client ( self , ** kwargs ) : <EOL> mock_client = MockClient ( <EOL> standalones = [ ] , <EOL> members = [ ] , <EOL> mongoses = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> host = '<STR_LIT>' , <EOL> connect = False , <EOL> ** kwargs ) <EOL> mock_client . mock_rtts [ '<STR_LIT>' ] = <NUM_LIT> <EOL> mock_client . mock_rtts [ '<STR_LIT>' ] = <NUM_LIT> <EOL> mock_client . mock_rtts [ '<STR_LIT>' ] = <NUM_LIT> <EOL> return mock_client <EOL> def test_lazy_connect ( self ) : <EOL> nthreads = <NUM_LIT:10> <EOL> client = self . mock_client ( ) <EOL> self . assertEqual ( <NUM_LIT:0> , len ( client . nodes ) ) <EOL> do_simple_op ( client , nthreads ) <EOL> wait_until ( lambda : len ( client . nodes ) == <NUM_LIT:3> , '<STR_LIT>' ) <EOL> def test_reconnect ( self ) : <EOL> nthreads = <NUM_LIT:10> <EOL> client = connected ( self . mock_client ( ) ) <EOL> wait_until ( lambda : len ( client . nodes ) == <NUM_LIT:3> , '<STR_LIT>' ) <EOL> client . close ( ) <EOL> do_simple_op ( client , nthreads ) <EOL> wait_until ( lambda : len ( client . nodes ) == <NUM_LIT:3> , <EOL> '<STR_LIT>' ) <EOL> def test_failover ( self ) : <EOL> nthreads = <NUM_LIT:10> <EOL> client = connected ( self . mock_client ( localThresholdMS = <NUM_LIT> ) ) <EOL> wait_until ( lambda : len ( client . nodes ) == <NUM_LIT:3> , '<STR_LIT>' ) <EOL> client . kill_host ( '<STR_LIT>' ) <EOL> passed = [ ] <EOL> def f ( ) : <EOL> try : <EOL> client . db . command ( '<STR_LIT>' ) <EOL> except AutoReconnect : <EOL> client . db . command ( '<STR_LIT>' ) <EOL> passed . append ( True ) <EOL> threads = [ threading . Thread ( target = f ) for _ in range ( nthreads ) ] <EOL> for t in threads : <EOL> t . start ( ) <EOL> for t in threads : <EOL> t . join ( ) <EOL> self . assertEqual ( nthreads , len ( passed ) ) <EOL> self . assertEqual ( <NUM_LIT:2> , len ( client . nodes ) ) <EOL> def test_local_threshold ( self ) : <EOL> client = connected ( self . mock_client ( localThresholdMS = <NUM_LIT:30> ) ) <EOL> self . assertEqual ( <NUM_LIT:30> , client . local_threshold_ms ) <EOL> wait_until ( lambda : len ( client . nodes ) == <NUM_LIT:3> , '<STR_LIT>' ) <EOL> topology = client . _topology <EOL> self . assertEqual ( set ( [ ( '<STR_LIT:a>' , <NUM_LIT:1> ) , ( '<STR_LIT:b>' , <NUM_LIT:2> ) , ( '<STR_LIT:c>' , <NUM_LIT:3> ) ] ) , <EOL> writable_addresses ( topology ) ) <EOL> client . admin . command ( '<STR_LIT>' ) <EOL> client = connected ( self . mock_client ( localThresholdMS = <NUM_LIT:0> ) ) <EOL> self . assertEqual ( <NUM_LIT:0> , client . local_threshold_ms ) <EOL> client . db . command ( '<STR_LIT>' ) <EOL> client . kill_host ( '<STR_LIT>' % next ( iter ( client . nodes ) ) ) <EOL> try : <EOL> client . db . command ( '<STR_LIT>' ) <EOL> except : <EOL> pass <EOL> client . db . command ( '<STR_LIT>' ) <EOL> def test_load_balancing ( self ) : <EOL> client = connected ( self . mock_client ( ) ) <EOL> wait_until ( lambda : len ( client . nodes ) == <NUM_LIT:3> , '<STR_LIT>' ) <EOL> with self . assertRaises ( InvalidOperation ) : <EOL> client . address <EOL> topology = client . _topology <EOL> self . assertEqual ( TOPOLOGY_TYPE . Sharded , <EOL> topology . description . topology_type ) <EOL> self . assertEqual ( set ( [ ( '<STR_LIT:a>' , <NUM_LIT:1> ) , ( '<STR_LIT:b>' , <NUM_LIT:2> ) ] ) , <EOL> writable_addresses ( topology ) ) <EOL> client . mock_rtts [ '<STR_LIT>' ] = <NUM_LIT> <EOL> wait_until ( lambda : set ( [ ( '<STR_LIT:b>' , <NUM_LIT:2> ) ] ) == writable_addresses ( topology ) , <EOL> '<STR_LIT>' ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> unittest . main ( ) </s>
