Skip to content

Commit

Permalink
Use uuid when creating unique consumer tag
Browse files Browse the repository at this point in the history
  • Loading branch information
Perttu Ranta-aho committed Jul 3, 2013
1 parent 4ee2738 commit 66f8ace
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
6 changes: 3 additions & 3 deletions pika/channel.py
Expand Up @@ -5,6 +5,7 @@
import collections
import logging
import warnings
import uuid

import pika.frame as frame
import pika.exceptions as exceptions
Expand Down Expand Up @@ -202,9 +203,8 @@ def basic_consume(self, consumer_callback, queue='', no_ack=False,
self._validate_channel_and_callback(consumer_callback)

# If a consumer tag was not passed, create one
consumer_tag = consumer_tag or 'ctag%i.%i' % (self.channel_number,
len(self._consumers) +
len(self._cancelled))
consumer_tag = consumer_tag or 'ctag%i.%s' % (self.channel_number,

This comment has been minimized.

Copy link
@vitaly-krugl

vitaly-krugl Jan 7, 2014

Member

What is the reason for generating consumer tags on the client side? Isn't the AMQP server able to generate consumer tags for us and returning them in the basic_consume response?

uuid.uuid4().get_hex())

if consumer_tag in self._consumers or consumer_tag in self._cancelled:
raise exceptions.DuplicateConsumerTag(consumer_tag)
Expand Down
24 changes: 13 additions & 11 deletions tests/channel_tests.py
Expand Up @@ -243,23 +243,25 @@ def test_basic_consume_calls_validate(self, validate):

def test_basic_consume_consumer_tag(self):
self.obj._set_state(self.obj.OPEN)
expectation = 'ctag1.0'
expectation = 'ctag1.'
mock_callback = mock.Mock()
self.assertEqual(self.obj.basic_consume(mock_callback, 'test-queue'),
self.assertEqual(self.obj.basic_consume(mock_callback, 'test-queue')[:6],
expectation)

def test_basic_consume_consumer_tag_appended(self):
def test_basic_consume_consumer_tag_cancelled_full(self):
self.obj._set_state(self.obj.OPEN)
consumer_tag = 'ctag1.0'
expectation = 'ctag1.'
mock_callback = mock.Mock()
self.obj.basic_consume(mock_callback, 'test-queue')
self.assertIn(consumer_tag, self.obj._consumers)
for ctag in ['ctag1.%i' % ii for ii in range(11)]:
self.obj._cancelled.append(ctag)
self.assertEqual(self.obj.basic_consume(mock_callback, 'test-queue')[:6],
expectation)

def test_basic_consume_consumer_tag_in_consumers(self):
self.obj._set_state(self.obj.OPEN)
consumer_tag = 'ctag1.0'
mock_callback = mock.Mock()
self.obj.basic_consume(mock_callback, 'test-queue')
self.obj.basic_consume(mock_callback, 'test-queue', consumer_tag=consumer_tag)
self.assertIn(consumer_tag, self.obj._consumers)

def test_basic_consume_duplicate_consumer_tag_raises(self):
Expand All @@ -277,21 +279,21 @@ def test_basic_consume_consumers_callback_value(self):
self.obj._set_state(self.obj.OPEN)
consumer_tag = 'ctag1.0'
mock_callback = mock.Mock()
self.obj.basic_consume(mock_callback, 'test-queue')
self.obj.basic_consume(mock_callback, 'test-queue', consumer_tag=consumer_tag)
self.assertEqual(self.obj._consumers[consumer_tag], mock_callback)

def test_basic_consume_has_pending_list(self):
self.obj._set_state(self.obj.OPEN)
consumer_tag = 'ctag1.0'
mock_callback = mock.Mock()
self.obj.basic_consume(mock_callback, 'test-queue')
self.obj.basic_consume(mock_callback, 'test-queue', consumer_tag=consumer_tag)
self.assertIn(consumer_tag, self.obj._pending)

def test_basic_consume_consumers_pending_list_is_empty(self):
self.obj._set_state(self.obj.OPEN)
consumer_tag = 'ctag1.0'
mock_callback = mock.Mock()
self.obj.basic_consume(mock_callback, 'test-queue')
self.obj.basic_consume(mock_callback, 'test-queue', consumer_tag=consumer_tag)
self.assertEqual(self.obj._pending[consumer_tag], list())

@mock.patch('pika.spec.Basic.Consume')
Expand All @@ -300,7 +302,7 @@ def test_basic_consume_consumers_rpc_called(self, rpc, unused):
self.obj._set_state(self.obj.OPEN)
consumer_tag = 'ctag1.0'
mock_callback = mock.Mock()
self.obj.basic_consume(mock_callback, 'test-queue')
self.obj.basic_consume(mock_callback, 'test-queue', consumer_tag=consumer_tag)
expectation = spec.Basic.Consume(queue='test-queue',
consumer_tag=consumer_tag,
no_ack=False,
Expand Down

0 comments on commit 66f8ace

Please sign in to comment.