@@ -418,12 +418,15 @@ describe("Runtime", () => {
418418 beforeEach ( async ( ) => {
419419 // Set AWS_LAMBDA_MAX_CONCURRENCY to ensure InvokeStoreMulti is used
420420 process . env . AWS_LAMBDA_MAX_CONCURRENCY = "1" ;
421+ // Use polling count of 1 for existing tests (they test handler concurrency, not multi-pending)
422+ process . env . AWS_LAMBDA_NODEJS_POLLING_COUNT = "1" ;
421423 InvokeStore . _testing ?. reset ?.( ) ;
422424 invokeStore = await InvokeStore . getInstanceAsync ( ) ;
423425 } ) ;
424426
425427 afterEach ( ( ) => {
426428 delete process . env . AWS_LAMBDA_MAX_CONCURRENCY ;
429+ delete process . env . AWS_LAMBDA_NODEJS_POLLING_COUNT ;
427430 InvokeStore . _testing ?. reset ( ) ;
428431 } ) ;
429432 it ( "should process invocations concurrently" , async ( ) => {
@@ -875,6 +878,202 @@ describe("Runtime", () => {
875878 } ) ;
876879 } ) ;
877880 } ) ;
881+
882+ describe ( "[multi-pending]" , ( ) => {
883+ beforeEach ( async ( ) => {
884+ process . env . AWS_LAMBDA_MAX_CONCURRENCY = "32" ;
885+ InvokeStore . _testing ?. reset ?.( ) ;
886+ await InvokeStore . getInstanceAsync ( ) ;
887+ } ) ;
888+
889+ afterEach ( ( ) => {
890+ delete process . env . AWS_LAMBDA_MAX_CONCURRENCY ;
891+ delete process . env . AWS_LAMBDA_NODEJS_POLLING_COUNT ;
892+ InvokeStore . _testing ?. reset ( ) ;
893+ } ) ;
894+
895+ it ( "should fire pollingCount /next calls at startup" , async ( ) => {
896+ // GIVEN
897+ process . env . AWS_LAMBDA_NODEJS_POLLING_COUNT = "4" ;
898+ // All calls hang (simulating RAPID holding /next connections)
899+ const mockRapidClient = {
900+ nextInvocation : vi . fn ( ) . mockImplementation ( ( ) => {
901+ return new Promise ( ( ) => { } ) ;
902+ } ) ,
903+ postInvocationResponse : vi . fn ( async ( ) => { } ) ,
904+ postInvocationError : vi . fn ( async ( ) => { } ) ,
905+ postInitError : vi . fn ( async ( ) => { } ) ,
906+ } as unknown as RAPIDClient ;
907+
908+ const handler : BufferedHandler = async ( ) => "done" ;
909+
910+ const runtime = Runtime . create ( {
911+ rapidClient : mockRapidClient ,
912+ handler,
913+ isMultiConcurrent : true ,
914+ } ) ;
915+
916+ // WHEN
917+ runtime . start ( ) ;
918+ await vi . advanceTimersByTimeAsync ( 10 ) ;
919+
920+ // THEN - exactly 4 calls issued (all pending, no resolutions)
921+ expect ( mockRapidClient . nextInvocation ) . toHaveBeenCalledTimes ( 4 ) ;
922+ } ) ;
923+
924+ it ( "should respect AWS_LAMBDA_NODEJS_POLLING_COUNT env var" , async ( ) => {
925+ // GIVEN
926+ process . env . AWS_LAMBDA_NODEJS_POLLING_COUNT = "2" ;
927+ const mockRapidClient = {
928+ nextInvocation : vi . fn ( ) . mockImplementation ( ( ) => {
929+ return new Promise ( ( ) => { } ) ; // all hang
930+ } ) ,
931+ postInvocationResponse : vi . fn ( async ( ) => { } ) ,
932+ postInvocationError : vi . fn ( async ( ) => { } ) ,
933+ postInitError : vi . fn ( async ( ) => { } ) ,
934+ } as unknown as RAPIDClient ;
935+
936+ const handler : BufferedHandler = async ( ) => "done" ;
937+
938+ const runtime = Runtime . create ( {
939+ rapidClient : mockRapidClient ,
940+ handler,
941+ isMultiConcurrent : true ,
942+ } ) ;
943+
944+ // WHEN
945+ runtime . start ( ) ;
946+ await vi . advanceTimersByTimeAsync ( 10 ) ;
947+
948+ // THEN - only 2 calls issued
949+ expect ( mockRapidClient . nextInvocation ) . toHaveBeenCalledTimes ( 2 ) ;
950+ } ) ;
951+
952+ it ( "should replenish /next after invocation resolves" , async ( ) => {
953+ // GIVEN
954+ vi . useRealTimers ( ) ;
955+ process . env . AWS_LAMBDA_NODEJS_POLLING_COUNT = "2" ;
956+ const resolvers : Array < ( value : unknown ) => void > = [ ] ;
957+
958+ const mockRapidClient = {
959+ nextInvocation : vi . fn ( ) . mockImplementation ( ( ) => {
960+ return new Promise ( ( resolve ) => {
961+ resolvers . push ( resolve ) ;
962+ } ) ;
963+ } ) ,
964+ postInvocationResponse : vi . fn ( async ( ) => { } ) ,
965+ postInvocationError : vi . fn ( async ( ) => { } ) ,
966+ postInitError : vi . fn ( async ( ) => { } ) ,
967+ } as unknown as RAPIDClient ;
968+
969+ const handler : BufferedHandler = async ( ) => "done" ;
970+
971+ const runtime = Runtime . create ( {
972+ rapidClient : mockRapidClient ,
973+ handler,
974+ isMultiConcurrent : true ,
975+ } ) ;
976+
977+ // WHEN - start: 2 pending
978+ runtime . start ( ) ;
979+ await new Promise ( ( r ) => setTimeout ( r , 10 ) ) ;
980+ expect ( mockRapidClient . nextInvocation ) . toHaveBeenCalledTimes ( 2 ) ;
981+ expect ( resolvers . length ) . toBe ( 2 ) ;
982+
983+ // Resolve one - should trigger replenishment
984+ resolvers [ 0 ] ( {
985+ bodyJson : JSON . stringify ( { key : "value" } ) ,
986+ headers : {
987+ "lambda-runtime-aws-request-id" : "test-id-1" ,
988+ "lambda-runtime-deadline-ms" : "1234567890" ,
989+ "lambda-runtime-invoked-function-arn" : "test:arn" ,
990+ } ,
991+ } ) ;
992+ await new Promise ( ( r ) => setTimeout ( r , 10 ) ) ;
993+
994+ // THEN - 2 initial + 1 replenishment = 3
995+ expect ( mockRapidClient . nextInvocation ) . toHaveBeenCalledTimes ( 3 ) ;
996+ } ) ;
997+
998+ it ( "should not exceed pollingCount pending calls" , async ( ) => {
999+ // GIVEN
1000+ process . env . AWS_LAMBDA_NODEJS_POLLING_COUNT = "3" ;
1001+ let concurrentPending = 0 ;
1002+ let maxConcurrentPending = 0 ;
1003+ let invocationCount = 0 ;
1004+
1005+ const mockRapidClient = {
1006+ nextInvocation : vi . fn ( ) . mockImplementation ( ( ) => {
1007+ invocationCount ++ ;
1008+ concurrentPending ++ ;
1009+ maxConcurrentPending = Math . max (
1010+ maxConcurrentPending ,
1011+ concurrentPending ,
1012+ ) ;
1013+
1014+ if ( invocationCount >= 10 ) {
1015+ return new Promise ( ( ) => { } ) ; // throttle
1016+ }
1017+
1018+ return new Promise ( ( resolve ) => {
1019+ setTimeout ( ( ) => {
1020+ concurrentPending -- ;
1021+ resolve ( {
1022+ ...baseMockInvocation ,
1023+ headers : {
1024+ ...baseMockInvocation . headers ,
1025+ "lambda-runtime-aws-request-id" : `test-id-${ invocationCount } ` ,
1026+ } ,
1027+ } ) ;
1028+ } , 10 ) ;
1029+ } ) ;
1030+ } ) ,
1031+ postInvocationResponse : vi . fn ( async ( ) => { } ) ,
1032+ postInvocationError : vi . fn ( async ( ) => { } ) ,
1033+ postInitError : vi . fn ( async ( ) => { } ) ,
1034+ } as unknown as RAPIDClient ;
1035+
1036+ const handler : BufferedHandler = async ( ) => "done" ;
1037+
1038+ const runtime = Runtime . create ( {
1039+ rapidClient : mockRapidClient ,
1040+ handler,
1041+ isMultiConcurrent : true ,
1042+ } ) ;
1043+
1044+ // WHEN
1045+ await startAndTickRuntime ( runtime , 100 ) ;
1046+
1047+ // THEN - pending never exceeded 3
1048+ expect ( maxConcurrentPending ) . toBeLessThanOrEqual ( 3 ) ;
1049+ } ) ;
1050+
1051+ it ( "should dispatch all handlers without capping in-flight" , async ( ) => {
1052+ // GIVEN
1053+ process . env . AWS_LAMBDA_NODEJS_POLLING_COUNT = "2" ;
1054+ let handlersDispatched = 0 ;
1055+
1056+ const mockRapidClient = createMockRapidClient ( 8 ) ; // allow 7 invocations
1057+
1058+ const handler : BufferedHandler = async ( ) => {
1059+ handlersDispatched ++ ;
1060+ await new Promise ( ( resolve ) => setTimeout ( resolve , 100 ) ) ;
1061+ return "done" ;
1062+ } ;
1063+
1064+ const runtime = Runtime . create ( {
1065+ rapidClient : mockRapidClient ,
1066+ handler,
1067+ isMultiConcurrent : true ,
1068+ } ) ;
1069+
1070+ // WHEN
1071+ await startAndTickRuntime ( runtime , 50 ) ;
1072+
1073+ // THEN - all resolved invocations dispatched (no cap)
1074+ expect ( handlersDispatched ) . toBeGreaterThanOrEqual ( 2 ) ;
1075+ } ) ;
1076+ } ) ;
8781077} ) ;
8791078
8801079const baseMockInvocation : InvocationRequest = {
@@ -902,7 +1101,7 @@ function createMockRapidClient(
9021101 return {
9031102 nextInvocation : vi . fn ( ) . mockImplementation ( ( ) => {
9041103 invocationCount ++ ;
905- if ( invocationCount = = throttleAt ) {
1104+ if ( throttleAt > 0 && invocationCount > = throttleAt ) {
9061105 return new Promise ( ( ) => { } ) ;
9071106 }
9081107 return Promise . resolve ( {
0 commit comments