def test_compat_properties(self): job = Request({"task": self.mytask.name, "id": uuid(), "args": [1], "kwargs": {"f": "x"}}, app=self.app) self.assertEqual(job.task_id, job.id) self.assertEqual(job.task_name, job.name) job.task_id = "ID" self.assertEqual(job.id, "ID") job.task_name = "NAME" self.assertEqual(job.name, "NAME")
def test_terminate__task_started(self): pool = Mock() signum = signal.SIGKILL job = Request({"task": self.mytask.name, "id": uuid(), "args": [1], "kwrgs": {"f": "x"}}, app=self.app) with assert_signal_called( task_revoked, sender=job.task, request=job, terminated=True, expired=False, signum=signum ): job.time_start = monotonic() job.worker_pid = 313 job.terminate(pool, signal="KILL") pool.terminate_job.assert_called_with(job.worker_pid, signum)
def test_compat_properties(self): job = Request({ 'task': self.mytask.name, 'id': uuid(), 'args': [1], 'kwargs': {'f': 'x'}, }, app=self.app) self.assertEqual(job.task_id, job.id) self.assertEqual(job.task_name, job.name) job.task_id = 'ID' self.assertEqual(job.id, 'ID') job.task_name = 'NAME' self.assertEqual(job.name, 'NAME')
def test_terminate__task_started(self): pool = Mock() signum = signal.SIGTERM job = Request({ 'task': self.mytask.name, 'id': uuid(), 'args': [1], 'kwrgs': {'f': 'x'}, }, app=self.app) with assert_signal_called( task_revoked, sender=job.task, request=job, terminated=True, expired=False, signum=signum): job.time_start = monotonic() job.worker_pid = 313 job.terminate(pool, signal='TERM') pool.terminate_job.assert_called_with(job.worker_pid, signum)
def test_revoked_expires_expired(self): job = Request({ 'task': self.mytask.name, 'id': uuid(), 'args': [1], 'kwargs': {'f': 'x'}, 'expires': datetime.utcnow() - timedelta(days=1), }, app=self.app) with assert_signal_called( task_revoked, sender=job.task, request=job, terminated=False, expired=True, signum=None): job.revoked() self.assertIn(job.id, revoked) self.assertEqual( self.mytask.backend.get_status(job.id), states.REVOKED, )
def test_revoked_expires_expired(self): job = Request( { "task": self.mytask.name, "id": uuid(), "args": [1], "kwargs": {"f": "x"}, "expires": datetime.utcnow() - timedelta(days=1), }, app=self.app, ) with assert_signal_called( task_revoked, sender=job.task, request=job, terminated=False, expired=True, signum=None ): job.revoked() self.assertIn(job.id, revoked) self.assertEqual(self.mytask.backend.get_status(job.id), states.REVOKED)
def test_process_task(self): worker = self.worker worker.pool = Mock() backend = Mock() m = create_message(backend, task=foo_task.name, args=[4, 8, 10], kwargs={}) task = Request.from_message(m, m.decode()) worker.process_task(task) self.assertEqual(worker.pool.apply_async.call_count, 1) worker.pool.stop()
def test_process_task_raise_regular(self): worker = self.worker worker.pool = Mock() worker.pool.apply_async.side_effect = KeyError("some exception") backend = Mock() m = create_message(backend, task=foo_task.name, args=[4, 8, 10], kwargs={}) task = Request.from_message(m, m.decode()) worker.process_task(task) worker.pool.stop()
def test_process_task_raise_SystemTerminate(self): worker = self.worker worker.pool = Mock() worker.pool.apply_async.side_effect = SystemTerminate() backend = Mock() m = create_message(backend, task=self.foo_task.name, args=[4, 8, 10], kwargs={}) task = Request.from_message(m, m.decode(), app=self.app) worker.steps = [] worker.blueprint.state = RUN with self.assertRaises(SystemExit): worker._process_task(task)
def test_process_task_raise_base(self): worker = self.worker worker.pool = Mock() worker.pool.apply_async.side_effect = KeyboardInterrupt("Ctrl+C") backend = Mock() m = create_message(backend, task=self.foo_task.name, args=[4, 8, 10], kwargs={}) task = Request.from_message(m, m.decode(), app=self.app) worker.steps = [] worker.blueprint.state = RUN with self.assertRaises(KeyboardInterrupt): worker._process_task(task)
def test_process_task_raise_base(self): worker = self.worker worker.pool = Mock() worker.pool.apply_async.side_effect = KeyboardInterrupt("Ctrl+C") backend = Mock() m = create_message(backend, task=foo_task.name, args=[4, 8, 10], kwargs={}) task = Request.from_message(m, m.decode()) worker.components = [] worker.namespace.state = RUN with self.assertRaises(KeyboardInterrupt): worker.process_task(task) self.assertEqual(worker.namespace.state, TERMINATE)
def test_process_task_raise_SystemTerminate(self): worker = self.worker worker.pool = Mock() worker.pool.apply_async.side_effect = SystemTerminate() backend = Mock() m = create_message(backend, task=foo_task.name, args=[4, 8, 10], kwargs={}) task = Request.from_message(m, m.decode()) worker.components = [] worker._state = worker.RUN with self.assertRaises(SystemExit): worker.process_task(task) self.assertEqual(worker._state, worker.TERMINATE)
def test_terminate__task_reserved(self): pool = Mock() job = Request({"task": self.mytask.name, "id": uuid(), "args": [1], "kwargs": {"f": "x"}}, app=self.app) job.time_start = None job.terminate(pool, signal="KILL") self.assertFalse(pool.terminate_job.called) self.assertTupleEqual(job._terminate_on_ack, (pool, "KILL")) job.terminate(pool, signal="KILL")
def test_on_retry(self): job = Request({"task": self.mytask.name, "id": uuid(), "args": [1], "kwargs": {"f": "x"}}, app=self.app) job.eventer = MockEventDispatcher() try: raise Retry("foo", KeyError("moofoobar")) except: einfo = ExceptionInfo() job.on_failure(einfo) self.assertIn("task-retried", job.eventer.sent) prev, module._does_info = module._does_info, False try: job.on_failure(einfo) finally: module._does_info = prev einfo.internal = True job.on_failure(einfo)
def test_process_task_raise_base(self): worker = self.worker worker.pool = Mock() worker.pool.apply_async.side_effect = KeyboardInterrupt("Ctrl+C") backend = Mock() m = create_message(backend, task=foo_task.name, args=[4, 8, 10], kwargs={}) task = Request.from_message(m, m.decode()) worker.components = [] worker._state = worker.RUN with self.assertRaises(KeyboardInterrupt): worker.process_task(task) self.assertEqual(worker._state, worker.TERMINATE)
def test_process_task_raise_SystemTerminate(self): worker = self.worker worker.pool = Mock() worker.pool.apply_async.side_effect = SystemTerminate() backend = Mock() m = create_message(backend, task=foo_task.name, args=[4, 8, 10], kwargs={}) task = Request.from_message(m, m.decode()) worker.steps = [] worker.blueprint.state = RUN with self.assertRaises(SystemExit): worker._process_task(task) self.assertEqual(worker.blueprint.state, TERMINATE)
def test_revoked_expires_expired(self): job = Request( { 'task': self.mytask.name, 'id': uuid(), 'args': [1], 'kwargs': { 'f': 'x' }, 'expires': datetime.utcnow() - timedelta(days=1), }, app=self.app) with assert_signal_called(task_revoked, sender=job.task, request=job, terminated=False, expired=True, signum=None): job.revoked() self.assertIn(job.id, revoked) self.assertEqual( self.mytask.backend.get_status(job.id), states.REVOKED, )
def test_on_retry(self): job = Request({ 'task': self.mytask.name, 'id': uuid(), 'args': [1], 'kwargs': {'f': 'x'}, }, app=self.app) job.eventer = MockEventDispatcher() try: raise Retry('foo', KeyError('moofoobar')) except: einfo = ExceptionInfo() job.on_failure(einfo) self.assertIn('task-retried', job.eventer.sent) prev, module._does_info = module._does_info, False try: job.on_failure(einfo) finally: module._does_info = prev einfo.internal = True job.on_failure(einfo)
def test_from_message_nonexistant_task(self): body = { 'task': 'cu.mytask.doesnotexist', 'id': uuid(), 'args': [2], 'kwargs': { 'æØåveéðƒeæ': 'bar' } } m = Message(None, body=anyjson.dumps(body), backend='foo', content_type='application/json', content_encoding='utf-8') with self.assertRaises(KeyError): Request(m.decode(), message=m, app=self.app)
def test_from_message(self): us = 'æØåveéðƒeæ' body = {'task': self.mytask.name, 'id': uuid(), 'args': [2], 'kwargs': {us: 'bar'}} m = Message(None, body=anyjson.dumps(body), backend='foo', content_type='application/json', content_encoding='utf-8') job = Request(m.decode(), message=m, app=self.app) self.assertIsInstance(job, Request) self.assertEqual(job.name, body['task']) self.assertEqual(job.id, body['id']) self.assertEqual(job.args, body['args']) us = from_utf8(us) if sys.version_info < (2, 6): self.assertEqual(next(keys(job.kwargs)), us) self.assertIsInstance(next(keys(job.kwargs)), str)
def test_terminate__task_reserved(self): pool = Mock() job = Request({ 'task': self.mytask.name, 'id': uuid(), 'args': [1], 'kwargs': {'f': 'x'}, }, app=self.app) job.time_start = None job.terminate(pool, signal='TERM') self.assertFalse(pool.terminate_job.called) self.assertTupleEqual(job._terminate_on_ack, (pool, 15)) job.terminate(pool, signal='TERM')
def test_terminate__task_reserved(self): pool = Mock() job = Request( { 'task': self.mytask.name, 'id': uuid(), 'args': [1], 'kwargs': { 'f': 'x' }, }, app=self.app) job.time_start = None job.terminate(pool, signal='TERM') self.assertFalse(pool.terminate_job.called) self.assertTupleEqual(job._terminate_on_ack, (pool, 15)) job.terminate(pool, signal='TERM')
def test_dump_schedule(self): consumer = Consumer(self.app) panel = self.create_panel(consumer=consumer) self.assertFalse(panel.handle('dump_schedule')) r = Request( { 'task': self.mytask.name, 'id': 'CAFEBABE', 'args': (), 'kwargs': {}, }, app=self.app) consumer.timer.schedule.enter_at( consumer.timer.Entry(lambda x: x, (r, )), datetime.now() + timedelta(seconds=10)) consumer.timer.schedule.enter_at( consumer.timer.Entry(lambda x: x, (object(), )), datetime.now() + timedelta(seconds=10)) self.assertTrue(panel.handle('dump_schedule'))
def xRequest(self, body=None, **kwargs): body = dict({'task': self.mytask.name, 'id': uuid(), 'args': [1], 'kwargs': {'f': 'x'}}, **body or {}) return Request(body, app=self.app, **kwargs)
def test_from_message_invalid_kwargs(self): body = dict(task=self.mytask.name, id=1, args=(), kwargs='foo') with self.assertRaises(InvalidTaskError): Request(body, message=None, app=self.app)