Skip to content

Commit

Permalink
Merge pull request #33 from iheartradio/autoregister-consumer
Browse files Browse the repository at this point in the history
Add support for automatically registering a consumer
  • Loading branch information
dirn committed Apr 8, 2016
2 parents af7583b + 573849b commit 5edaaea
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 30 deletions.
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Version 0.3.0
Release TBD

- Add support for ``Retry``
- Add ``REGISTER_CONSUMER`` setting


Version 0.2.0
Expand Down
56 changes: 30 additions & 26 deletions docs/settings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,32 +33,36 @@ Connection Settings
Consumer Settings
=================

+------------------------------------+----------------------------------------+
| ``AMQP_INBOUND_EXCHANGE`` | The name of the exchange that the |
| | consumer should read from. Defaults to |
| | ``''`` (the AMQP default exchange). |
+------------------------------------+----------------------------------------+
| ``AMQP_INBOUND_EXCHANGE_DURABLE`` | The durability setting of the exchange |
| | that the consumer reads from. Defaults |
| | to ``False``. |
+------------------------------------+----------------------------------------+
| ``AMQP_INBOUND_EXCHANGE_TYPE`` | The type of the inbound exchange. |
| | Defaults to ``'direct'``. |
+------------------------------------+----------------------------------------+
| ``AMQP_INBOUND_QUEUE`` | The name of the queue that the |
| | consumer should read from. Defaults to |
| | ``''`` (the AMQP default queue). |
+------------------------------------+----------------------------------------+
| ``AMQP_INBOUND_QUEUE_DURABLE`` | The durability setting of the queue |
| | the consumer reads from. Defaults to |
| | ``False``. |
+------------------------------------+----------------------------------------+
| ``AMQP_INBOUND_ROUTING_KEY`` | The routing key used to bind the |
| | inbound exchange and queue. Defaults |
| | to ``''``. |
+------------------------------------+----------------------------------------+
| ``AMQP_DISPATCH_METHOD`` | Reserved for future use. |
+------------------------------------+----------------------------------------+
+-----------------------------------+-----------------------------------------+
| ``REGISTER_CONSUMER`` | If ``True``, a consumer will be |
| | automatically created and assigned to |
| | the application. Defaults to ``False``. |
+-----------------------------------+-----------------------------------------+
| ``AMQP_INBOUND_EXCHANGE`` | The name of the exchange that the |
| | consumer should read from. Defaults to |
| | ``''`` (the AMQP default exchange). |
+-----------------------------------+-----------------------------------------+
| ``AMQP_INBOUND_EXCHANGE_DURABLE`` | The durability setting of the exchange |
| | that the consumer reads from. Defaults |
| | to ``False``. |
+-----------------------------------+-----------------------------------------+
| ``AMQP_INBOUND_EXCHANGE_TYPE`` | The type of the inbound exchange. |
| | Defaults to ``'direct'``. |
+-----------------------------------+-----------------------------------------+
| ``AMQP_INBOUND_QUEUE`` | The name of the queue that the |
| | consumer should read from. Defaults to |
| | ``''`` (the AMQP default queue). |
+-----------------------------------+-----------------------------------------+
| ``AMQP_INBOUND_QUEUE_DURABLE`` | The durability setting of the queue |
| | the consumer reads from. Defaults to |
| | ``False``. |
+-----------------------------------+-----------------------------------------+
| ``AMQP_INBOUND_ROUTING_KEY`` | The routing key used to bind the |
| | inbound exchange and queue. Defaults |
| | to ``''``. |
+-----------------------------------+-----------------------------------------+
| ``AMQP_DISPATCH_METHOD`` | Reserved for future use. |
+-----------------------------------+-----------------------------------------+

Producer Settings
=================
Expand Down
15 changes: 15 additions & 0 deletions henson_amqp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ class AMQP(Extension):
'AMQP_CONNECTION_KWARGS': {},

# Consumer settings
'REGISTER_CONSUMER': False,
'AMQP_DISPATCH_METHOD': 'ROUND_ROBIN',
'AMQP_INBOUND_EXCHANGE': '',
'AMQP_INBOUND_EXCHANGE_DURABLE': False,
Expand All @@ -310,6 +311,20 @@ class AMQP(Extension):
'AMQP_DELIVERY_MODE': DeliveryMode.NONPERSISTENT,
}

def init_app(self, app):
"""Initialize the application.
If the application's ``REGISTER_CONSUMER`` setting is truthy,
create a consumer and attach it to the application.
Args:
app (henson.base.Application): The application instance that
will be initialized.
"""
super().init_app(app)
if app.settings['REGISTER_CONSUMER']:
app.consumer = self.consumer()

def consumer(self):
"""Return a new AMQP consumer.
Expand Down
11 changes: 8 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,15 @@ class Settings:


@pytest.fixture
def test_amqp():
def test_app():
"""Return a test application."""
return Application('testing', Settings)


@pytest.fixture
def test_amqp(test_app):
"""Return an extension bound to the test app."""
app = Application('testing', Settings)
return AMQP(app)
return AMQP(test_app)


@pytest.fixture
Expand Down
15 changes: 14 additions & 1 deletion tests/test_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,20 @@

import pytest

from henson_amqp import Message
from henson_amqp import AMQP, Consumer, Message


def test_register_consumer(test_app):
"""Test that consumer registration behaves correctly."""
test_app.settings['REGISTER_CONSUMER'] = True
AMQP(test_app)
assert isinstance(test_app.consumer, Consumer)


def test_no_register_consumer(test_app):
"""Test that consumers are not registered if not explicitly set."""
AMQP(test_app)
assert not test_app.consumer


@pytest.mark.asyncio
Expand Down

0 comments on commit 5edaaea

Please sign in to comment.