Clean up type checking of Worker constructor

main
Vincent Driessen 10 years ago
parent 94258761ae
commit 4d0ae5da98

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

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

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

Loading…
Cancel
Save