#NEWLINE# @ defer . inlineCallbacks #NEWLINE# def test_get_named_channels( self ) : #NEWLINE# #INDENT# connection = providers . AMQPConnection ( 'test_name' , servers = [ 'tcp:host=server_1:port=5672' ] ) #NEWLINE# connection . setServiceParent ( self . service ) #NEWLINE# with patch . object ( providers . endpoints , 'clientFromString' ) as mocked_client_from_string : #NEWLINE# #INDENT# def connect( factory ) : #NEWLINE# #INDENT# protocol = factory . buildProtocol ( None ) #NEWLINE# protocol . transport = mock . Mock ( ) #NEWLINE# protocol . connectionReady ( protocol ) #NEWLINE# return protocol #NEWLINE# #UNINDENT# mocked_client_from_string . return_value . connect . side_effect = connect #NEWLINE# connection . startService ( ) #NEWLINE# self . assertTrue ( connection . ready ) #NEWLINE# protocol = connection . protocol #NEWLINE# channel_requests = list ( ) #NEWLINE# with patch . object ( protocol , 'channel' ) as mocked_channel : #NEWLINE# #INDENT# def channel( ) : #NEWLINE# #INDENT# d = defer . Deferred ( ) #NEWLINE# channel_requests . append ( d ) #NEWLINE# return d #NEWLINE# #UNINDENT# mocked_channel . side_effect = channel #NEWLINE# foo_1_d = protocol . get_named_channel ( 'foo' ) #NEWLINE# foo_2_d = protocol . get_named_channel ( 'foo' ) #NEWLINE# foo_3_d = protocol . get_named_channel ( 'foo' ) #NEWLINE# bar_d = protocol . get_named_channel ( 'bar' ) #NEWLINE# self . assertEquals ( len ( channel_requests ) , 2 ) #NEWLINE# mocked_foo = mock . Mock ( ) #NEWLINE# mocked_foo_close = event . Event ( ) #NEWLINE# mocked_foo . add_on_close_callback . side_effect = mocked_foo_close . handle #NEWLINE# mocked_bar = mock . Mock ( ) #NEWLINE# channel_requests . pop ( 0 ) . callback ( mocked_foo ) #NEWLINE# channel_requests . pop ( 0 ) . callback ( mocked_bar ) #NEWLINE# foo = ( yield defer . DeferredList ( [ foo_1_d , foo_2_d , foo_3_d ] ) ) #NEWLINE# self . assertEquals ( set ( foo ) , set ( [ ( True , mocked_foo ) ] ) ) #NEWLINE# bar = ( yield bar_d ) #NEWLINE# self . assertEquals ( bar , mocked_bar ) #NEWLINE# self . assertEquals ( len ( channel_requests ) , 0 ) #NEWLINE# another_foo = ( yield protocol . get_named_channel ( 'foo' ) ) #NEWLINE# self . assertEquals ( len ( channel_requests ) , 0 ) #NEWLINE# mocked_foo_close ( ) #NEWLINE# self . assertEquals ( len ( channel_requests ) , 0 ) #NEWLINE# new_foo = protocol . get_named_channel ( 'foo' ) #NEWLINE# self . assertEquals ( len ( channel_requests ) , 1 ) #NEWLINE# new_foo_2 = protocol . get_named_channel ( 'foo' ) #NEWLINE# test_exception = Exception ( ) #NEWLINE# channel_requests . pop ( 0 ) . callback ( failure . Failure ( test_exception ) ) #NEWLINE# for pending_foo in ( new_foo , new_foo_2 ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# ( yield pending_foo ) #NEWLINE# self . fail ( 'Expected {0!r} to be raised.' . format ( new_foo ) ) #NEWLINE# #UNINDENT# except Exception as e : #NEWLINE# #INDENT# self . assertEquals ( e , test_exception )