Permalink
Browse files

added a test for get_job and get_jobs

  • Loading branch information...
1 parent d1e02d4 commit 467e20c4650ab67dc64974620bafffb22dfa5826 @tarekziade committed Aug 8, 2012
Showing with 20 additions and 4 deletions.
  1. +5 −4 retools/queue.py
  2. +15 −0 retools/tests/test_queue.py
View
9 retools/queue.py
@@ -148,17 +148,18 @@ def get_job(self, job_id, queue_name=None, full_job=True):
full_queue_name = 'retools:queue:' + queue_name
current_len = self.redis.llen(full_queue_name)
+ # that's O(n), we should do better
for i in range(current_len):
# the list can change while doing this
# so we need to catch any index error
job = self.redis.lindex(full_queue_name, i)
- job = json.loads(job)
+ job_data = json.loads(job)
- if job['job_id'] == job_id:
+ if job_data['job_id'] == job_id:
if not full_job:
- return job['job_id']
+ return job_data['job_id']
- return Job(full_queue_name, job)
+ return Job(full_queue_name, job, self.redis)
raise IndexError(job_id)
View
15 retools/tests/test_queue.py
@@ -39,8 +39,23 @@ def test_enqueue_job_by_name(self):
mock_pipeline = Mock(spec=redis.client.Pipeline)
mock_redis.pipeline.return_value = mock_pipeline
qm = self._makeQM(redis=mock_redis)
+
job_id = qm.enqueue('retools.tests.jobs:echo_default',
default='hi there')
+
+ mock_redis.llen = Mock(return_value=1)
+ job = json.dumps({'job_id': job_id,
+ 'job': 'retools.tests.jobs:echo_default',
+ 'kwargs': {},
+ 'state': '',
+ 'events': {}})
+
+ mock_redis.lindex = Mock(return_value=job)
+
+ jobs = list(qm.get_jobs())
+ self.assertEqual(len(jobs), 1)
+ my_job = qm.get_job(job_id)
+ self.assertEqual(my_job.job_name, 'retools.tests.jobs:echo_default')
meth, args, kw = mock_pipeline.method_calls[0]
eq_('rpush', meth)
eq_(kw, {})

0 comments on commit 467e20c

Please sign in to comment.