# Path: kale/exceptions.py
# class ImproperlyConfiguredException(Exception):
# class TaskException(Exception):
# class ChubbyTaskException(TaskException):
# class TimeoutException(TaskException):
# class InvalidTimeLimitTaskException(TaskException):
# class InvalidTaskDelayException(TaskException):
# class BlacklistedException(TaskException):
# class QueueException(Exception):
# class SendMessagesException(QueueException):
# class DeleteMessagesException(QueueException):
# class ChangeMessagesVisibilityException(QueueException):
#     def __init__(self, msg_count):
#     def __init__(self, msg_count):
#     def __init__(self, msg_count):
# 
# Path: kale/task.py
# PERMANENT_FAILURE_UNRECOVERABLE = 'unrecoverable'
# PERMANENT_FAILURE_NO_RETRY = 'no_retries'
# PERMANENT_FAILURE_RETRIES_EXCEEDED = 'max_retries'
#     EXCEPTION_LIST = (
#         exceptions.BlacklistedException,
#         KeyError,
#         NotImplementedError,
#         SyntaxError,
#         UnboundLocalError)
# class Task(object):
#     def __init__(self, message_body=None, *args, **kwargs):
#     def _get_publisher():
#     def _get_task_id(cls, *args, **kwargs):
#     def publish(cls, app_data, *args, **kwargs):
#     def _get_delay_sec_for_retry(cls, current_retry_num):
#     def handle_failure(cls, message, raised_exception, increment_failure_num=True):
#     def republish(cls, message, failure_count):
#     def run(self, *args, **kwargs):
#     def run_task(self, *args, **kwargs):
#     def should_run_task(self, *args, **kwargs):
#     def _check_blacklist(self, *args, **kwargs):
#     def _dequeued(self, *args, **kwargs):
#     def _setup_task_environment():
#     def _clean_task_environment(task_id=None, task_name=None, exc=None):
#     def _pre_run(self, *args, **kwargs):
#     def _post_run(self, *args, **kwargs):
#     def _alert_runtime_exceeded(self, *args, **kwargs):
#     def _kill_runtime_exceeded(self, *args, **kwargs):
#     def _report_permanent_failure(cls, message, exception, failure_type,
#                                   log_exception):
# 
# Path: kale/test_utils.py
# class MockTask(task.Task):
# class FailTask(task.Task):
# class ShouldNotRunTask(task.Task):
# class TimeoutTask(task.Task):
# class SlowButNotTooSlowTask(task.Task):
# class FailTaskNoRetries(FailTask):
# class MockMessage(message.KaleMessage):
# class MockConsumer(object):
# class TestQueueClass(queue_info.TaskQueue):
# class TestQueueSelector(queue_selector.SelectQueueBase):
#     def _get_task_id(self, *args, **kwargs):
#     def run_task(self, *args, **kwargs):
#     def _get_task_id(self, *args, **kwargs):
#     def run_task(self, *args, **kwargs):
#     def _get_task_id(cls, *args, **kwargs):
#     def should_run_task(self, *args, **kwargs):
#     def run_task(self, *args, **kwargs):
#     def _get_task_id(self, *args, **kwargs):
#     def run_task(self, *args, **kwargs):
#     def _get_task_id(self, *args, **kwargs):
#     def run_task(self, *args, **kwargs):
#     def __init__(self, task_inst, task_args=None, task_kwargs=None, app_data=None,
#                  retry_num=0, failure_num=0):
#     def consume(*args, **kwargs):
#     def __init__(self, queue_info):
#     def get_queue(self, *args, **kwargs):
# def new_mock_message(task_class=None):
# def new_mock_task(task_class=None):


import mock
import unittest
from kale import exceptions
from kale import task
from kale import test_utils
from six.moves import range
    def testDelayedPublish(self):
        task_inst = test_utils.new_mock_task(task_class=test_utils.MockTask)
        delay_sec = 60
        random_arg = 99
        random_kwarg = 100
        payload = {
            'args': (random_arg,),
            'kwargs': {'random_kwarg': random_kwarg, 'delay_sec': delay_sec},
            'app_data': {}}
        with mock.patch(
                'kale.publisher.Publisher.publish') as publish_func:
            task_inst.publish({}, random_arg, delay_sec=delay_sec, random_kwarg=random_kwarg)
            message = test_utils.MockMessage(task_inst)

            publish_func.assert_called_once_with(test_utils.MockTask, message.task_id, payload,
                                                 delay_sec=delay_sec)

    def testTaskNoRetries(self):
        """Task task failing with retries disabled."""

        task_inst = test_utils.new_mock_task(
            task_class=test_utils.FailTaskNoRetries)
        message = test_utils.MockMessage(task_inst)

        with mock.patch(
                'kale.task.Task._report_permanent_failure') as fail_func:
            exc = exceptions.TaskException('Exception')
            retried = test_utils.FailTaskNoRetries.handle_failure(message, exc)
            self.assertFalse(retried)
            fail_func.assert_called_once_with(

# Path: kale/exceptions.py
# class ImproperlyConfiguredException(Exception):
# class TaskException(Exception):
# class ChubbyTaskException(TaskException):
# class TimeoutException(TaskException):
# class InvalidTimeLimitTaskException(TaskException):
# class InvalidTaskDelayException(TaskException):
# class BlacklistedException(TaskException):
# class QueueException(Exception):
# class SendMessagesException(QueueException):
# class DeleteMessagesException(QueueException):
# class ChangeMessagesVisibilityException(QueueException):
#     def __init__(self, msg_count):
#     def __init__(self, msg_count):
#     def __init__(self, msg_count):
# 
# Path: kale/task.py
# PERMANENT_FAILURE_UNRECOVERABLE = 'unrecoverable'
# PERMANENT_FAILURE_NO_RETRY = 'no_retries'
# PERMANENT_FAILURE_RETRIES_EXCEEDED = 'max_retries'
#     EXCEPTION_LIST = (
#         exceptions.BlacklistedException,
#         KeyError,
#         NotImplementedError,
#         SyntaxError,
#         UnboundLocalError)
# class Task(object):
#     def __init__(self, message_body=None, *args, **kwargs):
#     def _get_publisher():
#     def _get_task_id(cls, *args, **kwargs):
#     def publish(cls, app_data, *args, **kwargs):
#     def _get_delay_sec_for_retry(cls, current_retry_num):
#     def handle_failure(cls, message, raised_exception, increment_failure_num=True):
#     def republish(cls, message, failure_count):
#     def run(self, *args, **kwargs):
#     def run_task(self, *args, **kwargs):
#     def should_run_task(self, *args, **kwargs):
#     def _check_blacklist(self, *args, **kwargs):
#     def _dequeued(self, *args, **kwargs):
#     def _setup_task_environment():
#     def _clean_task_environment(task_id=None, task_name=None, exc=None):
#     def _pre_run(self, *args, **kwargs):
#     def _post_run(self, *args, **kwargs):
#     def _alert_runtime_exceeded(self, *args, **kwargs):
#     def _kill_runtime_exceeded(self, *args, **kwargs):
#     def _report_permanent_failure(cls, message, exception, failure_type,
#                                   log_exception):
# 
# Path: kale/test_utils.py
# class MockTask(task.Task):
# class FailTask(task.Task):
# class ShouldNotRunTask(task.Task):
# class TimeoutTask(task.Task):
# class SlowButNotTooSlowTask(task.Task):
# class FailTaskNoRetries(FailTask):
# class MockMessage(message.KaleMessage):
# class MockConsumer(object):
# class TestQueueClass(queue_info.TaskQueue):
# class TestQueueSelector(queue_selector.SelectQueueBase):
#     def _get_task_id(self, *args, **kwargs):
#     def run_task(self, *args, **kwargs):
#     def _get_task_id(self, *args, **kwargs):
#     def run_task(self, *args, **kwargs):
#     def _get_task_id(cls, *args, **kwargs):
#     def should_run_task(self, *args, **kwargs):
#     def run_task(self, *args, **kwargs):
#     def _get_task_id(self, *args, **kwargs):
#     def run_task(self, *args, **kwargs):
#     def _get_task_id(self, *args, **kwargs):
#     def run_task(self, *args, **kwargs):
#     def __init__(self, task_inst, task_args=None, task_kwargs=None, app_data=None,
#                  retry_num=0, failure_num=0):
#     def consume(*args, **kwargs):
#     def __init__(self, queue_info):
#     def get_queue(self, *args, **kwargs):
# def new_mock_message(task_class=None):
# def new_mock_task(task_class=None):


# Path: kale/tests/test_task.py
import mock
import unittest
from kale import exceptions
from kale import task
from kale import test_utils
from six.moves import range

    def testDelayedPublish(self):
        task_inst = test_utils.new_mock_task(task_class=test_utils.MockTask)
        delay_sec = 60
        random_arg = 99
        random_kwarg = 100
        payload = {
            'args': (random_arg,),
            'kwargs': {'random_kwarg': random_kwarg, 'delay_sec': delay_sec},
            'app_data': {}}
        with mock.patch(
                'kale.publisher.Publisher.publish') as publish_func:
            task_inst.publish({}, random_arg, delay_sec=delay_sec, random_kwarg=random_kwarg)
            message = test_utils.MockMessage(task_inst)

            publish_func.assert_called_once_with(test_utils.MockTask, message.task_id, payload,
                                                 delay_sec=delay_sec)

    def testTaskNoRetries(self):
        """Task task failing with retries disabled."""

        task_inst = test_utils.new_mock_task(
            task_class=test_utils.FailTaskNoRetries)
        message = test_utils.MockMessage(task_inst)

        with mock.patch(
                'kale.task.Task._report_permanent_failure') as fail_func:
            exc = exceptions.TaskException('Exception')
            retried = test_utils.FailTaskNoRetries.handle_failure(message, exc)
            self.assertFalse(retried)
            fail_func.assert_called_once_with(

                message, exc, task.PERMANENT_FAILURE_NO_RETRY, True)