Clean up type checking of Worker constructor

This commit is contained in:
Vincent Driessen 2015-06-03 09:52:26 +02:00
parent 94258761ae
commit 4d0ae5da98
3 changed files with 24 additions and 38 deletions

View File

@ -11,10 +11,6 @@ class InvalidJobOperationError(Exception):
pass
class NoQueueError(Exception):
pass
class UnpickleError(Exception):
def __init__(self, message, raw_data, inner_exception=None):
super(UnpickleError, self).__init__(message, inner_exception)

View File

@ -17,7 +17,7 @@ import warnings
from rq.compat import as_text, string_types, text_type
from .connections import get_current_connection
from .exceptions import DequeueTimeout, NoQueueError
from .exceptions import DequeueTimeout
from .job import Job, JobStatus
from .logutils import setup_loghandlers
from .queue import get_failed_queue, Queue
@ -163,7 +163,7 @@ class Worker(object):
"""Sanity check for the given queues."""
for queue in self.queues:
if not isinstance(queue, self.queue_class):
raise NoQueueError('{0} is not a queue'.format(queue))
raise TypeError('{0} is not a Queue or a string'.format(queue))
def process_queue_args(self, queue_args):
"""Allow for a string, a queue an iterable of strings or an iterable of queues"""

View File

@ -13,7 +13,6 @@ from tests.helpers import strip_microseconds
from rq import get_failed_queue, Queue, SimpleWorker, Worker
from rq.compat import as_text
from rq.exceptions import NoQueueError
from rq.job import Job, JobStatus
from rq.registry import StartedJobRegistry
from rq.suspension import resume, suspend
@ -26,44 +25,35 @@ class CustomJob(Job):
class TestWorker(RQTestCase):
def test_create_worker(self):
"""Worker creation."""
fooq, barq = Queue('foo'), Queue('bar')
w = Worker([fooq, barq])
self.assertEquals(w.queues, [fooq, barq])
"""Worker creation using various inputs."""
def test_create_worker_args_single_queue(self):
"""Test Worker creation with single queue instance arg"""
fooq = Queue('foo')
w = Worker(fooq)
self.assertEquals(w.queue_keys(), ['rq:queue:foo'])
def test_create_worker_args_single_string(self):
""" Test Worker creation with single string arg"""
# With single string argument
w = Worker('foo')
self.assertEquals(w.queue_keys(),['rq:queue:foo'])
self.assertEquals(w.queues[0].name, 'foo')
def test_create_worker_args_iterable_strings(self):
""" Test Worker creation with iterable of strings"""
# With list of strings
w = Worker(['foo', 'bar'])
self.assertEquals(w.queue_keys(),['rq:queue:foo', 'rq:queue:bar'])
self.assertEquals(w.queues[0].name, 'foo')
self.assertEquals(w.queues[1].name, 'bar')
def test_create_worker_args_iterable_queues(self):
""" Test Worker test worker creation
with an iterable of queue instance args"""
w = Worker(map(Queue, ['foo', 'bar']))
self.assertEquals(w.queue_keys(),['rq:queue:foo', 'rq:queue:bar'])
# With iterable of strings
w = Worker(iter(['foo', 'bar']))
self.assertEquals(w.queues[0].name, 'foo')
self.assertEquals(w.queues[1].name, 'bar')
def test_create_worker_args_list_map(self):
""" Test Worker test worker creation
with a list of queue from map"""
w = Worker(list(map(Queue, ['foo', 'bar'])))
self.assertEquals(w.queue_keys(),['rq:queue:foo', 'rq:queue:bar'])
# With single Queue
w = Worker(Queue('foo'))
self.assertEquals(w.queues[0].name, 'foo')
def test_create_worker_raises_noqueue_error(self):
""" make sure raises noqueue error if a
a non string or queue is passed"""
with self.assertRaises(NoQueueError):
w = Worker([1])
# With iterable of Queues
w = Worker(iter([Queue('foo'), Queue('bar')]))
self.assertEquals(w.queues[0].name, 'foo')
self.assertEquals(w.queues[1].name, 'bar')
# With list of Queues
w = Worker([Queue('foo'), Queue('bar')])
self.assertEquals(w.queues[0].name, 'foo')
self.assertEquals(w.queues[1].name, 'bar')
def test_work_and_quit(self):
"""Worker processes work, then quits."""