From 16ee71f26de5b0992625442fa59b72c0da928561 Mon Sep 17 00:00:00 2001 From: foxx Date: Mon, 8 Sep 2014 11:31:05 +0100 Subject: [PATCH 1/3] Use class for test worker, and performs PID check in unit tests, #412 --- rq/__init__.py | 2 +- rq/worker.py | 14 ++++++++++++++ tests/fixtures.py | 3 +++ tests/test_worker.py | 19 +++++++++++++++++-- 4 files changed, 35 insertions(+), 3 deletions(-) diff --git a/rq/__init__.py b/rq/__init__.py index 95050f3..0747d7a 100644 --- a/rq/__init__.py +++ b/rq/__init__.py @@ -7,7 +7,7 @@ from .connections import (Connection, get_current_connection, pop_connection, from .job import cancel_job, get_current_job, requeue_job from .queue import get_failed_queue, Queue from .version import VERSION -from .worker import Worker +from .worker import Worker, WorkerTest __all__ = [ 'use_connection', 'get_current_connection', diff --git a/rq/worker.py b/rq/worker.py index f800b28..4c288aa 100644 --- a/rq/worker.py +++ b/rq/worker.py @@ -558,3 +558,17 @@ class Worker(object): def pop_exc_handler(self): """Pops the latest exception handler off of the exc handler stack.""" return self._exc_handlers.pop() + +class WorkerTest(Worker): + def _install_signal_handlers(self, *args, **kwargs): + """Signal handlers are useless for test worker, as it + does not have fork() ability""" + pass + + def main_work_horse(self, *args, **kwargs): + raise NotImplementedError("Test worker does not implement this method") + + def execute_job(self, *args, **kwargs): + """Execute job in same thread/process, do not fork()""" + return self.perform_job(*args, **kwargs) + diff --git a/tests/fixtures.py b/tests/fixtures.py index 7a9ac58..fc7d64f 100644 --- a/tests/fixtures.py +++ b/tests/fixtures.py @@ -6,11 +6,14 @@ fixtures has a slighty different characteristics. from __future__ import (absolute_import, division, print_function, unicode_literals) +import os import time from rq import Connection, get_current_job from rq.decorators import job +def say_pid(): + return os.getpid() def say_hello(name=None): """A job with a single argument and a return value.""" diff --git a/tests/test_worker.py b/tests/test_worker.py index e02ee2a..8ea5f4b 100644 --- a/tests/test_worker.py +++ b/tests/test_worker.py @@ -4,13 +4,13 @@ from __future__ import (absolute_import, division, print_function, import os -from rq import get_failed_queue, Queue, Worker +from rq import get_failed_queue, Queue, Worker, WorkerTest from rq.compat import as_text from rq.job import Job, Status from tests import RQTestCase, slow from tests.fixtures import (create_file, create_file_after_timeout, div_by_zero, - say_hello) + say_hello, say_pid) from tests.helpers import strip_microseconds @@ -277,3 +277,18 @@ class TestWorker(RQTestCase): q = Queue() worker = Worker([q], job_class=CustomJob) self.assertEqual(worker.job_class, CustomJob) + + def test_work_via_workertest(self): + """Worker processes work, with forking disabled, + then returns.""" + fooq, barq = Queue('foo'), Queue('bar') + w = WorkerTest([fooq, barq], fork=False) + self.assertEquals(w.work(burst=True), False, + 'Did not expect any work on the queue.') + + job = fooq.enqueue(say_pid) + self.assertEquals(w.work(burst=True), True, + 'Expected at least some work done.') + self.assertEquals(job.result, os.getpid(), + 'PID mismatch, fork() is not supposed to happen here') + \ No newline at end of file From 23cae3a4209a392617122cc48c8ce8c830e55963 Mon Sep 17 00:00:00 2001 From: foxx Date: Mon, 8 Sep 2014 11:41:22 +0100 Subject: [PATCH 2/3] Fixed bug caused by broken merge, my bad --- tests/test_worker.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_worker.py b/tests/test_worker.py index 8ea5f4b..0c168da 100644 --- a/tests/test_worker.py +++ b/tests/test_worker.py @@ -282,7 +282,7 @@ class TestWorker(RQTestCase): """Worker processes work, with forking disabled, then returns.""" fooq, barq = Queue('foo'), Queue('bar') - w = WorkerTest([fooq, barq], fork=False) + w = WorkerTest([fooq, barq]) self.assertEquals(w.work(burst=True), False, 'Did not expect any work on the queue.') From 52c3ad0fbf4c4d74b1433c523690531af4b02493 Mon Sep 17 00:00:00 2001 From: foxx Date: Mon, 8 Sep 2014 11:43:41 +0100 Subject: [PATCH 3/3] Renamed to SimpleWorker --- rq/__init__.py | 2 +- rq/worker.py | 2 +- tests/test_worker.py | 7 +++---- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/rq/__init__.py b/rq/__init__.py index 0747d7a..5139266 100644 --- a/rq/__init__.py +++ b/rq/__init__.py @@ -7,7 +7,7 @@ from .connections import (Connection, get_current_connection, pop_connection, from .job import cancel_job, get_current_job, requeue_job from .queue import get_failed_queue, Queue from .version import VERSION -from .worker import Worker, WorkerTest +from .worker import Worker, SimpleWorker __all__ = [ 'use_connection', 'get_current_connection', diff --git a/rq/worker.py b/rq/worker.py index 4c288aa..be4a955 100644 --- a/rq/worker.py +++ b/rq/worker.py @@ -559,7 +559,7 @@ class Worker(object): """Pops the latest exception handler off of the exc handler stack.""" return self._exc_handlers.pop() -class WorkerTest(Worker): +class SimpleWorker(Worker): def _install_signal_handlers(self, *args, **kwargs): """Signal handlers are useless for test worker, as it does not have fork() ability""" diff --git a/tests/test_worker.py b/tests/test_worker.py index 0c168da..8aef2ac 100644 --- a/tests/test_worker.py +++ b/tests/test_worker.py @@ -4,7 +4,7 @@ from __future__ import (absolute_import, division, print_function, import os -from rq import get_failed_queue, Queue, Worker, WorkerTest +from rq import get_failed_queue, Queue, Worker, SimpleWorker from rq.compat import as_text from rq.job import Job, Status @@ -278,11 +278,11 @@ class TestWorker(RQTestCase): worker = Worker([q], job_class=CustomJob) self.assertEqual(worker.job_class, CustomJob) - def test_work_via_workertest(self): + def test_work_via_simpleworker(self): """Worker processes work, with forking disabled, then returns.""" fooq, barq = Queue('foo'), Queue('bar') - w = WorkerTest([fooq, barq]) + w = SimpleWorker([fooq, barq]) self.assertEquals(w.work(burst=True), False, 'Did not expect any work on the queue.') @@ -291,4 +291,3 @@ class TestWorker(RQTestCase): 'Expected at least some work done.') self.assertEquals(job.result, os.getpid(), 'PID mismatch, fork() is not supposed to happen here') - \ No newline at end of file