<s> """<STR_LIT>""" <EOL> import sys <EOL> from mock import Mock , call <EOL> from twisted . internet import reactor <EOL> from twisted . internet . defer import inlineCallbacks , Deferred <EOL> from twisted . python import log <EOL> from twisted . trial . unittest import TestCase <EOL> import txstatsd . client <EOL> import txstatsd . metrics . metric <EOL> import txstatsd . metrics . metrics <EOL> from txstatsd . metrics . metric import Metric <EOL> from txstatsd . client import ( <EOL> StatsDClientProtocol , TwistedStatsDClient , UdpStatsDClient , <EOL> ConsistentHashingClient <EOL> ) <EOL> from txstatsd . protocol import DataQueue , TransportGateway <EOL> class FakeClient ( object ) : <EOL> def __init__ ( self , host , port ) : <EOL> self . host = host <EOL> self . port = port <EOL> self . data = [ ] <EOL> self . connect_called = False <EOL> self . disconnect_called = False <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % ( self . host , self . port ) <EOL> def write ( self , data ) : <EOL> self . data . append ( data ) <EOL> def connect ( self ) : <EOL> self . connect_called = True <EOL> def disconnect ( self ) : <EOL> self . disconnect_called = True <EOL> class TestClient ( TestCase ) : <EOL> def setUp ( self ) : <EOL> super ( TestClient , self ) . setUp ( ) <EOL> self . client = None <EOL> self . exception = None <EOL> def tearDown ( self ) : <EOL> if self . client : <EOL> self . client . transport . stopListening ( ) <EOL> super ( TestClient , self ) . tearDown ( ) <EOL> def build_protocol ( self ) : <EOL> protocol = StatsDClientProtocol ( self . client ) <EOL> reactor . listenUDP ( <NUM_LIT:0> , protocol ) <EOL> def test_twistedstatsd_write ( self ) : <EOL> self . client = TwistedStatsDClient ( '<STR_LIT:127.0.0.1>' , <NUM_LIT> ) <EOL> self . build_protocol ( ) <EOL> self . client . host_resolved ( '<STR_LIT:127.0.0.1>' ) <EOL> def ensure_bytes_sent ( bytes_sent ) : <EOL> self . assertEqual ( bytes_sent , len ( '<STR_LIT:message>' ) ) <EOL> def exercise ( callback ) : <EOL> self . client . write ( '<STR_LIT:message>' , callback = callback ) <EOL> d = Deferred ( ) <EOL> d . addCallback ( ensure_bytes_sent ) <EOL> reactor . callWhenRunning ( exercise , d . callback ) <EOL> return d <EOL> @ inlineCallbacks <EOL> def test_twistedstatsd_write_with_host_resolved ( self ) : <EOL> self . client = TwistedStatsDClient . create ( <EOL> '<STR_LIT:localhost>' , <NUM_LIT> ) <EOL> self . build_protocol ( ) <EOL> yield self . client . resolve_later <EOL> def ensure_bytes_sent ( bytes_sent ) : <EOL> self . assertEqual ( bytes_sent , len ( '<STR_LIT:message>' ) ) <EOL> self . assertEqual ( self . client . host , '<STR_LIT:127.0.0.1>' ) <EOL> def exercise ( callback ) : <EOL> self . client . write ( '<STR_LIT:message>' , callback = callback ) <EOL> d = Deferred ( ) <EOL> d . addCallback ( ensure_bytes_sent ) <EOL> reactor . callWhenRunning ( exercise , d . callback ) <EOL> yield d <EOL> @ inlineCallbacks <EOL> def test_twistedstatsd_with_malformed_address_and_errback ( self ) : <EOL> exceptions_captured = [ ] <EOL> def capture_exception_raised ( failure ) : <EOL> exception = failure . getErrorMessage ( ) <EOL> self . assertTrue ( exception . startswith ( "<STR_LIT>" ) ) <EOL> exceptions_captured . append ( exception ) <EOL> self . client = TwistedStatsDClient . create ( <EOL> '<STR_LIT>' , <NUM_LIT:1> , <EOL> resolver_errback = capture_exception_raised ) <EOL> self . build_protocol ( ) <EOL> yield self . client . resolve_later <EOL> self . assertEqual ( len ( exceptions_captured ) , <NUM_LIT:1> ) <EOL> @ inlineCallbacks <EOL> def test_twistedstatsd_with_malformed_address_and_no_errback ( self ) : <EOL> exceptions_captured = [ ] <EOL> def capture_exception_raised ( failure ) : <EOL> exception = failure . getErrorMessage ( ) <EOL> self . assertTrue ( exception . startswith ( "<STR_LIT>" ) ) <EOL> exceptions_captured . append ( exception ) <EOL> self . patch ( log , "<STR_LIT>" , capture_exception_raised ) <EOL> self . client = TwistedStatsDClient . create ( <EOL> '<STR_LIT>' , <NUM_LIT:1> ) <EOL> self . build_protocol ( ) <EOL> yield self . client . resolve_later <EOL> self . assertEqual ( len ( exceptions_captured ) , <NUM_LIT:1> ) <EOL> def test_udpstatsd_wellformed_address ( self ) : <EOL> client = UdpStatsDClient ( '<STR_LIT:localhost>' , <NUM_LIT> ) <EOL> self . assertEqual ( client . host , '<STR_LIT:127.0.0.1>' ) <EOL> client = UdpStatsDClient ( None , None ) <EOL> self . assertEqual ( client . host , None ) <EOL> def test_udpstatsd_malformed_address ( self ) : <EOL> self . assertRaises ( ValueError , <EOL> UdpStatsDClient , '<STR_LIT:localhost>' , - <NUM_LIT:1> ) <EOL> self . assertRaises ( ValueError , <EOL> UdpStatsDClient , '<STR_LIT:localhost>' , '<STR_LIT>' ) <EOL> self . assertRaises ( ValueError , <EOL> UdpStatsDClient , <NUM_LIT:0> , <NUM_LIT> ) <EOL> def test_udpstatsd_socket_nonblocking ( self ) : <EOL> client = UdpStatsDClient ( '<STR_LIT:localhost>' , <NUM_LIT> ) <EOL> client . connect ( ) <EOL> self . assertEqual ( client . socket . gettimeout ( ) , <NUM_LIT:0.0> ) <EOL> def test_udp_client_can_be_imported_without_twisted ( self ) : <EOL> """<STR_LIT>""" <EOL> unloaded = [ ( name , mod ) for ( name , mod ) in sys . modules . items ( ) <EOL> if '<STR_LIT>' in name ] <EOL> def restore_modules ( ) : <EOL> for name , mod in unloaded : <EOL> sys . modules [ name ] = mod <EOL> reload ( txstatsd . client ) <EOL> reload ( txstatsd . metrics . metrics ) <EOL> reload ( txstatsd . metrics . metric ) <EOL> self . addCleanup ( restore_modules ) <EOL> for name , mod in unloaded : <EOL> sys . modules [ name ] = None <EOL> reload ( txstatsd . client ) <EOL> reload ( txstatsd . metrics . metrics ) <EOL> reload ( txstatsd . metrics . metric ) <EOL> for mod in sys . modules : <EOL> if '<STR_LIT>' in mod : <EOL> self . assertTrue ( sys . modules [ mod ] is None ) <EOL> def test_starts_with_data_queue ( self ) : <EOL> """<STR_LIT>""" <EOL> self . client = TwistedStatsDClient ( '<STR_LIT:127.0.0.1>' , <NUM_LIT> ) <EOL> self . build_protocol ( ) <EOL> self . assertIsInstance ( self . client . data_queue , DataQueue ) <EOL> def test_starts_with_transport_gateway_if_ip ( self ) : <EOL> """<STR_LIT>""" <EOL> self . client = TwistedStatsDClient ( '<STR_LIT:127.0.0.1>' , <NUM_LIT> ) <EOL> self . build_protocol ( ) <EOL> self . assertTrue ( self . client . transport_gateway is not None ) <EOL> def test_starts_without_transport_gateway_if_not_ip ( self ) : <EOL> """<STR_LIT>""" <EOL> self . client = TwistedStatsDClient ( '<STR_LIT:localhost>' , <NUM_LIT> ) <EOL> self . build_protocol ( ) <EOL> self . assertTrue ( self . client . transport_gateway is None ) <EOL> def test_passes_transport_to_gateway ( self ) : <EOL> """<STR_LIT>""" <EOL> self . client = TwistedStatsDClient ( '<STR_LIT:127.0.0.1>' , <NUM_LIT> ) <EOL> self . build_protocol ( ) <EOL> self . client . host_resolved ( '<STR_LIT:127.0.0.1>' ) <EOL> self . assertEqual ( self . client . transport_gateway . transport , <EOL> self . client . transport ) <EOL> def test_passes_reactor_to_gateway ( self ) : <EOL> """<STR_LIT>""" <EOL> self . client = TwistedStatsDClient ( '<STR_LIT:127.0.0.1>' , <NUM_LIT> ) <EOL> self . build_protocol ( ) <EOL> self . client . host_resolved ( '<STR_LIT:127.0.0.1>' ) <EOL> self . assertEqual ( self . client . transport_gateway . reactor , <EOL> self . client . reactor ) <EOL> def test_sets_ip_when_host_resolves ( self ) : <EOL> """<STR_LIT>""" <EOL> self . client = TwistedStatsDClient ( '<STR_LIT:localhost>' , <NUM_LIT> ) <EOL> self . build_protocol ( ) <EOL> self . assertEqual ( self . client . host , '<STR_LIT:localhost>' ) <EOL> self . client . host_resolved ( '<STR_LIT:127.0.0.1>' ) <EOL> self . assertEqual ( self . client . host , '<STR_LIT:127.0.0.1>' ) <EOL> def test_sets_transport_gateway_when_host_resolves ( self ) : <EOL> """<STR_LIT>""" <EOL> self . client = TwistedStatsDClient ( '<STR_LIT:localhost>' , <NUM_LIT> ) <EOL> self . build_protocol ( ) <EOL> self . client . transport_gateway = None <EOL> self . client . host_resolved ( '<STR_LIT:127.0.0.1>' ) <EOL> self . assertIsInstance ( self . client . transport_gateway , TransportGateway ) <EOL> def test_calls_connect_callback_when_host_resolves ( self ) : <EOL> """<STR_LIT>""" <EOL> self . client = TwistedStatsDClient ( '<STR_LIT:localhost>' , <NUM_LIT> ) <EOL> self . build_protocol ( ) <EOL> self . client . connect_callback = Mock ( ) <EOL> self . client . host_resolved ( '<STR_LIT:127.0.0.1>' ) <EOL> self . assertTrue ( self . client . connect_callback . called ) <EOL> self . client . connect_callback . assert_called_once_with ( ) <EOL> def test_sends_messages_to_gateway_after_host_resolves ( self ) : <EOL> """<STR_LIT>""" <EOL> self . client = TwistedStatsDClient ( '<STR_LIT:localhost>' , <NUM_LIT> ) <EOL> self . build_protocol ( ) <EOL> self . client . host_resolved ( '<STR_LIT:127.0.0.1>' ) <EOL> message = '<STR_LIT>' <EOL> bytes_sent = len ( message ) <EOL> self . client . data_queue = Mock ( spec = DataQueue ) <EOL> self . client . transport_gateway = Mock ( spec = TransportGateway ) <EOL> callback = Mock ( ) <EOL> self . client . transport_gateway . write . return_value = bytes_sent <EOL> self . assertEqual ( self . client . write ( message , callback ) , bytes_sent ) <EOL> self . client . transport_gateway . write . assert_called_once_with ( <EOL> message , callback ) <EOL> def test_sends_messages_to_queue_before_host_resolves ( self ) : <EOL> """<STR_LIT>""" <EOL> self . client = TwistedStatsDClient ( '<STR_LIT:localhost>' , <NUM_LIT> ) <EOL> self . build_protocol ( ) <EOL> message = '<STR_LIT>' <EOL> self . client . data_queue = Mock ( spec = DataQueue ) <EOL> callback = Mock ( ) <EOL> self . client . data_queue . write . return_value = None <EOL> result = self . client . write ( message , callback ) <EOL> self . client . data_queue . write . assert_called_once_with ( message , callback ) <EOL> self . assertEqual ( result , None ) <EOL> def test_flushes_queued_messages_to_the_gateway_when_host_resolves ( self ) : <EOL> """<STR_LIT>""" <EOL> self . client = TwistedStatsDClient ( '<STR_LIT:localhost>' , <NUM_LIT> ) <EOL> self . build_protocol ( ) <EOL> self . client . data_queue . write ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . client . data_queue . write ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . client . data_queue . write ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> mock_gateway_write = Mock ( ) <EOL> self . patch ( TransportGateway , '<STR_LIT>' , mock_gateway_write ) <EOL> self . client . host_resolved ( '<STR_LIT:127.0.0.1>' ) <EOL> self . assertTrue ( mock_gateway_write . call_count , <NUM_LIT:3> ) <EOL> expected = [ call ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> call ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> call ( '<STR_LIT>' , '<STR_LIT>' ) ] <EOL> self . assertEqual ( mock_gateway_write . call_args_list , expected ) <EOL> def test_sets_client_transport_when_connected ( self ) : <EOL> """<STR_LIT>""" <EOL> self . client = TwistedStatsDClient ( '<STR_LIT:localhost>' , <NUM_LIT> ) <EOL> transport = DummyTransport ( ) <EOL> self . client . connect ( transport ) <EOL> self . assertEqual ( self . client . transport , transport ) <EOL> def test_sets_gateway_transport_when_connected ( self ) : <EOL> """<STR_LIT>""" <EOL> self . client = TwistedStatsDClient ( '<STR_LIT:localhost>' , <NUM_LIT> ) <EOL> self . client . host_resolved ( '<STR_LIT:127.0.0.1>' ) <EOL> transport = DummyTransport ( ) <EOL> self . client . connect ( transport ) <EOL> self . assertEqual ( self . client . transport_gateway . transport , transport ) <EOL> class DataQueueTest ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> super ( DataQueueTest , self ) . setUp ( ) <EOL> self . queue = DataQueue ( limit = <NUM_LIT:2> ) <EOL> def test_queues_messages_and_callbacks ( self ) : <EOL> """<STR_LIT>""" <EOL> self . queue . write ( data = <NUM_LIT:1> , callback = '<STR_LIT:1>' ) <EOL> self . queue . write ( data = <NUM_LIT:2> , callback = '<STR_LIT:2>' ) <EOL> self . assertEqual ( self . queue . flush ( ) , [ <EOL> ( <NUM_LIT:1> , '<STR_LIT:1>' ) , <EOL> ( <NUM_LIT:2> , '<STR_LIT:2>' ) , <EOL> ] ) <EOL> def test_flushes_the_queue ( self ) : <EOL> """<STR_LIT>""" <EOL> self . queue . write ( data = <NUM_LIT:1> , callback = '<STR_LIT:1>' ) <EOL> self . queue . write ( data = <NUM_LIT:2> , callback = '<STR_LIT:2>' ) <EOL> self . queue . flush ( ) <EOL> self . assertEqual ( self . queue . flush ( ) , [ ] ) <EOL> def test_limits_number_of_messages ( self ) : <EOL> """<STR_LIT>""" <EOL> self . queue . write ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . queue . write ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . queue . write ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertEqual ( len ( self . queue . flush ( ) ) , <NUM_LIT:2> ) <EOL> def test_discards_messages_after_limit ( self ) : <EOL> """<STR_LIT>""" <EOL> self . queue . write ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . queue . write ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . queue . write ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertEqual ( set ( self . queue . flush ( ) ) , <EOL> set ( [ ( '<STR_LIT>' , '<STR_LIT>' ) ] ) ) <EOL> def test_makes_limit_optional ( self ) : <EOL> """<STR_LIT>""" <EOL> queue = DataQueue ( ) <EOL> self . assertTrue ( queue . _limit > <NUM_LIT:0> ) <EOL> class TestConsistentHashingClient ( TestCase ) : <EOL> def test_hash_with_single_client ( self ) : <EOL> clients = [ <EOL> FakeClient ( "<STR_LIT:127.0.0.1>" , <NUM_LIT> ) , <EOL> ] <EOL> client = ConsistentHashingClient ( clients ) <EOL> bar = Metric ( client , "<STR_LIT:bar>" ) <EOL> foo = Metric ( client , "<STR_LIT:foo>" ) <EOL> dba = Metric ( client , "<STR_LIT>" ) <EOL> bar . send ( "<STR_LIT:1>" ) <EOL> foo . send ( "<STR_LIT:1>" ) <EOL> dba . send ( "<STR_LIT:1>" ) <EOL> self . assertEqual ( clients [ <NUM_LIT:0> ] . data , [ "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" ] ) <EOL> def test_hash_with_two_clients ( self ) : <EOL> clients = [ <EOL> FakeClient ( "<STR_LIT:127.0.0.1>" , <NUM_LIT> ) , <EOL> FakeClient ( "<STR_LIT:127.0.0.1>" , <NUM_LIT> ) , <EOL> ] <EOL> client = ConsistentHashingClient ( clients ) <EOL> bar = Metric ( client , "<STR_LIT:bar>" ) <EOL> foo = Metric ( client , "<STR_LIT:foo>" ) <EOL> dba = Metric ( client , "<STR_LIT>" ) <EOL> bar . send ( "<STR_LIT:1>" ) <EOL> foo . send ( "<STR_LIT:1>" ) <EOL> dba . send ( "<STR_LIT:1>" ) <EOL> self . assertEqual ( clients [ <NUM_LIT:0> ] . data , [ "<STR_LIT>" , <EOL> "<STR_LIT>" ] ) <EOL> self . assertEqual ( clients [ <NUM_LIT:1> ] . data , [ "<STR_LIT>" ] ) <EOL> def test_hash_with_three_clients ( self ) : <EOL> clients = [ <EOL> FakeClient ( "<STR_LIT:127.0.0.1>" , <NUM_LIT> ) , <EOL> FakeClient ( "<STR_LIT:127.0.0.1>" , <NUM_LIT> ) , <EOL> FakeClient ( "<STR_LIT:127.0.0.1>" , <NUM_LIT> ) , <EOL> ] <EOL> client = ConsistentHashingClient ( clients ) <EOL> bar = Metric ( client , "<STR_LIT:bar>" ) <EOL> foo = Metric ( client , "<STR_LIT:foo>" ) <EOL> dba = Metric ( client , "<STR_LIT>" ) <EOL> bar . send ( "<STR_LIT:1>" ) <EOL> foo . send ( "<STR_LIT:1>" ) <EOL> dba . send ( "<STR_LIT:1>" ) <EOL> self . assertEqual ( clients [ <NUM_LIT:0> ] . data , [ "<STR_LIT>" ] ) <EOL> self . assertEqual ( clients [ <NUM_LIT:1> ] . data , [ "<STR_LIT>" ] ) <EOL> self . assertEqual ( clients [ <NUM_LIT:2> ] . data , [ "<STR_LIT>" ] ) <EOL> def test_connect_with_two_clients ( self ) : <EOL> clients = [ <EOL> FakeClient ( "<STR_LIT:127.0.0.1>" , <NUM_LIT> ) , <EOL> FakeClient ( "<STR_LIT:127.0.0.1>" , <NUM_LIT> ) , <EOL> ] <EOL> client = ConsistentHashingClient ( clients ) <EOL> client . connect ( ) <EOL> self . assertTrue ( clients [ <NUM_LIT:0> ] . connect_called ) <EOL> self . assertTrue ( clients [ <NUM_LIT:1> ] . connect_called ) <EOL> def test_disconnect_with_two_clients ( self ) : <EOL> clients = [ <EOL> FakeClient ( "<STR_LIT:127.0.0.1>" , <NUM_LIT> ) , <EOL> FakeClient ( "<STR_LIT:127.0.0.1>" , <NUM_LIT> ) , <EOL> ] <EOL> client = ConsistentHashingClient ( clients ) <EOL> client . disconnect ( ) <EOL> self . assertTrue ( clients [ <NUM_LIT:0> ] . disconnect_called ) <EOL> self . assertTrue ( clients [ <NUM_LIT:1> ] . disconnect_called ) <EOL> class DummyTransport ( object ) : <EOL> def stopListening ( self ) : <EOL> pass </s>
