<s> import collections <EOL> import mock <EOL> from rally . common import broker <EOL> from tests . unit import test <EOL> class BrokerTestCase ( test . TestCase ) : <EOL> def test__publisher ( self ) : <EOL> mock_publish = mock . MagicMock ( ) <EOL> queue = collections . deque ( ) <EOL> broker . _publisher ( mock_publish , queue ) <EOL> mock_publish . assert_called_once_with ( queue ) <EOL> def test__publisher_fails ( self ) : <EOL> mock_publish = mock . MagicMock ( side_effect = Exception ( ) ) <EOL> queue = collections . deque ( ) <EOL> broker . _publisher ( mock_publish , queue ) <EOL> def test__consumer ( self ) : <EOL> queue = collections . deque ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> mock_consume = mock . MagicMock ( ) <EOL> broker . _consumer ( mock_consume , queue ) <EOL> self . assertEqual ( <NUM_LIT:3> , mock_consume . call_count ) <EOL> self . assertEqual ( <NUM_LIT:0> , len ( queue ) ) <EOL> def test__consumer_cache ( self ) : <EOL> cache_keys_history = [ ] <EOL> def consume ( cache , item ) : <EOL> cache [ item ] = True <EOL> cache_keys_history . append ( list ( cache ) ) <EOL> queue = collections . deque ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> broker . _consumer ( consume , queue ) <EOL> self . assertEqual ( [ [ <NUM_LIT:1> ] , [ <NUM_LIT:1> , <NUM_LIT:2> ] , [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ] , cache_keys_history ) <EOL> def test__consumer_fails ( self ) : <EOL> queue = collections . deque ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> mock_consume = mock . MagicMock ( side_effect = Exception ( ) ) <EOL> broker . _consumer ( mock_consume , queue ) <EOL> self . assertEqual ( <NUM_LIT:0> , len ( queue ) ) <EOL> @ mock . patch ( "<STR_LIT>" ) <EOL> def test__consumer_indexerror ( self , mock_log ) : <EOL> consume = mock . Mock ( ) <EOL> consume . side_effect = IndexError ( ) <EOL> queue = collections . deque ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> broker . _consumer ( consume , queue ) <EOL> self . assertTrue ( mock_log . warning . called ) <EOL> self . assertFalse ( queue ) <EOL> expected = [ mock . call ( { } , <NUM_LIT:1> ) , mock . call ( { } , <NUM_LIT:2> ) , mock . call ( { } , <NUM_LIT:3> ) ] <EOL> self . assertEqual ( expected , consume . mock_calls ) <EOL> def test_run ( self ) : <EOL> def publish ( queue ) : <EOL> queue . append ( <NUM_LIT:1> ) <EOL> queue . append ( <NUM_LIT:2> ) <EOL> queue . append ( <NUM_LIT:3> ) <EOL> consumed = set ( ) <EOL> def consume ( cache , item ) : <EOL> consumed . add ( item ) <EOL> consumer_count = <NUM_LIT:2> <EOL> broker . run ( publish , consume , consumer_count ) <EOL> self . assertEqual ( set ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) , consumed ) </s>
