@@ -88,6 +88,28 @@ function countMatching<T>(items: readonly T[], predicate: (item: T) => boolean):
8888 return count ;
8989}
9090
91+ function createStalledVersionsFetch ( ) {
92+ const probeSignals : AbortSignal [ ] = [ ] ;
93+ const fetchImpl = vi . fn (
94+ async ( _input : string , init ?: Pick < RequestInit , "signal" > ) =>
95+ await new Promise < never > ( ( _resolve , reject ) => {
96+ const probeSignal = init ?. signal ?? undefined ;
97+ if ( ! probeSignal ) {
98+ reject ( new Error ( "versions probe signal missing" ) ) ;
99+ return ;
100+ }
101+ probeSignals . push ( probeSignal ) ;
102+ const rejectAborted = ( ) => reject ( new Error ( "versions probe aborted" ) ) ;
103+ if ( probeSignal . aborted ) {
104+ rejectAborted ( ) ;
105+ return ;
106+ }
107+ probeSignal . addEventListener ( "abort" , rejectAborted , { once : true } ) ;
108+ } ) ,
109+ ) ;
110+ return { fetchImpl, probeSignals } ;
111+ }
112+
91113describe ( "matrix harness runtime" , ( ) => {
92114 it ( "writes a pinned Tuwunel compose file" , async ( ) => {
93115 const outputDir = await mkdtemp ( path . join ( os . tmpdir ( ) , "matrix-qa-harness-" ) ) ;
@@ -315,52 +337,66 @@ describe("matrix harness runtime", () => {
315337 } ) ;
316338
317339 it ( "bounds a stalled versions probe by the remaining discovery deadline" , async ( ) => {
318- const probeSignals : AbortSignal [ ] = [ ] ;
319- const fetchImpl = vi . fn (
320- async ( _input : string , init ?: Pick < RequestInit , "signal" > ) =>
321- await new Promise < never > ( ( _resolve , reject ) => {
322- const probeSignal = init ?. signal ?? undefined ;
323- if ( ! probeSignal ) {
324- reject ( new Error ( "versions probe signal missing" ) ) ;
325- return ;
326- }
327- probeSignals . push ( probeSignal ) ;
328- const rejectAborted = ( ) => reject ( new Error ( "versions probe aborted" ) ) ;
329- if ( probeSignal . aborted ) {
330- rejectAborted ( ) ;
331- return ;
332- }
333- probeSignal . addEventListener ( "abort" , rejectAborted , { once : true } ) ;
334- } ) ,
335- ) ;
336- const sleepImpl = vi . fn ( async ( _ms : number ) => { } ) ;
337- const startedAt = Date . now ( ) ;
340+ vi . useFakeTimers ( ) ;
341+ const requestTimeout = new AbortController ( ) ;
342+ const timeoutSpy = vi . spyOn ( AbortSignal , "timeout" ) . mockReturnValue ( requestTimeout . signal ) ;
343+ try {
344+ const { fetchImpl, probeSignals } = createStalledVersionsFetch ( ) ;
345+ const sleepImpl = vi . fn ( async ( ) => { } ) ;
346+ const startedAt = Date . now ( ) ;
347+ const waiting = testing . waitForReachableMatrixBaseUrl ( {
348+ composeFile : "/tmp/docker-compose.matrix-qa.yml" ,
349+ containerBaseUrl : null ,
350+ fetchImpl,
351+ hostBaseUrl : "http://127.0.0.1:28008/" ,
352+ sleepImpl,
353+ timeoutMs : 25 ,
354+ pollMs : 1_000 ,
355+ } ) ;
356+ const rejection = expect ( waiting ) . rejects . toThrow ( "did not become healthy" ) ;
338357
339- await expect (
340- testing . waitForReachableMatrixBaseUrl ( {
358+ await vi . advanceTimersByTimeAsync ( 25 ) ;
359+ await rejection ;
360+
361+ expect ( Date . now ( ) - startedAt ) . toBe ( 25 ) ;
362+ expect ( probeSignals ) . toHaveLength ( 1 ) ;
363+ expect ( probeSignals [ 0 ] ?. aborted ) . toBe ( true ) ;
364+ expect ( sleepImpl ) . not . toHaveBeenCalled ( ) ;
365+ } finally {
366+ timeoutSpy . mockRestore ( ) ;
367+ vi . useRealTimers ( ) ;
368+ }
369+ } ) ;
370+
371+ it ( "does not re-poll when the request timeout wins at the discovery deadline" , async ( ) => {
372+ vi . useFakeTimers ( ) ;
373+ const requestTimeout = new AbortController ( ) ;
374+ const timeoutSpy = vi . spyOn ( AbortSignal , "timeout" ) . mockReturnValue ( requestTimeout . signal ) ;
375+ try {
376+ const { fetchImpl } = createStalledVersionsFetch ( ) ;
377+ const startedAt = Date . now ( ) ;
378+ const sleepImpl = vi . fn ( async ( ms : number ) => {
379+ vi . setSystemTime ( Date . now ( ) + ms ) ;
380+ } ) ;
381+ const waiting = testing . waitForReachableMatrixBaseUrl ( {
341382 composeFile : "/tmp/docker-compose.matrix-qa.yml" ,
342383 containerBaseUrl : null ,
343384 fetchImpl,
344385 hostBaseUrl : "http://127.0.0.1:28008/" ,
345386 sleepImpl,
346387 timeoutMs : 25 ,
347388 pollMs : 1_000 ,
348- } ) ,
349- ) . rejects . toThrow ( "did not become healthy" ) ;
350-
351- expect ( Date . now ( ) - startedAt ) . toBeLessThan ( 500 ) ;
352- expect ( probeSignals ) . not . toHaveLength ( 0 ) ;
353- expect ( probeSignals . length ) . toBeLessThanOrEqual ( 2 ) ;
354- expect ( probeSignals . every ( ( signal ) => signal . aborted ) ) . toBe ( true ) ;
355- // The inner AbortSignal.timeout (clamped to the remaining budget) and the
356- // outer deadline timer race at ~timeoutMs. When the inner fires first the
357- // loop takes one residual micro-sleep before Date.now() crosses the
358- // deadline. The protected invariant is bounded exit, not zero sleeps:
359- // allow at most one sleep and require it to be within the deadline budget.
360- expect ( sleepImpl . mock . calls . length ) . toBeLessThanOrEqual ( 1 ) ;
361- const residualSleepMs = sleepImpl . mock . calls [ 0 ] ?. [ 0 ] ;
362- if ( residualSleepMs !== undefined ) {
363- expect ( residualSleepMs ) . toBeLessThanOrEqual ( 25 ) ;
389+ } ) ;
390+
391+ vi . setSystemTime ( startedAt + 24 ) ;
392+ requestTimeout . abort ( new DOMException ( "request timed out" , "TimeoutError" ) ) ;
393+ await expect ( waiting ) . rejects . toThrow ( "did not become healthy" ) ;
394+
395+ expect ( fetchImpl ) . toHaveBeenCalledTimes ( 1 ) ;
396+ expect ( sleepImpl ) . not . toHaveBeenCalled ( ) ;
397+ } finally {
398+ timeoutSpy . mockRestore ( ) ;
399+ vi . useRealTimers ( ) ;
364400 }
365401 } ) ;
366402
0 commit comments