From f68aeff4810a7e87a16351f570ca5ec5c4e72041 Mon Sep 17 00:00:00 2001 From: Benjamin Root Date: Mon, 9 Jan 2017 11:49:36 -0500 Subject: [PATCH] Added integration test for the metadata persistence feature --- tests/fixtures.py | 6 ++++++ tests/test_worker.py | 22 +++++++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/tests/fixtures.py b/tests/fixtures.py index d040b9d..df946c4 100644 --- a/tests/fixtures.py +++ b/tests/fixtures.py @@ -61,6 +61,12 @@ def access_self(): assert get_current_job() is not None +def modify_self(meta): + j = get_current_job() + j.meta.update(meta) + j.save() + + def echo(*args, **kwargs): return (args, kwargs) diff --git a/tests/test_worker.py b/tests/test_worker.py index 4449aa8..10942bf 100644 --- a/tests/test_worker.py +++ b/tests/test_worker.py @@ -19,7 +19,8 @@ import mock 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) + run_dummy_heroku_worker, access_self, + modify_self) from tests.helpers import strip_microseconds from rq import (get_failed_queue, Queue, SimpleWorker, Worker, @@ -606,6 +607,25 @@ class TestWorker(RQTestCase): # So before that fix the call count was 4 instead of 3 self.assertEqual(mocked.call_count, 3) + def test_self_modification_persistence(self): + """Make sure that any meta modification done by + the job itself persists completely through the + queue/worker/job stack.""" + q = Queue() + # Also make sure that previously existing metadata + # persists properly + job = q.enqueue(modify_self, meta={'foo': 'bar', 'baz': 42}, + args=[{'baz': 10, 'newinfo': 'waka'}]) + + w = Worker([q]) + w.work(burst=True) + + job_check = Job.fetch(job.id) + self.assertEqual(set(job_check.meta.keys()), {'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 kill_worker(pid, double_kill): # wait for the worker to be started over on the main process