From 719cd7b2afaef2d16ae913248f18cde985bf40db Mon Sep 17 00:00:00 2001 From: Ask Solem Date: Wed, 26 Oct 2016 17:41:50 -0700 Subject: [PATCH] Producer.auto_declare now declares on first publish --- kombu/messaging.py | 13 ++++++------- t/unit/test_messaging.py | 8 ++++++++ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/kombu/messaging.py b/kombu/messaging.py index f85c4fe3..135da6d0 100644 --- a/kombu/messaging.py +++ b/kombu/messaging.py @@ -47,9 +47,8 @@ class Producer(object): #: Default compression method. Disabled by default. compression = None - #: By default the exchange is declared at instantiation. - #: If you want to declare manually then you can set this - #: to :const:`False`. + #: By default, if a defualt exchange is set, + #: that exchange will be declare when publishing a message. auto_declare = True #: Basic return callback. @@ -167,6 +166,10 @@ class Producer(object): body, serializer, content_type, content_encoding, compression, headers) + if self.auto_declare and self.exchange.name: + declare = [] if declare is None else declare + declare.append(self.exchange) + if retry: _publish = self.connection.ensure(self, _publish, **retry_policy) return _publish( @@ -225,10 +228,6 @@ class Producer(object): if self.on_return: self._channel.events['basic_return'].add(self.on_return) self.exchange = self.exchange(channel) - if self.auto_declare: - # auto_decare is not recommended as this will force - # evaluation of the channel. - self.declare() def __enter__(self): return self diff --git a/t/unit/test_messaging.py b/t/unit/test_messaging.py index fbc8d72b..b0a5876f 100644 --- a/t/unit/test_messaging.py +++ b/t/unit/test_messaging.py @@ -59,6 +59,9 @@ class test_Producer: assert p.exchange is not self.exchange assert p.exchange.is_bound # auto_declare declares exchange' + assert 'exchange_declare' not in channel + + p.publish('foo') assert 'exchange_declare' in channel def test_manual_declare(self): @@ -124,12 +127,14 @@ class test_Producer: def test_publish_with_Exchange_instance(self): p = self.connection.Producer() p.channel = Mock() + p.channel.connection.client.declared_entities = set() p.publish('hello', exchange=Exchange('foo'), delivery_mode='transient') assert p._channel.basic_publish.call_args[1]['exchange'] == 'foo' def test_publish_with_expiration(self): p = self.connection.Producer() p.channel = Mock() + p.channel.connection.client.declared_entities = set() p.publish('hello', exchange=Exchange('foo'), expiration=10) properties = p._channel.prepare_message.call_args[0][5] assert properties['expiration'] == '10000' @@ -137,6 +142,8 @@ class test_Producer: def test_publish_with_reply_to(self): p = self.connection.Producer() p.channel = Mock() + p.channel.connection.client.declared_entities = set() + assert not p.exchange.name p.publish('hello', exchange=Exchange('foo'), reply_to=Queue('foo')) properties = p._channel.prepare_message.call_args[0][5] assert properties['reply_to'] == 'foo' @@ -151,6 +158,7 @@ class test_Producer: def test_publish_retry_calls_ensure(self): p = Producer(Mock()) p._connection = Mock() + p._connection.declared_entities = set() ensure = p.connection.ensure = Mock() p.publish('foo', exchange='foo', retry=True) ensure.assert_called()