@@ -6,7 +6,7 @@ import { IOS_DEVICE, IOS_SIMULATOR, MACOS_DEVICE } from './device-fixtures.ts';
66import { appleRunnerTestHost } from '../test-host.ts' ;
77import type { ExecOptions } from '../host.ts' ;
88import {
9- createRunnerPhaseDeadline ,
9+ createRunnerPhaseBudget ,
1010 diffComparableRunnerCacheMetadata ,
1111 resolveRunnerBundleBuildSettings ,
1212 resolveRunnerMaxConcurrentDestinationsFlag ,
@@ -278,37 +278,16 @@ describe('toolchain probe budget', () => {
278278 assert . deepEqual ( xcodebuildTimeouts , [ COLD_TOOLCHAIN_PROBE_TIMEOUT_MS , 15_000 ] ) ;
279279 } ) ;
280280
281- test ( 'a toolchain host that never returns stops at the shared budget instead of once per probe' , ( ) => {
282- const clock = installFakeToolchainClock ( ) ;
283- runCmdSync . mockImplementation ( ( command : string , args : string [ ] , options : ExecOptions ) => {
284- throw blockForWholeTimeout ( clock , command , args , options ) ;
285- } ) ;
286- runCmdSync . mockClear ( ) ;
287-
288- assert . throws (
289- ( ) => resolveExpectedRunnerCacheMetadata ( MACOS_DEVICE ) ,
290- // A budget spent before the remaining probes could start is not an
291- // unreadable toolchain: nothing probed it, so the error says the budget
292- // ran out rather than pointing at `xcode-select`.
293- ( error : unknown ) => expectRunnerPhaseBudgetExhausted ( error ) ,
294- ) ;
295- // 30 s + a 15 s retry spends the whole budget on the first probe; the two
296- // xcrun probes then fail on the budget instead of blocking for 30 s each.
297- assert . equal ( runCmdSync . mock . calls . length , 2 ) ;
298- assert . equal ( clock . nowMs , 45_000 ) ;
299- } ) ;
300-
301281 test ( 'an owning phase with 4 s left gets one 4 s attempt and no retry' , ( ) => {
302282 const clock = installFakeToolchainClock ( ) ;
303- const phaseDeadline = createRunnerPhaseDeadline ( 4_000 ) ;
283+ const phaseBudget = createRunnerPhaseBudget ( 4_000 , undefined ) ;
304284 runCmdSync . mockImplementation ( ( command : string , args : string [ ] , options : ExecOptions ) => {
305285 throw blockForWholeTimeout ( clock , command , args , options ) ;
306286 } ) ;
307287 runCmdSync . mockClear ( ) ;
308288
309289 assert . throws (
310- ( ) =>
311- resolveExpectedRunnerCacheMetadata ( IOS_SIMULATOR , undefined , { deadline : phaseDeadline } ) ,
290+ ( ) => resolveExpectedRunnerCacheMetadata ( IOS_SIMULATOR , undefined , phaseBudget ) ,
312291 ( error : unknown ) => expectRunnerPhaseBudgetExhausted ( error ) ,
313292 ) ;
314293 assert . equal ( runCmdSync . mock . calls . length , 1 ) ;
@@ -317,75 +296,6 @@ describe('toolchain probe budget', () => {
317296 assert . equal ( clock . nowMs , 4_000 ) ;
318297 } ) ;
319298
320- test ( 'a request canceled while a probe blocked surfaces the cancellation instead of retrying' , ( ) => {
321- const clock = installFakeToolchainClock ( ) ;
322- const request = new AbortController ( ) ;
323- runCmdSync . mockImplementation ( ( command : string , args : string [ ] , options : ExecOptions ) => {
324- const timeout = blockForWholeTimeout ( clock , command , args , options ) ;
325- request . abort ( ) ;
326- throw timeout ;
327- } ) ;
328- runCmdSync . mockClear ( ) ;
329-
330- assert . throws (
331- ( ) =>
332- resolveExpectedRunnerCacheMetadata ( IOS_SIMULATOR , undefined , { signal : request . signal } ) ,
333- ( error : unknown ) => isRequestCanceledError ( error ) ,
334- ) ;
335- assert . equal ( runCmdSync . mock . calls . length , 1 ) ;
336- } ) ;
337-
338- test ( 'a non-timeout error that also cancels the request on the last probe surfaces cancellation, not an unavailable toolchain' , ( ) => {
339- const request = new AbortController ( ) ;
340- runCmdSync . mockImplementation ( ( command : string , args : string [ ] ) => {
341- // The final probe: abort the request and fail with a plain command
342- // error, not the exec layer's structured timeout -- there is no next
343- // attempt left to catch the cancellation, so the catch here must.
344- if ( command === 'xcrun' && args . includes ( '--show-sdk-build-version' ) ) {
345- request . abort ( ) ;
346- throw new AppError ( 'COMMAND_FAILED' , 'xcrun: unexpected error' , { } ) ;
347- }
348- return appleToolchainProbeResult ( command , args ) ;
349- } ) ;
350- runCmdSync . mockClear ( ) ;
351-
352- assert . throws (
353- ( ) =>
354- resolveExpectedRunnerCacheMetadata ( IOS_SIMULATOR , undefined , { signal : request . signal } ) ,
355- ( error : unknown ) => isRequestCanceledError ( error ) ,
356- ) ;
357- assert . equal ( runCmdSync . mock . calls . length , 3 ) ;
358- } ) ;
359-
360- test ( 'an already-canceled request runs no toolchain probe at all, cold or with the fingerprint cache warm' , ( ) => {
361- installFakeToolchainClock ( ) ;
362- runCmdSync . mockClear ( ) ;
363-
364- assert . throws (
365- ( ) =>
366- resolveExpectedRunnerCacheMetadata ( IOS_SIMULATOR , undefined , {
367- signal : AbortSignal . abort ( ) ,
368- } ) ,
369- ( error : unknown ) => isRequestCanceledError ( error ) ,
370- ) ;
371- assert . equal ( runCmdSync . mock . calls . length , 0 ) ;
372-
373- // Warm the real fingerprint memo with an ordinary request, then repeat
374- // with an already-aborted signal: the cache-hit path must check
375- // cancellation before it returns the memoized value, not skip it (#2422).
376- resolveExpectedRunnerCacheMetadata ( IOS_SIMULATOR ) ;
377- runCmdSync . mockClear ( ) ;
378-
379- assert . throws (
380- ( ) =>
381- resolveExpectedRunnerCacheMetadata ( IOS_SIMULATOR , undefined , {
382- signal : AbortSignal . abort ( ) ,
383- } ) ,
384- ( error : unknown ) => isRequestCanceledError ( error ) ,
385- ) ;
386- assert . equal ( runCmdSync . mock . calls . length , 0 ) ;
387- } ) ;
388-
389299 test ( 'a probe that failed on its own and merely says "timed out" in its message is not retried' , ( ) => {
390300 installFakeToolchainClock ( ) ;
391301 runCmdSync . mockImplementation ( ( command : string , args : string [ ] ) => {
@@ -409,6 +319,174 @@ describe('toolchain probe budget', () => {
409319 ) ;
410320 expect ( runCmdSync . mock . calls . filter ( ( [ command ] ) => command === 'xcodebuild' ) ) . toHaveLength ( 1 ) ;
411321 } ) ;
322+
323+ /**
324+ * One row per way a fingerprint read can be interrupted: when the owning request aborts,
325+ * what the probe that abort lands on does, and what the phase must then surface.
326+ * `spawnSync` cannot be interrupted once it has started, so a cancellation is only ever
327+ * observed between attempts -- the exec count is what pins which attempt each row stopped.
328+ */
329+ type ToolchainProbeCancellationCase = {
330+ label : string ;
331+ /** Which execs refuse to answer, and how they end; every other exec answers. */
332+ failing ?: { at : 'first' | 'final' | 'every' ; as : 'exec-timeout' | 'command-failure' } ;
333+ /**
334+ * When the owning request aborts: never, before the phase runs a probe at all, while
335+ * the failing exec is still blocked, or as that exec's timeout unwinds.
336+ */
337+ aborts : 'never' | 'before-any-probe' | 'while-it-blocks' | 'as-it-unwinds' ;
338+ /** Whether the process-wide fingerprint memo already holds an answer. */
339+ fingerprintCache ?: 'warm' ;
340+ expected : 'request-canceled' | 'budget-exhausted' | 'fingerprint' ;
341+ /** Execs the phase is allowed to have run by the time it settles. */
342+ execs : number ;
343+ /** Wall clock the phase spent: only an exec that blocks for its whole timeout moves it. */
344+ clockMs : number ;
345+ } ;
346+
347+ const CANCELLATION_CASES : ToolchainProbeCancellationCase [ ] = [
348+ {
349+ label : 'aborted before the first probe, cold cache' ,
350+ aborts : 'before-any-probe' ,
351+ expected : 'request-canceled' ,
352+ execs : 0 ,
353+ clockMs : 0 ,
354+ } ,
355+ {
356+ // A cache hit must not answer a request that is already gone (#2422 round 4).
357+ label : 'aborted before the first probe, fingerprint cache warm' ,
358+ aborts : 'before-any-probe' ,
359+ fingerprintCache : 'warm' ,
360+ expected : 'request-canceled' ,
361+ execs : 0 ,
362+ clockMs : 0 ,
363+ } ,
364+ {
365+ label : 'aborted while the first probe blocks, and it then times out' ,
366+ failing : { at : 'first' , as : 'exec-timeout' } ,
367+ aborts : 'while-it-blocks' ,
368+ expected : 'request-canceled' ,
369+ execs : 1 ,
370+ clockMs : COLD_TOOLCHAIN_PROBE_TIMEOUT_MS ,
371+ } ,
372+ {
373+ label : 'aborted while the first probe fails with a non-timeout error' ,
374+ failing : { at : 'first' , as : 'command-failure' } ,
375+ aborts : 'while-it-blocks' ,
376+ expected : 'request-canceled' ,
377+ execs : 1 ,
378+ clockMs : 0 ,
379+ } ,
380+ {
381+ // The budget still had 15 s: only the cancellation stops the retry.
382+ label : "aborted as the first attempt's timeout unwinds, before its retry" ,
383+ failing : { at : 'first' , as : 'exec-timeout' } ,
384+ aborts : 'as-it-unwinds' ,
385+ expected : 'request-canceled' ,
386+ execs : 1 ,
387+ clockMs : COLD_TOOLCHAIN_PROBE_TIMEOUT_MS ,
388+ } ,
389+ {
390+ // Nothing is left to catch the cancellation on a later attempt, so the failing
391+ // probe's own catch must: an unavailable toolchain would be the wrong verdict.
392+ label : 'aborted while the final probe fails with a non-timeout error' ,
393+ failing : { at : 'final' , as : 'command-failure' } ,
394+ aborts : 'while-it-blocks' ,
395+ expected : 'request-canceled' ,
396+ execs : 3 ,
397+ clockMs : 0 ,
398+ } ,
399+ {
400+ label : 'aborted while the final probe blocks, and it then times out' ,
401+ failing : { at : 'final' , as : 'exec-timeout' } ,
402+ aborts : 'while-it-blocks' ,
403+ expected : 'request-canceled' ,
404+ execs : 3 ,
405+ clockMs : COLD_TOOLCHAIN_PROBE_TIMEOUT_MS ,
406+ } ,
407+ {
408+ // 30 s plus a 15 s retry spends the whole 45 s ceiling on the first probe. The two
409+ // xcrun probes never ran, so the verdict is the budget, not an unreadable toolchain:
410+ // an `xcode-select` hint here would point at the wrong thing.
411+ label : 'never aborted, the first probe and its retry spend the whole budget' ,
412+ failing : { at : 'every' , as : 'exec-timeout' } ,
413+ aborts : 'never' ,
414+ expected : 'budget-exhausted' ,
415+ execs : 2 ,
416+ clockMs : 45_000 ,
417+ } ,
418+ {
419+ label : 'never aborted, the first probe times out and its retry recovers' ,
420+ failing : { at : 'first' , as : 'exec-timeout' } ,
421+ aborts : 'never' ,
422+ expected : 'fingerprint' ,
423+ execs : 4 ,
424+ clockMs : COLD_TOOLCHAIN_PROBE_TIMEOUT_MS ,
425+ } ,
426+ ] ;
427+
428+ test . each ( CANCELLATION_CASES ) ( 'cancellation matrix: $label' , ( testCase ) => {
429+ const clock = installFakeToolchainClock ( ) ;
430+ const request = new AbortController ( ) ;
431+ if ( testCase . fingerprintCache === 'warm' ) {
432+ runCmdSync . mockImplementation ( appleToolchainProbeResult ) ;
433+ resolveExpectedRunnerCacheMetadata ( IOS_SIMULATOR ) ;
434+ }
435+ if ( testCase . aborts === 'before-any-probe' ) request . abort ( ) ;
436+
437+ let execs = 0 ;
438+ runCmdSync . mockImplementation ( ( command : string , args : string [ ] , options : ExecOptions ) => {
439+ execs += 1 ;
440+ if ( ! failsOnExec ( testCase , execs ) ) return appleToolchainProbeResult ( command , args ) ;
441+ if ( testCase . aborts === 'while-it-blocks' ) request . abort ( ) ;
442+ const failure =
443+ testCase . failing ?. as === 'exec-timeout'
444+ ? blockForWholeTimeout ( clock , command , args , options )
445+ : // No `timeoutMs` detail: the tool failed on its own, so nothing retries it.
446+ new AppError ( 'COMMAND_FAILED' , `${ command } : unexpected error` , { cmd : command , args } ) ;
447+ if ( testCase . aborts === 'as-it-unwinds' ) request . abort ( ) ;
448+ throw failure ;
449+ } ) ;
450+
451+ const readFingerprint = ( ) =>
452+ resolveExpectedRunnerCacheMetadata (
453+ IOS_SIMULATOR ,
454+ undefined ,
455+ createRunnerPhaseBudget ( undefined , request . signal ) ,
456+ ) ;
457+
458+ if ( testCase . expected === 'fingerprint' ) {
459+ const metadata = readFingerprint ( ) ;
460+ assert . equal ( metadata . xcodeVersion , '26.2' , testCase . label ) ;
461+ assert . equal ( metadata . xcodeBuildVersion , '17C52' , testCase . label ) ;
462+ } else {
463+ assert . throws ( readFingerprint , ( error : unknown ) => {
464+ assert . ok (
465+ testCase . expected === 'request-canceled'
466+ ? isRequestCanceledError ( error )
467+ : expectRunnerPhaseBudgetExhausted ( error ) ,
468+ `${ testCase . label } : expected ${ testCase . expected } , got ${ String ( error ) } ` ,
469+ ) ;
470+ return true ;
471+ } ) ;
472+ }
473+ assert . equal ( execs , testCase . execs , `${ testCase . label } : exec count` ) ;
474+ assert . equal ( clock . nowMs , testCase . clockMs , `${ testCase . label } : wall clock spent` ) ;
475+ } ) ;
476+
477+ /** Which exec a row's failing probe is: the first, the last of the three, or all of them. */
478+ function failsOnExec ( testCase : ToolchainProbeCancellationCase , exec : number ) : boolean {
479+ switch ( testCase . failing ?. at ) {
480+ case 'first' :
481+ return exec === 1 ;
482+ case 'final' :
483+ return exec === 3 ;
484+ case 'every' :
485+ return true ;
486+ default :
487+ return false ;
488+ }
489+ }
412490} ) ;
413491
414492/** The error a runner phase raises when a step is reached with nothing left to spend. */
0 commit comments