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
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ test('managed launch preparation consumes the same explicit startup budget as lo
throw new Error('Managed launch reached local tooling');
});
appleRunnerTestHost.update({ runXcrun: localTool, runCmdBackground: localTool });
let finish!: () => void;
const exited = new Promise<void>((resolve) => {
finish = resolve;
});
const { promise: exited, resolve: finish } = Promise.withResolvers<void>();
const launcher: RunnerLauncher = {
endpoint: { port: 8100, http: { url: 'https://instance.invalid/command' } },
async launch() {
Expand Down Expand Up @@ -47,14 +44,8 @@ test('managed launch preparation consumes the same explicit startup budget as lo
for (const operation of ['stop', 'release'] as const) {
test(`${operation} cancels a pending launch and admits a subsequent generation`, async () => {
let attempts = 0;
let entered!: () => void;
const launching = new Promise<void>((resolve) => {
entered = resolve;
});
let finish!: () => void;
const exited = new Promise<void>((resolve) => {
finish = resolve;
});
const { promise: launching, resolve: entered } = Promise.withResolvers<void>();
const { promise: exited, resolve: finish } = Promise.withResolvers<void>();
const provider = createLaunchedAppleRunnerProvider(
IOS_SIMULATOR,
{
Expand Down Expand Up @@ -108,9 +99,8 @@ test('a failed termination stays owned and blocks replacement until termination
endpoint: { port: 8100, http: { url: 'https://instance.invalid/command' } },
async launch() {
attempts++;
const exited = new Promise<void>((resolve) => {
finish = resolve;
});
const { promise: exited, resolve } = Promise.withResolvers<void>();
finish = resolve;
return {
exited,
output: (async function* () {
Expand Down Expand Up @@ -146,14 +136,8 @@ test('a failed termination stays owned and blocks replacement until termination

test('an output failure and failed termination remain observable without an unhandled rejection', async () => {
let failTermination = true;
let failOutput!: () => void;
let finish!: () => void;
const outputFailure = new Promise<void>((resolve) => {
failOutput = resolve;
});
const exited = new Promise<void>((resolve) => {
finish = resolve;
});
const { promise: outputFailure, resolve: failOutput } = Promise.withResolvers<void>();
const { promise: exited, resolve: finish } = Promise.withResolvers<void>();
const provider = createLaunchedAppleRunnerProvider(
IOS_SIMULATOR,
{
Expand Down Expand Up @@ -204,9 +188,8 @@ test('retained reopen shares a generation and an exited generation flushes logs
endpoint: { port: server.port, http: { url: `http://127.0.0.1:${server.port}/command` } },
async launch() {
const generation = ++attempts;
const exited = new Promise<void>((resolve) => {
finish = resolve;
});
const { promise: exited, resolve } = Promise.withResolvers<void>();
finish = resolve;
return {
exited,
output: (async function* () {
Expand Down Expand Up @@ -254,9 +237,8 @@ test('a canceled launch retains a generation whose termination failed before pub
endpoint: { port: 8100, http: { url: 'https://instance.invalid/command' } },
async launch() {
attempts++;
const exited = new Promise<void>((resolve) => {
finish = resolve;
});
const { promise: exited, resolve } = Promise.withResolvers<void>();
finish = resolve;
cancellation.abort(new Error('caller left'));
return {
exited,
Expand Down Expand Up @@ -300,9 +282,8 @@ test('an immediate output failure cannot publish a draining generation as ready'
endpoint: { port: 8100, http: { url: 'https://instance.invalid/command' } },
async launch() {
const generation = ++attempts;
const exited = new Promise<void>((resolve) => {
finish = resolve;
});
const { promise: exited, resolve } = Promise.withResolvers<void>();
finish = resolve;
return {
exited,
output: (async function* () {
Expand Down Expand Up @@ -342,10 +323,7 @@ for (const operation of ['abort', 'stop'] as const) {
await import('../runner-session.ts');
let failRegistered = true;
const registered = (suffix: string, shouldFail: () => boolean) => {
let finish!: () => void;
const exited = new Promise<void>((resolve) => {
finish = resolve;
});
const { promise: exited, resolve: finish } = Promise.withResolvers<void>();
return createLaunchedAppleRunnerProvider(
{ ...IOS_SIMULATOR, id: `${IOS_SIMULATOR.id}-${suffix}` },
{
Expand All @@ -371,29 +349,17 @@ for (const operation of ['abort', 'stop'] as const) {
const healthy = registered('healthy-stop', () => false);
await failing.ensure({});
await healthy.ensure({});
let enter!: () => void;
const entered = new Promise<void>((resolve) => {
enter = resolve;
});
let aborted!: () => void;
const canceled = new Promise<void>((resolve) => {
aborted = resolve;
});
let release!: () => void;
const held = new Promise<void>((resolve) => {
release = resolve;
});
let finish!: () => void;
const exited = new Promise<void>((resolve) => {
finish = resolve;
});
const { promise: entered, resolve: enter } = Promise.withResolvers<void>();
const { promise: canceled, resolve: aborted } = Promise.withResolvers<void>();
const { promise: held, resolve: release } = Promise.withResolvers<void>();
const { promise: exited, resolve: finish } = Promise.withResolvers<void>();
let failTermination = true;
const provider = createLaunchedAppleRunnerProvider(
IOS_SIMULATOR,
{
endpoint: { port: 8100, http: { url: 'https://instance.invalid/command' } },
async launch(_endpoint, signal) {
signal.addEventListener('abort', aborted, { once: true });
signal.addEventListener('abort', () => aborted(), { once: true });
enter();
await held;
return {
Expand Down
5 changes: 1 addition & 4 deletions packages/apple-runner/src/runner-retention.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@ export function observeRunnerListener(
if (session.state !== 'ready' || !needsListenerWatch(session) || session.listenerWatch) return;
const socket = net.connect(session.endpoint.port, '127.0.0.1');
socket.unref();
let settle!: (connected: boolean) => void;
const ready = new Promise<boolean>((resolve) => {
settle = resolve;
});
const { promise: ready, resolve: settle } = Promise.withResolvers<boolean>();
const connectTimer = setTimeout(() => socket.destroy(), LISTENER_CONNECT_TIMEOUT_MS);
connectTimer.unref();
const watch: RunnerListenerWatch = {
Expand Down
10 changes: 7 additions & 3 deletions packages/capture-kit/src/app-log-pid-monitor.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { describe, expect, test, vi } from 'vitest';
import type { AppLogBackgroundProcess } from '@agent-device/contracts/app-log-runtime';
import { monitorPidScopedProcess } from './app-log-pid-monitor.ts';
import { controlledSleeps, deferred, processFixture } from './app-log-pid-process.fixtures.ts';
import { controlledSleeps, processFixture } from './app-log-pid-process.fixtures.ts';

describe('PID-scoped app-log monitor', () => {
test('rotates a live stream when the app PID changes', async () => {
const first = processFixture(deferred<{ stdout: string; stderr: string; exitCode: number }>());
const second = processFixture(deferred<{ stdout: string; stderr: string; exitCode: number }>());
const first = processFixture(
Promise.withResolvers<{ stdout: string; stderr: string; exitCode: number }>(),
);
const second = processFixture(
Promise.withResolvers<{ stdout: string; stderr: string; exitCode: number }>(),
);
const sleeps = controlledSleeps();
const resolvePid = vi.fn(async () => '456');
const startProcess = vi.fn(async () => second.process);
Expand Down
10 changes: 1 addition & 9 deletions packages/capture-kit/src/app-log-pid-process.fixtures.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
import { vi } from 'vitest';
import type { AppLogBackgroundProcess } from '@agent-device/contracts/app-log-runtime';

export function deferred<T>() {
let resolve!: (value: T) => void;
const promise = new Promise<T>((settle) => {
resolve = settle;
});
return { promise, resolve };
}

export function processFixture(
wait: ReturnType<typeof deferred<{ stdout: string; stderr: string; exitCode: number }>>,
wait: PromiseWithResolvers<{ stdout: string; stderr: string; exitCode: number }>,
) {
const terminate = vi.fn(async () => wait.resolve({ stdout: '', stderr: '', exitCode: 0 }));
const dispose = vi.fn(async () => {});
Expand Down
29 changes: 18 additions & 11 deletions packages/capture-kit/src/app-log-pid-process.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ import type {
AppLogOutputSink,
AppLogRuntimeHost,
} from '@agent-device/contracts/app-log-runtime';
import { deferred, processFixture } from './app-log-pid-process.fixtures.ts';
import { processFixture } from './app-log-pid-process.fixtures.ts';
import { createPidScopedAppLogProcess } from './app-log-pid-process.ts';

describe('PID-scoped app-log process lifecycle', () => {
test('retries output disposal after an unconfirmed finish', async () => {
const output = outputFixture();
output.dispose.mockRejectedValueOnce(new Error('Close unavailable'));
const started =
processFixture(deferred<{ stdout: string; stderr: string; exitCode: number }>());
const started = processFixture(
Promise.withResolvers<{ stdout: string; stderr: string; exitCode: number }>(),
);
const handle = await createPidScopedAppLogProcess({
host: hostFixture(output.sink),
backend: 'android',
Expand All @@ -30,9 +31,13 @@ describe('PID-scoped app-log process lifecycle', () => {
expect(output.dispose).toHaveBeenCalledTimes(2);
});
test('cleans a later process that resolves after finish begins', async () => {
const initialExit = deferred<{ stdout: string; stderr: string; exitCode: number }>();
const laterExit = deferred<{ stdout: string; stderr: string; exitCode: number }>();
const laterStart = deferred<AppLogBackgroundProcess>();
const initialExit = Promise.withResolvers<{
stdout: string;
stderr: string;
exitCode: number;
}>();
const laterExit = Promise.withResolvers<{ stdout: string; stderr: string; exitCode: number }>();
const laterStart = Promise.withResolvers<AppLogBackgroundProcess>();
const initial = processFixture(initialExit);
const later = processFixture(laterExit);
const output = outputFixture();
Expand Down Expand Up @@ -98,7 +103,7 @@ describe('PID-scoped app-log process lifecycle', () => {
});

test('re-probes the app PID after an empty recovering observation', async () => {
const exit = deferred<{ stdout: string; stderr: string; exitCode: number }>();
const exit = Promise.withResolvers<{ stdout: string; stderr: string; exitCode: number }>();
const started = processFixture(exit);
const output = outputFixture();
const resolvePid = vi.fn().mockResolvedValueOnce('').mockResolvedValueOnce('456');
Expand Down Expand Up @@ -254,8 +259,9 @@ describe('PID-scoped app-log capture after a failed PID poll', () => {
}

test('keeps the active stream and keeps polling after one rejected poll', async () => {
const started =
processFixture(deferred<{ stdout: string; stderr: string; exitCode: number }>());
const started = processFixture(
Promise.withResolvers<{ stdout: string; stderr: string; exitCode: number }>(),
);
const resolvePid = vi
.fn<() => Promise<string>>()
.mockResolvedValueOnce('123')
Expand All @@ -271,8 +277,9 @@ describe('PID-scoped app-log capture after a failed PID poll', () => {
});

test('starts the stream once a rejected waiting poll is followed by a PID', async () => {
const started =
processFixture(deferred<{ stdout: string; stderr: string; exitCode: number }>());
const started = processFixture(
Promise.withResolvers<{ stdout: string; stderr: string; exitCode: number }>(),
);
const resolvePid = vi
.fn<() => Promise<string>>()
.mockResolvedValueOnce('')
Expand Down
5 changes: 1 addition & 4 deletions packages/capture-kit/src/durable-capture/fence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,7 @@ async function serializeResource<Result>(
task: () => Promise<Result>,
): Promise<Result> {
const previous = resourceFenceTails.get(resourcePath) ?? Promise.resolve();
let release!: () => void;
const current = new Promise<void>((resolve) => {
release = resolve;
});
const { promise: current, resolve: release } = Promise.withResolvers<void>();
const tail = previous.catch(() => {}).then(() => current);
resourceFenceTails.set(resourcePath, tail);
await previous.catch(() => {});
Expand Down
Loading
Loading