2012-01-28 06:58:40 +00:00
|
|
|
from tests import RQTestCase
|
|
|
|
from tests import testjob
|
|
|
|
from rq import Queue
|
2011-11-15 07:13:16 +00:00
|
|
|
|
|
|
|
|
2011-11-14 13:18:21 +00:00
|
|
|
class TestQueue(RQTestCase):
|
|
|
|
def test_create_queue(self):
|
|
|
|
"""Creating queues."""
|
|
|
|
q = Queue('my-queue')
|
|
|
|
self.assertEquals(q.name, 'my-queue')
|
|
|
|
|
2011-11-15 20:15:51 +00:00
|
|
|
def test_create_default_queue(self):
|
|
|
|
"""Instantiating the default queue."""
|
|
|
|
q = Queue()
|
|
|
|
self.assertEquals(q.name, 'default')
|
|
|
|
|
2011-11-16 11:45:16 +00:00
|
|
|
|
|
|
|
def test_equality(self):
|
|
|
|
"""Mathematical equality of queues."""
|
|
|
|
q1 = Queue('foo')
|
|
|
|
q2 = Queue('foo')
|
|
|
|
q3 = Queue('bar')
|
|
|
|
|
|
|
|
self.assertEquals(q1, q2)
|
|
|
|
self.assertEquals(q2, q1)
|
|
|
|
self.assertNotEquals(q1, q3)
|
|
|
|
self.assertNotEquals(q2, q3)
|
|
|
|
|
|
|
|
|
2011-11-14 13:18:21 +00:00
|
|
|
def test_queue_empty(self):
|
|
|
|
"""Detecting empty queues."""
|
2012-02-07 23:40:43 +00:00
|
|
|
q = Queue('example')
|
2012-02-07 19:53:06 +00:00
|
|
|
self.assertEquals(q.is_empty(), True)
|
2011-11-14 13:18:21 +00:00
|
|
|
|
2012-02-07 23:40:43 +00:00
|
|
|
self.testconn.rpush('rq:queue:example', 'sentinel message')
|
2012-02-07 19:53:06 +00:00
|
|
|
self.assertEquals(q.is_empty(), False)
|
2011-11-14 11:10:59 +00:00
|
|
|
|
2011-11-15 08:36:32 +00:00
|
|
|
|
|
|
|
def test_enqueue(self):
|
2012-02-08 13:18:17 +00:00
|
|
|
"""Enqueueing job onto queues."""
|
2012-02-07 23:40:43 +00:00
|
|
|
q = Queue()
|
2012-02-07 19:53:06 +00:00
|
|
|
self.assertEquals(q.is_empty(), True)
|
2011-11-15 07:13:16 +00:00
|
|
|
|
2011-11-15 07:43:06 +00:00
|
|
|
# testjob spec holds which queue this is sent to
|
2012-02-07 23:40:43 +00:00
|
|
|
job = q.enqueue(testjob, 'Nick', foo='bar')
|
|
|
|
job_id = job.id
|
|
|
|
|
|
|
|
# Inspect data inside Redis
|
|
|
|
q_key = 'rq:queue:default'
|
|
|
|
self.assertEquals(self.testconn.llen(q_key), 1)
|
|
|
|
self.assertEquals(self.testconn.lrange(q_key, 0, -1)[0], job_id)
|
2011-11-15 07:43:06 +00:00
|
|
|
|
2012-01-30 18:41:13 +00:00
|
|
|
|
2012-02-08 13:18:17 +00:00
|
|
|
def test_pop_job_id(self):
|
|
|
|
"""Popping job IDs from queues."""
|
|
|
|
# Set up
|
|
|
|
q = Queue()
|
|
|
|
uuid = '112188ae-4e9d-4a5b-a5b3-f26f2cb054da'
|
|
|
|
q.push_job_id(uuid)
|
|
|
|
|
|
|
|
# Pop it off the queue...
|
|
|
|
self.assertEquals(q.count, 1)
|
|
|
|
self.assertEquals(q.pop_job_id(), uuid)
|
|
|
|
|
|
|
|
# ...and assert the queue count when down
|
|
|
|
self.assertEquals(q.count, 0)
|
|
|
|
|
2011-11-15 08:36:29 +00:00
|
|
|
def test_dequeue(self):
|
2012-02-08 13:18:17 +00:00
|
|
|
"""Dequeueing jobs from queues."""
|
|
|
|
# Set up
|
|
|
|
q = Queue()
|
|
|
|
result = q.enqueue(testjob, 'Rick', foo='bar')
|
2011-11-15 08:36:29 +00:00
|
|
|
|
2012-02-08 13:18:17 +00:00
|
|
|
# Dequeue a job (not a job ID) off the queue
|
|
|
|
self.assertEquals(q.count, 1)
|
2011-11-16 11:44:33 +00:00
|
|
|
job = q.dequeue()
|
2012-02-08 13:18:17 +00:00
|
|
|
self.assertEquals(job.id, result.id)
|
2011-11-16 11:44:33 +00:00
|
|
|
self.assertEquals(job.func, testjob)
|
2012-02-08 16:55:38 +00:00
|
|
|
self.assertEquals(job.origin, q.name)
|
2011-11-16 11:44:33 +00:00
|
|
|
self.assertEquals(job.args[0], 'Rick')
|
|
|
|
self.assertEquals(job.kwargs['foo'], 'bar')
|
|
|
|
|
2012-02-08 13:18:17 +00:00
|
|
|
# ...and assert the queue count when down
|
|
|
|
self.assertEquals(q.count, 0)
|
|
|
|
|
|
|
|
def test_dequeue_ignores_nonexisting_jobs(self):
|
|
|
|
"""Dequeuing silently ignores non-existing jobs."""
|
|
|
|
|
|
|
|
q = Queue()
|
|
|
|
uuid = '49f205ab-8ea3-47dd-a1b5-bfa186870fc8'
|
|
|
|
q.push_job_id(uuid)
|
2012-02-08 14:18:24 +00:00
|
|
|
q.push_job_id(uuid)
|
|
|
|
result = q.enqueue(testjob, 'Nick', foo='bar')
|
|
|
|
q.push_job_id(uuid)
|
2012-02-08 13:18:17 +00:00
|
|
|
|
|
|
|
# Dequeue simply ignores the missing job and returns None
|
2012-02-08 14:18:24 +00:00
|
|
|
self.assertEquals(q.count, 4)
|
|
|
|
self.assertEquals(q.dequeue().id, result.id)
|
|
|
|
self.assertIsNone(q.dequeue())
|
2012-02-08 13:18:17 +00:00
|
|
|
self.assertEquals(q.count, 0)
|
|
|
|
|
2011-11-16 11:44:33 +00:00
|
|
|
def test_dequeue_any(self):
|
|
|
|
"""Fetching work from any given queue."""
|
|
|
|
fooq = Queue('foo')
|
|
|
|
barq = Queue('bar')
|
|
|
|
|
|
|
|
self.assertEquals(Queue.dequeue_any([fooq, barq], False), None)
|
|
|
|
|
|
|
|
# Enqueue a single item
|
|
|
|
barq.enqueue(testjob)
|
2012-02-08 16:55:38 +00:00
|
|
|
job, queue = Queue.dequeue_any([fooq, barq], False)
|
2011-11-16 11:44:33 +00:00
|
|
|
self.assertEquals(job.func, testjob)
|
2012-02-08 16:55:38 +00:00
|
|
|
self.assertEquals(queue, barq)
|
2011-11-16 11:44:33 +00:00
|
|
|
|
|
|
|
# Enqueue items on both queues
|
|
|
|
barq.enqueue(testjob, 'for Bar')
|
|
|
|
fooq.enqueue(testjob, 'for Foo')
|
|
|
|
|
2012-02-08 16:55:38 +00:00
|
|
|
job, queue = Queue.dequeue_any([fooq, barq], False)
|
|
|
|
self.assertEquals(queue, fooq)
|
2011-11-16 11:44:33 +00:00
|
|
|
self.assertEquals(job.func, testjob)
|
2012-02-08 16:55:38 +00:00
|
|
|
self.assertEquals(job.origin, fooq.name)
|
2011-11-16 11:44:33 +00:00
|
|
|
self.assertEquals(job.args[0], 'for Foo', 'Foo should be dequeued first.')
|
|
|
|
|
2012-02-08 16:55:38 +00:00
|
|
|
job, queue = Queue.dequeue_any([fooq, barq], False)
|
|
|
|
self.assertEquals(queue, barq)
|
2011-11-16 11:44:33 +00:00
|
|
|
self.assertEquals(job.func, testjob)
|
2012-02-08 16:55:38 +00:00
|
|
|
self.assertEquals(job.origin, barq.name)
|
2011-11-16 11:44:33 +00:00
|
|
|
self.assertEquals(job.args[0], 'for Bar', 'Bar should be dequeued second.')
|
|
|
|
|
2012-02-08 13:18:17 +00:00
|
|
|
def test_dequeue_any_ignores_nonexisting_jobs(self):
|
|
|
|
"""Dequeuing (from any queue) silently ignores non-existing jobs."""
|
2012-01-27 14:15:13 +00:00
|
|
|
|
2012-02-08 13:18:17 +00:00
|
|
|
q = Queue('low')
|
|
|
|
uuid = '49f205ab-8ea3-47dd-a1b5-bfa186870fc8'
|
|
|
|
q.push_job_id(uuid)
|
2012-01-27 14:15:13 +00:00
|
|
|
|
2012-02-08 13:18:17 +00:00
|
|
|
# Dequeue simply ignores the missing job and returns None
|
|
|
|
self.assertEquals(q.count, 1)
|
|
|
|
self.assertEquals(Queue.dequeue_any([Queue(), Queue('low')], False), None)
|
|
|
|
self.assertEquals(q.count, 0)
|
2012-01-27 14:15:13 +00:00
|
|
|
|