def test_publish_messages_to_dead_letter_queue(self): """Test publisher to DLQ logic.""" sqs_inst = sqs.SQSTalk() mock_publisher = publisher.Publisher(sqs_inst) mock_queue = mock.MagicMock() mock_publisher._get_or_create_queue = mock.MagicMock( return_value=mock_queue) payload = {'args': [], 'kwargs': {}} kale_msg = message.KaleMessage( task_class=test_utils.MockTask, task_id=test_utils.MockTask._get_task_id(), payload=payload, current_retry_num=5) kale_msg.id = 'test-id' test_body = 'test-body' kale_msg.encode = mock.MagicMock(return_value=test_body) mock_messages = [kale_msg] with mock.patch.object(mock_queue, 'send_messages') as mock_write: mock_publisher.publish_messages_to_dead_letter_queue( 'dlq_name', mock_messages) expected_args = [{ 'Id': kale_msg.id, 'MessageBody': test_body, 'DelaySeconds': 0 }] mock_write.assert_called_once_with(Entries=expected_args)
def test_message_with_appdata(self): payload = {'args': [], 'kwargs': {}, 'app_data': {}} # Test create kale_msg = message.KaleMessage(task_class=task.Task, task_id=1, payload=payload, current_retry_num=None) self.assertIsNotNone(kale_msg) self.assertEqual({}, kale_msg.task_app_data)
def test_delete(self): payload = {'args': [], 'kwargs': {}} mock_delete = mock.MagicMock() kale_msg = message.KaleMessage(task_class=task.Task, task_id=1, payload=payload, current_retry_num=None, delete_func=mock_delete) kale_msg.delete() mock_delete.assert_called()
def publish(self, task_class, task_id, payload, current_retry_num=None, current_failure_num=None, delay_sec=None): """Publish the given task type to the queue with the provided payload. :param obj task_class: class of the task that we are publishing. :param str task_id: unique identifying string for this task. :param dict payload: dictionary for the task payload. :param int current_retry_num: current task retry count. If 0, this is the first attempt to run the task. :param int current_failure_num: current task failure count. :param int delay_sec: time (in seconds) that a task should stay in the queue before being released to consumers. :raises: TaskTooChubbyException: This task is outrageously chubby. The publisher of the task should handle this exception and determine how to proceed. """ if delay_sec is not None and delay_sec > settings.SQS_MAX_TASK_DELAY_SEC: raise exceptions.InvalidTaskDelayException( 'Invalid task delay_sec (%d > %d).' % (delay_sec, settings.SQS_MAX_TASK_DELAY_SEC)) queue_class = utils.class_import_from_path(settings.QUEUE_CLASS) q_info = queue_info.QueueInfo(settings.QUEUE_CONFIG, self, queue_class) queue_obj = q_info.get_queue(task_class.queue) if task_class.time_limit >= queue_obj.visibility_timeout_sec: raise exceptions.InvalidTimeLimitTaskException( 'Invalid task time limit: %d >= %d from %s' % (task_class.time_limit, queue_obj.visibility_timeout_sec, settings.QUEUE_CONFIG)) sqs_queue = self._get_or_create_queue(queue_obj.name) kale_msg = message.KaleMessage(task_class=task_class, task_id=task_id, payload=payload, current_retry_num=current_retry_num, current_failure_num=current_failure_num) sqs_queue.send_message(MessageBody=kale_msg.encode(), DelaySeconds=delay_sec or 1) logger.debug('Published task. Task id: %s; Task name: %s' % (task_id, '%s.%s' % (task_class.__module__, task_class.__name__)))
def test_message(self, mock_get_current_timestamp): mock_get_current_timestamp.return_value = 123 payload = {'args': [], 'kwargs': {}} # Test create kale_msg = message.KaleMessage(task_class=task.Task, task_id=1, payload=payload, current_retry_num=None) self.assertIsNotNone(kale_msg) self.assertEqual('kale.task.Task', kale_msg.task_name) self.assertEqual(123, kale_msg._enqueued_time) self.assertEqual(0, kale_msg.task_retry_num) self.assertEqual(1, kale_msg.task_id) self.assertEqual([], kale_msg.task_args) self.assertEqual({}, kale_msg.task_kwargs)
def test_encode(self, mock_get_current_timestamp, mock__get_publisher_data): payload = {'args': [], 'kwargs': {}, 'app_data': {}} mock_get_current_timestamp.return_value = 123 mock__get_publisher_data.return_value = 'test_publisher' kale_msg = message.KaleMessage( task_class=task.Task, task_id=1, payload=payload, current_retry_num=None) with mock.patch('kale.message.pickle') as pickle: pickle.dumps.return_value = b'\x80' actual = kale_msg.encode() expected = 'OKyZrDvbdIV4hnAi07xWGg==' self.assertEqual(expected, actual)