def testConsumerGetNextEvent(self):
        """ Tests getting messages using a consumer and getNextEvent(). """
        self._connectToClient()
        exchangeName = "testExchange"
        exchangeType = "direct"
        queueName = "testQueue"
        routingKey = "testKey"

        self.client.declareExchange(exchangeName, exchangeType)
        self.client.declareQueue(queueName)
        self.client.bindQueue(queueName, exchangeName, routingKey)

        for i in range(0, _NUM_TEST_MESSAGES):
            # Test random numbers of messages sent to the queue
            self.client.publish(Message("test-msg-%d" % (i)), exchangeName,
                                routingKey)
        self._verifyQueue(queueName, testMessageCount=_NUM_TEST_MESSAGES)

        consumer = self.client.createConsumer(queueName)

        for i in range(0, _NUM_TEST_MESSAGES):
            message = self.client.getNextEvent()
            self.assertEqual(message.body, "test-msg-%d" % (i))
            self.assertEqual(message.properties, BasicProperties())
            self.assertEqual(
                message.methodInfo,
                MessageDeliveryInfo(consumerTag=consumer.tag,
                                    deliveryTag=(i + 1),
                                    redelivered=False,
                                    exchange=exchangeName,
                                    routingKey=routingKey))
    def testRecoverUnackedMessages(self):
        """ Tests the recover method to re-queue unacked messages. """
        self._connectToClient()
        exchangeName = "testExchange"
        exchangeType = "direct"
        queueName = "testQueue"
        routingKey = "testKey"

        self.client.declareExchange(exchangeName, exchangeType)
        self.client.declareQueue(queueName)
        self.client.bindQueue(queueName, exchangeName, routingKey)

        for i in range(0, _NUM_TEST_MESSAGES):
            # Test random numbers of messages sent to the queue
            self.client.publish(Message("test-msg-%d" % (i)), exchangeName,
                                routingKey)
        self._verifyQueue(queueName, testMessageCount=_NUM_TEST_MESSAGES)

        consumer = self.client.createConsumer(queueName)

        for i in range(0, _NUM_TEST_MESSAGES):
            message = self.client.getNextEvent()
            self.assertEqual(message.body, "test-msg-%d" % (i))
            self.assertEqual(message.properties, BasicProperties())
            self.assertEqual(
                message.methodInfo,
                MessageDeliveryInfo(consumerTag=consumer.tag,
                                    deliveryTag=(i + 1),
                                    redelivered=False,
                                    exchange=exchangeName,
                                    routingKey=routingKey))

        self._verifyUnacknowledgedMessages(queueName)

        self.client.recover(requeue=True)

        @_RETRY_ON_ASSERTION_ERROR
        def _verifyRecoveredMessages():
            """
      Verifies that messages are unacknowledged on server in nested function to
      use the _RETRY_ON_ASSERTION_ERROR decorator.
      """
            queue = requests.get(url="http://%s:%s/api/queues/%s/%s" %
                                 (self.connParams.host, self.connParams.port,
                                  self.connParams.vhost, queueName),
                                 auth=(self.connParams.username,
                                       self.connParams.password)).json()
            self.assertIn("redeliver", queue["message_stats"])
            self.assertEqual(queue["message_stats"]["redeliver"],
                             _NUM_TEST_MESSAGES)

        _verifyRecoveredMessages()

        for i in range(0, _NUM_TEST_MESSAGES):
            message = self.client.getNextEvent()
            self.assertEqual(message.body, "test-msg-%d" % (i))
            self.assertEqual(message.properties, BasicProperties())
            self.assertEqual(
                message.methodInfo,
                MessageDeliveryInfo(consumerTag=consumer.tag,
                                    deliveryTag=(_NUM_TEST_MESSAGES + i + 1),
                                    redelivered=True,
                                    exchange=exchangeName,
                                    routingKey=routingKey))