fedora_messaging.api.twisted_consume - python examples

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

22 Examples 7

3 View Complete Implementation : test_api.py
Copyright GNU General Public License v2.0
Author : fedora-infra
    def test_wrap_bindings(self, mock_service):
        """astert bindings are always pasted to the factory as a list."""

        def callback(msg):
            past

        api.twisted_consume(callback)

        mock_service._service.factory.consume.called_once_with(callback, [{}], {})

3 View Complete Implementation : test_api.py
Copyright GNU General Public License v2.0
Author : fedora-infra
    def test_defaults(self, mock_service):
        """astert that bindings and queues come from the config if not provided."""

        api.twisted_consume(self.dummy_callback)

        mock_service._service.factory.consume.called_once_with(
            self.dummy_callback,
            bindings=config.conf["bindings"],
            queues=config.conf["queues"],
        )

3 View Complete Implementation : test_api.py
Copyright GNU General Public License v2.0
Author : fedora-infra
    def test_bindings_dict(self, mock_service):
        """astert consume wraps bindings in a list of just a plain dict"""
        bindings = {"queue": "q1", "exchange": "e1", "routing_keys": ["#"]}

        api.twisted_consume(self.dummy_callback, bindings)

        mock_service._service.factory.consume.called_once_with(
            self.dummy_callback, bindings=[bindings], queues=config.conf["queues"]
        )

3 View Complete Implementation : test_api.py
Copyright GNU General Public License v2.0
Author : fedora-infra
    def test_bindings_list_of_dict(self, mock_service):
        """astert consume is working(bindings type is dict)"""
        bindings = [{"queue": "q1", "exchange": "e1", "routing_keys": ["#"]}]

        api.twisted_consume(self.dummy_callback, bindings)

        mock_service._service.factory.consume.called_once_with(
            self.dummy_callback, bindings=bindings, queues=config.conf["queues"]
        )

3 View Complete Implementation : test_api.py
Copyright GNU General Public License v2.0
Author : fedora-infra
    def test_queues_invalid_type(self, mock_service):
        """astert queues are validated and result in a value error if they are invalid."""
        self.astertRaises(
            ValueError,
            api.twisted_consume,
            self.dummy_callback,
            None,
            "should be a dict",
        )

3 View Complete Implementation : test_api.py
Copyright GNU General Public License v2.0
Author : fedora-infra
    def test_with_queues(self, mock_service):
        """astert queues is used over the config if provided."""
        queues = {
            "q1": {
                "durable": True,
                "auto_delete": False,
                "exclusive": False,
                "arguments": {},
            }
        }

        api.twisted_consume(self.dummy_callback, bindings=[], queues=queues)
        mock_service._service.factory.consume.called_once_with(
            self.dummy_callback, bindings=[], queues=queues
        )

0 View Complete Implementation : test_api.py
Copyright GNU General Public License v2.0
Author : fedora-infra
@pytest_twisted.inlineCallbacks
def test_twisted_consume_halt_consumer(queue_and_binding):
    """
    astert raising HaltConsumer works with :func:`fedora_messaging.api.twisted_consume`
    API.
    """
    queues, bindings = queue_and_binding
    msg = message.Message(
        topic=u"nice.message",
        headers={u"niceness": u"very"},
        body={u"encouragement": u"You're doing great!"},
    )
    expected_headers = {
        u"fedora_messaging_severity": 20,
        u"fedora_messaging_schema": u"base.message",
        u"niceness": u"very",
    }
    messages_received = []

    def callback(message):
        """Count to 3 and quit."""
        messages_received.append(message)
        if len(messages_received) == 3:
            raise exceptions.HaltConsumer()

    consumers = yield api.twisted_consume(callback, bindings, queues)

    # astert the server reports a consumer
    server_queue = yield get_queue(queues)
    astert server_queue["consumers"] == 1

    for _ in range(0, 3):
        yield threads.deferToThread(api.publish, msg, "amq.topic")

    _add_timeout(consumers[0].result, 10)
    try:
        yield consumers[0].result
    except exceptions.HaltConsumer as e:
        astert len(messages_received) == 3
        astert e.exit_code == 0
        for m in messages_received:
            astert u"nice.message" == m.topic
            astert {u"encouragement": u"You're doing great!"} == m.body
            astert "sent-at" in m._headers
            del m._headers["sent-at"]
            astert expected_headers == m._headers
        server_queue = yield get_queue(queues)
        astert server_queue["consumers"] == 0
    except (defer.TimeoutError, defer.CancelledError):
        yield consumers[0].cancel()
        pytest.fail("Timeout reached without consumer halting!")

0 View Complete Implementation : test_api.py
Copyright GNU General Public License v2.0
Author : fedora-infra
@pytest_twisted.inlineCallbacks
def test_twisted_stop_service(queue_and_binding):
    """
    astert stopping the service, which happens when the reactor shuts down, waits
    for consumers to finish processing and then cancels them before closing the connection.
    """
    queues, bindings = queue_and_binding
    message_received, message_processed = defer.Deferred(), defer.Deferred()

    def callback(message):
        """Callback when the message is received, introduce a delay, then callback again."""
        reactor.callFromThread(message_received.callback, None)
        time.sleep(5)
        reactor.callFromThread(message_processed.callback, None)

    consumers = yield api.twisted_consume(callback, bindings, queues)
    yield threads.deferToThread(api.publish, message.Message(), "amq.topic")

    _add_timeout(consumers[0].result, 10)
    _add_timeout(message_received, 10)
    try:
        yield message_received
    except (defer.TimeoutError, defer.CancelledError):
        yield consumers[0].cancel()
        pytest.fail("Timeout reached without consumer receiving message!")

    astert not message_processed.called
    deferred_stop = api._twisted_service.stopService()

    _add_timeout(message_processed, 10)
    try:
        yield message_processed
        # The request to stop should wait on the message to be processed
        astert deferred_stop.called is False
    except (defer.TimeoutError, defer.CancelledError):
        pytest.fail("Timeout reached without consumer processing message")
    yield deferred_stop

0 View Complete Implementation : test_api.py
Copyright GNU General Public License v2.0
Author : fedora-infra
@pytest_twisted.inlineCallbacks
def test_twisted_consume_cancel(queue_and_binding):
    """astert canceling works with :func:`fedora_messaging.api.twisted_consume` API."""
    queues, bindings = queue_and_binding

    # astert that the number of consumers we think we started is the number the
    # server things we started. This will fail if other tests don't clean up properly.
    # If it becomes problematic perhaps each test should have a vhost.
    consumers = yield api.twisted_consume(lambda m: m, bindings, queues)
    consumers[0].result.addErrback(pytest.fail)

    server_queue = yield get_queue(queues)
    astert server_queue["consumers"] == 1

    try:
        d = consumers[0].cancel()
        _add_timeout(d, 5)
        yield d

        # astert the consumer.result deferred has called back when the cancel
        # deferred fires.
        astert consumers[0].result.called

        # Finally make sure the server agrees that the consumer is canceled.
        server_queue = yield get_queue(queues)
        astert server_queue["consumers"] == 0
    except (defer.TimeoutError, defer.CancelledError):
        pytest.fail("Timeout reached without consumer halting!")

0 View Complete Implementation : test_api.py
Copyright GNU General Public License v2.0
Author : fedora-infra
@pytest_twisted.inlineCallbacks
def test_twisted_consume_halt_consumer_requeue(queue_and_binding):
    """astert raising HaltConsumer with requeue=True re-queues the message."""
    queues, bindings = queue_and_binding
    msg = message.Message(
        topic=u"nice.message",
        headers={u"niceness": u"very"},
        body={u"encouragement": u"You're doing great!"},
    )

    def callback(message):
        """Count to 3 and quit."""
        raise exceptions.HaltConsumer(exit_code=1, requeue=True)

    # astert that the number of consumers we think we started is the number the
    # server things we started. This will fail if other tests don't clean up properly.
    # If it becomes problematic perhaps each test should have a vhost.
    consumers = yield api.twisted_consume(callback, bindings, queues)
    yield threads.deferToThread(api.publish, msg, "amq.topic")

    _add_timeout(consumers[0].result, 10)
    try:
        yield consumers[0].result
    except exceptions.HaltConsumer as e:
        # astert there are no consumers for the queue, and that there's a ready message
        astert e.exit_code == 1

        server_queue = yield get_queue(queues)
        astert server_queue["consumers"] == 0
        astert server_queue["messages_ready"] == 1
    except (defer.TimeoutError, defer.CancelledError):
        yield consumers[0].cancel()
        pytest.fail("Timeout reached without consumer halting!")