# 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/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/worker.py
# SIGNALS_TO_HANDLE = (
#     signal.SIGABRT,
#     # This will catch Ctrl-C interrupts.
#     signal.SIGINT,
#     signal.SIGQUIT,
#     signal.SIGTERM,
#     # Handle Ctrl-Z/suspend.
#     signal.SIGTSTP)
# LOG_TASK_RESULT_DEFERRED = 'deferred'
# LOG_TASK_RESULT_ERROR = 'error'
# LOG_TASK_RESULT_SUCCESS = 'success'
# class Worker(object):
#     def __init__(self):
#     def _get_publisher():
#     def _on_pre_run_worker(self):
#     def _on_exceeding_memory_limit(self, ru_maxrss_mb):
#     def _on_sigtstp(self, num_completed, num_incomplete):
#     def _on_shutdown(self, num_completed, num_incomplete):
#     def _on_pre_batch_run(self, messages):
#     def _on_post_batch_run(self, num_completed, num_incomplete, messages):
#     def _on_permanent_failure_batch(self):
#     def _on_task_deferred(self, message, time_remaining_sec):
#     def _on_task_failed(self, message, time_remaining_sec, err,
#                         permanent_failure):
#     def _on_task_succeeded(self, message, time_remaining_sec):
#     def run(self):
#     def _check_process_resources(self):
#     def _cleanup_worker(self, signum, frame):
#     def _run_single_iteration(self):
#     def _release_batch(self):
#     def _run_batch(self, message_batch):
#     def _run_single_message(self, message, time_remaining_sec):
#     def remove_message_or_exit(self, message):
#     def should_run_task(self, message):
#     def run_task(self, message):


import mock
import signal
import unittest
from kale import exceptions
from kale import test_utils
from kale import worker
from six.moves import range
        message = test_utils.new_mock_message(task_class=test_utils.FailTask)
        message_batch = [message]
        num_messages = len(message_batch)

        worker_inst._run_batch(message_batch)

        fail_msg, fail_exc = mock_failure.call_args[0]
        self.assertEqual(fail_msg, message)
        self.assertTrue(type(fail_exc) == exceptions.TaskException)
        self.assertEqual(0, len(worker_inst._incomplete_messages))
        self.assertEqual(0, len(worker_inst._successful_messages))
        self.assertEqual(0, len(worker_inst._permanent_failures))
        self.assertEqual(num_messages, len(worker_inst._failed_messages))

    def testRunBatchTaskShouldNotRun(self):
        """Test batch with a task which should not run."""
        mock_consumer = self._create_patch('kale.consumer.Consumer')
        get_time = self._create_patch('time.time')
        mock_republish = self._create_patch(
            'kale.test_utils.ShouldNotRunTask.republish')

        worker_inst = worker.Worker()
        worker_inst._batch_queue = worker_inst._queue_selector.get_queue()
        mock_consumer.assert_called_once_with()

        worker_inst._batch_stop_time = 100
        # _batch_stop_time - (get_time + task.time_limit) > 0
        # (100 - (10 + 60)) > 0)
        get_time.return_value = 10


# 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/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/worker.py
# SIGNALS_TO_HANDLE = (
#     signal.SIGABRT,
#     # This will catch Ctrl-C interrupts.
#     signal.SIGINT,
#     signal.SIGQUIT,
#     signal.SIGTERM,
#     # Handle Ctrl-Z/suspend.
#     signal.SIGTSTP)
# LOG_TASK_RESULT_DEFERRED = 'deferred'
# LOG_TASK_RESULT_ERROR = 'error'
# LOG_TASK_RESULT_SUCCESS = 'success'
# class Worker(object):
#     def __init__(self):
#     def _get_publisher():
#     def _on_pre_run_worker(self):
#     def _on_exceeding_memory_limit(self, ru_maxrss_mb):
#     def _on_sigtstp(self, num_completed, num_incomplete):
#     def _on_shutdown(self, num_completed, num_incomplete):
#     def _on_pre_batch_run(self, messages):
#     def _on_post_batch_run(self, num_completed, num_incomplete, messages):
#     def _on_permanent_failure_batch(self):
#     def _on_task_deferred(self, message, time_remaining_sec):
#     def _on_task_failed(self, message, time_remaining_sec, err,
#                         permanent_failure):
#     def _on_task_succeeded(self, message, time_remaining_sec):
#     def run(self):
#     def _check_process_resources(self):
#     def _cleanup_worker(self, signum, frame):
#     def _run_single_iteration(self):
#     def _release_batch(self):
#     def _run_batch(self, message_batch):
#     def _run_single_message(self, message, time_remaining_sec):
#     def remove_message_or_exit(self, message):
#     def should_run_task(self, message):
#     def run_task(self, message):


# Path: kale/tests/test_worker.py
import mock
import signal
import unittest
from kale import exceptions
from kale import test_utils
from kale import worker
from six.moves import range

        message = test_utils.new_mock_message(task_class=test_utils.FailTask)
        message_batch = [message]
        num_messages = len(message_batch)

        worker_inst._run_batch(message_batch)

        fail_msg, fail_exc = mock_failure.call_args[0]
        self.assertEqual(fail_msg, message)
        self.assertTrue(type(fail_exc) == exceptions.TaskException)
        self.assertEqual(0, len(worker_inst._incomplete_messages))
        self.assertEqual(0, len(worker_inst._successful_messages))
        self.assertEqual(0, len(worker_inst._permanent_failures))
        self.assertEqual(num_messages, len(worker_inst._failed_messages))

    def testRunBatchTaskShouldNotRun(self):
        """Test batch with a task which should not run."""
        mock_consumer = self._create_patch('kale.consumer.Consumer')
        get_time = self._create_patch('time.time')
        mock_republish = self._create_patch(
            'kale.test_utils.ShouldNotRunTask.republish')

        worker_inst = worker.Worker()
        worker_inst._batch_queue = worker_inst._queue_selector.get_queue()
        mock_consumer.assert_called_once_with()

        worker_inst._batch_stop_time = 100
        # _batch_stop_time - (get_time + task.time_limit) > 0
        # (100 - (10 + 60)) > 0)
        get_time.return_value = 10


        message = test_utils.new_mock_message(task_class=test_utils.ShouldNotRunTask)