mirror of https://github.com/peter4431/rq.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
79 lines
2.6 KiB
Python
79 lines
2.6 KiB
Python
11 years ago
|
# -*- coding: utf-8 -*-
|
||
|
from __future__ import absolute_import
|
||
|
|
||
|
from rq.job import Job
|
||
11 years ago
|
from rq.queue import FailedQueue, Queue
|
||
11 years ago
|
from rq.utils import current_timestamp
|
||
11 years ago
|
from rq.worker import Worker
|
||
10 years ago
|
from rq.registry import StartedJobRegistry
|
||
11 years ago
|
|
||
|
from tests import RQTestCase
|
||
11 years ago
|
from tests.fixtures import div_by_zero, say_hello
|
||
11 years ago
|
|
||
|
|
||
|
class TestQueue(RQTestCase):
|
||
|
|
||
|
def setUp(self):
|
||
|
super(TestQueue, self).setUp()
|
||
10 years ago
|
self.registry = StartedJobRegistry(connection=self.testconn)
|
||
11 years ago
|
|
||
|
def test_add_and_remove(self):
|
||
10 years ago
|
"""Adding and removing job to StartedJobRegistry."""
|
||
11 years ago
|
timestamp = current_timestamp()
|
||
|
job = Job()
|
||
|
|
||
|
# Test that job is added with the right score
|
||
10 years ago
|
self.registry.add(job, 1000)
|
||
|
self.assertLess(self.testconn.zscore(self.registry.key, job.id),
|
||
10 years ago
|
timestamp + 1002)
|
||
11 years ago
|
|
||
|
# Ensure that job is properly removed from sorted set
|
||
10 years ago
|
self.registry.remove(job)
|
||
|
self.assertIsNone(self.testconn.zscore(self.registry.key, job.id))
|
||
11 years ago
|
|
||
|
def test_get_job_ids(self):
|
||
10 years ago
|
"""Getting job ids from StartedJobRegistry."""
|
||
|
self.testconn.zadd(self.registry.key, 1, 'foo')
|
||
|
self.testconn.zadd(self.registry.key, 10, 'bar')
|
||
|
self.assertEqual(self.registry.get_job_ids(), ['foo', 'bar'])
|
||
11 years ago
|
|
||
|
def test_get_expired_job_ids(self):
|
||
10 years ago
|
"""Getting expired job ids form StartedJobRegistry."""
|
||
11 years ago
|
timestamp = current_timestamp()
|
||
|
|
||
10 years ago
|
self.testconn.zadd(self.registry.key, 1, 'foo')
|
||
|
self.testconn.zadd(self.registry.key, timestamp + 10, 'bar')
|
||
11 years ago
|
|
||
10 years ago
|
self.assertEqual(self.registry.get_expired_job_ids(), ['foo'])
|
||
11 years ago
|
|
||
|
def test_cleanup(self):
|
||
|
"""Moving expired jobs to FailedQueue."""
|
||
|
failed_queue = FailedQueue(connection=self.testconn)
|
||
|
self.assertTrue(failed_queue.is_empty())
|
||
10 years ago
|
self.testconn.zadd(self.registry.key, 1, 'foo')
|
||
|
self.registry.cleanup()
|
||
11 years ago
|
self.assertIn('foo', failed_queue.job_ids)
|
||
|
|
||
11 years ago
|
def test_job_execution(self):
|
||
10 years ago
|
"""Job is removed from StartedJobRegistry after execution."""
|
||
|
registry = StartedJobRegistry(connection=self.testconn)
|
||
11 years ago
|
queue = Queue(connection=self.testconn)
|
||
|
worker = Worker([queue])
|
||
|
|
||
|
job = queue.enqueue(say_hello)
|
||
|
|
||
|
worker.prepare_job_execution(job)
|
||
10 years ago
|
self.assertIn(job.id, registry.get_job_ids())
|
||
11 years ago
|
|
||
|
worker.perform_job(job)
|
||
10 years ago
|
self.assertNotIn(job.id, registry.get_job_ids())
|
||
11 years ago
|
|
||
|
# Job that fails
|
||
|
job = queue.enqueue(div_by_zero)
|
||
|
|
||
|
worker.prepare_job_execution(job)
|
||
10 years ago
|
self.assertIn(job.id, registry.get_job_ids())
|
||
11 years ago
|
|
||
|
worker.perform_job(job)
|
||
10 years ago
|
self.assertNotIn(job.id, registry.get_job_ids())
|