Skip to content

Commit

Permalink
Test the timing out of jobs.
Browse files Browse the repository at this point in the history
Really looking for a way to speed up this test.  It takes up a whole
second doing nothing now, really.
  • Loading branch information
nvie committed Feb 22, 2012
1 parent 9ac9c23 commit e807748
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
6 changes: 6 additions & 0 deletions tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
This file contains all jobs that are used in tests. Each of these test
fixtures has a slighty different characteristics.
"""
import time


def say_hello(name=None):
Expand Down Expand Up @@ -33,3 +34,8 @@ def create_file(path):
job ran."""
with open(path, 'w') as f:
f.write('Just a sentinel.')


def create_file_after_timeout(path, timeout):
time.sleep(timeout)
create_file(path)
2 changes: 1 addition & 1 deletion tests/test_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ def test_persistence_of_typical_jobs(self):
['created_at', 'data', 'description'])

def test_store_then_fetch(self):
"""Store, then fetch."""
job = Job.create(some_calculation, 3, 4, z=2)
job.save()

Expand Down Expand Up @@ -149,4 +150,3 @@ def test_fetching_unreadable_data(self):
self.testconn.hset(job.key, 'data', unimportable_data)
with self.assertRaises(UnpickleError):
job.refresh()

30 changes: 29 additions & 1 deletion tests/test_worker.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
from tests import RQTestCase
from tests.fixtures import say_hello, div_by_zero, do_nothing, create_file
from tests.fixtures import say_hello, div_by_zero, do_nothing, create_file, \
create_file_after_timeout
from tests.helpers import strip_milliseconds
from rq import Queue, Worker, Job

Expand Down Expand Up @@ -129,3 +130,30 @@ def test_cleaning_up_of_jobs(self):
# results are immediately removed
assert self.testconn.ttl(job_with_rv.key) > 0
assert self.testconn.exists(job_without_rv.key) == False


def test_timeouts(self):
"""Worker kills jobs after timeout."""
sentinel_file = '/tmp/.rq_sentinel'

q = Queue()
w = Worker([q])

# Put it on the queue with a timeout value
res = q.enqueue(
create_file_after_timeout, sentinel_file, 4,
timeout=1)

try:
os.unlink(sentinel_file)
except OSError as e:
if e.errno == 2:
pass

self.assertEquals(os.path.exists(sentinel_file), False)
w.work(burst=True)
self.assertEquals(os.path.exists(sentinel_file), False)

# TODO: Having to do the manual refresh() here is really ugly!
res.refresh()
self.assertIn('JobTimeoutException', res.exc_info)

0 comments on commit e807748

Please sign in to comment.