Skip to content

Commit

Permalink
Merge pull request rq#477 from glaslos/ttl_tests_fixes
Browse files Browse the repository at this point in the history
TTL tests and fixes
  • Loading branch information
selwin committed Mar 25, 2015
2 parents 04b8ea4 + 071c47d commit cff6fbf
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 8 deletions.
2 changes: 2 additions & 0 deletions rq/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,8 @@ def delete(self, pipeline=None):
# Job execution
def perform(self): # noqa
"""Invokes the job function with the job arguments."""
self.connection.persist(self.key)
self.ttl = -1
_job_stack.push(self.id)
try:
self._result = self.func(*self.args, **self.kwargs)
Expand Down
2 changes: 1 addition & 1 deletion rq/queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ def enqueue_call(self, func, args=None, kwargs=None, timeout=None,

job = self.job_class.create(
func, args, kwargs, connection=self.connection,
result_ttl=result_ttl, status=JobStatus.QUEUED,
result_ttl=result_ttl, ttl=ttl, status=JobStatus.QUEUED,
description=description, depends_on=depends_on,
timeout=timeout, id=job_id, origin=self.name)

Expand Down
5 changes: 3 additions & 2 deletions tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,6 @@ def decorated_job(x, y):
return x + y


def long_running_job():
time.sleep(10)
def long_running_job(timeout=10):
time.sleep(timeout)
return 'Done sleeping...'
28 changes: 23 additions & 5 deletions tests/test_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@

from datetime import datetime

from tests import RQTestCase
from tests.fixtures import (access_self, CallableObject, Number, say_hello,
some_calculation)
from tests.helpers import strip_microseconds

from rq.compat import as_text, PY2
from rq.exceptions import NoSuchJobError, UnpickleError
from rq.job import get_current_job, Job
from rq.queue import Queue
from rq.registry import DeferredJobRegistry
from rq.utils import utcformat

from tests import RQTestCase
from tests.fixtures import (access_self, CallableObject, Number, say_hello,
some_calculation, long_running_job)
from tests.helpers import strip_microseconds

try:
from cPickle import loads, dumps
except ImportError:
Expand Down Expand Up @@ -334,6 +334,24 @@ def test_get_job_ttl(self):
job.save()
self.assertEqual(job.get_ttl(), None)

def test_ttl_via_enqueue(self):
ttl = 1
queue = Queue(connection=self.testconn)
job = queue.enqueue(say_hello, ttl=ttl)
self.assertEqual(job.get_ttl(), ttl)

def test_never_expire_during_execution(self):
"""Test what happens when job expires during execution"""
ttl = 1
queue = Queue(connection=self.testconn)
job = queue.enqueue(long_running_job, args=(2,), ttl=ttl)
self.assertEqual(job.get_ttl(), ttl)
job.save()
job.perform()
self.assertEqual(job.get_ttl(), -1)
self.assertTrue(job.exists(job.id))
self.assertEqual(job.result, 'Done sleeping...')

def test_cleanup(self):
"""Test that jobs and results are expired properly."""
job = Job.create(func=say_hello)
Expand Down

0 comments on commit cff6fbf

Please sign in to comment.