Here are the examples of the python api pytest_twisted.blockon taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

43 Examples 7

			 
			     def setup_function(function):
    """Ensure each test starts with a fresh Service and configuration."""
    config.conf = config.LazyConfig()
    config.conf["client_properties"]["app"] = function.__name__
    if api._twisted_service:
        pytest_twisted.blockon(api._twisted_service.stopService())
    api._twisted_service = None
				 
			
			 
			         def test_on_client_ready_no_objects(self):
        """Assert factories with no objects to create on startup work."""
        factory = FedoraMessagingFactory(pika.URLParameters("amqp://"))
        factory.buildProtocol(None)
        mock_channel = mock.Mock()
        factory.client._allocate_channel = mock.Mock(return_value=mock_channel)
        d = factory._on_client_ready()

        d.addCallback(lambda _: self.assertTrue(factory._client_ready.called))

        return pytest_twisted.blockon(d)
				 
			
			 
			         def test_on_client_ready_queues(self):
        """Assert factories with queues to create on startup work."""
        factory = FedoraMessagingFactory(None, queues=[{"queue": "my_queue"}])
        factory.buildProtocol(None)
        mock_channel = mock.Mock()
        factory.client._allocate_channel = mock.Mock(return_value=mock_channel)

        d = factory._on_client_ready()

        def _check(_):
            mock_channel.queue_declare.assert_called_once_with(
                queue="my_queue", passive=False
            )
            self.assertTrue(factory._client_ready.called)

        d.addCallback(_check)

        return pytest_twisted.blockon(d)
				 
			
			 
			         def test_on_client_ready_exchanges(self):
        """Assert factories with exchanges to create on startup work."""
        factory = FedoraMessagingFactory(None, exchanges=[{"exchange": "my_exchange"}])
        factory.buildProtocol(None)
        mock_channel = mock.Mock()
        factory.client._allocate_channel = mock.Mock(return_value=mock_channel)

        d = factory._on_client_ready()

        def _check(_):
            mock_channel.exchange_declare.assert_called_once_with(
                exchange="my_exchange", passive=False
            )
            self.assertTrue(factory._client_ready.called)

        d.addCallback(_check)

        return pytest_twisted.blockon(d)
				 
			
			 
			         def test_on_client_ready_consumers(self):
        """Assert factories with bindings to create on startup work."""
        factory = FedoraMessagingFactory(None)
        factory.buildProtocol(None)
        factory.consume(lambda x: x, "my_queue")
        mock_channel = mock.Mock()
        mock_channel.basic_consume.return_value = mock.Mock(), None
        factory.client._allocate_channel = mock.Mock(return_value=mock_channel)

        d = factory._on_client_ready()

        def _check(_):
            mock_channel.basic_consume.assert_called()
            self.assertIn("my_queue", factory.client._consumers)
            self.assertTrue(factory._client_ready.called)

        d.addCallback(_check)

        return pytest_twisted.blockon(d)
				 
			
			 
			         def test_stopTrying(self):
        # The _client_ready deferred must errback when we stop trying to
        # reconnect.
        self.factory._client_ready.addCallbacks(
            self.fail, lambda f: f.trap(pika.exceptions.AMQPConnectionError)
        )
        self.factory.stopTrying()
        return pytest_twisted.blockon(self.factory._client_ready)
				 
			
			 
			         def test_stopFactory(self):
        # The protocol should be stopped when the factory is stopped.
        self.protocol.stopProducing.side_effect = lambda: defer.succeed(None)
        self.factory.buildProtocol(None)
        d = self.factory.stopFactory()

        def _check(_):
            self.protocol.stopProducing.assert_called_once()

        d.addCallback(_check)
        return pytest_twisted.blockon(d)
				 
			
			 
			         def test_consume(self):
        """Assert when there is an active protocol, consume calls are forwarded to it."""
        callback = mock.Mock()
        self.protocol.consume.side_effect = lambda cb, queue: defer.succeed(None)
        self.factory.client = self.protocol
        # Pretend the factory is ready to trigger protocol setup.
        self.factory._client_ready.callback(None)
        d = self.factory.consume(callback, "my_queue")

        def _check(_):
            self.assertEqual({"my_queue": callback}, self.factory.consumers)

        d.addCallback(_check)
        return pytest_twisted.blockon(d)
				 
			
			 
			         def test_when_connected(self):
        """Assert whenConnected returns the current client once _client_ready fires"""
        self.factory.client = mock.Mock()
        self.factory._client_ready.callback(None)
        d = self.factory.whenConnected()
        d.addCallback(lambda client: self.assertEqual(self.factory.client, client))
        return pytest_twisted.blockon(d)
				 
			
			 
			         def test_publish(self):
        """Assert publish forwards to the next available protocol instance."""
        self.factory.whenConnected = mock.Mock(
            return_value=defer.succeed(self.protocol)
        )
        self.protocol.publish.side_effect = lambda *a: defer.succeed(None)
        d = self.factory.publish("test-message", "test-exchange")

        def _check(_):
            self.protocol.publish.assert_called_once_with(
                "test-message", "test-exchange"
            )

        d.addCallback(_check)
        return pytest_twisted.blockon(d)
				 
			
			 
			         def test_publish_connection_closed(self):
        """Assert publish retries when a connection error occurs."""
        self.factory.whenConnected = mock.Mock(
            side_effect=[defer.succeed(self.protocol), defer.succeed(self.protocol)]
        )
        self.protocol.publish.side_effect = [
            ConnectionException(reason="I wanted to"),
            defer.succeed(None),
        ]
        d = self.factory.publish("test-message", "test-exchange")

        def _check(_):
            self.assertEqual(
                [call[0] for call in self.protocol.publish.call_args_list],
                [("test-message", "test-exchange"), ("test-message", "test-exchange")],
            )

        d.addCallback(_check)
        return pytest_twisted.blockon(d)
				 
			
			 
			         def test_when_connected(self):
        """Assert when_connected returns the current client once _client_deferred fires"""
        self.factory.buildProtocol(None)
        d = self.factory.when_connected()
        d.addCallback(lambda client: self.assertEqual(self.factory._client, client))
        d.addCallback(
            lambda _: self.assertEqual(self.factory._client_deferred.called, True)
        )
        self.protocol.ready.callback(None)
        return pytest_twisted.blockon(d)
				 
			
			 
			         def test_allocate_channel_no_more_channels(self):
        """Assert a pika NoFreeChannels exception turns into a fedora_messaging exception."""
        self.protocol.channel = mock.Mock(
            name="channel", side_effect=pika.exceptions.NoFreeChannels()
        )

        d = self.protocol._allocate_channel()
        d.addCallback(pytest.fail, "Expected a NoFreeChannels exception")
        d.addErrback(lambda f: f.trap(NoFreeChannels))
        pytest_twisted.blockon(d)
				 
			
			 
			         @mock.patch(
        "fedora_messaging.twisted.protocol.uuid.uuid4", mock.Mock(return_value="tag1")
    )
    def test_consume_not_running(self):
        """Assert calling consume results in resumeProducing being invoked."""
        self.protocol._running = False
        func = mock.Mock()

        def _check(consumer):
            assert self.protocol._running is True
            consumer.channel.basic_consume.assert_called_once_with(
                queue="my_queue", consumer_tag="tag1"
            )
            assert self.protocol._consumers["my_queue"] == consumer

        d = self.protocol.consume(func, "my_queue")
        d.addCallback(_check)

        return pytest_twisted.blockon(d)
				 
			
			 
			         def test_consume_twice(self):
        """Assert calling consume on the same queue updates the callback."""

        def cb1():
            pass

        def cb2():
            pass

        def _check(_):
            self.assertEqual(1, len(self.protocol._consumers))
            self.assertEqual(cb2, self.protocol._consumers["my_queue"].callback)

        d = self.protocol.consume(cb1, "my_queue")
        d.addCallback(lambda _: self.protocol.consume(cb2, "my_queue"))
        d.addCallback(_check)

        return pytest_twisted.blockon(d)
				 
			
			 
			         @mock.patch(
        "fedora_messaging.twisted.protocol.uuid.uuid4", mock.Mock(return_value="tag1")
    )
    def test_consume_running(self):
        """Assert when running, consume sets up the AMQP consumer"""
        self.protocol._running = True
        func = mock.Mock()

        def _check(consumer):
            consumer.channel.basic_consume.assert_called_once_with(
                queue="my_queue", consumer_tag="tag1"
            )
            assert self.protocol._consumers["my_queue"] == consumer

        d = self.protocol.consume(func, "my_queue")
        d.addCallback(_check)

        return pytest_twisted.blockon(d)
				 
			
			 
			         def test_cancel(self):
        """Assert a consumer is removed from the consumer list and canceled."""

        def cb():
            pass

        def _check(_):
            self.assertEqual(1, len(self.protocol._consumers))

        d = self.protocol.consume(cb, "my_queue")
        d.addCallback(lambda _: self.protocol.consume(cb, "my_queue2"))
        d.addCallback(lambda consumer: self.protocol.cancel(consumer.queue))
        d.addCallback(_check)

        return pytest_twisted.blockon(d)
				 
			
			 
			         def test_cancel_no_consumer(self):
        """Assert canceling a non-existent consumer just returns None."""
        d = self.protocol.cancel("my_invalid_queue")
        d.addCallback(lambda ret: self.assertIsNone(ret))

        return pytest_twisted.blockon(d)
				 
			
			 
			         def test_cancel_channel_error(self):
        """Assert channel errors are caught; a closed channel cancels consumers."""
        consumer = Consumer("consumer1", "my_queue", lambda _: _, mock.Mock())
        consumer.channel.basic_cancel.side_effect = pika.exceptions.AMQPChannelError()
        self.protocol._consumers = {"my_queue": consumer}

        def _check(_):
            self.assertEqual({}, self.protocol._consumers)
            consumer.channel.basic_cancel.assert_called_once_with(
                consumer_tag=consumer.tag
            )

        d = self.protocol.consume(consumer.callback, consumer.queue)
        d.addCallback(lambda consumer: self.protocol.cancel(consumer.queue))
        d.addCallback(_check)

        return pytest_twisted.blockon(d)
				 
			
			 
			         def test_connection_ready(self):
        # Check the ready Deferred.
        def _check(_):
            self.protocol._channel.basic_qos.assert_called_with(
                prefetch_count=config.conf["qos"]["prefetch_count"],
                prefetch_size=config.conf["qos"]["prefetch_size"],
                global_qos=True,
            )

        d = self.protocol.ready
        d.addCallback(_check)

        self.protocol._on_connection_ready(None)

        return pytest_twisted.blockon(d)
				 
			
			 
			         def test_pauseProducing_not_running(self):
        # The pauseProducing method should do nothing when the protocol is not
        # consuming messages..
        self.protocol._channel.consumer_tags = ["ct1", "ct2"]
        self.protocol._running = False
        d = self.protocol.pauseProducing()

        def _check(_):
            self.protocol._channel.basic_cancel.assert_not_called()

        d.addCallback(_check)
        return pytest_twisted.blockon(d)
				 
			
			 
			         def test_pauseProducing_no_channel(self):
        # The pauseProducing method should do nothing when the protocol has no
        # channel yet.
        self.protocol._channel = None
        d = self.protocol.pauseProducing()
        return pytest_twisted.blockon(d)
				 
			
			 
			         def test_stopProducing(self):
        # Check the stopProducing method.
        self.protocol._channel.consumer_tags = []
        self.protocol.close = mock.Mock()
        self.protocol._running = True
        self.protocol._impl.is_closed = False
        d = self.protocol.stopProducing()

        def _check(_):
            self.assertFalse(self.protocol._running)
            self.protocol.close.assert_called()
            self.assertIsNone(self.protocol._channel)

        d.addCallback(_check)
        return pytest_twisted.blockon(d)
				 
			
			 
			         def test_read_not_running(self):
        # When not running, _read() should do nothing.
        self.queue.get.side_effect = lambda: defer.succeed(None)
        self.protocol._running = False
        d = self.protocol._read(self.queue, self.consumer)

        def _check(_):
            self.queue.get.assert_not_called()

        d.addCallback(_check)
        return pytest_twisted.blockon(d)
				 
			
			 
			         def test_read_no_body(self):
        # When a message has no body, it should not be passed to the callback.
        self.queue.get.side_effect = [
            defer.succeed((None, None, None, None)),
            defer.succeed((None, None, None, "")),
        ]
        d = self.protocol._read(self.queue, self.consumer)

        def _check(_):
            self.protocol._on_message.assert_not_called()

        d.addCallback(_check)
        return pytest_twisted.blockon(d)
				 
			
			 
			         def test_on_message(self):
        # Check the nominal case.
        channel = self.protocol._consumers["my_queue_name"].channel
        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")

        d.addCallback(_check)
        return pytest_twisted.blockon(d)
				 
			
			 
			         def test_on_message_invalid(self):
        # When a message is invalid, it should be Nacked.
        channel = self.protocol._consumers["my_queue_name"].channel
        d = self._call_on_message("testing.topic", {}, "testing")

        def _check(_):
            self._message_callback.assert_not_called()
            channel.basic_nack.assert_called_with(
                delivery_tag="delivery_tag", requeue=False
            )

        d.addCallback(_check)
        return pytest_twisted.blockon(d)
				 
			
			 
			         def test_on_message_nack(self):
        # When the callback raises a Nack, the server should be notified.
        self._message_callback.side_effect = Nack()
        channel = self.protocol._consumers["my_queue_name"].channel
        d = self._call_on_message("testing.topic", {}, {"key": "value"})

        def _check(_):
            self._message_callback.assert_called()
            channel.basic_nack.assert_called_with(
                delivery_tag="delivery_tag", requeue=True
            )

        d.addCallback(_check)
        return pytest_twisted.blockon(d)
				 
			
			 
			         def test_on_message_drop(self):
        # When the callback raises a Drop, the server should be notified.
        self._message_callback.side_effect = Drop()
        channel = self.protocol._consumers["my_queue_name"].channel
        d = self._call_on_message("testing.topic", {}, {"key": "value"})

        def _check(_):
            self._message_callback.assert_called()
            channel.basic_nack.assert_called_with(
                delivery_tag="delivery_tag", requeue=False
            )

        d.addCallback(_check)
        return pytest_twisted.blockon(d)
				 
			
			 
			         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_exception(self):
        # On an unknown exception, the consumer should stop and all
        # unacknowledged messages should be requeued.
        self._message_callback.side_effect = ValueError()
        channel = self.protocol._consumers["my_queue_name"].channel
        self.protocol._running = True
        d = self._call_on_message("testing.topic", {}, {"key": "value"})

        def _check(_):
            self._message_callback.assert_called()
            channel.basic_nack.assert_called_with(
                delivery_tag=0, multiple=True, requeue=True
            )
            self.assertTrue(self.protocol._running)
            channel.close.assert_called_once_with()

        d.addCallback(_check)
        return pytest_twisted.blockon(d)
				 
			
			 
			         def test_consume_connection_exception(self):
        """If consuming fails due to a non-permission error, a ConnectionException happens."""
        proto = FedoraMessagingProtocolV2(None)
        mock_channel = mock.Mock()
        mock_channel.basic_consume.side_effect = pika.exceptions.ChannelClosed(
            400, "Bad Request!"
        )
        deferred_channel = defer.succeed(mock_channel)
        proto._allocate_channel = mock.Mock(return_value=deferred_channel)

        def check(failure):
            assert isinstance(failure.value, ConnectionException)

        d = proto.consume(lambda x: x, "test_queue")
        d.addBoth(check)
        return pytest_twisted.blockon(d)
				 
			
			 
			         def test_twisted12_timeout(self):
        """Assert timeouts work for Twisted 12.2 (EL7)"""
        d = defer.Deferred()
        d.addTimeout = mock.Mock(side_effect=AttributeError())
        _add_timeout(d, 0.1)

        d.addCallback(self.fail, "Expected errback to be called")
        d.addErrback(
            lambda failure: self.assertIsInstance(failure.value, defer.CancelledError)
        )

        return pytest_twisted.blockon(d)
				 
			
			 
			         def test_passive_declares(self):
        """Assert queues and exchanges are created passively when configured so."""
        factory = FedoraMessagingFactory(
            None,
            queues=[{"queue": "my_queue"}],
            exchanges=[{"exchange": "my_exchange"}],
        )
        factory.buildProtocol(None)
        mock_channel = mock.Mock()
        factory.client._allocate_channel = mock.Mock(return_value=mock_channel)

        def _check(_):
            mock_channel.queue_declare.assert_called_once_with(
                queue="my_queue", passive=True
            )
            mock_channel.exchange_declare.assert_called_once_with(
                exchange="my_exchange", passive=True
            )

        with mock.patch.dict(config.conf, {"passive_declares": True}):
            d = factory._on_client_ready()
            d.addCallback(_check)

        return pytest_twisted.blockon(d)
				 
			
			 
			         def test_on_client_ready_bindings(self):
        """Assert factories with bindings to create on startup work."""
        factory = FedoraMessagingFactory(
            None, bindings=[{"queue": "my_queue", "exchange": "my_exchange"}]
        )
        factory.buildProtocol(None)
        mock_channel = mock.Mock()
        factory.client._allocate_channel = mock.Mock(return_value=mock_channel)

        d = factory._on_client_ready()

        def _check(_):
            mock_channel.queue_bind.assert_called_once_with(
                queue="my_queue", exchange="my_exchange"
            )
            self.assertTrue(factory._client_ready.called)

        d.addCallback(_check)

        return pytest_twisted.blockon(d)
				 
			
			 
			         def test_buildProtocol_twice(self):
        """Assert buildProtocol works when reconnecting"""

        def _get_protocol(*a, **kw):
            protocol = mock.Mock(name="protocol mock")
            protocol.ready = defer.succeed(None)
            return protocol

        self.factory.protocol = _get_protocol
        connector = mock.Mock()
        connected_d = self.factory.when_connected()
        self.factory.buildProtocol(None)
        self.factory.clientConnectionLost(connector, None)
        with mock.patch("fedora_messaging.twisted.factory._std_log") as mock_log:
            protocol = self.factory.buildProtocol(None)
        self.assertFalse(mock_log.exception.called)
        self.assertFalse(mock_log.error.called)
        d = defer.DeferredList([connected_d, protocol.ready], fireOnOneErrback=True)
        d.addErrback(lambda f: f.value.subFailure)
        return pytest_twisted.blockon(d)
				 
			
			 
			         def _test_when_connected_disconnected(self, error_class, error_msg):
        """Assert when_connected errbacks on disconnections."""

        def _get_protocol(*a, **kw):
            protocol = mock.Mock(name="protocol mock")
            # Disconnect immediately
            protocol.ready = defer.fail(error_class())
            return protocol

        def _check(f):
            f.trap(ConnectionException)
            # Make sure a new Deferred has been generated for when_connected()
            new_d = self.factory.when_connected()
            assert new_d.called is False
            assert new_d != connected_d
            assert f.value.reason == error_msg

        self.factory.protocol = _get_protocol
        connected_d = self.factory.when_connected()
        connected_d.addCallbacks(
            lambda r: ValueError("This should fail but I got: {!r}".format(r)), _check
        )
        self.factory.buildProtocol(None)
        return pytest_twisted.blockon(connected_d)
				 
			
			 
			         def test_when_connected_unexpected_failure(self):
        """Assert when_connected errbacks when the connection fails."""

        class DummyError(Exception):
            pass

        def _get_protocol(*a, **kw):
            protocol = mock.Mock(name="protocol mock")
            # Fail immediately
            protocol.ready = defer.fail(DummyError())
            return protocol

        def _check(f):
            f.trap(DummyError)
            # Make sure a new Deferred has been generated for when_connected()
            new_d = self.factory.when_connected()
            assert new_d.called is False
            assert new_d != connected_d

        self.factory.protocol = _get_protocol
        connected_d = self.factory.when_connected()
        connected_d.addCallbacks(
            lambda r: ValueError("This should fail but I got: {!r}".format(r)), _check
        )
        with mock.patch("fedora_messaging.twisted.factory._std_log") as mock_log:
            self.factory.buildProtocol(None)
        mock_log.error.assert_called()
        last_log_call_args = mock_log.error.call_args_list[-1][0]
        assert last_log_call_args[0] == (
            "The connection failed with an unexpected exception; please report this bug: %s"
        )
        assert last_log_call_args[1].startswith("Traceback (most recent call last):")
        return pytest_twisted.blockon(connected_d)
				 
			
			 
			         def test_publish(self):
        # Check the publish method.
        body = {"bodykey": "bodyvalue"}
        headers = {"headerkey": "headervalue"}
        message = Message(body, headers, "testing.topic")
        d = self.protocol.publish(message, "test-exchange")

        def _check(_):
            self.protocol._channel.basic_publish.assert_called_once()
            args = self.protocol._channel.basic_publish.call_args_list[0][1]
            self.assertEqual(args["exchange"], "test-exchange")
            self.assertEqual(args["routing_key"], b"testing.topic")
            self.assertEqual(args["body"], json.dumps(body).encode("utf-8"))
            props = args["properties"]
            self.assertEqual(props.headers, headers)
            self.assertEqual(props.content_encoding, "utf-8")
            self.assertEqual(props.content_type, "application/json")
            self.assertEqual(props.delivery_mode, 2)

        d.addCallback(_check)
        return pytest_twisted.blockon(d)
				 
			
			 
			         def test_resumeProducing(self):
        # Check the resumeProducing method.
        self.protocol._running = False
        callback = mock.Mock()
        self.protocol._consumers = {
            "testqueue1": Consumer(
                "consumer1", "testqueue1", callback, self.protocol._channel
            ),
            "testqueue2": Consumer(
                "consumer2", "testqueue2", callback, self.protocol._channel
            ),
            "testqueue3": Consumer(
                "consumer3", "testqueue3", callback, self.protocol._channel
            ),
        }
        self.protocol._read = mock.Mock(side_effect=lambda _, __: defer.succeed(None))
        d = self.protocol.resumeProducing()

        def _check(_):
            self.assertTrue(self.protocol._running)
            self.assertEqual(self.protocol._channel.basic_consume.call_count, 3)
            called_queues = [
                kw["queue"]
                for arg, kw in self.protocol._channel.basic_consume.call_args_list
            ]
            self.assertEqual(
                sorted(called_queues), sorted(self.protocol._consumers.keys())
            )
            self.assertEqual(self.protocol._read.call_count, 3)

        d.addCallback(_check)
        return pytest_twisted.blockon(d)
				 
			
			 
			         def test_pauseProducing(self):
        # Check the pauseProducing method.
        self.protocol._running = True
        self.protocol._consumers = {
            "testqueue1": Consumer("ct1", "testqueue1", None, self.protocol._channel),
            "testqueue2": Consumer("ct2", "testqueue2", None, self.protocol._channel),
        }
        d = self.protocol.pauseProducing()

        def _check(_):
            self.assertFalse(self.protocol._running)
            self.assertEqual(self.protocol._channel.basic_cancel.call_count, 2)
            called_cts = [
                kw["consumer_tag"]
                for arg, kw in self.protocol._channel.basic_cancel.call_args_list
            ]
            self.assertEqual(sorted(called_cts), sorted(["ct1", "ct2"]))

        d.addCallback(_check)
        return pytest_twisted.blockon(d)
				 
			
			 
			         def test_read(self):
        # Check the nominal case for _read().
        self.queue.get.side_effect = [
            defer.succeed((None, "df1", "prop1", "body1")),
            defer.succeed((None, "df2", "prop2", "body2")),
            defer.succeed((None, "df3", "prop3", "body3")),
            pika.exceptions.ChannelClosed(42, "testing"),
        ]

        def _check(_):
            self.assertEqual(
                self.protocol._on_message.call_args_list,
                [
                    (("df1", "prop1", "body1", self.consumer), {}),
                    (("df2", "prop2", "body2", self.consumer), {}),
                    (("df3", "prop3", "body3", self.consumer), {}),
                ],
            )

        d = self.protocol._read(self.queue, self.consumer)
        d.addCallback(_check)
        return pytest_twisted.blockon(d)
				 
			
			 
			         def test_read_exit_loop(self):
        # Check the exceptions that cause the read loop to exit.
        exceptions = [
            error.ConnectionDone(),
            pika.exceptions.ChannelClosed(42, "testing"),
            pika.exceptions.ConsumerCancelled(),
            RuntimeError(),
            pika.exceptions.ChannelClosedByClient(42, "testing"),
        ]

        deferreds = []
        for exc in exceptions:
            queue = mock.Mock()
            queue.get.side_effect = exc
            deferreds.append(self.protocol._read(queue, self.consumer))

        def _check(_):
            self.protocol._on_message.assert_not_called()

        d = defer.DeferredList(deferreds)
        d.addCallback(_check)
        return pytest_twisted.blockon(d)