def test_consumer_json(self):
        """
        The consumer listening to RabbitMQ should parse incoming data as
        JSON and then pass it to the callback.
        """
        callback = Mock()
        shove.consume("myqueue", callback)

        consumer = self.channel.basic_consume.call_args[0][0]
        consumer(None, None, None, '{"foo": "bar", "baz": 1}')
        callback.assert_called_with({"foo": "bar", "baz": 1})
    def test_stats_consuming(self):
        """
        Ensure that calling consume will set up a consumer on the
        requested queue.
        """
        callback = Mock()
        shove.consume("myqueue", callback)

        self.channel.queue_declare.assert_called_once_with(queue="myqueue", durable=True)
        self.channel.basic_consume.assert_called_once_with(ANY, queue="myqueue", no_ack=True)
        assert_true(self.channel.start_consuming.called)
        assert_true(self.connection.close.called)
    def test_consumer_json_invalid(self):
        """
        If the incoming data is invalid JSON, log a warning and do not
        call the callback.
        """
        callback = Mock()
        shove.consume("myqueue", callback)
        consumer = self.channel.basic_consume.call_args[0][0]

        with patch("captain.projects.shove.log") as log:
            consumer(None, None, None, "invalid{json}")
            assert_false(callback.called)
            assert_true(log.warning.called)
    def handle(self, *args, **kwargs):
        # Log INFO events to make the commandline output a little more
        # useful.
        fmt = '[%(threadName)10s] %(asctime)s - %(levelname)s: %(message)s'
        logging.basicConfig(format=fmt, level=logging.INFO)

        # While we're updating instances, let's look out for inactive
        # ones too.
        marking_thread = MarkInactiveShoveInstancesThread(name='marking')
        marking_thread.start()

        try:
            shove.consume(settings.HEARTBEAT_QUEUE, handle_heartbeat_event)
        except Exception as error:
            log.error('Error during monitoring: ' + unicode(error))

        marking_thread.stop()
        marking_thread.join()
 def handle(self, *args, **kwargs):
     # Log INFO events to make the commandline output a little more useful.
     logging.basicConfig(format='%(asctime)s - %(levelname)s: %(message)s', level=logging.INFO)
     shove.consume(settings.LOGGING_QUEUE, handle_log_event)