Exemple #1
0
    def test_basic_cancel(self):
        # Verify that basic_cancels calls clean up the consumer queue.
        queue_obj = mock.Mock()
        queue_obj_2 = mock.Mock()
        self.channel._consumers["test-consumer"] = queue_obj
        self.channel._consumers["test-consumer-2"] = queue_obj_2
        self.channel._queue_name_to_consumer_tags.update({
            "testqueue":
            set(["test-consumer"]),
            "testqueue-2":
            set(["test-consumer-2"]),
        })
        d = self.channel.basic_cancel("test-consumer")

        def check(result):
            self.assertTrue(isinstance(result, Method))
            queue_obj.close.assert_called_once()
            self.assertTrue(
                isinstance(queue_obj.close.call_args[0][0], ConsumerCancelled))
            self.assertEqual(len(self.channel._consumers), 1)
            queue_obj_2.close.assert_not_called()
            self.assertEqual(
                self.channel._queue_name_to_consumer_tags["testqueue"], set())

        d.addCallback(check)
        self.pika_channel.basic_cancel.assert_called_once()
        self.pika_channel.basic_cancel.call_args[1]["callback"](Method(
            1, spec.Basic.CancelOk(consumer_tag="test-consumer")))
        return d
Exemple #2
0
 def send_message(_result):
     d = self.channel.basic_publish("testexch", "testrk", "testbody")
     # Send the Basic.Return frame
     method = spec.Basic.Return(exchange="testexch",
                                routing_key="testrk")
     return_cb(self.channel, method, spec.BasicProperties(), "testbody")
     # Send the Basic.Nack frame
     frame = Method(1, spec.Basic.Nack(delivery_tag=1))
     self.channel._on_delivery_confirmation(frame)
     return d
Exemple #3
0
    def test_basic_cancel_no_consumer(self):
        # Verify that basic_cancel does not crash if there is no consumer.
        d = self.channel.basic_cancel("test-consumer")

        def check(result):
            self.assertTrue(isinstance(result, Method))

        d.addCallback(check)
        self.pika_channel.basic_cancel.assert_called_once()
        self.pika_channel.basic_cancel.call_args[1]["callback"](Method(
            1, spec.Basic.CancelOk(consumer_tag="test-consumer")))
        return d
Exemple #4
0
 def test_consumer_cancelled_by_broker(self):
     # Verify that server-originating cancels are handled.
     self.pika_channel.add_on_cancel_callback.assert_called_with(
         self.channel._on_consumer_cancelled_by_broker)
     queue_obj = mock.Mock()
     self.channel._consumers["test-consumer"] = queue_obj
     self.channel._queue_name_to_consumer_tags["testqueue"] = set(
         ["test-consumer"])
     self.channel._on_consumer_cancelled_by_broker(
         Method(1, spec.Basic.Cancel(consumer_tag="test-consumer")))
     queue_obj.close.assert_called_once()
     self.assertTrue(
         isinstance(queue_obj.close.call_args[0][0], ConsumerCancelled))
     self.assertEqual(self.channel._consumers, {})
     self.assertEqual(
         self.channel._queue_name_to_consumer_tags["testqueue"], set())
Exemple #5
0
    def test_basic_consume(self):
        # Verify that the basic_consume method works properly.
        d = self.channel.basic_consume(queue="testqueue")
        self.pika_channel.basic_consume.assert_called_once()
        kwargs = self.pika_channel.basic_consume.call_args_list[0][1]
        self.assertEqual(kwargs["queue"], "testqueue")
        on_message = kwargs["on_message_callback"]

        def check_cb(result):
            queue, _consumer_tag = result
            # Make sure the queue works
            queue_get_d = queue.get()
            queue_get_d.addCallback(
                self.assertEqual,
                (self.channel, "testmethod", "testprops", "testbody"))
            # Simulate reception of a message
            on_message("testchan", "testmethod", "testprops", "testbody")
            return queue_get_d

        d.addCallback(check_cb)
        # Simulate a ConsumeOk from the server
        frame = Method(1, spec.Basic.ConsumeOk(consumer_tag="testconsumertag"))
        kwargs["callback"](frame)
        return d
Exemple #6
0
 def send_message(_result):
     d1 = self.channel.basic_publish("testexch", "testrk", "testbody1")
     d2 = self.channel.basic_publish("testexch", "testrk", "testbody2")
     frame = Method(1, spec.Basic.Ack(delivery_tag=2, multiple=True))
     self.channel._on_delivery_confirmation(frame)
     return defer.DeferredList([d1, d2])
Exemple #7
0
 def send_message(_result):
     d = self.channel.basic_publish("testexch", "testrk", "testbody")
     frame = Method(1, spec.Basic.Nack(delivery_tag=1))
     self.channel._on_delivery_confirmation(frame)
     return d