@@ -353,6 +353,30 @@ describe("Runtime", () => {
353353 ] ) ;
354354 } ) ;
355355
356+ it ( "should set tenantId in InvokeStore for buffered invocations" , async ( ) => {
357+ // GIVEN
358+ const mockRapidClient = createMockRapidClient ( - 1 , false , true ) ;
359+ const storeOrder : ( string | undefined ) [ ] = [ ] ;
360+
361+ const BufferedHandler : BufferedHandler = async ( ) => {
362+ storeOrder . push ( invokeStore . getTenantId ( ) ) ;
363+ await new Promise ( ( resolve ) => setTimeout ( resolve , 50 ) ) ; // Simulate work
364+ return "done" ;
365+ } ;
366+
367+ const runtime = Runtime . create ( {
368+ rapidClient : mockRapidClient ,
369+ handler : BufferedHandler ,
370+ isMultiConcurrent : false ,
371+ } ) ;
372+
373+ // WHEN
374+ await startAndTickRuntime ( runtime , 60 ) ;
375+
376+ // THEN - tenantId is pinned to the sandbox, constant across invocations
377+ expect ( storeOrder ) . toEqual ( [ "test-tenant" , "test-tenant" ] ) ;
378+ } ) ;
379+
356380 it ( "should set requestId and traceId in InvokeStore for streaming invocations" , async ( ) => {
357381 // GIVEN
358382 const mockRapidClient = createMockRapidClient ( 3 , true ) ;
@@ -556,6 +580,45 @@ describe("Runtime", () => {
556580 expect ( mockRapidClient . postInvocationResponse ) . toHaveBeenCalledTimes ( 2 ) ;
557581 } ) ;
558582
583+ it ( "should set tenantId in InvokeStore for concurrent invocations" , async ( ) => {
584+ // GIVEN
585+ const mockRapidClient = createMockRapidClient ( 3 , false , true ) ;
586+ const storeOrder : ( string | undefined ) [ ] = [ ] ;
587+ const completionOrder : ( string | undefined ) [ ] = [ ] ;
588+
589+ const BufferedHandler : BufferedHandler = async ( ) => {
590+ const invocationNumber = storeOrder . length + 1 ;
591+
592+ storeOrder . push ( invokeStore . getTenantId ( ) ) ;
593+
594+ await new Promise ( ( resolve ) =>
595+ setTimeout ( resolve , invocationNumber === 1 ? 10 : 5 ) ,
596+ ) ;
597+
598+ completionOrder . push ( invokeStore . getTenantId ( ) ) ;
599+
600+ return "done" ;
601+ } ;
602+
603+ const runtime = Runtime . create ( {
604+ rapidClient : mockRapidClient ,
605+ handler : BufferedHandler ,
606+ isMultiConcurrent : true ,
607+ } ) ;
608+
609+ // WHEN
610+ runtime . start ( ) ;
611+ await new Promise ( ( resolve ) => setTimeout ( resolve , 20 ) ) ;
612+
613+ // THEN - tenantId is pinned to the sandbox, so every concurrent
614+ // invocation reads the same value, both at start and after async gaps.
615+ expect ( storeOrder ) . toEqual ( [ "test-tenant" , "test-tenant" ] ) ;
616+ expect ( completionOrder ) . toEqual ( [ "test-tenant" , "test-tenant" ] ) ;
617+
618+ expect ( mockRapidClient . nextInvocation ) . toHaveBeenCalledTimes ( 3 ) ;
619+ expect ( mockRapidClient . postInvocationResponse ) . toHaveBeenCalledTimes ( 2 ) ;
620+ } ) ;
621+
559622 it ( "should maintain context when mixing different timer types in RIC" , async ( ) => {
560623 // GIVEN
561624 const mockRapidClient = createMockRapidClient ( 3 , true ) ;
@@ -897,6 +960,7 @@ async function startAndTickRuntime(
897960function createMockRapidClient (
898961 throttleAt : number = - 1 ,
899962 passXRayTraceId : boolean = false ,
963+ passTenantId : boolean = false ,
900964) : RAPIDClient {
901965 let invocationCount = 0 ;
902966 return {
@@ -915,6 +979,14 @@ function createMockRapidClient(
915979 "lambda-runtime-trace-id" : `trace-id-${ invocationCount } ` ,
916980 }
917981 : { } ) ,
982+ ...( passTenantId
983+ ? {
984+ // tenantId is pinned to the sandbox, so it's constant across
985+ // every invocation in the execution environment's lifecycle
986+ // (unlike requestId/traceId, which change per invocation).
987+ "lambda-runtime-aws-tenant-id" : "test-tenant" ,
988+ }
989+ : { } ) ,
918990 } ,
919991 } ) ;
920992 } ) ,
0 commit comments