Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .nx/version-plans/version-plan-1784624669908.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
__default__: patch
---

iOS Harness runs now end cleanly after XCTest-agent shutdown instead of lingering for the shutdown timeout.
24 changes: 24 additions & 0 deletions packages/platform-ios/src/__tests__/xctest-agent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,30 @@ describe('xctest-agent orchestration', () => {
expect(mocks.kill).not.toHaveBeenCalled();
});

it('clears the shutdown timer after graceful shutdown', async () => {
vi.useFakeTimers();

try {
const controller = createXCTestAgentController({
port: 49154,
shutdownTimeoutMs: 30_000,
target: {
kind: 'simulator',
id: 'sim-timeout',
},
});

await controller.ensureStarted();
await controller.dispose();

expect(mocks.shutdown).toHaveBeenCalledTimes(1);
expect(mocks.kill).not.toHaveBeenCalled();
expect(vi.getTimerCount()).toBe(0);
} finally {
vi.useRealTimers();
}
});

it('kills the agent process when graceful shutdown times out', async () => {
mocks.shutdown.mockResolvedValue(undefined);

Expand Down
64 changes: 29 additions & 35 deletions packages/platform-ios/src/xctest-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -762,51 +762,45 @@ const waitForAgentReady = async (options: {
);
};

const waitForShutdown = async (options: {
processTask: Promise<void> | null;
shutdownTimeoutMs: number;
}): Promise<boolean> => {
if (!options.processTask) {
return true;
}

const timedOut = Symbol('timedOut');
const result = await Promise.race([
options.processTask.then(() => undefined),
delay(options.shutdownTimeoutMs).then(() => timedOut),
]);

return result !== timedOut;
};

const waitForGracefulShutdown = async (options: {
client: ReturnType<typeof createXCTestAgentClient>;
processTask: Promise<void> | null;
shutdownTimeoutMs: number;
}): Promise<{ didStop: boolean; requestError: unknown | null }> => {
let requestError: unknown | null = null;
let shutdownTimer: ReturnType<typeof setTimeout> | null = null;
const timedOut = Symbol('timedOut');
const timeoutTask = new Promise<typeof timedOut>((resolve) => {
shutdownTimer = setTimeout(
() => resolve(timedOut),
options.shutdownTimeoutMs
);
});

const result = await Promise.race([
(async () => {
try {
await options.client.shutdown();
} catch (error) {
requestError = error;
}
try {
const result = await Promise.race([
(async () => {
try {
await options.client.shutdown();
} catch (error) {
requestError = error;
}

return await waitForShutdown({
processTask: options.processTask,
shutdownTimeoutMs: options.shutdownTimeoutMs,
});
})(),
delay(options.shutdownTimeoutMs).then(() => timedOut),
]);
await options.processTask;
return true;
})(),
timeoutTask,
]);

return {
didStop: result !== timedOut && result === true,
requestError,
};
return {
didStop: result !== timedOut,
requestError,
};
} finally {
if (shutdownTimer !== null) {
clearTimeout(shutdownTimer);
}
}
};

const waitForChildProcessExit = async (subprocess: Subprocess) => {
Expand Down
Loading