#NEWLINE# @ defer . inlineCallbacks #NEWLINE# def test_reconnect_if_connect_fails( self ) : #NEWLINE# #INDENT# connection = providers . AMQPConnection ( 'test_name' , servers = [ 'tcp:host=server_1:port=5672' , 'tcp:host=server_2:port=5673' ] , reconnect_interval = 0 ) #NEWLINE# connection . setServiceParent ( self . service ) #NEWLINE# endpoints = list ( ) #NEWLINE# connect_return_values = [ error . ConnectError ( 'test_error' ) , error . DNSLookupError ( 'test_error' ) , error . ConnectionDone ( 'test_error' ) , Exception ( 'test_exception' ) , defer . Deferred ( ) ] #NEWLINE# expected_errors = list ( connect_return_values [ : ( - 1 ) ] ) #NEWLINE# disconnects = list ( ) #NEWLINE# connection . on_disconnected += disconnects . append #NEWLINE# with patch . object ( connection , '_connect' ) as mocked_connect : #NEWLINE# #INDENT# def connect( endpoint ) : #NEWLINE# #INDENT# endpoints . append ( dict ( host = endpoint . _host , port = endpoint . _port ) ) #NEWLINE# return_value = connect_return_values . pop ( 0 ) #NEWLINE# if isinstance ( return_value , Exception ) : #NEWLINE# #INDENT# raise return_value #NEWLINE# #UNINDENT# return return_value #NEWLINE# #UNINDENT# mocked_connect . side_effect = connect_return_values #NEWLINE# with patch . object ( providers . logger , 'warn' ) as mocked_warn : #NEWLINE# #INDENT# connection . startService ( ) #NEWLINE# while connect_return_values : #NEWLINE# #INDENT# ( yield util . wait ( 0 ) ) #NEWLINE# #UNINDENT# self . assertEquals ( mocked_warn . call_count , len ( expected_errors ) ) #NEWLINE# #UNINDENT# server_1 = dict ( host = 'server_1' , port = 5672 ) #NEWLINE# server_2 = dict ( host = 'server_2' , port = 5673 ) #NEWLINE# self . assertEquals ( endpoints , [ server_1 , server_2 , server_1 , server_2 , server_1 ] ) #NEWLINE# errors = [ disconnect . value for disconnect in disconnects ] #NEWLINE# self . assertEquals ( errors , expected_errors ) #NEWLINE# #UNINDENT# self . assertIsInstance ( connection . _reconnecting , defer . Deferred )