class TestEnqueueNext(TransactionTestCase): def setUp(self): self.q = Queue() self.job = Job.create(func=some_calculation, args=(3, 4), kwargs=dict(z=2), repeat=1, interval=60) self.job.save() def test_enqueue_next(self): """Schedule the next job""" job = self.q.enqueue_next(self.job) self.assertIsNotNone(job.id) self.assertNotEqual(job.id, self.job.id) self.assertEqual(job.scheduled_for, self.job.scheduled_for + self.job.interval) def test_schedule_repeat_infinity(self): """Schedule repeats for infinity""" self.job.repeat = -1 job = self.q.enqueue_next(self.job) self.assertIsNotNone(job.id) self.assertNotEqual(job.id, self.job.id) self.assertEqual(job.repeat, self.job.repeat) def test_schedule_repeat_until(self): """Schedule repeat until datetime""" self.job.repeat = datetime(2999,1,1, tzinfo=utc) job = self.q.enqueue_next(self.job) self.assertIsNotNone(job.id) self.assertNotEqual(job.id, self.job.id) self.assertEqual(job.repeat, self.job.repeat)
class TestWorkIsUnreadable(TransactionTestCase): def setUp(self): self.q = Queue() self.q.save() self.fq = get_failed_queue() self.w = Worker.create([self.q]) def test_work_is_unreadable(self): """Unreadable jobs are put on the failed queue.""" self.assertEquals(self.fq.count, 0) self.assertEquals(self.q.count, 0) # NOTE: We have to fake this enqueueing for this test case. # What we're simulating here is a call to a function that is not # importable from the worker process. job = Job.create(func=div_by_zero, args=(3,)) job.save() job.instance = 'nonexisting_job' job.queue = self.q job.save() self.assertEquals(self.q.count, 1) # All set, we're going to process it self.w.work(burst=True) # should silently pass self.assertEquals(self.q.count, 0) self.assertEquals(self.fq.count, 1)
class TestNotify(TransactionTestCase): def setUp(self): self.q = Queue() def test_notify(self): """Postgresql NOTIFY on channel with default connection""" self.q.notify(1)
class TestWorkerWithJobs(TransactionTestCase): def setUp(self): self.fooq, self.barq = Queue('foo'), Queue('bar') self.w = Worker.create([self.fooq, self.barq]) self.fooq.enqueue(say_hello, name='Frank') def test_worker_with_jobs(self): self.assertEqual(self.w.work(burst=True), True, 'Expected at least some work done.')
class TestPQWorkerSerial(TransactionTestCase): reset_sequences = True def setUp(self): self.q = Queue() self.q.save_queue() self.sq = SerialQueue() self.sq.save_queue() def test_pq_worker_serial(self): call_command('pqworker', 'serial', 'default', burst=True)
class TestDequeueAnySingle(TransactionTestCase): def setUp(self): self.fooq = Queue('foo') self.barq = Queue('bar') # Enqueue a single item self.barq.enqueue(say_hello) def test_dequeue_any_single(self): job, queue = PQ.dequeue_any([self.fooq, self.barq], None) self.assertEqual(job.func, say_hello) self.assertEqual(queue, self.barq)
class TestRequeueAdminAction(TestCase): def setUp(self): self.q = Queue() self.q.enqueue_call(div_by_zero, args=(1,)) w = Worker.create(self.q) w.work(burst=True) def test_requeue_admin_action(self): self.assertEqual(0, len(Job.objects.filter(queue_id="default"))) requeue_failed_jobs(None, None, Job.objects.filter(queue_id="failed")) self.assertEqual(0, len(Job.objects.filter(queue_id="failed"))) self.assertEqual("test_pq.fixtures.div_by_zero", Job.objects.get(queue_id="default").func_name)
def setUp(self): q = Queue() q.enqueue(say_hello, kwargs={'name':'bob'}, result_ttl=1) # expires q.enqueue(say_hello, kwargs={'name':'polly'}) # won't expire in this test lifecycle q.enqueue(say_hello, kwargs={'name':'frank'}, result_ttl=-1) # never expires w = Worker.create([q]) w.work(burst=True) q.enqueue(say_hello, kwargs={'name':'david'}) # hasn't run yet self.q = q
class TestDequeueInstanceMethods(TransactionTestCase): def setUp(self): self.q = Queue() self.c = Calculator(2) self.result = self.q.enqueue(self.c.calculate, 3, 4) def test_dequeue_instance_method(self): """Dequeueing instance method jobs from queues.""" job = self.q.dequeue() self.assertEqual(job.func.__name__, 'calculate') self.assertEqual(job.args, (3, 4))
class TestJobAdmin(TransactionTestCase): def setUp(self): password = '******' user = User.objects.create_superuser('test', '*****@*****.**', password) self.client.login(username=user.username, password=password) self.q = Queue() self.q.enqueue_call(say_hello, args=('you',)) self.q.enqueue_call(div_by_zero, args=(1,)) self.q.schedule(datetime(2099, 1, 1, tzinfo=utc), say_hello, 'later') w = Worker.create(self.q) w.work(burst=True) self.q.enqueue_call(say_hello, args=('me',)) def test_changelist(self): data = ( ("failedjob", FailedJob), ("queuedjob", QueuedJob), ("dequeuedjob", DequeuedJob), ("scheduledjob", ScheduledJob), ) for modelname, Model in data: url = reverse("admin:pq_%s_changelist" % modelname) response = self.client.get(url, follow=True) msg = "%s != %s -> %s, url: %s" % ( response.status_code, 200, repr(Model), url ) self.assertEqual(response.status_code, 200, msg)
class TestListenForJobs(TransactionTestCase): def setUp(self): self.q = Queue() # pre-call this so we don't need to use multi-process # otherwise this is called within the classmethod PQ.listen('default', ['default']) # Fire off a notification of a fake job enqueued self.q.notify(1) def test_listen_for_jobs(self): """Test the first part of the _listen_for_jobs method which polls for notifications""" queue_name = PQ._listen_for_jobs(['default'], 'default', 1) self.assertEqual('default', queue_name)
class TestEnqueueAsyncFalse(TestCase): def setUp(self): self.q = Queue() def test_enqueue_async_false(self): job = self.q.enqueue(some_calculation, args=(2, 3), async=False) self.assertEqual(job.result, 6) def test_enqueue_call_async_false(self): job = self.q.enqueue_call(some_calculation, args=(2, 3), async=False) self.assertEqual(job.result, 6) def test_schedule_call_async_false(self): job = self.q.enqueue_call(some_calculation, args=(2, 3), async=False) self.assertEqual(job.result, 6)
def setUp(self): self.q = Queue(scheduled=True) # simulate the default worker timeout self.timeout = 60 future = now() + timedelta(seconds=self.timeout/2) # enqueue a job for 30 seconds time in the future self.job = self.q.schedule_call(future, do_nothing)
class TestDequeueOnEmpty(TransactionTestCase): def setUp(self): self.q = Queue() def test_pop_job_on_empty(self): job = self.q.dequeue() self.assertIsNone(job)
def setUp(self): self.q = Queue() # pre-call this so we don't need to use multi-process # otherwise this is called within the classmethod PQ.listen('default', ['default']) # Fire off a notification of a fake job enqueued self.q.notify(1)
class TestWorkFails(TransactionTestCase): def setUp(self): self.q = Queue() self.fq = get_failed_queue() self.w = Worker.create([self.q]) self.job = self.q.enqueue(div_by_zero) self.enqueued_at = self.job.enqueued_at def test_work_fails(self): """Failing jobs are put on the failed queue.""" self.w.work(burst=True) # should silently pass # Postconditions self.assertEquals(self.q.count, 0) self.assertEquals(self.fq.count, 1) # Check the job job = Job.objects.get(id=self.job.id) self.assertEquals(job.origin, self.q.name) # Should be the original enqueued_at date, not the date of enqueueing # to the failed queue self.assertEquals(job.enqueued_at, self.enqueued_at) self.assertIsNotNone(job.exc_info) # should contain exc_info
class TestWorkerTimeouts(TransactionTestCase): def setUp(self): self.sentinel_file = '/tmp/.rq_sentinel' self.q = Queue() self.fq = get_failed_queue() self.w = Worker.create([self.q]) def test_timeouts(self): """Worker kills jobs after timeout.""" # Put it on the queue with a timeout value jobr = self.q.enqueue( create_file_after_timeout, args=(self.sentinel_file, 4), timeout=1) self.assertEquals(os.path.exists(self.sentinel_file), False) self.w.work(burst=True) self.assertEquals(os.path.exists(self.sentinel_file), False) job = Job.objects.get(id=jobr.id) self.assertIn('JobTimeoutException', job.exc_info) def tearDown(self): try: os.unlink(self.sentinel_file) except OSError as e: if e.errno == 2: pass
class TestWorkerCustomExcHandling(TransactionTestCase): def setUp(self): self.q = Queue() self.fq = get_failed_queue() def black_hole(job, *exc_info): # Don't fall through to default behaviour of moving to failed queue return False self.black_hole = black_hole self.job = self.q.enqueue(div_by_zero) def test_custom_exc_handling(self): """Custom exception handling.""" w = Worker.create([self.q], exc_handler=self.black_hole) w.work(burst=True) # should silently pass # Postconditions self.assertEquals(self.q.count, 0) self.assertEquals(self.fq.count, 0) # Check the job job = Job.objects.get(id=self.job.id) self.assertEquals(job.status, Job.FAILED)
def setUp(self): self.q = Queue() self.job = Job.create(func=some_calculation, args=(3, 4), kwargs=dict(z=2), repeat=1, interval=60) self.job.save()
def setUp(self): self.q = Queue() self.fq = get_failed_queue() def black_hole(job, *exc_info): # Don't fall through to default behaviour of moving to failed queue return False self.black_hole = black_hole self.job = self.q.enqueue(div_by_zero)
def setUp(self): self.q = Queue() self.q.save() self.job = Job.create( func=say_hello, args=('Nick',), kwargs=dict(foo='bar') )
class TestScheduleJobs(TransactionTestCase): def setUp(self): self.q = Queue() self.w = Worker.create([self.q]) def test_shedule_call(self): """Schedule to fire now""" job = self.q.schedule_call(now(), do_nothing) self.w.work(burst=True) with self.assertRaises(Job.DoesNotExist) as exc: Job.objects.get(queue_id='default', pk=job.id) def test_schedule_future_call(self): """Schedule to fire in the distant future""" job = self.q.schedule_call(datetime(2999,12,1, tzinfo=utc), do_nothing) self.w.work(burst=True) # check it is still in the queue self.assertIsNotNone(Job.objects.get(queue_id='default', pk=job.id))
def setUp(self): password = "******" user = User.objects.create_superuser("test", "*****@*****.**", password) self.client.login(username=user.username, password=password) self.q = Queue() self.q.enqueue_call(say_hello, args=("you",)) self.q.enqueue_call(div_by_zero, args=(1,)) w = Worker.create(self.q) w.work(burst=True) self.q.enqueue_call(say_hello, args=("me",))
class TestQueueInstanceMethods(TransactionTestCase): def setUp(self): self.q = Queue() def test_enqueue(self): # noqa """Enqueueing job onto queues.""" # say_hello spec holds which queue this is sent to job = self.q.enqueue(say_hello, 'Nick', foo='bar') self.assertEqual(job.queue, self.q)
def setUp(self): # setup a job in the very near future which # should execute self.q = Queue(scheduled=True) # simulate the default worker timeout self.timeout = 60 future = now() + timedelta(seconds=1) # enqueue a job for 1 second time in the future self.job = self.q.schedule_call(future, do_nothing) time.sleep(1)
class Test_get_job_or_promise(TransactionTestCase): """Test the Job._get_job_or_promise classmethod""" def setUp(self): self.q = Queue(scheduled=True) # simulate the default worker timeout self.timeout = 60 future = now() + timedelta(seconds=self.timeout/2) # enqueue a job for 30 seconds time in the future self.job = self.q.schedule_call(future, do_nothing) def test_get_job_or_promise(self): """Test get a promise of a job in the future""" job, promise, timeout = Job._get_job_or_promise( self.q.connection, self.q, self.timeout) self.assertLessEqual(timeout, self.timeout) self.assertIsNone(job) self.assertEqual(promise, self.q.name) def test_get_no_job_no_promise(self): """Test get no job and no promise""" # job is in the future beyond the current # worker timeout job, promise, timeout = Job._get_job_or_promise( self.q.connection, self.q, 1) self.assertEqual(timeout, 1) self.assertIsNone(job) self.assertIsNone(promise) def test_get_earlier_job_no_promise(self): """Test get earlier job and no promise""" # Job enqueue after the first scheduled job # but to be exec ahead of the scheduled job now_job = self.q.enqueue(do_nothing) job, promise, timeout = Job._get_job_or_promise( self.q.connection, self.q, 60) # timeout should remain the same self.assertEqual(timeout, 60) self.assertEqual(now_job.id, job.id) self.assertIsNone(promise)
def setUp(self): password = '******' user = User.objects.create_superuser('test', '*****@*****.**', password) self.client.login(username=user.username, password=password) self.q = Queue() self.q.enqueue_call(say_hello, args=('you',)) self.q.enqueue_call(div_by_zero, args=(1,)) self.q.schedule(datetime(2099, 1, 1, tzinfo=utc), say_hello, 'later') w = Worker.create(self.q) w.work(burst=True) self.q.enqueue_call(say_hello, args=('me',))
class TestEnqueue(TransactionTestCase): def setUp(self): self.q = Queue() self.job = Job.create(func=say_hello, args=('Nick',), kwargs=dict(foo='bar')) def test_enqueue_sets_metadata(self): """Enqueueing job onto queues modifies meta data.""" # Preconditions self.assertIsNone(self.job.origin) self.assertIsNone(self.job.enqueued_at) # Action self.q.enqueue_job(self.job) # Postconditions self.assertEquals(self.job.origin, self.q.name) self.assertIsNotNone(self.job.enqueued_at)
class TestDequeue(TransactionTestCase): def setUp(self): self.q = Queue() self.result = self.q.enqueue(say_hello, 'Rick', foo='bar') # self.result2 = q.enqueue(c.calculate, 3, 4) # self.c = Calculator(2) def test_dequeue(self): """Dequeueing jobs from queues.""" # Dequeue a job (not a job ID) off the queue self.assertEqual(self.q.count, 1) job = self.q.dequeue() self.assertEqual(job.id, self.result.id) self.assertEqual(job.func, say_hello) self.assertEqual(job.origin, self.q.name) self.assertEqual(job.args[0], 'Rick') self.assertEqual(job.kwargs['foo'], 'bar') # ...and assert the queue count when down self.assertEqual(self.q.count, 0)
class TestWorkViaStringArg(TransactionTestCase): def setUp(self): self.q = Queue('foo') self.w = Worker.create([self.q]) self.job = self.q.enqueue('test_pq.fixtures.say_hello', name='Frank') def test_work_via_string_argument(self): """Worker processes work fed via string arguments.""" self.assertEquals(self.w.work(burst=True), True, 'Expected at least some work done.') job = Job.objects.get(id=self.job.id) self.assertEquals(job.result, 'Hi there, Frank!')
def setUp(self): self.fooq, self.barq = Queue('foo'), Queue('bar')
def setUp(self): self.q = Queue() self.w = Worker.create([self.q])
def setUp(self): self.q = Queue() self.w = Worker.create([self.q]) self.job = self.q.enqueue(say_hello, args=('Bob', ), result_ttl=1) self.w.work(burst=True)
def setUp(self): self.fooq, self.barq = Queue('foo'), Queue('bar') self.w = Worker.create([self.fooq, self.barq])
def setUp(self): self.q = Queue() self.q.save_queue()
def setUp(self): self.q = Queue() self.fq = get_failed_queue() self.w = Worker.create([self.q])
def setUp(self): self.q = Queue()
def setUp(self): self.q = Queue() self.fq = get_failed_queue() self.w = Worker.create([self.q]) self.job = self.q.enqueue(div_by_zero) self.enqueued_at = self.job.enqueued_at
def setUp(self): self.q = Queue() self.w = Worker(self.q) with Flow(self.q, name='test') as f: self.job = f.enqueue(say_hello, 'Bob') self.n_job = f.enqueue(do_nothing)
def setUp(self): self.q = Queue() self.w = Worker(self.q, default_result_ttl=1) with Flow(self.q, name='test') as f: self.n_job = f.enqueue(do_nothing)
def setUp(self): self.q = Queue() self.w = Worker.create([self.q], name='Test') self.w.heartbeat = datetime(2010, 1, 1, tzinfo=utc)
def setUp(self): self.sentinel_file = '/tmp/.rq_sentinel' self.q = Queue() self.fq = get_failed_queue() self.w = Worker.create([self.q])
def setUp(self): self.q = Queue('foo') self.w = Worker.create([self.q]) self.job = self.q.enqueue('test_pq.fixtures.say_hello', name='Frank')
def setUp(self): self.q = Queue() self.q.save_queue() self.sq = SerialQueue() self.sq.save_queue()
def setUp(self): self.fooq, self.barq = Queue('foo'), Queue('bar') self.w = Worker.create([self.fooq, self.barq]) self.fooq.enqueue(say_hello, name='Frank')
def setUp(self): self.q = Queue() self.w = Worker.create([self.q], expires_after=1, default_worker_ttl=1)
def setUp(self): self.q = Queue() self.q.enqueue_call(div_by_zero, args=(1, )) w = Worker.create(self.q) w.work(burst=True)