Allow arguments passed to queue declare #10

Closed
wants to merge 4 commits into
from
Jump to file or symbol
Failed to load files and symbols.
+47 −5
Diff settings

Always

Just for now

View
@@ -26,6 +26,10 @@ messaging configuration. Included in the sparkplug source is an example::
auto_delete = False
# Is the queue exclusive to this program?
exclusive = False
+ # Extra arguments to be passed down to `channel.queue_declare`
+ # See http://amqp.readthedocs.org/en/latest/reference/amqp.channel.html#amqp.channel.Channel.queue_declare
+ # The value is parsed as a JSON string into a Python dictionary
+ arguments = {}
[exchange:postoffice]
# The exchange type ('direct', 'fanout', or 'topic')
@@ -25,7 +25,7 @@
"""
from sparkplug.config import DependencyConfigurer
-from sparkplug.config.types import convert, parse_bool
+from sparkplug.config.types import convert, parse_bool, parse_dict
from sparkplug.logutils import LazyLogger
_log = LazyLogger(__name__)
@@ -36,17 +36,18 @@ def __init__(self, name, **kwargs):
DependencyConfigurer.__init__(self)
self.queue = name
-
+
create_args = dict(kwargs)
convert(create_args, 'durable', parse_bool)
convert(create_args, 'auto_delete', parse_bool)
convert(create_args, 'exclusive', parse_bool)
convert(create_args, 'passive', parse_bool)
+ convert(create_args, 'arguments', parse_dict)
self.create_args = create_args
-
+
def start(self, channel):
_log.debug("Declaring queue %s (%r)", self.queue, self.create_args)
-
+
channel.queue_declare(queue=self.queue, **self.create_args)
def __repr__(self):
View
@@ -1,3 +1,6 @@
+import json
+
+
def convert(dict, key, type):
if key in dict:
dict[key] = type(dict[key])
@@ -7,7 +10,7 @@ def parse_bool(value):
"""Semi-blind boolean conversion that treats "False" and u"False"
as False, not True. If fed a boolean, nothing happens and the
value is returned as-is.
-
+
>>> parse_bool("False")
False
>>> parse_bool("True")
@@ -21,3 +24,14 @@ def parse_bool(value):
'True': True,
'False': False
}.get(value, value))
+
+
+def parse_dict(value):
+ """Convert a JSON string to dictionary
+
+ >>> parse_dict('{"a": 1}')
+ {"a": 1}
+ >>> parse_dict('{}')
+ {}
+ """
+ return json.loads(value)
@@ -0,0 +1,23 @@
+from nose.tools import eq_
+from mock import Mock, call
+from sparkplug.config.queue import QueueConfigurer
+
+
+def test_queue_configurer_arguments_not_passed():
+ qc = QueueConfigurer('some_queue', durable='True')
+ eq_(qc.create_args, {'durable': True})
+ channel = Mock()
+ qc.start(channel)
+ eq_([call(queue='some_queue', durable=True)],
+ channel.queue_declare.call_args_list)
+
+
+def test_queue_configurer_takes_arguments():
+ arguments = '{"x-dead-letter-exchange": "dlx", "x-ttl": 6000}'
+ qc = QueueConfigurer('some_queue', arguments=arguments)
+ channel = Mock()
+ qc.start(channel)
+ eq_([call(queue='some_queue', arguments={
+ 'x-dead-letter-exchange': 'dlx',
+ 'x-ttl': 6000})],
+ channel.queue_declare.call_args_list)