@@ -70,8 +70,23 @@ vi.mock('../runner-xctestrun.ts', async () => {
7070
7171import { createRequestCanceledError , isRequestCanceledError } from '@agent-device/kernel/errors' ;
7272import { abortAllIosRunnerSessions , readRunnerSessionLiveness } from '../runner-session.ts' ;
73+ import { RUNNER_STARTUP_TIMEOUT_MS } from '../runner-startup-transport.ts' ;
7374import type { RunnerLease } from '../runner-lease.ts' ;
7475import { executeRunnerCommand , prepareLocalIosRunner } from '../runner-lifecycle.ts' ;
76+ import { captureDiagnostics } from './runner-session-fixtures.ts' ;
77+
78+ const SNAPSHOT = { command : 'snapshot' , appBundleId : 'com.example.demo' } as const ;
79+
80+ function callerDeadline ( ) : DOMException {
81+ return new DOMException ( 'Wait deadline exceeded' , 'TimeoutError' ) ;
82+ }
83+
84+ function readinessCanceled ( error : unknown ) : boolean {
85+ return (
86+ isRequestCanceledError ( error ) &&
87+ ( error as { details ?: { readinessPhase ?: string } } ) . details ?. readinessPhase === 'runner-start'
88+ ) ;
89+ }
7590
7691// Root-registry writers (`request/cancel.ts`) are not visible to the package;
7792// this reproduces the same canceled-set/AbortController-map model locally and
@@ -182,45 +197,139 @@ test('direct command cancellation reaches runner launch without a registered req
182197/**
183198 * A `wait` poll bounds each attempt with a `TimeoutError` deadline. When that deadline lands while the
184199 * runner is still starting, the start it interrupts is the one the retry needs: the launch must not be
185- * killed and the session must stay registered as starting, so the next request joins it (#2894).
200+ * killed and the session must stay registered as starting, so the next request joins it and spends
201+ * what is left of the launch budget, not a fresh one (#2894).
186202 */
187203test ( 'a caller deadline during runner start leaves the starting runner for the next request' , async ( ) => {
204+ vi . useFakeTimers ( { toFake : [ 'Date' ] } ) ;
205+ try {
206+ const controller = new AbortController ( ) ;
207+ const device = { ...IOS_SIMULATOR , id : 'runner-caller-deadline-sim' } ;
208+ let launchSignal : AbortSignal | undefined ;
209+ mockRunCmdBackground . mockImplementationOnce ( ( _cmd , _args , options ) => {
210+ launchSignal = options ?. signal ;
211+ return makeBackgroundRunner ( 4545 ) ;
212+ } ) ;
213+ mockWaitForRunner . mockImplementationOnce ( async ( ) => {
214+ controller . abort ( callerDeadline ( ) ) ;
215+ throw createRequestCanceledError ( ) ;
216+ } ) ;
217+
218+ await assert . rejects (
219+ executeRunnerCommand ( device , SNAPSHOT , {
220+ signal : controller . signal ,
221+ logPath : '/tmp/runner.log' ,
222+ } ) ,
223+ readinessCanceled ,
224+ ) ;
225+ assert . equal ( launchSignal ?. aborted , false , 'the launch outlives the caller deadline' ) ;
226+ assert . equal ( readRunnerSessionLiveness ( device . id ) ?. liveness , 'starting' ) ;
227+ assert . deepEqual ( readRetainedLeaseDeviceIds ( ) , [ device . id ] ) ;
228+
229+ vi . setSystemTime ( Date . now ( ) + 10_000 ) ;
230+ await executeRunnerCommand ( device , SNAPSHOT , { logPath : '/tmp/runner.log' } ) ;
231+
232+ assert . equal ( mockRunCmdBackground . mock . calls . length , 1 , 'the retry did not launch again' ) ;
233+ assert . equal (
234+ mockWaitForRunner . mock . calls [ 1 ] ?. [ 4 ] ,
235+ RUNNER_STARTUP_TIMEOUT_MS - 10_000 ,
236+ 'the joiner measures readiness from the launch, not from its own arrival' ,
237+ ) ;
238+ assert . equal ( readRunnerSessionLiveness ( device . id ) ?. liveness , 'ready' ) ;
239+ } finally {
240+ vi . useRealTimers ( ) ;
241+ }
242+ } ) ;
243+
244+ /**
245+ * A runner whose process stays up but never answers must not be joined forever: once the launch
246+ * budget recorded on the session is spent, the next request retires it and launches again, however
247+ * the earlier joiners' waits ended (#2894).
248+ */
249+ test ( 'a request joining a start past its launch budget retires the runner and launches again' , async ( ) => {
250+ vi . useFakeTimers ( { toFake : [ 'Date' ] } ) ;
251+ try {
252+ const first = new AbortController ( ) ;
253+ const device = { ...IOS_SIMULATOR , id : 'runner-launch-budget-sim' } ;
254+ mockRunCmdBackground
255+ . mockReturnValueOnce ( makeBackgroundRunner ( 4646 ) )
256+ . mockReturnValueOnce ( makeBackgroundRunner ( 4747 ) ) ;
257+ mockWaitForRunner . mockImplementationOnce ( async ( ) => {
258+ first . abort ( callerDeadline ( ) ) ;
259+ throw createRequestCanceledError ( ) ;
260+ } ) ;
261+
262+ await assert . rejects (
263+ executeRunnerCommand ( device , SNAPSHOT , { signal : first . signal , logPath : '/tmp/runner.log' } ) ,
264+ readinessCanceled ,
265+ ) ;
266+ const hung = readRunnerSessionLiveness ( device . id ) ;
267+ assert . equal ( hung ?. liveness , 'starting' ) ;
268+
269+ vi . setSystemTime ( Date . now ( ) + RUNNER_STARTUP_TIMEOUT_MS + 1 ) ;
270+ const second = new AbortController ( ) ;
271+ const diagnostics = await captureDiagnostics ( async ( ) => {
272+ await executeRunnerCommand ( device , SNAPSHOT , {
273+ signal : second . signal ,
274+ logPath : '/tmp/runner.log' ,
275+ } ) ;
276+ } ) ;
277+
278+ assert . match ( diagnostics , / r u n n e r _ l a u n c h _ b u d g e t _ e x h a u s t e d / ) ;
279+ assert . equal ( mockRunCmdBackground . mock . calls . length , 2 , 'the hung runner was replaced' ) ;
280+ const relaunched = readRunnerSessionLiveness ( device . id ) ;
281+ assert . equal ( relaunched ?. liveness , 'ready' ) ;
282+ assert . notEqual ( relaunched ?. sessionId , hung ?. sessionId ) ;
283+ assert . deepEqual ( readRetainedLeaseDeviceIds ( ) , [ device . id ] ) ;
284+ } finally {
285+ vi . useRealTimers ( ) ;
286+ }
287+ } ) ;
288+
289+ /**
290+ * The start runs detached under the session lock. A caller whose deadline lands during the cold
291+ * xctestrun build leaves on time with the readiness verdict, the build is neither killed nor
292+ * repeated, and the next request queues behind it and joins the session it registers (#2894).
293+ */
294+ test ( 'a caller deadline during the xctestrun build leaves on time and the next request joins that start' , async ( ) => {
188295 const controller = new AbortController ( ) ;
189- const device = { ...IOS_SIMULATOR , id : 'runner-caller-deadline-sim' } ;
190- mockRunCmdBackground . mockImplementationOnce ( ( _cmd , _args , options ) => {
191- controller . abort ( new DOMException ( 'Wait deadline exceeded' , 'TimeoutError' ) ) ;
192- assert . equal ( options ?. signal ?. aborted , false , 'the launch outlives the caller deadline' ) ;
193- return makeBackgroundRunner ( 4545 ) ;
296+ const device = { ...IOS_SIMULATOR , id : 'runner-build-deadline-sim' } ;
297+ let releaseBuild : ( ) => void = ( ) => { } ;
298+ const buildReleased = new Promise < void > ( ( resolve ) => {
299+ releaseBuild = resolve ;
194300 } ) ;
195- mockWaitForRunner . mockImplementationOnce ( async ( ) => {
196- throw createRequestCanceledError ( ) ;
301+ let buildSignal : AbortSignal | undefined ;
302+ mockEnsureXctestrunArtifact . mockImplementationOnce ( async ( _device , options ) => {
303+ buildSignal = options . budget ?. signal ;
304+ controller . abort ( callerDeadline ( ) ) ;
305+ await buildReleased ;
306+ return {
307+ xctestrunPath : '/tmp/base-runner.xctestrun' ,
308+ derived : '/tmp/derived' ,
309+ cache : 'miss' ,
310+ artifact : 'rebuilt' ,
311+ buildMs : 12 ,
312+ xctestrunPathSource : 'build' ,
313+ } ;
197314 } ) ;
198315
199316 await assert . rejects (
200- executeRunnerCommand (
201- device ,
202- { command : 'snapshot' , appBundleId : 'com.example.demo' } ,
203- { signal : controller . signal , logPath : '/tmp/runner.log' } ,
204- ) ,
205- ( error : unknown ) =>
206- isRequestCanceledError ( error ) &&
207- ( error as { details ?: { readinessPhase ?: string } } ) . details ?. readinessPhase ===
208- 'runner-start' ,
317+ executeRunnerCommand ( device , SNAPSHOT , {
318+ signal : controller . signal ,
319+ logPath : '/tmp/runner.log' ,
320+ } ) ,
321+ readinessCanceled ,
209322 ) ;
210- assert . equal ( readRunnerSessionLiveness ( device . id ) ?. liveness , 'starting' ) ;
211- assert . deepEqual ( readRetainedLeaseDeviceIds ( ) , [ device . id ] ) ;
323+ assert . equal ( mockRunCmdBackground . mock . calls . length , 0 , 'the caller left before the launch' ) ;
324+ assert . equal ( buildSignal ?. aborted , false , 'the build outlives the caller deadline' ) ;
325+ assert . equal ( readRunnerSessionLiveness ( device . id ) , null ) ;
212326
213- await executeRunnerCommand (
214- device ,
215- { command : 'snapshot' , appBundleId : 'com.example.demo' } ,
216- { logPath : '/tmp/runner.log' } ,
217- ) ;
327+ const joined = executeRunnerCommand ( device , SNAPSHOT , { logPath : '/tmp/runner.log' } ) ;
328+ releaseBuild ( ) ;
329+ await joined ;
218330
219- assert . equal (
220- mockRunCmdBackground . mock . calls . length ,
221- 1 ,
222- 'the retry did not launch a second runner' ,
223- ) ;
331+ assert . equal ( mockEnsureXctestrunArtifact . mock . calls . length , 1 , 'the joiner did not build again' ) ;
332+ assert . equal ( mockRunCmdBackground . mock . calls . length , 1 , 'the joiner did not launch again' ) ;
224333 assert . equal ( readRunnerSessionLiveness ( device . id ) ?. liveness , 'ready' ) ;
225334} ) ;
226335
0 commit comments