Skip to content

Commit

Permalink
Added the claim_id parameter to message deletion calls.
Browse files Browse the repository at this point in the history
  • Loading branch information
EdLeafe committed Nov 6, 2013
1 parent 3069bd0 commit 3e85985
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 12 deletions.
38 changes: 31 additions & 7 deletions pyrax/queueing.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,13 @@ def get_message(self, msg_id):
return self._message_manager.get(msg_id)


def delete_message(self, msg_id):
def delete_message(self, msg_id, claim_id=None):
"""
Deletes the message whose ID matches the supplied msg_id from this
queue.
Deletes the message whose ID matches the supplied msg_id from the
specified queue. If the message has been claimed, the ID of that claim
must be passed as the 'claim_id' parameter.
"""
return self._message_manager.delete(msg_id)
return self._message_manager.delete(msg_id, claim_id=claim_id)


def list(self, include_claimed=False, echo=False, marker=None, limit=None):
Expand Down Expand Up @@ -251,6 +252,14 @@ def _add_details(self, info):
self.claim_id = query.split("claim_id=")[-1]


def delete(self, claim_id=None):
"""
Deletes this message from its queue. If the message has been claimed,
the ID of that claim must be passed as the 'claim_id' parameter.
"""
return self.manager.delete(self, claim_id=claim_id)



class QueueClaim(BaseResource):
"""
Expand Down Expand Up @@ -330,6 +339,20 @@ def _iterate_list(self, include_claimed, echo, marker, limit):
return ret


def delete(self, msg, claim_id=None):
"""
Deletes the specified message from its queue. If the message has been
claimed, the ID of that claim must be passed as the 'claim_id'
parameter.
"""
msg_id = utils.get_id(msg)
if claim_id:
uri = "/%s/%s?claim_id=%s" % (self.uri_base, msg_id, claim_id)
else:
uri = "/%s/%s" % (self.uri_base, msg_id)
return self._delete(uri)


def list_by_ids(self, ids):
"""
If you wish to retrieve a list of messages from this queue and know the
Expand Down Expand Up @@ -600,12 +623,13 @@ def get_message(self, queue, msg_id):


@assure_queue
def delete_message(self, queue, msg_id):
def delete_message(self, queue, msg_id, claim_id=None):
"""
Deletes the message whose ID matches the supplied msg_id from the
specified queue.
specified queue. If the message has been claimed, the ID of that claim
must be passed as the 'claim_id' parameter.
"""
return queue.delete_message(msg_id)
return queue.delete_message(msg_id, claim_id=claim_id)


@assure_queue
Expand Down
42 changes: 37 additions & 5 deletions tests/unit/test_queues.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,11 @@ def test_queue_get_message(self):
def test_queue_delete_message(self):
q = self.queue
q._message_manager.delete = Mock()
msgid = utils.random_unicode()
q.delete_message(msgid)
q._message_manager.delete.assert_called_once_with(msgid)
msg_id = utils.random_unicode()
claim_id = utils.random_unicode()
q.delete_message(msg_id, claim_id=claim_id)
q._message_manager.delete.assert_called_once_with(msg_id,
claim_id=claim_id)

def test_queue_list(self):
q = self.queue
Expand Down Expand Up @@ -251,6 +253,15 @@ def test_msg_add_details_no_href(self):
self.assertIsNone(msg.id)
self.assertIsNone(msg.claim_id)

def test_msg_delete(self):
q = self.queue
mgr = q._message_manager
claim_id = utils.random_unicode()
mgr.delete = Mock()
msg = QueueMessage(manager=mgr, info={})
msg.delete(claim_id=claim_id)
mgr.delete.assert_called_once_with(msg, claim_id=claim_id)

def test_claim(self):
msgs = []
num = random.randint(1, 9)
Expand Down Expand Up @@ -315,6 +326,26 @@ def test_queue_msg_mgr_no_limit_or_body(self):
msgs = mgr.list(include_claimed=include_claimed, echo=echo,
marker=marker)

def test_queue_msg_mgr_delete_claim(self):
q = self.queue
mgr = q._message_manager
msg = utils.random_unicode()
claim_id = utils.random_unicode()
mgr._delete = Mock()
expected_uri = "/%s/%s?claim_id=%s" % (mgr.uri_base, msg, claim_id)
mgr.delete(msg, claim_id=claim_id)
mgr._delete.assert_called_once_with(expected_uri)

def test_queue_msg_mgr_delete_no_claim(self):
q = self.queue
mgr = q._message_manager
msg = utils.random_unicode()
claim_id = None
mgr._delete = Mock()
expected_uri = "/%s/%s" % (mgr.uri_base, msg)
mgr.delete(msg, claim_id=claim_id)
mgr._delete.assert_called_once_with(expected_uri)

def test_queue_msg_mgr_list_by_ids(self):
q = self.queue
mgr = q._message_manager
Expand Down Expand Up @@ -597,9 +628,10 @@ def test_clt_delete_message(self):
clt = self.client
q = self.queue
msg_id = utils.random_unicode()
claim_id = utils.random_unicode()
q.delete_message = Mock()
clt.delete_message(q, msg_id)
q.delete_message.assert_called_once_with(msg_id)
clt.delete_message(q, msg_id, claim_id=claim_id)
q.delete_message.assert_called_once_with(msg_id, claim_id=claim_id)

def test_clt_list_messages(self):
clt = self.client
Expand Down

0 comments on commit 3e85985

Please sign in to comment.