|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | 3 | import json |
| 4 | +import logging |
4 | 5 | import uuid |
5 | 6 | from random import randint |
6 | 7 | from typing import TYPE_CHECKING, Any |
@@ -861,3 +862,76 @@ async def simple_async_handler(record: SQSRecord): |
861 | 862 | # THEN record is processed successfully using asyncio.run() |
862 | 863 | assert result == {"batchItemFailures": []} |
863 | 864 | assert result == {"batchItemFailures": []} |
| 865 | + |
| 866 | + |
| 867 | +def test_batch_processor_logs_exception_with_injected_logger(sqs_event_factory, caplog): |
| 868 | + fail_record = sqs_event_factory("fail") |
| 869 | + success_record = sqs_event_factory("success") |
| 870 | + |
| 871 | + def handler(record): |
| 872 | + if "fail" in record["body"]: |
| 873 | + raise ValueError("intentional failure") |
| 874 | + return record["body"] |
| 875 | + |
| 876 | + test_logger = logging.getLogger("test_logger") |
| 877 | + processor = BatchProcessor(event_type=EventType.SQS, logger=test_logger) |
| 878 | + |
| 879 | + with caplog.at_level(logging.WARNING, logger="test_logger"): |
| 880 | + process_partial_response( |
| 881 | + event={"Records": [fail_record, success_record]}, |
| 882 | + record_handler=handler, |
| 883 | + processor=processor, |
| 884 | + ) |
| 885 | + |
| 886 | + warning_records = [r for r in caplog.records if r.levelno == logging.WARNING] |
| 887 | + assert len(warning_records) == 1, f"Expected 1 WARNING log, got {len(warning_records)}" |
| 888 | + assert "intentional failure" in warning_records[0].getMessage() or warning_records[0].exc_info is not None |
| 889 | + assert warning_records[0].exc_info is not None, "Expected exc_info (traceback) in log record" |
| 890 | + assert warning_records[0].exc_info[0] is ValueError |
| 891 | + |
| 892 | + |
| 893 | +def test_batch_processor_does_not_log_without_injected_logger(sqs_event_factory, caplog): |
| 894 | + fail_record = sqs_event_factory("fail") |
| 895 | + |
| 896 | + def handler(record): |
| 897 | + raise ValueError("intentional failure") |
| 898 | + |
| 899 | + processor = BatchProcessor(event_type=EventType.SQS, raise_on_entire_batch_failure=False, logger=None) |
| 900 | + |
| 901 | + with caplog.at_level(logging.WARNING, logger="aws_lambda_powertools.utilities.batch.base"): |
| 902 | + process_partial_response( |
| 903 | + event={"Records": [fail_record]}, |
| 904 | + record_handler=handler, |
| 905 | + processor=processor, |
| 906 | + ) |
| 907 | + |
| 908 | + warning_records = [r for r in caplog.records if r.levelno == logging.WARNING] |
| 909 | + assert len(warning_records) == 0, "Expected no WARNING logs when logger is None" |
| 910 | + |
| 911 | + |
| 912 | +def test_sqs_fifo_circuit_breaker_does_not_log(sqs_event_fifo_factory, caplog): |
| 913 | + failing_record = sqs_event_fifo_factory("fail", "group-1") |
| 914 | + short_circuited_record = sqs_event_fifo_factory("would-succeed", "group-1") |
| 915 | + |
| 916 | + def handler(record): |
| 917 | + if "fail" in record["body"]: |
| 918 | + raise ValueError("first record failure") |
| 919 | + return record["body"] |
| 920 | + |
| 921 | + test_logger = logging.getLogger("test_logger") |
| 922 | + processor = SqsFifoPartialProcessor(logger=test_logger) |
| 923 | + processor.raise_on_entire_batch_failure = False |
| 924 | + |
| 925 | + with caplog.at_level(logging.WARNING, logger="test_logger"): |
| 926 | + process_partial_response( |
| 927 | + event={"Records": [failing_record, short_circuited_record]}, |
| 928 | + record_handler=handler, |
| 929 | + processor=processor, |
| 930 | + ) |
| 931 | + |
| 932 | + warning_records = [r for r in caplog.records if r.levelno == logging.WARNING] |
| 933 | + assert len(warning_records) == 1, ( |
| 934 | + f"Expected exactly 1 WARNING (real exception only), got {len(warning_records)}: " |
| 935 | + + str([r.getMessage() for r in warning_records]) |
| 936 | + ) |
| 937 | + assert warning_records[0].exc_info[0] is ValueError |
0 commit comments