Skip to content
Merged
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
51 changes: 51 additions & 0 deletions e2e/slow-ios-window.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,5 +258,56 @@ describe.skipIf(process.platform !== "darwin")(
}
},
);
it(
"a leased simulator on Xcode's set takes the lease's label as its name",
{ timeout: 600_000 },
async () => {
const { env } = await withHttpDaemon((httpPort) => ({
configOverrides: {
drivers: { ios: { deviceSet: "default" } },
http: { enabled: true, host: "127.0.0.1", port: httpPort },
ios: { simulator: { headless: false } },
},
driver: "real",
}));
const nameOf = async (udid: string): Promise<string | undefined> => {
const listed = await execFileAsync("xcrun", ["simctl", "list", "devices", "-j"]);
const runtimes = (
JSON.parse(listed.stdout) as {
devices: Record<string, { name: string; udid: string }[]>;
}
).devices;
return Object.values(runtimes)
.flat()
.find((device) => device.udid === udid)?.name;
};
try {
const labelled = await env.cli(
[
"lease",
"--platform",
"ios",
"--agent-id",
"ios-label",
"--label",
"feature-x",
"--detach",
],
{ timeout: DEVICE_READY_MS },
);
expect(labelled.code, `lease failed: ${labelled.stdout} ${labelled.stderr}`).toBe(0);
const grant = labelled.json as {
device: { driverDeviceId: string };
lease: { id: string };
};
expect(await nameOf(grant.device.driverDeviceId)).toBe("feature-x");
await env.cli(["release", grant.lease.id]).catch(() => undefined);
} finally {
await env
.cli(["nuke", "--delete-devices", "--yes"], { timeout: 180_000 })
.catch(() => undefined);
}
},
);
},
);
9 changes: 6 additions & 3 deletions e2e/warm-pool-reserve.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,9 @@ describe("warm pool reserve and requests that wait for a booting device", () =>
driverScript: { ios: IOS },
});
// why: the first lease's kind is learned, so the pool boots a spare while it is held (ADR 0069);
// the slow boot is set first so that spare is still booting when the five leases arrive.
await env.driverScript.merge({ ios: { latencyMs: { makeReady: 4_000 } } });
// the slow boot is set first so that spare is still booting when the five leases arrive, with
// room for a slow runner to reach the check before that boot ends and frees a slot.
await env.driverScript.merge({ ios: { latencyMs: { makeReady: 10_000 } } });
const warm = await lease(env, "warm", ["--timeout", "60s"]);
await waitFor(async () => (await makeReadyCalls(env)) === 2, {
label: "the warm pool's boot of the spare for the held lease has started",
Expand All @@ -130,14 +131,16 @@ describe("warm pool reserve and requests that wait for a booting device", () =>
await new Promise((resolve) => setTimeout(resolve, 1_500));
expect(await makeReadyCalls(env)).toBe(4);

// why: the check is made; later boots run at full speed.
await env.driverScript.merge({ ios: { latencyMs: { makeReady: 0 } } });
const granted = await Promise.all(grants);
await untilQuiet(env);

expect(new Set(granted.map((grant) => grant.device.id)).size).toBe(5);
// why: five held copies leave one spare ready, the seventh boot (ADR 0069).
expect(await makeReadyCalls(env)).toBe(7);
expect((await deviceStates(env)).filter((state) => state === "leased")).toHaveLength(5);
// why: the boots are still slow, so the spare may still be booting when the pool goes quiet.
// why: the spare may still be booting when the pool goes quiet.
await waitFor(
async () => (await deviceStates(env)).filter((state) => state === "ready").length === 1,
{ label: "the one spare is ready" },
Expand Down
3 changes: 3 additions & 0 deletions src/core/device-provisioner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ export interface ProvisionDeviceOptions {
readonly claim?: { readonly kind: "boot"; readonly owner?: string };
/** why: the categories a slim lease keeps running, handed to the driver at the boot before the grant (ADR 0066 §2). */
readonly except?: readonly string[];
/** why: the lease's label, handed to the driver at the boot before the grant so the device carries it from its first boot. */
readonly label?: string;
readonly onProgress?: (progress: ProvisionProgress) => void;
readonly reservation: CapacityReservation;
}
Expand Down Expand Up @@ -174,6 +176,7 @@ export class DeviceProvisioner {
device,
bootClaim,
options.except,
options.label,
);
if (ready === undefined)
throw new Error(`Registered device could not be made ready: ${device.id}`);
Expand Down
2 changes: 2 additions & 0 deletions src/core/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,8 @@ export interface Driver {
readonly purpose: "prepare" | "recover";
readonly mode: DeviceMode;
readonly except?: readonly string[];
/** why: the lease's label, given on a `prepare` boot made for a lease; a driver that can name its device by it does. */
readonly label?: string;
},
): Promise<DriverDevice>;
reclaim(
Expand Down
1 change: 1 addition & 0 deletions src/core/fake-driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,7 @@ export class FakeDriver implements Driver {
readonly purpose: "prepare" | "recover";
readonly mode: DeviceMode;
readonly except?: readonly string[];
readonly label?: string;
},
): Promise<DriverDevice> {
await this.#beforeCall("makeReady", device, options);
Expand Down
10 changes: 8 additions & 2 deletions src/core/managed-device-lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,9 @@ export class ManagedDeviceLifecycle {
target: VirtualDevice,
claim: DeviceOperationClaim,
except?: readonly string[],
label?: string,
): Promise<ReadyDeviceHandoff | undefined> {
return this.#makeReadyForLease(target, "shutdown", claim, except);
return this.#makeReadyForLease(target, "shutdown", claim, except, label);
}

/**
Expand Down Expand Up @@ -107,6 +108,7 @@ export class ManagedDeviceLifecycle {
target: VirtualDevice,
claim: DeviceOperationClaim,
except: readonly string[],
label?: string,
): Promise<ReadyDeviceHandoff | undefined> {
const claimed = await this.#claim(target, ["ready"], "boot", claim);
if (claimed === undefined) return undefined;
Expand All @@ -116,6 +118,7 @@ export class ManagedDeviceLifecycle {
await driver.shutdown(toDriverDevice(claimed.device));
ready = await driver.makeReady(toDriverDevice(claimed.device), {
except,
...(label === undefined ? {} : { label }),
mode: specMode(claimed.device.spec),
purpose: "prepare",
});
Expand Down Expand Up @@ -161,8 +164,9 @@ export class ManagedDeviceLifecycle {
target: VirtualDevice,
claim?: DeviceOperationClaim,
except?: readonly string[],
label?: string,
): Promise<ReadyDeviceHandoff | undefined> {
return this.#makeReadyForLease(target, "provisioning", claim, except);
return this.#makeReadyForLease(target, "provisioning", claim, except, label);
}

async shutdown(
Expand Down Expand Up @@ -303,6 +307,7 @@ export class ManagedDeviceLifecycle {
expectedState: "provisioning" | "shutdown",
existingClaim?: DeviceOperationClaim,
except?: readonly string[],
label?: string,
): Promise<ReadyDeviceHandoff | undefined> {
const claimed = await this.#claim(target, [expectedState], "boot", existingClaim);
if (claimed === undefined) return undefined;
Expand All @@ -315,6 +320,7 @@ export class ManagedDeviceLifecycle {
mode: specMode(claimed.device.spec),
purpose: "prepare",
...(except === undefined ? {} : { except }),
...(label === undefined ? {} : { label }),
});
} catch (error: unknown) {
await this.#release(claimed);
Expand Down
2 changes: 1 addition & 1 deletion src/drivers/ios/AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Resolve and boot:

- A slim spec is returned only for a slim request the driver will slim. The registry alone records a device's mode.
- `RuntimeMissingError` is only for what a download can fix: an OS outside the device type's range, an installed runtime not paired with the type, and anything below iOS 16 are refused instead.
- A boot is `simctl boot`, `bootstatus -b`, then `listapps` until it succeeds, on one deadline: bootstatus can finish before the installer is ready.
- A boot is `simctl rename` (prepare only: label, else stored name; failure logged), `simctl boot`, `bootstatus -b`, then `listapps` until it succeeds, on one deadline: bootstatus can finish before the installer is ready.
- Slim (ADR 0060), on `prepare` of a slim spec only: the managed labels are merged into the simulator's `disabled.plist` before the boot (other entries kept; a failed write is logged, the boot goes on), and `launchctl print-disabled system` is read back after it. A label not as written is `full`; a failed readback is `slim`; both call `onSlimSkipped`, logged only. All as written and the write ok: `slim`, and `onSlimmed` is `device.slimmed`. Nothing written off and no kept category: `full`, no readback.
- Named services (ADR 0083): `exclude` beats `include` beats a category; `simlock-managed.json` records the names, so one dropped from both is written back on. A baseline saved before it keeps a dropped name off; `include` it.
- Slim `except` (ADR 0066): an excepted category's labels are written `false`; an unknown name is `BAD_REQUEST`; a kept label not read back throws `KeptCategoriesError`.
Expand Down
9 changes: 6 additions & 3 deletions src/drivers/ios/boot-courtesies.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ describe("IosSimctlDriver simulator window", () => {
const xcrun = (...args: string[]) => ({ command: "xcrun", args: ["simctl", ...args] });
const open = (...args: string[]) => ({ command: "open", args: ["-g", ...args] });
const clipboard = deviceHubEdit(`Set :DevicePreferences:${udid}:pasteboardSyncEnabled false`);
const renameCall = xcrun("rename", udid, driverData.name);
const bootCalls = [xcrun("boot", udid), xcrun("bootstatus", udid, "-b"), xcrun("listapps", udid)];
const deviceHub = open(
"-b",
Expand Down Expand Up @@ -131,8 +132,9 @@ describe("IosSimctlDriver simulator window", () => {
])(
"shows a simulator in a window without taking focus when ios.simulator.headless is false: $name",
async (c) => {
const renamed = (c.options?.purpose ?? "prepare") === "prepare" ? [renameCall] : [];
const runner = new ScriptedProcessRunner(
[...clipboard, ...c.expected].map((match) => ({
[...renamed, ...clipboard, ...c.expected].map((match) => ({
match,
...(match === deviceHub && c.deviceHubResult !== undefined
? { result: c.deviceHubResult }
Expand Down Expand Up @@ -162,6 +164,7 @@ describe("IosSimctlDriver simulator window", () => {
),
).resolves.toMatchObject({ address: udid });
expect(runner.calls.map(({ command, args }) => ({ command, args }))).toEqual([
...renamed,
...clipboard,
...c.expected,
]);
Expand Down Expand Up @@ -209,7 +212,7 @@ describe("IosSimctlDriver simulator window", () => {
"makes the simulator ready and reports the failure when opening its window fails: $name",
async (c) => {
const runner = new ScriptedProcessRunner([
...[...clipboard, ...bootCalls].map((match) => ({ match })),
...[renameCall, ...clipboard, ...bootCalls].map((match) => ({ match })),
...c.script,
]);
const clock = new FakeClock();
Expand All @@ -234,7 +237,7 @@ describe("IosSimctlDriver simulator window", () => {
);
if (c.script.some((step) => "hangs" in step)) {
const state = promiseState(ready);
await waitForCalls(runner, clipboard.length + bootCalls.length + c.script.length);
await waitForCalls(runner, 1 + clipboard.length + bootCalls.length + c.script.length);
clock.advance(29_999);
await flushMicrotasks();
expect(state.state).toBe("pending");
Expand Down
11 changes: 7 additions & 4 deletions src/drivers/ios/boot-deadline.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ describe("IosSimctlDriver boot deadline", () => {
async ({ budgetMs, config, data, options }) => {
const clock = new FakeClock();
const runner = new ScriptedProcessRunner([
{ match: { command: "xcrun", args: simctlArgs("rename", data.udid, driverData.name) } },
{ hangs: true, match: { command: "xcrun", args: simctlArgs("boot", data.udid) } },
]);
const driver = await bootBudgetDriver(runner, clock, config);
Expand All @@ -80,10 +81,10 @@ describe("IosSimctlDriver boot deadline", () => {
options,
);
void ready.catch(() => undefined);
await waitForCalls(runner, 1);
await waitForCalls(runner, 2);
const readyState = promiseState(ready);

expect(runner.calls[0]?.options).toEqual({ timeoutMs: budgetMs });
expect(runner.calls[1]?.options).toEqual({ timeoutMs: budgetMs });
clock.advance(budgetMs - 1);
await flushMicrotasks221();
expect(readyState.state).toBe("pending");
Expand All @@ -98,6 +99,7 @@ describe("IosSimctlDriver boot deadline", () => {
async ({ budgetMs, config, data, options }) => {
const clock = new FakeClock();
const runner = new ScriptedProcessRunner([
{ match: { command: "xcrun", args: simctlArgs("rename", data.udid, driverData.name) } },
{
match: { command: "xcrun", args: simctlArgs("boot", data.udid) },
onSpawn: () => clock.advance(100_000),
Expand All @@ -115,17 +117,18 @@ describe("IosSimctlDriver boot deadline", () => {
options,
);
void ready.catch(() => undefined);
await waitForCalls(runner, 2);
await waitForCalls(runner, 3);
const readyState = promiseState(ready);

expect(runner.calls[1]?.options).toEqual({ timeoutMs: budgetMs - 100_000 });
expect(runner.calls[2]?.options).toEqual({ timeoutMs: budgetMs - 100_000 });
clock.advance(budgetMs - 100_000 - 1);
await flushMicrotasks221();
expect(readyState.state).toBe("pending");

clock.advance(1);
expect(await settledValue(ready)).toBeInstanceOf(BootTimeoutError);
expect(runner.calls.map((call) => call.args)).toEqual([
simctlArgs("rename", data.udid, driverData.name),
simctlArgs("boot", data.udid),
simctlArgs("bootstatus", data.udid, "-b"),
simctlArgs("shutdown", data.udid),
Expand Down
8 changes: 6 additions & 2 deletions src/drivers/ios/clipboard.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ describe("IosSimctlDriver DeviceHub clipboard", () => {
const exportDomain = ["defaults", "export", "com.apple.dt.Devices", expect.any(String)];
const importDomain = ["defaults", "import", "com.apple.dt.Devices", expect.any(String)];

const rename = ["xcrun", "simctl", "rename", udid, driverData.name];
const syncOffThenBoot = [
exportDomain,
staged(`Set :DevicePreferences:${udid}:pasteboardSyncEnabled false`),
Expand All @@ -158,6 +159,7 @@ describe("IosSimctlDriver DeviceHub clipboard", () => {
expect(hub.domain).toEqual({ other: { pasteboardSyncEnabled: true, windowScale: 50 } });

expect(calls).toEqual([
rename,
...syncOffThenBoot,
...syncOffThenBoot,
["xcrun", "simctl", "shutdown", udid],
Expand Down Expand Up @@ -200,14 +202,16 @@ describe("IosSimctlDriver DeviceHub clipboard", () => {
await expect(driver.makeReady(device, prepareFull)).rejects.toThrow(
"Could not disable DeviceHub clipboard sync",
);
expect(calls.every((call) => call[0] === "defaults")).toBe(true);
expect(calls[0]).toEqual(rename);
expect(calls.slice(1).every((call) => call[0] === "defaults")).toBe(true);
expect(hub.domain[udid]?.pasteboardSyncEnabled).toBe(true);

calls.length = 0;
hub.failing = "";
hub.warm = true;
await driver.makeReady(device, prepareFull);
expect(calls.slice(0, 6)).toEqual([
expect(calls.slice(0, 7)).toEqual([
rename,
...syncOffThenBoot.slice(0, 4),
["xcrun", "simctl", "shutdown", udid],
["xcrun", "simctl", "boot", udid],
Expand Down
1 change: 1 addition & 0 deletions src/drivers/ios/index-device-set.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ describe("IosSimctlDriver device set", () => {
sources: true,
stdout: `${driverData.udid}\n`,
},
{ args: ["rename", driverData.udid, driverData.name], scoped: true },
{ args: ["boot", driverData.udid], scoped: true },
{ args: ["bootstatus", driverData.udid, "-b"], scoped: true },
{ args: ["listapps", driverData.udid], scoped: true },
Expand Down
Loading
Loading