@@ -569,6 +569,56 @@ function isRetryableGatewayCallError(details: string): boolean {
569569 ) ;
570570}
571571
572+ async function callQaGatewayWithRetry < T > ( params : {
573+ deadlineMs ?: number ;
574+ logs : ( ) => string ;
575+ request : ( options : { deadlineMs ?: number ; timeoutMs : number } ) => Promise < T > ;
576+ throwChildFailure : ( ) => void ;
577+ timeoutMs : number ;
578+ waitForReady : ( timeoutMs : number ) => Promise < void > ;
579+ } ) {
580+ const remainingMs = ( ) =>
581+ params . deadlineMs === undefined ? undefined : params . deadlineMs - Date . now ( ) ;
582+ const deadlineError = ( ) =>
583+ new Error ( `gateway call deadline exceeded${ formatQaGatewayLogsForError ( params . logs ( ) ) } ` ) ;
584+ let lastDetails = "" ;
585+ for ( let attempt = 1 ; attempt <= 3 ; attempt += 1 ) {
586+ params . throwChildFailure ( ) ;
587+ const requestRemainingMs = remainingMs ( ) ;
588+ if ( requestRemainingMs !== undefined && requestRemainingMs <= 0 ) {
589+ throw deadlineError ( ) ;
590+ }
591+ try {
592+ return await params . request ( {
593+ ...( params . deadlineMs === undefined ? { } : { deadlineMs : params . deadlineMs } ) ,
594+ timeoutMs :
595+ requestRemainingMs === undefined
596+ ? params . timeoutMs
597+ : Math . min ( params . timeoutMs , requestRemainingMs ) ,
598+ } ) ;
599+ } catch ( error ) {
600+ params . throwChildFailure ( ) ;
601+ const details = formatErrorMessage ( error ) ;
602+ lastDetails = details ;
603+ if ( attempt >= 3 || ! isRetryableGatewayCallError ( details ) ) {
604+ throw new Error ( `${ details } ${ formatQaGatewayLogsForError ( params . logs ( ) ) } ` , {
605+ cause : error ,
606+ } ) ;
607+ }
608+ const readinessRemainingMs = remainingMs ( ) ;
609+ if ( readinessRemainingMs !== undefined && readinessRemainingMs <= 0 ) {
610+ throw deadlineError ( ) ;
611+ }
612+ await params . waitForReady (
613+ readinessRemainingMs === undefined
614+ ? Math . max ( 10_000 , params . timeoutMs )
615+ : Math . min ( Math . max ( 10_000 , params . timeoutMs ) , readinessRemainingMs ) ,
616+ ) ;
617+ }
618+ }
619+ throw new Error ( `${ lastDetails } ${ formatQaGatewayLogsForError ( params . logs ( ) ) } ` ) ;
620+ }
621+
572622function createQaGatewayChildLogCollector ( ) {
573623 const chunks : Buffer [ ] = [ ] ;
574624 return {
@@ -633,6 +683,7 @@ function formatQaGatewayProcessBoundaryStartupFailure(error: unknown, logs: stri
633683async function fetchLocalGatewayHealth ( params : {
634684 baseUrl : string ;
635685 healthPath : "/readyz" | "/healthz" ;
686+ timeoutMs ?: number ;
636687} ) : Promise < boolean > {
637688 const { response, release } = await fetchWithSsrFGuard ( {
638689 url : `${ params . baseUrl } ${ params . healthPath } ` ,
@@ -641,7 +692,7 @@ async function fetchLocalGatewayHealth(params: {
641692 headers : {
642693 connection : "close" ,
643694 } ,
644- signal : AbortSignal . timeout ( 2_000 ) ,
695+ signal : AbortSignal . timeout ( params . timeoutMs ?? 2_000 ) ,
645696 } ,
646697 policy : { allowPrivateNetwork : true } ,
647698 auditContext : "qa-lab-gateway-child-health" ,
@@ -698,6 +749,7 @@ export const testing = {
698749 buildQaRuntimeEnv,
699750 cleanupQaGatewayTempRoots,
700751 fetchLocalGatewayHealth,
752+ callQaGatewayWithRetry,
701753 isRetryableGatewayCallError,
702754 isRetryableRpcStartupError,
703755 classifyQaGatewayStartupRetry,
@@ -1093,8 +1145,9 @@ async function waitForGatewayReady(params: {
10931145 getChildFailure ?: ( ) => QaChildFailure | null ;
10941146 timeoutMs ?: number ;
10951147} ) {
1096- const startedAt = Date . now ( ) ;
1097- while ( Date . now ( ) - startedAt < ( params . timeoutMs ?? 60_000 ) ) {
1148+ const deadline = Date . now ( ) + ( params . timeoutMs ?? 60_000 ) ;
1149+ let remainingMs : number ;
1150+ while ( ( remainingMs = deadline - Date . now ( ) ) > 0 ) {
10981151 throwQaGatewayChildFailure ( params . getChildFailure , params . logs ) ;
10991152 if ( params . child . exitCode !== null || params . child . signalCode !== null ) {
11001153 throw new QaSuiteInfraError (
@@ -1104,13 +1157,19 @@ async function waitForGatewayReady(params: {
11041157 }
11051158 // Listener liveness can turn green before the Gateway can admit startup or restart work.
11061159 try {
1107- if ( await fetchLocalGatewayHealth ( { baseUrl : params . baseUrl , healthPath : "/readyz" } ) ) {
1160+ if (
1161+ await fetchLocalGatewayHealth ( {
1162+ baseUrl : params . baseUrl ,
1163+ healthPath : "/readyz" ,
1164+ timeoutMs : Math . min ( 2_000 , remainingMs ) ,
1165+ } )
1166+ ) {
11081167 return ;
11091168 }
11101169 } catch {
11111170 // retry until timeout
11121171 }
1113- await sleep ( 250 ) ;
1172+ await sleep ( Math . min ( 250 , Math . max ( 0 , deadline - Date . now ( ) ) ) ) ;
11141173 }
11151174 throw new QaSuiteInfraError (
11161175 "gateway_startup_unhealthy" ,
@@ -1853,34 +1912,28 @@ export async function startQaGatewayChild(params: {
18531912 async call (
18541913 method : string ,
18551914 rpcParams ?: unknown ,
1856- opts ?: { expectFinal ?: boolean ; timeoutMs ?: number } ,
1915+ opts ?: { deadlineMs ?: number ; expectFinal ?: boolean ; timeoutMs ?: number } ,
18571916 ) {
18581917 const timeoutMs = opts ?. timeoutMs ?? 20_000 ;
1859- let lastDetails = "" ;
1860- for ( let attempt = 1 ; attempt <= 3 ; attempt += 1 ) {
1861- throwActiveChildFailure ( ) ;
1862- try {
1863- return await activeRpcClient . request ( method , rpcParams , {
1918+ return await callQaGatewayWithRetry ( {
1919+ deadlineMs : opts ?. deadlineMs ,
1920+ logs ,
1921+ request : async ( requestOptions ) =>
1922+ await activeRpcClient . request ( method , rpcParams , {
18641923 ...opts ,
1865- timeoutMs,
1866- } ) ;
1867- } catch ( error ) {
1868- throwActiveChildFailure ( ) ;
1869- const details = formatErrorMessage ( error ) ;
1870- lastDetails = details ;
1871- if ( attempt >= 3 || ! isRetryableGatewayCallError ( details ) ) {
1872- throw new Error ( `${ details } ${ formatQaGatewayLogsForError ( logs ( ) ) } ` , { cause : error } ) ;
1873- }
1924+ ...requestOptions ,
1925+ } ) ,
1926+ throwChildFailure : throwActiveChildFailure ,
1927+ timeoutMs,
1928+ waitForReady : async ( readinessTimeoutMs ) =>
18741929 await waitForGatewayReady ( {
18751930 baseUrl,
18761931 logs,
18771932 child : activeChild ,
18781933 getChildFailure : activeGetChildFailure ,
1879- timeoutMs : Math . max ( 10_000 , timeoutMs ) ,
1880- } ) ;
1881- }
1882- }
1883- throw new Error ( `${ lastDetails } ${ formatQaGatewayLogsForError ( logs ( ) ) } ` ) ;
1934+ timeoutMs : readinessTimeoutMs ,
1935+ } ) ,
1936+ } ) ;
18841937 } ,
18851938 async stop ( opts ?: { keepTemp ?: boolean ; preserveToDir ?: string } ) {
18861939 await activeRpcClient . stop ( ) . catch ( ( ) => { } ) ;
0 commit comments