Skip to content

Commit 38527ee

Browse files
committed
test(apple-runner): split bad-cache recovery out of the retry ratchet file (#3220)
The fence work grew runner-command-retry.test.ts past its merge-base length, which the test-file size ratchet refuses. The two bad-cache recovery tests mirror the prepare artifact decision, so they move to runner-lifecycle-prepare-artifact.test.ts, which already carries that harness.
1 parent 7ca8147 commit 38527ee

2 files changed

Lines changed: 175 additions & 169 deletions

File tree

‎packages/platform-apple/src/runner/__tests__/runner-command-retry.test.ts‎

Lines changed: 0 additions & 169 deletions
Original file line numberDiff line numberDiff line change
@@ -68,78 +68,6 @@ beforeEach(() => {
6868
});
6969
});
7070

71-
test('prepareIosRunner marks a bad restored artifact and rebuilds once after health failure', async () => {
72-
const fixtures = makeBadCacheRecoveryFixtures();
73-
74-
mockEnsureRunnerSession
75-
.mockResolvedValueOnce(fixtures.restoredSession)
76-
.mockResolvedValueOnce(fixtures.rebuiltSession);
77-
mockExecuteRunnerCommandWithSession
78-
.mockRejectedValueOnce(runnerConnectFailure('runner_connect_refused'))
79-
.mockResolvedValueOnce({ uptimeMs: 42 });
80-
81-
const result = await prepareIosRunner(IOS_SIMULATOR, {
82-
healthTimeoutMs: 90_000,
83-
buildTimeoutMs: 300_000,
84-
});
85-
86-
assertRecoveredPrepareResult(result);
87-
assertBadCacheRecoverySideEffects(fixtures);
88-
assertRecoveredPrepareDiagnostics();
89-
});
90-
91-
test('prepareIosRunner invalidates rebuilt sessions when bad-cache recovery health fails', async () => {
92-
const restoredArtifact = makeRunnerArtifact({
93-
xctestrunPath: '/tmp/restored.xctestrun',
94-
cache: 'exact',
95-
artifact: 'valid',
96-
});
97-
const rebuiltArtifact = makeRunnerArtifact({
98-
xctestrunPath: '/tmp/rebuilt.xctestrun',
99-
cache: 'miss',
100-
artifact: 'rebuilt',
101-
});
102-
const restoredSession = makeRunnerSession({
103-
port: 8100,
104-
xctestrunPath: restoredArtifact.xctestrunPath,
105-
xctestrunArtifact: restoredArtifact,
106-
});
107-
const rebuiltSession = makeRunnerSession({
108-
port: 8101,
109-
xctestrunPath: rebuiltArtifact.xctestrunPath,
110-
xctestrunArtifact: rebuiltArtifact,
111-
});
112-
113-
mockEnsureRunnerSession
114-
.mockResolvedValueOnce(restoredSession)
115-
.mockResolvedValueOnce(rebuiltSession);
116-
mockExecuteRunnerCommandWithSession
117-
.mockRejectedValueOnce(runnerConnectFailure('runner_endpoint_probe_exhausted'))
118-
.mockRejectedValueOnce(new AppError('COMMAND_FAILED', 'Runner health timed out'));
119-
120-
await assert.rejects(
121-
() => prepareIosRunner(IOS_SIMULATOR, { healthTimeoutMs: 90_000 }),
122-
(error: unknown) => {
123-
assert.ok(error instanceof AppError);
124-
assert.equal(error.message, 'artifact restored but runner did not connect');
125-
assert.equal(error.details?.restoredFailureReason, 'Runner endpoint probe failed');
126-
assert.equal(error.details?.xctestrunPath, '/tmp/rebuilt.xctestrun');
127-
assert.equal(error.details?.artifact, 'rebuilt');
128-
assert.equal(error.details?.cache, 'miss');
129-
return true;
130-
},
131-
);
132-
133-
assert.deepEqual(mockInvalidateRunnerSession.mock.calls, [
134-
[restoredSession, 'prepare_cached_runner_health_failed'],
135-
[rebuiltSession, 'prepare_rebuilt_runner_health_failed'],
136-
]);
137-
assert.deepEqual(mockMarkRunnerXctestrunArtifactBadForRun.mock.calls[0], [
138-
restoredArtifact,
139-
'Runner endpoint probe failed',
140-
]);
141-
});
142-
14371
test('prepareIosRunner retries a fresh launch session when the health check cannot connect', async () => {
14472
const stuckSession = makeRunnerSession({ port: 8100 });
14573
const relaunchedSession = makeRunnerSession({ port: 8101 });
@@ -1008,103 +936,6 @@ test('sequence invalidates the session when the status probe fails', async () =>
1008936
});
1009937
});
1010938

1011-
function makeBadCacheRecoveryFixtures() {
1012-
const restoredArtifact = makeRunnerArtifact({
1013-
xctestrunPath: '/tmp/restored.xctestrun',
1014-
cache: 'exact',
1015-
artifact: 'valid',
1016-
});
1017-
const rebuiltArtifact = makeRunnerArtifact({
1018-
xctestrunPath: '/tmp/rebuilt.xctestrun',
1019-
cache: 'miss',
1020-
artifact: 'rebuilt',
1021-
buildMs: 123,
1022-
});
1023-
const restoredSession = makeRunnerSession({
1024-
port: 8100,
1025-
xctestrunPath: restoredArtifact.xctestrunPath,
1026-
xctestrunArtifact: restoredArtifact,
1027-
});
1028-
const rebuiltSession = makeRunnerSession({
1029-
port: 8101,
1030-
xctestrunPath: rebuiltArtifact.xctestrunPath,
1031-
xctestrunArtifact: rebuiltArtifact,
1032-
});
1033-
1034-
return { restoredArtifact, restoredSession, rebuiltSession };
1035-
}
1036-
1037-
function assertRecoveredPrepareResult(result: Awaited<ReturnType<typeof prepareIosRunner>>): void {
1038-
assert.deepEqual(result, {
1039-
runner: { uptimeMs: 42 },
1040-
cache: 'miss',
1041-
artifact: 'rebuilt',
1042-
buildMs: 123,
1043-
connectMs: result.connectMs,
1044-
healthCheckMs: result.healthCheckMs,
1045-
xctestrunPath: '/tmp/rebuilt.xctestrun',
1046-
recoveryReason: 'Runner did not accept connection',
1047-
});
1048-
assert.equal(result.failureReason, undefined);
1049-
assert.equal(result.connectMs >= 0, true);
1050-
assert.equal(result.healthCheckMs >= 0, true);
1051-
}
1052-
1053-
function assertBadCacheRecoverySideEffects(
1054-
fixtures: ReturnType<typeof makeBadCacheRecoveryFixtures>,
1055-
): void {
1056-
assert.deepEqual(mockInvalidateRunnerSession.mock.calls[0], [
1057-
fixtures.restoredSession,
1058-
'prepare_cached_runner_health_failed',
1059-
]);
1060-
assert.deepEqual(mockMarkRunnerXctestrunArtifactBadForRun.mock.calls[0], [
1061-
fixtures.restoredArtifact,
1062-
'Runner did not accept connection',
1063-
]);
1064-
const firstCallOptions = mockEnsureRunnerSession.mock.calls[0]?.[1] as
1065-
| { startAdmission?: unknown }
1066-
| undefined;
1067-
const retryOptions = mockEnsureRunnerSession.mock.calls[1]?.[1] as
1068-
| Record<string, unknown>
1069-
| undefined;
1070-
assert.deepEqual(retryOptions, {
1071-
healthTimeoutMs: 90_000,
1072-
buildTimeoutMs: 300_000,
1073-
cleanStaleBundles: true,
1074-
forceRunnerXctestrunRebuild: true,
1075-
startAdmission: firstCallOptions?.startAdmission,
1076-
});
1077-
// Both attempts of the prepare loop share one admission token: the retry
1078-
// is the replacement build for the first attempt, not a newcomer.
1079-
assert.ok(firstCallOptions?.startAdmission);
1080-
assert.equal(mockExecuteRunnerCommandWithSession.mock.calls.length, 2);
1081-
assert.equal(mockExecuteRunnerCommandWithSession.mock.calls[0]?.[2].command, 'uptime');
1082-
assert.equal(mockExecuteRunnerCommandWithSession.mock.calls[0]?.[4], 90_000);
1083-
assert.equal(mockExecuteRunnerCommandWithSession.mock.calls[1]?.[1], fixtures.rebuiltSession);
1084-
}
1085-
1086-
function assertRecoveredPrepareDiagnostics(): void {
1087-
assert.ok(
1088-
mockEmitDiagnostic.mock.calls.some(
1089-
([event]) => event.phase === 'ios_runner_prepare_bad_cache_recovered',
1090-
),
1091-
);
1092-
const prepareDiagnostic = mockEmitDiagnostic.mock.calls.find(
1093-
([event]) => event.phase === 'apple_runner_prepare',
1094-
)?.[0];
1095-
assert.ok(prepareDiagnostic);
1096-
assert.equal(prepareDiagnostic.level, 'info');
1097-
assert.equal(prepareDiagnostic.data?.cache, 'miss');
1098-
assert.equal(prepareDiagnostic.data?.artifact, 'rebuilt');
1099-
assert.equal(prepareDiagnostic.data?.xctestrunPath, '/tmp/rebuilt.xctestrun');
1100-
assert.equal(prepareDiagnostic.data?.recoveryReason, 'Runner did not accept connection');
1101-
assert.equal(prepareDiagnostic.data?.failureReason, undefined);
1102-
assert.deepEqual(prepareDiagnostic.data?.timingContainment, {
1103-
connectMs: ['buildMs'],
1104-
healthCheckMs: [],
1105-
});
1106-
}
1107-
1108939
function assertDiagnosticDecision(expected: {
1109940
decision: 'skipped' | 'retained';
1110941
reason: string;

‎packages/platform-apple/src/runner/__tests__/runner-lifecycle-prepare-artifact.test.ts‎

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
makeRunnerArtifact,
99
createTestRequestCancellation,
1010
makeRunnerSession,
11+
runnerConnectFailure,
1112
} from './runner-session-fixtures.ts';
1213

1314
const {
@@ -110,3 +111,177 @@ test('a prepare deadline spent during boot keeps the restored artifact and retri
110111
vi.useRealTimers();
111112
}
112113
});
114+
115+
// The bad-cache recovery decision: a restored artifact whose runner refuses the connection is
116+
// indicted, wiped for the run, and replaced by one forced rebuild — under one admission token
117+
// owned by the prepare loop, so the rebuild is the retry a fence governs rather than a fresh
118+
// start that slips past it (#3220).
119+
120+
test('prepareIosRunner marks a bad restored artifact and rebuilds once after health failure', async () => {
121+
const fixtures = makeBadCacheRecoveryFixtures();
122+
123+
mockEnsureRunnerSession
124+
.mockResolvedValueOnce(fixtures.restoredSession)
125+
.mockResolvedValueOnce(fixtures.rebuiltSession);
126+
mockExecuteRunnerCommandWithSession
127+
.mockRejectedValueOnce(runnerConnectFailure('runner_connect_refused'))
128+
.mockResolvedValueOnce({ uptimeMs: 42 });
129+
130+
const result = await prepareIosRunner(IOS_SIMULATOR, {
131+
healthTimeoutMs: 90_000,
132+
buildTimeoutMs: 300_000,
133+
});
134+
135+
assertRecoveredPrepareResult(result);
136+
assertBadCacheRecoverySideEffects(fixtures);
137+
assertRecoveredPrepareDiagnostics();
138+
});
139+
140+
test('prepareIosRunner invalidates rebuilt sessions when bad-cache recovery health fails', async () => {
141+
const restoredArtifact = makeRunnerArtifact({
142+
xctestrunPath: '/tmp/restored.xctestrun',
143+
cache: 'exact',
144+
artifact: 'valid',
145+
});
146+
const rebuiltArtifact = makeRunnerArtifact({
147+
xctestrunPath: '/tmp/rebuilt.xctestrun',
148+
cache: 'miss',
149+
artifact: 'rebuilt',
150+
});
151+
const restoredSession = makeRunnerSession({
152+
port: 8100,
153+
xctestrunPath: restoredArtifact.xctestrunPath,
154+
xctestrunArtifact: restoredArtifact,
155+
});
156+
const rebuiltSession = makeRunnerSession({
157+
port: 8101,
158+
xctestrunPath: rebuiltArtifact.xctestrunPath,
159+
xctestrunArtifact: rebuiltArtifact,
160+
});
161+
162+
mockEnsureRunnerSession
163+
.mockResolvedValueOnce(restoredSession)
164+
.mockResolvedValueOnce(rebuiltSession);
165+
mockExecuteRunnerCommandWithSession
166+
.mockRejectedValueOnce(runnerConnectFailure('runner_endpoint_probe_exhausted'))
167+
.mockRejectedValueOnce(new AppError('COMMAND_FAILED', 'Runner health timed out'));
168+
169+
await assert.rejects(
170+
() => prepareIosRunner(IOS_SIMULATOR, { healthTimeoutMs: 90_000 }),
171+
(error: unknown) => {
172+
assert.ok(error instanceof AppError);
173+
assert.equal(error.message, 'artifact restored but runner did not connect');
174+
assert.equal(error.details?.restoredFailureReason, 'Runner endpoint probe failed');
175+
assert.equal(error.details?.xctestrunPath, '/tmp/rebuilt.xctestrun');
176+
assert.equal(error.details?.artifact, 'rebuilt');
177+
assert.equal(error.details?.cache, 'miss');
178+
return true;
179+
},
180+
);
181+
182+
assert.deepEqual(mockInvalidateRunnerSession.mock.calls, [
183+
[restoredSession, 'prepare_cached_runner_health_failed'],
184+
[rebuiltSession, 'prepare_rebuilt_runner_health_failed'],
185+
]);
186+
assert.deepEqual(mockMarkRunnerXctestrunArtifactBadForRun.mock.calls[0], [
187+
restoredArtifact,
188+
'Runner endpoint probe failed',
189+
]);
190+
});
191+
192+
function makeBadCacheRecoveryFixtures() {
193+
const restoredArtifact = makeRunnerArtifact({
194+
xctestrunPath: '/tmp/restored.xctestrun',
195+
cache: 'exact',
196+
artifact: 'valid',
197+
});
198+
const rebuiltArtifact = makeRunnerArtifact({
199+
xctestrunPath: '/tmp/rebuilt.xctestrun',
200+
cache: 'miss',
201+
artifact: 'rebuilt',
202+
buildMs: 123,
203+
});
204+
const restoredSession = makeRunnerSession({
205+
port: 8100,
206+
xctestrunPath: restoredArtifact.xctestrunPath,
207+
xctestrunArtifact: restoredArtifact,
208+
});
209+
const rebuiltSession = makeRunnerSession({
210+
port: 8101,
211+
xctestrunPath: rebuiltArtifact.xctestrunPath,
212+
xctestrunArtifact: rebuiltArtifact,
213+
});
214+
215+
return { restoredArtifact, restoredSession, rebuiltSession };
216+
}
217+
218+
function assertRecoveredPrepareResult(result: Awaited<ReturnType<typeof prepareIosRunner>>): void {
219+
assert.deepEqual(result, {
220+
runner: { uptimeMs: 42 },
221+
cache: 'miss',
222+
artifact: 'rebuilt',
223+
buildMs: 123,
224+
connectMs: result.connectMs,
225+
healthCheckMs: result.healthCheckMs,
226+
xctestrunPath: '/tmp/rebuilt.xctestrun',
227+
recoveryReason: 'Runner did not accept connection',
228+
});
229+
assert.equal(result.failureReason, undefined);
230+
assert.equal(result.connectMs >= 0, true);
231+
assert.equal(result.healthCheckMs >= 0, true);
232+
}
233+
234+
function assertBadCacheRecoverySideEffects(
235+
fixtures: ReturnType<typeof makeBadCacheRecoveryFixtures>,
236+
): void {
237+
assert.deepEqual(mockInvalidateRunnerSession.mock.calls[0], [
238+
fixtures.restoredSession,
239+
'prepare_cached_runner_health_failed',
240+
]);
241+
assert.deepEqual(mockMarkRunnerXctestrunArtifactBadForRun.mock.calls[0], [
242+
fixtures.restoredArtifact,
243+
'Runner did not accept connection',
244+
]);
245+
const firstCallOptions = mockEnsureRunnerSession.mock.calls[0]?.[1] as
246+
| { startAdmission?: unknown }
247+
| undefined;
248+
const retryOptions = mockEnsureRunnerSession.mock.calls[1]?.[1] as
249+
| Record<string, unknown>
250+
| undefined;
251+
assert.deepEqual(retryOptions, {
252+
healthTimeoutMs: 90_000,
253+
buildTimeoutMs: 300_000,
254+
cleanStaleBundles: true,
255+
forceRunnerXctestrunRebuild: true,
256+
startAdmission: firstCallOptions?.startAdmission,
257+
});
258+
// Both attempts of the prepare loop share one admission token: the retry
259+
// is the replacement build for the first attempt, not a newcomer.
260+
assert.ok(firstCallOptions?.startAdmission);
261+
assert.equal(mockExecuteRunnerCommandWithSession.mock.calls.length, 2);
262+
assert.equal(mockExecuteRunnerCommandWithSession.mock.calls[0]?.[2].command, 'uptime');
263+
assert.equal(mockExecuteRunnerCommandWithSession.mock.calls[0]?.[4], 90_000);
264+
assert.equal(mockExecuteRunnerCommandWithSession.mock.calls[1]?.[1], fixtures.rebuiltSession);
265+
}
266+
267+
function assertRecoveredPrepareDiagnostics(): void {
268+
assert.ok(
269+
mockEmitDiagnostic.mock.calls.some(
270+
([event]) => event.phase === 'ios_runner_prepare_bad_cache_recovered',
271+
),
272+
);
273+
const prepareDiagnostic = mockEmitDiagnostic.mock.calls.find(
274+
([event]) => event.phase === 'apple_runner_prepare',
275+
)?.[0];
276+
assert.ok(prepareDiagnostic);
277+
assert.equal(prepareDiagnostic.level, 'info');
278+
assert.equal(prepareDiagnostic.data?.cache, 'miss');
279+
assert.equal(prepareDiagnostic.data?.artifact, 'rebuilt');
280+
assert.equal(prepareDiagnostic.data?.xctestrunPath, '/tmp/rebuilt.xctestrun');
281+
assert.equal(prepareDiagnostic.data?.recoveryReason, 'Runner did not accept connection');
282+
assert.equal(prepareDiagnostic.data?.failureReason, undefined);
283+
assert.deepEqual(prepareDiagnostic.data?.timingContainment, {
284+
connectMs: ['buildMs'],
285+
healthCheckMs: [],
286+
});
287+
}

0 commit comments

Comments
 (0)