def test_dispatch_queues(self, mock_urlopen, mock_vhosts):
        """
        Assert queues are dispatched with the correct data.
        Args:
        :param mock_urlopen: a patched :mod:`rabbit.urllib2.urlopen` object
        :param mock_vhosts: a patched method from a :mod:`CollectdPlugin`
        """
        self.collectd_plugin.rabbit.get_queues = Mock()
        self.collectd_plugin.rabbit.get_queues.return_value = [
            dict(name='TestQueue1'),
            dict(name='TestQueue2'),
        ]

        mock_urlopen.side_effect = create_mock_url_repsonse
        mock_vhosts.return_value = [dict(name='test_vhost')]

        self.collectd_plugin.dispatch_values = MagicMock()
        self.collectd_plugin.dispatch_queues('test_vhost')

        q1_stats = get_message_stats_data('TestQueue1')['message_stats']
        q2_stats = get_message_stats_data('TestQueue2')['message_stats']

        self.collectd_plugin.dispatch_values.assert_any_call(
            q1_stats['publish_in'], 'rabbitmq_test_vhost', 'queues',
            'TestQueue1', 'publish_in')

        self.collectd_plugin.dispatch_values.assert_any_call(
            q1_stats['publish_in_details']['rate'], 'rabbitmq_test_vhost',
            'queues', 'TestQueue1', 'publish_in_details', 'rate')

        self.collectd_plugin.dispatch_values.assert_any_call(
            q1_stats['publish_out'], 'rabbitmq_test_vhost', 'queues',
            'TestQueue1', 'publish_out')

        self.collectd_plugin.dispatch_values.assert_any_call(
            q1_stats['publish_out_details']['rate'], 'rabbitmq_test_vhost',
            'queues', 'TestQueue1', 'publish_out_details', 'rate')

        self.collectd_plugin.dispatch_values.assert_any_call(
            q2_stats['publish_in'], 'rabbitmq_test_vhost', 'queues',
            'TestQueue2', 'publish_in')

        self.collectd_plugin.dispatch_values.assert_any_call(
            q2_stats['publish_in_details']['rate'], 'rabbitmq_test_vhost',
            'queues', 'TestQueue2', 'publish_in_details', 'rate')

        self.collectd_plugin.dispatch_values.assert_any_call(
            q2_stats['publish_out'], 'rabbitmq_test_vhost', 'queues',
            'TestQueue2', 'publish_out')

        self.collectd_plugin.dispatch_values.assert_any_call(
            q2_stats['publish_out_details']['rate'], 'rabbitmq_test_vhost',
            'queues', 'TestQueue2', 'publish_out_details', 'rate')
    def test_dispatch_queues_empty_values(self, mock_urlopen, mock_vhosts):
        """
        Assert empty values are not dispatched.
        Args:
        :param mock_urlopen: a patched :mod:`rabbit.urllib2.urlopen` object
        :param mock_vhosts: a patched method from a :mod:`CollectdPlugin`
        """
        self.collectd_plugin.rabbit.get_queues = Mock()
        self.collectd_plugin.rabbit.get_queues.return_value = [
            dict(name='TestQueue'),
        ]

        mock_urlopen.side_effect = create_mock_url_repsonse
        mock_vhosts.return_value = [dict(name='test_vhost')]

        self.collectd_plugin.dispatch_values = MagicMock()
        self.collectd_plugin.dispatch_queues('test_vhost')

        reported_metrics = [
            call[0][4]
            for call in self.collectd_plugin.dispatch_values.call_args_list
        ]
        self.assertEqual(
            set(reported_metrics),
            set(get_message_stats_data('TestQueue')['message_stats'].keys()))
    def test_dispatch_queues(self, mock_urlopen, mock_vhosts):
        """
        Assert queues are dispatched with the correct data.
        Args:
        :param mock_urlopen: a patched :mod:`rabbit.urllib2.urlopen` object
        :param mock_vhosts: a patched method from a :mod:`CollectdPlugin`
        """
        self.collectd_plugin.rabbit.get_queues = Mock()
        self.collectd_plugin.rabbit.get_queues.return_value = [
            dict(name='TestQueue1'),
            dict(name='TestQueue2'),
        ]

        mock_urlopen.side_effect = create_mock_url_repsonse
        mock_vhosts.return_value = [dict(name='test_vhost')]

        self.collectd_plugin.dispatch_values = MagicMock()
        self.collectd_plugin.dispatch_queues('test_vhost')

        q1_stats = get_message_stats_data('TestQueue1')['message_stats']
        q2_stats = get_message_stats_data('TestQueue2')['message_stats']

        self.collectd_plugin.dispatch_values.assert_any_call(
            q1_stats['publish_in'], 'rabbitmq_test_vhost', 'queues',
            'TestQueue1', 'publish_in'
        )

        self.collectd_plugin.dispatch_values.assert_any_call(
            q1_stats['publish_in_details']['rate'], 'rabbitmq_test_vhost',
            'queues', 'TestQueue1', 'publish_in_details', 'rate'
        )

        self.collectd_plugin.dispatch_values.assert_any_call(
            q1_stats['publish_out'], 'rabbitmq_test_vhost', 'queues',
            'TestQueue1', 'publish_out'
        )

        self.collectd_plugin.dispatch_values.assert_any_call(
            q1_stats['publish_out_details']['rate'], 'rabbitmq_test_vhost',
            'queues', 'TestQueue1', 'publish_out_details', 'rate'
        )

        self.collectd_plugin.dispatch_values.assert_any_call(
            q2_stats['publish_in'], 'rabbitmq_test_vhost', 'queues',
            'TestQueue2', 'publish_in'
        )

        self.collectd_plugin.dispatch_values.assert_any_call(
            q2_stats['publish_in_details']['rate'], 'rabbitmq_test_vhost',
            'queues', 'TestQueue2', 'publish_in_details', 'rate'
        )

        self.collectd_plugin.dispatch_values.assert_any_call(
            q2_stats['publish_out'], 'rabbitmq_test_vhost', 'queues',
            'TestQueue2', 'publish_out'
        )

        self.collectd_plugin.dispatch_values.assert_any_call(
            q2_stats['publish_out_details']['rate'], 'rabbitmq_test_vhost',
            'queues', 'TestQueue2', 'publish_out_details', 'rate'
        )