def test_infinite_retryable_error(self): test_job = Job(func=retryable_error_task, max_retries=0) self.assertEqual(test_job.retry, 0) with self.assertRaises(RetryableJobError): test_job.perform(self.session) self.assertEqual(test_job.retry, 1)
def test_perform_args(self): test_job = Job(func=dummy_task_args, model_name='res.users', args=('o', 'k'), kwargs={'c': '!'}) result = test_job.perform(self.session) self.assertEqual(result, 'ok!')
def _run_job(self, job_id): job_obj = self.registry('queue.job') job = job_obj.browse(self.cr, self.uid, job_id) (func_name, args, kwargs) = _unpickle(job.func) runnable_job = Job(func=func_name, args=args, kwargs=kwargs, priority=job.priority, eta=None, job_uuid=job.uuid, description=job.name) runnable_job.user_id = self.session.uid return runnable_job.perform(self.session)
def test_set_pending(self): job_a = Job(func=task_a) job_a.set_pending(result='test') self.assertEquals(job_a.state, PENDING) self.assertFalse(job_a.date_enqueued) self.assertFalse(job_a.date_started) self.assertEquals(job_a.retry, 0) self.assertEquals(job_a.result, 'test')
def test_set_started(self): job_a = Job(func=task_a) datetime_path = "openerp.addons.connector.queue.job.datetime" with mock.patch(datetime_path, autospec=True) as mock_datetime: mock_datetime.now.return_value = datetime(2015, 3, 15, 16, 41, 0) job_a.set_started() self.assertEquals(job_a.state, STARTED) self.assertEquals(job_a.date_started, datetime(2015, 3, 15, 16, 41, 0))
def test_job_worker(self): worker = self.env["queue.worker"].create({"uuid": "57569b99-c2c1-47b6-aad1-72f953c92c87"}) test_job = Job(func=dummy_task_args, model_name="res.users", args=("o", "k"), kwargs={"c": "!"}) test_job.worker_uuid = worker.uuid storage = OpenERPJobStorage(self.session) self.assertEqual(storage._worker_id(worker.uuid), worker.id) storage.store(test_job) job_read = storage.load(test_job.uuid) self.assertEqual(job_read.worker_uuid, worker.uuid)
def test_postpone(self): job_a = Job(func=task_a) datetime_path = 'openerp.addons.connector.queue.job.datetime' with mock.patch(datetime_path, autospec=True) as mock_datetime: mock_datetime.now.return_value = datetime(2015, 3, 15, 16, 41, 0) job_a.postpone(result='test', seconds=60) self.assertEquals(job_a.eta, datetime(2015, 3, 15, 16, 42, 0)) self.assertEquals(job_a.result, 'test') self.assertFalse(job_a.exc_info)
def test_read(self): eta = datetime.now() + timedelta(hours=5) test_job = Job(func=dummy_task_args, model_name='res.users', args=('o', 'k'), kwargs={'c': '!'}, priority=15, eta=eta, description="My description") test_job.user_id = 1 test_job.company_id = self.env.ref("base.main_company").id storage = OpenERPJobStorage(self.session) storage.store(test_job) job_read = storage.load(test_job.uuid) self.assertEqual(test_job.uuid, job_read.uuid) self.assertEqual(test_job.model_name, job_read.model_name) self.assertEqual(test_job.func, job_read.func) self.assertEqual(test_job.args, job_read.args) self.assertEqual(test_job.kwargs, job_read.kwargs) self.assertEqual(test_job.func_name, job_read.func_name) self.assertEqual(test_job.func_string, job_read.func_string) self.assertEqual(test_job.description, job_read.description) self.assertEqual(test_job.state, job_read.state) self.assertEqual(test_job.priority, job_read.priority) self.assertEqual(test_job.exc_info, job_read.exc_info) self.assertEqual(test_job.result, job_read.result) self.assertEqual(test_job.user_id, job_read.user_id) self.assertEqual(test_job.company_id, job_read.company_id) delta = timedelta(seconds=1) # DB does not keep milliseconds self.assertAlmostEqual(test_job.date_created, job_read.date_created, delta=delta) self.assertAlmostEqual(test_job.date_started, job_read.date_started, delta=delta) self.assertAlmostEqual(test_job.date_enqueued, job_read.date_enqueued, delta=delta) self.assertAlmostEqual(test_job.date_done, job_read.date_done, delta=delta) self.assertAlmostEqual(test_job.eta, job_read.eta, delta=delta) test_date = datetime(2015, 3, 15, 21, 7, 0) job_read.date_enqueued = test_date job_read.date_started = test_date job_read.date_done = test_date job_read.canceled = True storage.store(job_read) job_read = storage.load(test_job.uuid) self.assertAlmostEqual(job_read.date_started, test_date, delta=delta) self.assertAlmostEqual(job_read.date_enqueued, test_date, delta=delta) self.assertAlmostEqual(job_read.date_done, test_date, delta=delta) self.assertEqual(job_read.canceled, True)
def test_set_done(self): job_a = Job(func=task_a) datetime_path = "openerp.addons.connector.queue.job.datetime" with mock.patch(datetime_path, autospec=True) as mock_datetime: mock_datetime.now.return_value = datetime(2015, 3, 15, 16, 41, 0) job_a.set_done(result="test") self.assertEquals(job_a.state, DONE) self.assertEquals(job_a.result, "test") self.assertEquals(job_a.date_done, datetime(2015, 3, 15, 16, 41, 0)) self.assertFalse(job_a.worker_uuid) self.assertFalse(job_a.exc_info)
def test_set_enqueued(self): job_a = Job(func=task_a) worker = mock.Mock(name="Worker") uuid = "ae7d1161-dc34-40b1-af06-8057c049133e" worker.uuid = "ae7d1161-dc34-40b1-af06-8057c049133e" datetime_path = "openerp.addons.connector.queue.job.datetime" with mock.patch(datetime_path, autospec=True) as mock_datetime: mock_datetime.now.return_value = datetime(2015, 3, 15, 16, 41, 0) job_a.set_enqueued(worker) self.assertEquals(job_a.state, ENQUEUED) self.assertEquals(job_a.date_enqueued, datetime(2015, 3, 15, 16, 41, 0)) self.assertEquals(job_a.worker_uuid, uuid) self.assertFalse(job_a.date_started)
def test_job_worker(self): worker = self.env['queue.worker'].create( {'uuid': '57569b99-c2c1-47b6-aad1-72f953c92c87'} ) test_job = Job(func=dummy_task_args, model_name='res.users', args=('o', 'k'), kwargs={'c': '!'}) test_job.worker_uuid = worker.uuid storage = OpenERPJobStorage(self.session) self.assertEqual(storage._worker_id(worker.uuid), worker.id) storage.store(test_job) job_read = storage.load(test_job.uuid) self.assertEqual(job_read.worker_uuid, worker.uuid)
def test_eta_integer(self): """ When an `eta` is an integer, it adds n seconds up to now """ datetime_path = 'openerp.addons.connector.queue.job.datetime' with mock.patch(datetime_path, autospec=True) as mock_datetime: mock_datetime.now.return_value = datetime(2015, 3, 15, 16, 41, 0) job_a = Job(func=task_a, eta=60) self.assertEqual(job_a.eta, datetime(2015, 3, 15, 16, 42, 0))
def test_perform_args(self): job = Job(func=dummy_task_args, model_name='res.users', args=('o', 'k'), kwargs={'c': '!'}) result = job.perform(self.session) self.assertEqual(result, 'ok!')
def test_on_method(self): class A(object): def method(self): pass with self.assertRaises(NotImplementedError): Job(A.method)
def test_retry_pattern_no_zero(self): """ When we specify a retry pattern without 0, uses RETRY_INTERVAL""" test_pattern = {3: 180} job(retryable_error_task, retry_pattern=test_pattern) test_job = Job(func=retryable_error_task, max_retries=0) test_job.retry += 1 self.assertEqual(test_job.retry, 1) self.assertEqual(test_job._get_retry_seconds(), RETRY_INTERVAL) test_job.retry += 1 self.assertEqual(test_job.retry, 2) self.assertEqual(test_job._get_retry_seconds(), RETRY_INTERVAL) test_job.retry += 1 self.assertEqual(test_job.retry, 3) self.assertEqual(test_job._get_retry_seconds(), 180) test_job.retry += 1 self.assertEqual(test_job.retry, 4) self.assertEqual(test_job._get_retry_seconds(), 180)
def test_unicode(self): test_job = Job(func=dummy_task_args, model_name='res.users', args=(u'öô¿‽', u'ñě'), kwargs={'c': u'ßø'}, priority=15, description=u"My dé^Wdescription") test_job.user_id = 1 storage = OpenERPJobStorage(self.session) storage.store(test_job) job_read = storage.load(test_job.uuid) self.assertEqual(test_job.args, job_read.args) self.assertEqual(job_read.args, ('res.users', u'öô¿‽', u'ñě')) self.assertEqual(test_job.kwargs, job_read.kwargs) self.assertEqual(job_read.kwargs, {'c': u'ßø'}) self.assertEqual(test_job.description, job_read.description) self.assertEqual(job_read.description, u"My dé^Wdescription")
def test_accented_bytestring(self): job = Job(func=dummy_task_args, model_name='res.users', args=('öô¿‽', 'ñě'), kwargs={'c': 'ßø'}, priority=15, description="My dé^Wdescription") job.user_id = 1 storage = OpenERPJobStorage(self.session) storage.store(job) job_read = storage.load(job.uuid) self.assertEqual(job.args, job_read.args) self.assertEqual(job_read.args, ('res.users', 'öô¿‽', 'ñě')) self.assertEqual(job.kwargs, job_read.kwargs) self.assertEqual(job_read.kwargs, {'c': 'ßø'}) # the job's description has been created as bytestring but is # decoded to utf8 by the ORM so make them comparable self.assertEqual(job.description, job_read.description.encode('utf8')) self.assertEqual(job_read.description, "My dé^Wdescription".decode('utf8'))
def test_unicode(self): test_job = Job( func=dummy_task_args, model_name="res.users", args=(u"öô¿‽", u"ñě"), kwargs={"c": u"ßø"}, priority=15, description=u"My dé^Wdescription", ) test_job.user_id = 1 storage = OpenERPJobStorage(self.session) storage.store(test_job) job_read = storage.load(test_job.uuid) self.assertEqual(test_job.args, job_read.args) self.assertEqual(job_read.args, ("res.users", u"öô¿‽", u"ñě")) self.assertEqual(test_job.kwargs, job_read.kwargs) self.assertEqual(job_read.kwargs, {"c": u"ßø"}) self.assertEqual(test_job.description, job_read.description) self.assertEqual(job_read.description, u"My dé^Wdescription")
def test_retryable_error(self): test_job = Job(func=retryable_error_task, max_retries=3) with self.assertRaises(RetryableJobError): test_job.perform(self.session) with self.assertRaises(RetryableJobError): test_job.perform(self.session) with self.assertRaises(FailedJobError): test_job.perform(self.session)
def test_accented_bytestring(self): test_job = Job( func=dummy_task_args, model_name="res.users", args=("öô¿‽", "ñě"), kwargs={"c": "ßø"}, priority=15, description="My dé^Wdescription", ) test_job.user_id = 1 storage = OpenERPJobStorage(self.session) storage.store(test_job) job_read = storage.load(test_job.uuid) self.assertEqual(test_job.args, job_read.args) self.assertEqual(job_read.args, ("res.users", "öô¿‽", "ñě")) self.assertEqual(test_job.kwargs, job_read.kwargs) self.assertEqual(job_read.kwargs, {"c": "ßø"}) # the job's description has been created as bytestring but is # decoded to utf8 by the ORM so make them comparable self.assertEqual(test_job.description, job_read.description.encode("utf8")) self.assertEqual(job_read.description, "My dé^Wdescription".decode("utf8"))
def test_read(self): eta = datetime.now() + timedelta(hours=5) job = Job(func=dummy_task_args, model_name='res.users', args=('o', 'k'), kwargs={'c': '!'}, priority=15, eta=eta, description="My description") job.user_id = 1 storage = OpenERPJobStorage(self.session) storage.store(job) job_read = storage.load(job.uuid) self.assertEqual(job.uuid, job_read.uuid) self.assertEqual(job.model_name, job_read.model_name) self.assertEqual(job.func, job_read.func) self.assertEqual(job.args, job_read.args) self.assertEqual(job.kwargs, job_read.kwargs) self.assertEqual(job.func_name, job_read.func_name) self.assertEqual(job.func_string, job_read.func_string) self.assertEqual(job.description, job_read.description) self.assertEqual(job.state, job_read.state) self.assertEqual(job.priority, job_read.priority) self.assertEqual(job.exc_info, job_read.exc_info) self.assertEqual(job.result, job_read.result) self.assertEqual(job.user_id, job_read.user_id) delta = timedelta(seconds=1) # DB does not keep milliseconds self.assertAlmostEqual(job.date_created, job_read.date_created, delta=delta) self.assertAlmostEqual(job.date_started, job_read.date_started, delta=delta) self.assertAlmostEqual(job.date_enqueued, job_read.date_enqueued, delta=delta) self.assertAlmostEqual(job.date_done, job_read.date_done, delta=delta) self.assertAlmostEqual(job.eta, job_read.eta, delta=delta)
def test_context_uuid(self): test_job = Job(func=dummy_task_context) result = test_job.perform(self.session) key_present = 'job_uuid' in result self.assertTrue(key_present) self.assertEqual(result['job_uuid'], test_job._uuid)
def test_cancel(self): job_a = Job(func=task_a) job_a.cancel(msg='test') self.assertTrue(job_a.canceled) self.assertEquals(job_a.state, DONE) self.assertEquals(job_a.result, 'test')
def test_set_failed(self): job_a = Job(func=task_a) job_a.set_failed(exc_info='failed test') self.assertEquals(job_a.state, FAILED) self.assertEquals(job_a.exc_info, 'failed test') self.assertFalse(job_a.worker_uuid)
def test_retry_pattern(self): """ When we specify a retry pattern, the eta must follow it""" datetime_path = 'openerp.addons.connector.queue.job.datetime' test_pattern = { 1: 60, 2: 180, 3: 10, 5: 300, } job(retryable_error_task, retry_pattern=test_pattern) with mock.patch(datetime_path, autospec=True) as mock_datetime: mock_datetime.now.return_value = datetime(2015, 6, 1, 15, 10, 0) test_job = Job(func=retryable_error_task, max_retries=0) test_job.retry += 1 test_job.postpone(self.session) self.assertEqual(test_job.retry, 1) self.assertEqual(test_job.eta, datetime(2015, 6, 1, 15, 11, 0)) test_job.retry += 1 test_job.postpone(self.session) self.assertEqual(test_job.retry, 2) self.assertEqual(test_job.eta, datetime(2015, 6, 1, 15, 13, 0)) test_job.retry += 1 test_job.postpone(self.session) self.assertEqual(test_job.retry, 3) self.assertEqual(test_job.eta, datetime(2015, 6, 1, 15, 10, 10)) test_job.retry += 1 test_job.postpone(self.session) self.assertEqual(test_job.retry, 4) self.assertEqual(test_job.eta, datetime(2015, 6, 1, 15, 10, 10)) test_job.retry += 1 test_job.postpone(self.session) self.assertEqual(test_job.retry, 5) self.assertEqual(test_job.eta, datetime(2015, 6, 1, 15, 15, 00))
def test_perform_args(self): test_job = Job(func=dummy_task_args, model_name="res.users", args=("o", "k"), kwargs={"c": "!"}) result = test_job.perform(self.session) self.assertEqual(result, "ok!")
def test_perform(self): test_job = Job(func=dummy_task) result = test_job.perform(self.session) self.assertEqual(result, 'ok')