Allow queues to be emptied.

This commit is contained in:
Vincent Driessen 2012-02-14 17:53:09 +01:00
parent 9e33e1a08f
commit e4055ca42f
2 changed files with 18 additions and 1 deletions

View File

@ -42,6 +42,10 @@ class Queue(object):
"""Returns the Redis key for this Queue."""
return self._key
def empty(self):
"""Removes all messages on the queue."""
conn.delete(self.key)
def is_empty(self):
"""Returns whether the current queue is empty."""
return self.count == 0

View File

@ -28,7 +28,20 @@ class TestQueue(RQTestCase):
self.assertNotEquals(q2, q3)
def test_queue_empty(self):
def test_empty_queue(self):
"""Emptying queues."""
q = Queue('example')
self.testconn.rpush('rq:queue:example', 'foo')
self.testconn.rpush('rq:queue:example', 'bar')
self.assertEquals(q.is_empty(), False)
q.empty()
self.assertEquals(q.is_empty(), True)
self.assertIsNone(self.testconn.lpop('rq:queue:example'))
def test_queue_is_empty(self):
"""Detecting empty queues."""
q = Queue('example')
self.assertEquals(q.is_empty(), True)