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 @@ -186,9 +186,15 @@ export type SessionProviderOptions = {
stalledCleanup?: boolean;
/** How many `forward --remove` calls hang until their cleanup budget aborts them. */
stalledForwardRemovals?: number;
/**
* How many snapshot requests the first helper process leaves unanswered. A stall belongs to the
* helper that hung, so a helper started after it answers.
*/
stalledSnapshots?: number;
/** How many snapshot requests each helper process answers before `stalledSnapshots` applies. */
/** How many snapshot requests the first helper answers before `stalledSnapshots` applies. */
answeredSnapshotsBeforeStall?: number;
/** Called when the helper receives a request it leaves unanswered. */
onStalledSnapshot?: () => void;
/** Whether `adb features` advertises the shell protocol that forwards device exit status. */
shellProtocolV2?: boolean;
/** Make the `adb features` probe fail the way an adb too old to know the command does. */
Expand All @@ -211,14 +217,16 @@ export type FakeAndroidHelperRuntimeRelease =

export function createSessionProvider(options: SessionProviderOptions): AndroidAdbProvider {
bindAndroidAdbTestHost();
let stalledSnapshots = options.stalledSnapshots ?? 0;
let spawnedHelpers = 0;
return {
exec: createSessionExec(options),
spawn: (args) => {
options.spawnArgs?.push(args);
const port = readSessionPort(args);
const process = new FakeAndroidProcess();
options.processes?.push(process);
spawnedHelpers += 1;
let stalledSnapshots = spawnedHelpers === 1 ? (options.stalledSnapshots ?? 0) : 0;
let snapshotCount = 0;
const sockets = new Set<net.Socket>();
const server = net.createServer((socket) => {
Expand Down Expand Up @@ -263,6 +271,7 @@ export function createSessionProvider(options: SessionProviderOptions): AndroidA
snapshotCount += 1;
if (snapshotCount > (options.answeredSnapshotsBeforeStall ?? 0) && stalledSnapshots > 0) {
stalledSnapshots -= 1;
options.onStalledSnapshot?.();
return;
}
const body = `<hierarchy><node text="snapshot ${snapshotCount}" /></hierarchy>`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,53 +99,69 @@ test('retires a persistent session that exceeds the helper command budget', asyn
);
});

test('cancels a stalled snapshot, retires its helper, and starts the next capture cleanly', async () => {
const calls: (readonly string[])[] = [];
const processes: FakeAndroidProcess[] = [];
const provider = createSessionProvider({ calls, processes, stalledSnapshots: 1 });
const controller = new AbortController();
const capture = captureAndroidSnapshotWithHelperSession({
adb: provider.exec,
adbProvider: provider,
deviceKey: 'android:emulator-5554',
signal: controller.signal,
});
setTimeout(() => controller.abort(new Error('wait deadline exceeded')), 10);
// A cancellation lands wherever the caller's deadline falls (#4030): before the helper reports
// ready, or while the helper holds the snapshot request it never answers.
for (const cancelPoint of ['before the helper is ready', 'while the helper stalls'] as const) {
test(`cancels a capture ${cancelPoint}, retires its helper, and starts the next capture cleanly`, async () => {
const calls: (readonly string[])[] = [];
const processes: FakeAndroidProcess[] = [];
const controller = new AbortController();
const cancel = () => controller.abort(new Error('wait deadline exceeded'));
const provider = createSessionProvider({
calls,
processes,
stalledSnapshots: 1,
...(cancelPoint === 'while the helper stalls' ? { onStalledSnapshot: cancel } : {}),
});
const capture = captureAndroidSnapshotWithHelperSession({
adb: provider.exec,
adbProvider: {
...provider,
spawn: (args, options) => {
const process = provider.spawn!(args, options);
if (cancelPoint === 'before the helper is ready') cancel();
return process;
},
},
deviceKey: 'android:emulator-5554',
signal: controller.signal,
});

await assert.rejects(capture, /wait deadline exceeded/);
assert.equal(processes[0]?.killed, true);
assert.equal(
calls.some((args) => args[0] === 'forward' && args[1] === '--remove'),
true,
);
await assert.rejects(capture, /wait deadline exceeded/);
assert.equal(processes[0]?.killed, true);
assert.equal(
calls.some((args) => args[0] === 'forward' && args[1] === '--remove'),
true,
);

const next = await captureAndroidSnapshotWithHelperSession({
adb: provider.exec,
adbProvider: provider,
deviceKey: 'android:emulator-5554',
const next = await captureAndroidSnapshotWithHelperSession({
adb: provider.exec,
adbProvider: provider,
deviceKey: 'android:emulator-5554',
});
assert.match(next?.xml ?? '', /snapshot 1/);
assert.equal(next?.metadata.sessionReused, false);
assert.equal(processes.length, 2);
});
assert.match(next?.xml ?? '', /snapshot 1/);
assert.equal(next?.metadata.sessionReused, false);
assert.equal(processes.length, 2);
});
}

test('canceled capture joins canceled external cleanup before returning', async () => {
const calls: (readonly string[])[] = [];
const cleanupAborts: (readonly string[])[] = [];
const controller = new AbortController();
const provider = createSessionProvider({
calls,
cleanupAborts,
stalledSnapshots: 1,
stalledCleanup: true,
onStalledSnapshot: () => controller.abort(new Error('wait deadline exceeded')),
});
const controller = new AbortController();
const capture = captureAndroidSnapshotWithHelperSession({
adb: provider.exec,
adbProvider: provider,
deviceKey: 'android:emulator-5554',
signal: controller.signal,
});
setTimeout(() => controller.abort(new Error('wait deadline exceeded')), 10);

const outcome = await Promise.race([
capture.then(
Expand Down
Loading
Loading