diff --git a/tests/fixtures.py b/tests/fixtures.py index df946c45..031a0e25 100644 --- a/tests/fixtures.py +++ b/tests/fixtures.py @@ -67,6 +67,13 @@ def modify_self(meta): j.save() +def modify_self_and_error(meta): + j = get_current_job() + j.meta.update(meta) + j.save() + return 1 / 0 + + def echo(*args, **kwargs): return (args, kwargs) diff --git a/tests/test_worker.py b/tests/test_worker.py index 10942bff..885de296 100644 --- a/tests/test_worker.py +++ b/tests/test_worker.py @@ -20,7 +20,7 @@ from tests import RQTestCase, slow from tests.fixtures import (create_file, create_file_after_timeout, div_by_zero, do_nothing, say_hello, say_pid, run_dummy_heroku_worker, access_self, - modify_self) + modify_self, modify_self_and_error) from tests.helpers import strip_microseconds from rq import (get_failed_queue, Queue, SimpleWorker, Worker, @@ -621,7 +621,34 @@ class TestWorker(RQTestCase): w.work(burst=True) job_check = Job.fetch(job.id) - self.assertEqual(set(job_check.meta.keys()), {'foo', 'baz', 'newinfo'}) + self.assertEqual(set(job_check.meta.keys()), + set(['foo', 'baz', 'newinfo'])) + self.assertEqual(job_check.meta['foo'], 'bar') + self.assertEqual(job_check.meta['baz'], 10) + self.assertEqual(job_check.meta['newinfo'], 'waka') + + def test_self_modification_persistence_with_error(self): + """Make sure that any meta modification done by + the job itself persists completely through the + queue/worker/job stack -- even if the job errored""" + q = Queue() + failed_q = get_failed_queue() + # Also make sure that previously existing metadata + # persists properly + job = q.enqueue(modify_self_and_error, meta={'foo': 'bar', 'baz': 42}, + args=[{'baz': 10, 'newinfo': 'waka'}]) + + w = Worker([q]) + w.work(burst=True) + + # Postconditions + self.assertEqual(q.count, 0) + self.assertEqual(failed_q.count, 1) + self.assertEqual(w.get_current_job_id(), None) + + job_check = Job.fetch(job.id) + self.assertEqual(set(job_check.meta.keys()), + set(['foo', 'baz', 'newinfo'])) self.assertEqual(job_check.meta['foo'], 'bar') self.assertEqual(job_check.meta['baz'], 10) self.assertEqual(job_check.meta['newinfo'], 'waka')