2012-02-10 16:17:38 +00:00
|
|
|
import times
|
2012-02-07 23:40:43 +00:00
|
|
|
from datetime import datetime
|
2012-01-28 06:58:40 +00:00
|
|
|
from tests import RQTestCase
|
2012-02-15 20:55:06 +00:00
|
|
|
from tests.fixtures import some_calculation
|
2012-02-10 16:17:38 +00:00
|
|
|
from tests.helpers import strip_milliseconds
|
2012-02-13 12:25:46 +00:00
|
|
|
from cPickle import loads
|
2012-02-15 14:54:02 +00:00
|
|
|
from rq import Job
|
2012-02-07 23:40:43 +00:00
|
|
|
from rq.exceptions import NoSuchJobError, UnpickleError
|
2012-01-28 08:00:02 +00:00
|
|
|
|
|
|
|
|
2012-01-28 06:58:40 +00:00
|
|
|
class TestJob(RQTestCase):
|
2012-02-07 23:40:43 +00:00
|
|
|
def test_create_empty_job(self):
|
|
|
|
"""Creation of new empty jobs."""
|
|
|
|
job = Job()
|
|
|
|
|
2012-02-10 16:17:38 +00:00
|
|
|
# Jobs have a random UUID and a creation date
|
2012-02-07 23:40:43 +00:00
|
|
|
self.assertIsNotNone(job.id)
|
|
|
|
self.assertIsNotNone(job.created_at)
|
|
|
|
|
2012-02-10 16:17:38 +00:00
|
|
|
# ...and nothing else
|
|
|
|
self.assertIsNone(job.func, None)
|
|
|
|
self.assertIsNone(job.args, None)
|
|
|
|
self.assertIsNone(job.kwargs, None)
|
|
|
|
self.assertIsNone(job.origin, None)
|
|
|
|
self.assertIsNone(job.enqueued_at, None)
|
|
|
|
self.assertIsNone(job.ended_at, None)
|
2012-02-13 15:43:27 +00:00
|
|
|
self.assertIsNone(job.return_value, None)
|
2012-02-10 16:17:38 +00:00
|
|
|
self.assertIsNone(job.exc_info, None)
|
|
|
|
|
|
|
|
def test_create_typical_job(self):
|
2012-02-07 23:40:43 +00:00
|
|
|
"""Creation of jobs for function calls."""
|
2012-02-15 20:55:06 +00:00
|
|
|
job = Job.create(some_calculation, 3, 4, z=2)
|
2012-02-07 23:40:43 +00:00
|
|
|
|
|
|
|
# Jobs have a random UUID
|
|
|
|
self.assertIsNotNone(job.id)
|
|
|
|
self.assertIsNotNone(job.created_at)
|
2012-02-10 16:17:38 +00:00
|
|
|
self.assertIsNotNone(job.description)
|
2012-02-07 23:40:43 +00:00
|
|
|
|
|
|
|
# Job data is set...
|
2012-02-15 20:55:06 +00:00
|
|
|
self.assertEquals(job.func, some_calculation)
|
2012-01-28 08:00:02 +00:00
|
|
|
self.assertEquals(job.args, (3, 4))
|
|
|
|
self.assertEquals(job.kwargs, {'z': 2})
|
2012-02-07 23:40:43 +00:00
|
|
|
|
|
|
|
# ...but metadata is not
|
2012-01-28 08:00:02 +00:00
|
|
|
self.assertIsNone(job.origin)
|
2012-01-30 15:49:15 +00:00
|
|
|
self.assertIsNone(job.enqueued_at)
|
2012-02-13 15:43:27 +00:00
|
|
|
self.assertIsNone(job.return_value)
|
2012-02-07 23:40:43 +00:00
|
|
|
|
|
|
|
|
2012-02-13 16:32:16 +00:00
|
|
|
def test_save(self): # noqa
|
2012-02-07 23:40:43 +00:00
|
|
|
"""Storing jobs."""
|
2012-02-15 20:55:06 +00:00
|
|
|
job = Job.create(some_calculation, 3, 4, z=2)
|
2012-01-28 08:00:02 +00:00
|
|
|
|
2012-02-07 23:40:43 +00:00
|
|
|
# Saving creates a Redis hash
|
|
|
|
self.assertEquals(self.testconn.exists(job.key), False)
|
|
|
|
job.save()
|
|
|
|
self.assertEquals(self.testconn.type(job.key), 'hash')
|
|
|
|
|
|
|
|
# Saving writes pickled job data
|
|
|
|
unpickled_data = loads(self.testconn.hget(job.key, 'data'))
|
2012-02-15 20:55:06 +00:00
|
|
|
self.assertEquals(unpickled_data[0], some_calculation)
|
2012-02-07 23:40:43 +00:00
|
|
|
|
|
|
|
def test_fetch(self):
|
|
|
|
"""Fetching jobs."""
|
|
|
|
# Prepare test
|
2012-02-13 16:32:16 +00:00
|
|
|
self.testconn.hset('rq:job:some_id', 'data',
|
2012-02-15 20:55:06 +00:00
|
|
|
"(ctest_job\nsome_calculation\np0\n(I3\nI4\ntp1\n(dp2\nS'z'\np3\nI2\nstp4\n.") # noqa
|
2012-02-13 16:32:16 +00:00
|
|
|
self.testconn.hset('rq:job:some_id', 'created_at',
|
|
|
|
"2012-02-07 22:13:24+0000")
|
2012-02-07 23:40:43 +00:00
|
|
|
|
|
|
|
# Fetch returns a job
|
|
|
|
job = Job.fetch('some_id')
|
|
|
|
self.assertEquals(job.id, 'some_id')
|
2012-02-15 20:55:06 +00:00
|
|
|
self.assertEquals(job.func, some_calculation)
|
2012-02-07 23:40:43 +00:00
|
|
|
self.assertEquals(job.args, (3, 4))
|
|
|
|
self.assertEquals(job.kwargs, dict(z=2))
|
|
|
|
self.assertEquals(job.created_at, datetime(2012, 2, 7, 22, 13, 24))
|
|
|
|
|
|
|
|
|
2012-02-13 16:32:16 +00:00
|
|
|
def test_persistence_of_empty_jobs(self): # noqa
|
2012-02-10 16:17:38 +00:00
|
|
|
"""Storing empty jobs."""
|
|
|
|
job = Job()
|
|
|
|
job.save()
|
|
|
|
|
|
|
|
expected_date = strip_milliseconds(job.created_at)
|
|
|
|
stored_date = self.testconn.hget(job.key, 'created_at')
|
|
|
|
self.assertEquals(
|
|
|
|
times.to_universal(stored_date),
|
|
|
|
expected_date)
|
|
|
|
|
|
|
|
# ... and no other keys are stored
|
|
|
|
self.assertItemsEqual(
|
|
|
|
self.testconn.hkeys(job.key),
|
|
|
|
['created_at'])
|
|
|
|
|
|
|
|
def test_persistence_of_typical_jobs(self):
|
|
|
|
"""Storing typical jobs."""
|
2012-02-15 20:55:06 +00:00
|
|
|
job = Job.create(some_calculation, 3, 4, z=2)
|
2012-02-10 16:17:38 +00:00
|
|
|
job.save()
|
|
|
|
|
|
|
|
expected_date = strip_milliseconds(job.created_at)
|
|
|
|
stored_date = self.testconn.hget(job.key, 'created_at')
|
|
|
|
self.assertEquals(
|
|
|
|
times.to_universal(stored_date),
|
|
|
|
expected_date)
|
|
|
|
|
|
|
|
# ... and no other keys are stored
|
|
|
|
self.assertItemsEqual(
|
|
|
|
self.testconn.hkeys(job.key),
|
|
|
|
['created_at', 'data', 'description'])
|
|
|
|
|
|
|
|
def test_store_then_fetch(self):
|
2012-02-15 20:55:06 +00:00
|
|
|
job = Job.create(some_calculation, 3, 4, z=2)
|
2012-02-07 23:40:43 +00:00
|
|
|
job.save()
|
|
|
|
|
|
|
|
job2 = Job.fetch(job.id)
|
2012-01-28 08:00:02 +00:00
|
|
|
self.assertEquals(job.func, job2.func)
|
|
|
|
self.assertEquals(job.args, job2.args)
|
|
|
|
self.assertEquals(job.kwargs, job2.kwargs)
|
|
|
|
|
2012-02-07 23:40:43 +00:00
|
|
|
# Mathematical equation
|
|
|
|
self.assertEquals(job, job2)
|
|
|
|
|
|
|
|
def test_fetching_can_fail(self):
|
|
|
|
"""Fetching fails for non-existing jobs."""
|
|
|
|
with self.assertRaises(NoSuchJobError):
|
|
|
|
Job.fetch('b4a44d44-da16-4620-90a6-798e8cd72ca0')
|
|
|
|
|
2012-02-10 16:17:38 +00:00
|
|
|
def test_fetching_unreadable_data(self):
|
|
|
|
"""Fetching fails on unreadable data."""
|
2012-02-08 13:18:17 +00:00
|
|
|
# Set up
|
2012-02-15 20:55:06 +00:00
|
|
|
job = Job.create(some_calculation, 3, 4, z=2)
|
2012-02-08 13:18:17 +00:00
|
|
|
job.save()
|
2012-01-28 08:00:02 +00:00
|
|
|
|
2012-02-08 13:18:17 +00:00
|
|
|
# Just replace the data hkey with some random noise
|
|
|
|
self.testconn.hset(job.key, 'data', 'this is no pickle string')
|
2012-01-28 08:00:02 +00:00
|
|
|
with self.assertRaises(UnpickleError):
|
2012-02-08 13:18:17 +00:00
|
|
|
job.refresh()
|
2012-01-28 08:00:02 +00:00
|
|
|
|
2012-02-08 13:18:17 +00:00
|
|
|
# Set up (part B)
|
2012-02-15 20:55:06 +00:00
|
|
|
job = Job.create(some_calculation, 3, 4, z=2)
|
2012-02-08 13:18:17 +00:00
|
|
|
job.save()
|
2012-01-28 06:58:40 +00:00
|
|
|
|
2012-02-08 13:18:17 +00:00
|
|
|
# Now slightly modify the job to make it unpickl'able (this is
|
|
|
|
# equivalent to a worker not having the most up-to-date source code and
|
|
|
|
# unable to import the function)
|
|
|
|
data = self.testconn.hget(job.key, 'data')
|
2012-02-15 20:55:06 +00:00
|
|
|
unimportable_data = data.replace('some_calculation', 'broken')
|
2012-02-08 13:18:17 +00:00
|
|
|
self.testconn.hset(job.key, 'data', unimportable_data)
|
|
|
|
with self.assertRaises(UnpickleError):
|
|
|
|
job.refresh()
|
2012-02-15 15:59:27 +00:00
|
|
|
|