def test_message_halt_requeue(self):
     """Assert messages are requeued when the HaltConsumer exception is raised with requeue"""
     self.consumer._consumer_callback.side_effect = HaltConsumer(requeue=True)
     self.consumer._on_message(self.channel, self.frame, self.properties, b'"body"')
     self.channel.basic_nack.assert_called_with(delivery_tag="testtag", requeue=True)
     self.assertFalse(self.consumer._running)
     self.consumer._connection.close.assert_called_once()
 def test_message_halt(self):
     self.consumer._consumer_callback.side_effect = HaltConsumer()
     self.consumer._on_message(
         self.channel, self.frame, self.properties, b'"body"')
     self.channel.basic_nack.assert_called_with(
         delivery_tag="testtag", requeue=True)
     self.assertFalse(self.consumer._running)
     self.consumer._connection.close.assert_called_once()
 def test_message_halt_no_requeue(self):
     """Assert messages are not requeued by default with HaltConsumer"""
     self.consumer._consumer_callback.side_effect = HaltConsumer()
     self.consumer._on_message(self.channel, self.frame, self.properties,
                               b'"body"')
     self.channel.basic_nack.assert_called_with(delivery_tag="testtag",
                                                requeue=False)
     self.assertFalse(self.consumer._running)
     self.consumer._connection.close.assert_called_once()
 def test_message_halt_exitcode_not_0(self):
     """Assert HaltConsumer exception is re-raised when exit code is not 0"""
     self.consumer._consumer_callback.side_effect = HaltConsumer(
         exit_code=1)
     self.assertRaises(
         HaltConsumer,
         self.consumer._on_message,
         self.channel,
         self.frame,
         self.properties,
         b'"body"',
     )
     self.channel.basic_nack.assert_called_with(delivery_tag="testtag",
                                                requeue=False)
     self.assertFalse(self.consumer._running)
     self.consumer._connection.close.assert_called_once()
Beispiel #5
0
    def test_on_message_halt(self):
        """Assert the consumer is canceled when HaltConsumer is raised"""
        self._message_callback.side_effect = HaltConsumer()
        channel = self.protocol._consumers["my_queue_name"].channel
        tag = self.protocol._consumers["my_queue_name"].tag
        self.protocol._running = True
        d = self._call_on_message("testing.topic", {}, {"key": "value"})

        def _check(_):
            self._message_callback.assert_called()
            channel.basic_ack.assert_called_with(delivery_tag="delivery_tag")
            channel.basic_cancel.assert_called_with(consumer_tag=tag)
            channel.close.assert_called_once_with()
            self.assertTrue(self.protocol._running)

        d.addCallback(_check)
        return pytest_twisted.blockon(d)
    def test_on_message_halt(self):
        # When the callback raises a HaltConsumer exception, the consumer
        # should stop.
        self.protocol._message_callback.side_effect = HaltConsumer()
        channel = self.protocol._channel
        self.protocol.close = mock.Mock()
        d = self._call_on_message("testing.topic", {}, {"key": "value"})

        def _check(_):
            self.protocol._message_callback.assert_called()
            channel.basic_ack.assert_called_with(delivery_tag="delivery_tag")
            self.assertFalse(self.protocol._running)
            self.protocol.close.assert_called()
            self.assertIsNone(self.protocol._channel)

        d.addCallback(_check)
        return pytest_twisted.blockon(d)
Beispiel #7
0
    def __call__(self, message):
        """
        Invoked when a message is received by the consumer.

        Args:
            message (fedora_messaging.api.Message): The message from AMQP.
        """
        # fedmsg wraps message bodies in the following dictionary. We need to
        # wrap messages bridged back into ZMQ with it so old consumers don't
        # explode with KeyErrors.
        self._message_counter += 1
        msg_id = message.id
        if msg_id is None:
            _log.error("Message is missing a message id, dropping it")
            return
        if not YEAR_PREFIX_RE.match(msg_id[:5]):
            msg_id = "{}-{}".format(datetime.datetime.utcnow().year, msg_id)
        wrapped_body = {
            "topic": message.topic,
            "msg": message.body,
            "timestamp": int(time.time()),
            "msg_id": msg_id,
            "i": self._message_counter,
            "username": "******",
        }
        message.body = wrapped_body

        if fedmsg_config.conf["sign_messages"]:
            # Find the cert name
            if not fedmsg_config.conf.get("certname"):
                hostname = socket.gethostname().split(".", 1)[0]
                if "cert_prefix" in fedmsg_config.conf:
                    cert_index = "%s.%s" % (fedmsg_config.conf["cert_prefix"], hostname)
                else:
                    cert_index = fedmsg_config.conf["name"]
                    if cert_index == "relay_inbound":
                        cert_index = "shell.%s" % hostname
                fedmsg_config.conf["certname"] = fedmsg_config.conf["certnames"][
                    cert_index
                ]
            # Sign the message
            try:
                message.body = fedmsg.crypto.sign(message.body, **fedmsg_config.conf)
            except ValueError as e:
                _log.error("Unable to sign message with fedmsg: %s", str(e))
                raise HaltConsumer(exit_code=1, reason=e)

        try:
            _log.debug(
                'Publishing message on "%s" to the ZeroMQ PUB socket "%s"',
                message.topic,
                self.publish_endpoint,
            )
            zmq_message = [
                message.topic.encode("utf-8"),
                json.dumps(message.body).encode("utf-8"),
            ]
            self.pub_socket.send_multipart(zmq_message)
        except zmq.ZMQError as e:
            _log.error("Message delivery failed: %r", e)
            raise Nack()