diff --git a/rq/queue.py b/rq/queue.py index 2ca5f532..562d7fc4 100644 --- a/rq/queue.py +++ b/rq/queue.py @@ -19,5 +19,9 @@ class Queue(object): def empty(self): return conn.llen(self.key) == 0 + @property + def messages(self): + return conn.lrange(self.key, 0, -1) + def __str__(self): return self.name diff --git a/tests/test_rq.py b/tests/test_rq.py index 26c46cb9..2a9b29b0 100644 --- a/tests/test_rq.py +++ b/tests/test_rq.py @@ -1,7 +1,12 @@ import unittest +from pickle import loads from blinker import signal from redis import Redis -from rq import conn, Queue +from rq import conn, Queue, job + +@job('my-queue') +def testjob(): + return 'hi there' class RQTestCase(unittest.TestCase): @@ -32,6 +37,16 @@ class RQTestCase(unittest.TestCase): super(RQTestCase, self).tearDown() + def assertQueueContains(self, queue, that_func): + # Do a queue scan (this is O(n), but we're in a test, so hey) + for message in queue.messages: + f, _, args, kwargs = loads(message) + if f == that_func: + return + self.fail('Queue %s does not contain message for function %s' % + (queue.key, that_func)) + + class TestQueue(RQTestCase): def test_create_queue(self): """Creating queues.""" @@ -46,6 +61,15 @@ class TestQueue(RQTestCase): conn.rpush('rq:my-queue', 'some val') self.assertEquals(q.empty, False) + def test_put_work_on_queue(self): + """Putting work on queues.""" + q = Queue('my-queue') + self.assertEquals(q.empty, True) + + testjob.delay() + self.assertEquals(q.empty, False) + self.assertQueueContains(q, testjob) + if __name__ == '__main__': unittest.main()