@@ -767,3 +767,66 @@ async def test_async_process_partial_response_raises_unexpected_batch_type(event
767767 assert "Unexpected batch event type. Possible values are: SQS, KinesisDataStreams, DynamoDBStreams" in str (
768768 exc_info .value ,
769769 )
770+
771+
772+ def test_async_batch_processor_lambda_cold_start_creates_new_loop (sqs_event_factory , monkeypatch ):
773+ """Test async processing creates new event loop in Lambda cold start (Python 3.14+ compatibility)"""
774+ import asyncio
775+
776+ # GIVEN Lambda environment is set (cold start scenario)
777+ monkeypatch .setenv ("LAMBDA_TASK_ROOT" , "/var/task" )
778+
779+ # Close any existing event loop to simulate cold start
780+ try :
781+ loop = asyncio .get_event_loop ()
782+ if not loop .is_closed ():
783+ loop .close ()
784+ except RuntimeError :
785+ pass
786+
787+ # Simple async handler without external dependencies
788+ async def simple_async_handler (record : SQSRecord ):
789+ await asyncio .sleep (0.001 ) # Yield control to event loop
790+ return {"processed" : record .body }
791+
792+ records = [sqs_event_factory ("success" ), sqs_event_factory ("success" )]
793+ event = {"Records" : records }
794+ processor = AsyncBatchProcessor (event_type = EventType .SQS )
795+
796+ # WHEN calling async_process_partial_response synchronously (like Lambda handler does)
797+ result = async_process_partial_response (
798+ event = event ,
799+ record_handler = simple_async_handler ,
800+ processor = processor ,
801+ )
802+
803+ # THEN all records are processed successfully with new event loop created
804+ assert result == {"batchItemFailures" : []}
805+
806+
807+ def test_async_batch_processor_non_lambda_uses_asyncio_run (sqs_event_factory , monkeypatch ):
808+ """Test async processing uses asyncio.run outside Lambda environment"""
809+ import asyncio
810+
811+ # GIVEN Lambda environment is NOT set
812+ monkeypatch .delenv ("LAMBDA_TASK_ROOT" , raising = False )
813+
814+ # Simple async handler without external dependencies
815+ async def simple_async_handler (record : SQSRecord ):
816+ await asyncio .sleep (0.001 ) # Yield control to event loop
817+ return {"processed" : record .body }
818+
819+ records = [sqs_event_factory ("success" )]
820+ event = {"Records" : records }
821+ processor = AsyncBatchProcessor (event_type = EventType .SQS )
822+
823+ # WHEN calling async_process_partial_response outside Lambda
824+ result = async_process_partial_response (
825+ event = event ,
826+ record_handler = simple_async_handler ,
827+ processor = processor ,
828+ )
829+
830+ # THEN record is processed successfully using asyncio.run()
831+ assert result == {"batchItemFailures" : []}
832+ assert result == {"batchItemFailures" : []}
0 commit comments