def test_serial_jobs3(self): """Mix queueJob and queueSerialJobs. """ job = self.async.queueJob(createDocument, self.folder, 'anid3', 'atitle', 'adescr', 'abody') self.assertEqual(job.quota_names, ('default',)) job2 = self.async.queueSerialJobs( makeJob(publishDocument, self.folder, 'anid3'), makeJob(createDocument, self.folder, 'anotherid3', 'atitle', 'adescr', 'abody'), ) self.assertEqual(job2.quota_names, ('default',)) job3 = self.async.queueJob(publishDocument, self.folder, 'anotherid3') self.assertEqual(job3.quota_names, ('default',)) transaction.commit() wait_for_result(job3) self.assertEqual(job.result, 'anid3') self.assertEqual(job2.result[0].result, 'workflow_change') self.assertEqual(job2.result[1].result, 'anotherid3') self.assertEqual(job3.result, 'workflow_change') wt = getToolByName(self.folder, 'portal_workflow') doc = self.folder['anid3'] self.assertEqual(wt.getInfoFor(doc, 'review_state'), 'pending') doc = self.folder['anotherid3'] self.assertEqual(wt.getInfoFor(doc, 'review_state'), 'pending')
def test_indexing(self): """Queue indexing. """ self.folder.invokeFactory('Document', 'anid4', title='Foo', description='Foo', text='foo') doc = self.folder['anid4'] doc.setDescription('bar') ct = getToolByName(self.folder, 'portal_catalog') res = ct.searchResults(Description='bar') self.assertEqual(len(res), 0) job = self.async.queueJob(reindexDocument, doc) transaction.commit() wait_for_result(job) res = ct.searchResults(Description='bar') self.assertEqual(len(res), 1) """Demonstrate calling an object's method. """ self.folder.invokeFactory('Document', 'anid5', title='Foo', description='Foo', text='foo') doc = self.folder['anid5'] doc.setDescription('bar') ct = getToolByName(self.folder, 'portal_catalog') res = ct.searchResults(Description='bar') self.assertEqual(len(res), 1) job = self.async.queueJob(doc.__class__.reindexObject, doc) transaction.commit() wait_for_result(job) res = ct.searchResults(Description='bar') self.assertEqual(len(res), 2)
def test_pasync_queue_is_job_pending_running_and_finished(self): transaction.commit() job_infos = self.cqueue.get_job_infos(job=test_job) job, job_infos = self.make_simpleOne(job_infos) self.assertTrue(self.cqueue.is_job_pending(job_infos)) self.assertFalse(self.cqueue.is_job_running(job_infos)) transaction.commit() wait_for_start(job) self.assertTrue(self.cqueue.is_job_running(job_infos)) wait_for_result(job) self.assertTrue(self.cqueue.is_job_finished(job_infos)) fjob = self.cqueue.get_job_present(job_infos) # run another job and verify that the related one is # this job and not the first run now = datetime.datetime.now() + datetime.timedelta(minutes=5) set_now(now) transaction.commit() job1_infos = self.cqueue.get_job_infos(job=test_job) job1, job1_infos = self.make_simpleOne(job1_infos) self.assertFalse(self.cqueue.is_job_running(job1_infos)) transaction.commit() wait_for_start(job1) self.assertTrue(self.cqueue.is_job_running(job1_infos)) wait_for_result(job1) self.assertTrue(self.cqueue.is_job_finished(job1_infos)) # we cant determine which was first anyway, those are the same job ffjob = self.cqueue.get_job_in_agents(job_infos) sjob = self.cqueue.get_job_in_agents(job1_infos) self.assertTrue(ffjob.key == sjob.key) self.assertTrue(job1.key == sjob.key)
def test_failure(self): events[:] = [] job = self.async.queueJob(failingJob, self.folder) transaction.commit() wait_for_result(job) result = events[0].object self.assertEquals(str(result.type), 'exceptions.RuntimeError') self.assertEquals(str(result.value), 'FooBar')
def test_job_with_delay(self): before = datetime.datetime.now(pytz.UTC) job = self.async.queueJobWithDelay( None, before + datetime.timedelta(seconds=1), searchForDocument, self.folder, self.folder.getId()) transaction.commit() wait_for_result(job) after = datetime.datetime.now(pytz.UTC) self.assertTrue((after - before).seconds >= 1)
def test_callback_retry(self): from plone.app.async import Job, queue doom_once.retries = 0 job = queue(Job(addNumbers, 1, 1)) job.addCallback(doom_once) transaction.commit() wait_for_result(job) self.assertEqual(2, job.result) self.assertEqual(2, doom_once.retries)
def test_retry(self): """A job that causes a conflict while committing should be retried.""" doom.retries = 0 from plone.app.async import Job, queue job = queue(Job(doom)) transaction.commit() wait_for_result(job) self.assertTrue(job.result.type is transaction.interfaces.DoomedTransaction) self.assertEqual(5, doom.retries)
def wait_for_all_jobs(seconds=6, assert_successful=True): """Wait for all jobs in the queue to complete""" begin() service = component.getUtility(IAsyncService) queue = service.getQueues()[''] for job in queue: wait_for_result(job, seconds) if assert_successful: assert not isinstance(job.result, Failure), str(job.result) commit()
def test_delayed_retry(self): from datetime import timedelta from plone.app.async import Job, queue, RetryWithDelay fail_once.retries = 0 job = queue(Job(fail_once)) job.retry_policy_factory = RetryWithDelay(timedelta(seconds=5)) transaction.commit() wait_for_result(job) self.assertTrue(2, fail_once.retries) self.assertTrue(job.result > 5)
def test_add_job(self): """Tests adding a computational job and getting the result. """ from plone.app.async import Job, queue job = queue(Job(addNumbers, 40, 2)) transaction.commit() self.assertEqual(job.status, u'pending-status') wait_for_result(job) self.assertEqual(job.status, u'completed-status') self.assertEqual(job.result, 42)
def test_add_job(self): """Tests adding a computational job and getting the result. """ context = self.folder job = self.async.queueJob(addNumbers, context, 40, 2) transaction.commit() self.assertEqual(job.status, u'pending-status') wait_for_result(job) self.assertEqual(job.status, u'completed-status') self.assertEqual(job.result, 42)
def test_default_quota(self): """When adding using queueJob, the quota is set to 1 always. So, everything will be performed serialy. """ results[:] = [] self.async.queueJob(job1, self.folder) self.async.queueJob(job2, self.folder) j3 = self.async.queueJob(job3, self.folder) transaction.commit() wait_for_result(j3, seconds=20) self.assertEquals(results, [1, 2, 3])
def test_timing_parallel(self): """Tests whether the jobs are *really* performed in parallel. """ results[:] = [] j1 = (job1, self.folder, (), {}) j2 = (job2, self.folder, (), {}) j3 = (job3, self.folder, (), {}) job = self.async.queueParallelJobs(j1,j2,j3) transaction.commit() wait_for_result(job, seconds=20) self.assertEquals(set(results), set([3, 2, 1]))
def test_timing_serial(self): """Tests whether the jobs are *really* performed serially. """ results[:] = [] j1 = (job1, self.folder, (), {}) j2 = (job2, self.folder, (), {}) j3 = (job3, self.folder, (), {}) job = self.async.queueSerialJobs(j1,j2,j3) transaction.commit() wait_for_result(job, seconds=20) self.assertEquals(results, [1, 2, 3])
def test_serial_jobs2(self): """Queue a job that queues another job. """ job = self.async.queueJob(createDocumentAndPublish, self.folder, 'anid23', 'atitle', 'adescr', 'abody') transaction.commit() wait_for_result(job) self.assertEqual(job.result, 'workflow_change') doc = self.folder['anid23'] wt = getToolByName(self.folder, 'portal_workflow') self.assertEqual(wt.getInfoFor(doc, 'review_state'), 'pending')
def test_serial_jobs(self): """Queue two jobs the one after the other. """ job1 = (createDocument, self.folder, ('anid2', 'atitle', 'adescr', 'abody'), {}) job2 = (publishDocument, self.folder, ('anid2',), {}) job = self.async.queueSerialJobs(job1,job2) transaction.commit() wait_for_result(job) self.assertEqual(job.result[0].result, 'anid2') self.assertEqual(job.result[1].result, 'workflow_change') doc = self.folder['anid2'] wt = getToolByName(self.folder, 'portal_workflow') self.assertEqual(wt.getInfoFor(doc, 'review_state'), 'pending')
def test_add_persistent(self): """Adding a job that creates persistent objects. """ job = self.async.queueJob(createDocument, self.folder, 'anide', 'atitle', 'adescr', 'abody') transaction.commit() self.assertEqual(job.status, u'pending-status') wait_for_result(job) self.assertEqual(job.status, u'completed-status') self.assertEqual(job.result, 'anide') self.failUnless('anide' in self.folder.objectIds()) document = self.folder['anide'] self.assertEqual(document.Creator(), default_user)
def test_add_persistent(self): """Adding a job that creates persistent objects. """ from plone.app.async import Job, queue self.setRoles(['Manager']) job = queue(Job(self.folder.invokeFactory, 'Document', 'anid', title='atitle', description='adescr', text='abody')) transaction.commit() self.assertEqual(job.status, u'pending-status') wait_for_result(job) self.assertEqual(job.status, u'completed-status') self.assertEqual(job.result, 'anid') self.failUnless('anid' in self.folder.objectIds()) document = self.folder['anid'] self.assertEqual(document.Creator(), default_user)
def test_non_default_quota(self): """If we set a quota with size 2, then job1,2 will start. 2 will finish, 3 will start and finish, and then 1 will finish. """ results[:] = [] queue = self.async.getQueues()[''] queue.quotas.create('size2', size=2) j1 = self.async.queueJobInQueue(queue, ('size2',), job1, self.folder) j1.quota_names = ('size2',) j2 = self.async.queueJobInQueue(queue, ('size2',), job2, self.folder) j2.quota_names = ('size2',) j3 = self.async.queueJobInQueue(queue, ('size2',), job3, self.folder) j3.quota_names = ('size2',) transaction.commit() wait_for_result(j1, seconds=20) self.assertEquals(results, [2, 3, 1])
def test_callback(self): results[:] = [] job = self.async.queueJob(job1, self.folder) job.addCallback(job_success_callback) transaction.commit() wait_for_result(job) self.assertEquals(results, [1, "Success: 1"]) results[:] = [] job = self.async.queueJob(job1, self.folder) job.addCallbacks(failure=job_failure_callback) job.addCallbacks(success=job_success_callback) transaction.commit() wait_for_result(job) self.assertEquals(results, ["Failure"]) failure = job.result exception = failure.value self.assertEquals(str(exception), 'The id "1" is invalid - it is already in use.')
def test_job_as_anonymous(self): # Add new document self.folder.invokeFactory('Document', 'anid6', title='Foo', description='Foo', text='foo') doc = self.folder['anid6'] wt = getToolByName(self.folder, 'portal_workflow') # Document must be private (not accessible by anon) self.failUnless(wt.getInfoFor(doc, 'review_state') == 'private') job = self.async.queueJob(searchForDocument, doc, doc.getId()) transaction.commit() # ok, owner can search for it self.assertEqual(wait_for_result(job), 1) # Let's try as anoymous self.logout() job = self.async.queueJob(searchForDocument, doc, doc.getId()) transaction.commit() # not accessible by anon self.assertEqual(wait_for_result(job), 0)
def test_job_sees_main_db(self): job = self.async.queueJob(dbUsed, self.folder) transaction.commit() wait_for_result(job) self.assertEqual(job.result,str(self.app._p_jar.db()))
def test_queue_job_during_job(self): from plone.app.async import queue, Job job = queue(Job(deferred_queue)) transaction.commit() wait_for_result(job, 900) self.assertTrue(job.result is None)
def test_success(self): events[:] = [] job = self.async.queueJob(successJob, self.folder) transaction.commit() wait_for_result(job) self.assertEquals(events[0].object, 'Success')