diff --git a/src/editors/vscode/src/test/suite/debug-attach-reliability-e2e.test.ts b/src/editors/vscode/src/test/suite/debug-attach-reliability-e2e.test.ts index b0d18d1a..ff35e0b4 100644 --- a/src/editors/vscode/src/test/suite/debug-attach-reliability-e2e.test.ts +++ b/src/editors/vscode/src/test/suite/debug-attach-reliability-e2e.test.ts @@ -1,8 +1,12 @@ -// Attach reliability: twenty consecutive attaches to ONE long-running process. -// netcoredbg's attach is known to refuse intermittently (Samsung/netcoredbg -// #205, `0x80070057`), and [DEBUG-GAPS] commits SharpLsp to absorb that with -// retries. Each attach must be accepted, pause the live thread, and detach -// without killing the process, so the twenty-first attach finds it alive. +// Attach reliability: twenty consecutive attaches. netcoredbg's attach is known +// to refuse intermittently (Samsung/netcoredbg #205, `0x80070057`), and +// [DEBUG-GAPS] commits SharpLsp to absorb that with retries. Each attach must be +// accepted, pause the live thread, and detach without killing the process. +// +// Every attempt attaches to a FRESH process: the fixture waits 30 seconds and +// then exits, and twenty attaches on a slow runner outlast that. Each attempt's +// session is the one that STARTED for it, never `activeDebugSession`, which can +// still name the previous attempt's ending session. // // Implements the "Attach reliability" P1 row of [DEBUG-FEATURES-LAUNCH]. import * as assert from 'node:assert/strict'; @@ -16,10 +20,11 @@ import { } from './debug-attach-kit'; import type { DapRecorder } from './debug-dap-kit'; import { CMD_STOP, assertStopReason } from './debug-drive-kit'; -import { startAttached } from './debug-flow-kit'; +import type { DebugFixture } from './debug-fixture-programs'; import { assertCleanSession, stopDebuggee, useDebuggee } from './debug-suite-kit'; -import { eq, sleep } from './test-helpers'; -import { DEBUG_TEST_MS, QUIET_MS } from './test-timeouts'; +import { DebugSessionRecorder } from './run-debug-kit'; +import { eq, requireAt } from './test-helpers'; +import { DEBUG_TEST_MS } from './test-timeouts'; /** Consecutive attaches the plan's reliability row asks for. */ const ATTEMPTS = 20; @@ -27,33 +32,46 @@ const ATTEMPTS = 20; /** At least 95% of them must succeed. */ const REQUIRED = Math.ceil(ATTEMPTS * 0.95); -/** Why one attach failed, or `undefined` when it attached, paused and detached. */ -async function attachOnce( - folder: vscode.WorkspaceFolder, - pid: number, - recorder: DapRecorder, -): Promise { - const before = recorder.stops().length; +/** What one attempt needs: where to attach from, and what records it. */ +interface Attempt { + readonly fixture: DebugFixture; + readonly folder: vscode.WorkspaceFolder; + readonly recorder: DapRecorder; + readonly sessions: DebugSessionRecorder; +} + +/** Attach to `pid` and pause it; the session is the one this attach started. */ +async function attachAndPause(attempt: Attempt, pid: number): Promise { + const started = attempt.sessions.ours.length; + const stops = attempt.recorder.stops().length; + assert.ok(await vscode.debug.startDebugging(attempt.folder, attachConfig({ processId: pid }))); + await attempt.sessions.waitForSessions(started + 1); + const session = attempt.sessions.liveOurs.find( + (live) => live.id === requireAt(attempt.sessions.ours, started, 'this attach').id, + ); + assert.ok(session, 'the session this attach started is live'); + eq(Number(session.configuration['processId']), pid, 'the attach aims at this attempt’s pid'); + await pauseAttached(session); + await attempt.recorder.waitForStops(stops + 1); +} + +/** Why one attempt failed, or `undefined` when it attached, paused and detached. */ +async function attachOnce(attempt: Attempt): Promise { + const running = await startAliveOutside(attempt.fixture, 'each attempt starts a live process'); + let failure: string | undefined; try { - const session = await startAttached( - folder, - attachConfig({ processId: pid }), - 'refused', - 'no session', - ); - eq(Number(session.configuration['processId']), pid, 'each attach aims at the same pid'); - await pauseAttached(session); - await recorder.waitForStops(before + 1); - return undefined; + await attachAndPause(attempt, running.pid); } catch (error) { - return String(error); - } finally { - await vscode.commands.executeCommand(CMD_STOP); - await stopDebuggee(); + failure = String(error); } + await vscode.commands.executeCommand(CMD_STOP); + await stopDebuggee(); + assert.ok(isAlive(running.pid), 'stopping an attach detaches; the process keeps running'); + process.kill(running.pid); + return failure; } -suite('Debug attach — twenty consecutive attaches to one running process', () => { +suite('Debug attach — twenty consecutive attaches', () => { const debuggee = useDebuggee('debug-attach-twenty-', 'csharp'); teardown(async function () { @@ -61,38 +79,33 @@ suite('Debug attach — twenty consecutive attaches to one running process', () await killSpawned(); }); - test('at least 19 of 20 attaches pause the live process, and every detach leaves it running', async function () { + test('at least 19 of 20 attaches pause a live process, and every detach leaves it running', async function () { this.timeout(DEBUG_TEST_MS * ATTEMPTS); const { fixture, folder, recorder } = debuggee(); - const running = await startAliveOutside( - fixture, - 'the process is alive before the first attach', - ); - - const failures: string[] = []; - for (let attempt = 1; attempt <= ATTEMPTS; attempt += 1) { - const failure = await attachOnce(folder, running.pid, recorder); - if (failure !== undefined) failures.push(`attempt ${String(attempt)}: ${failure}`); + const sessions = new DebugSessionRecorder(); + try { + const failures: string[] = []; + for (let attempt = 1; attempt <= ATTEMPTS; attempt += 1) { + const failure = await attachOnce({ fixture, folder, recorder, sessions }); + if (failure !== undefined) failures.push(`attempt ${String(attempt)}: ${failure}`); + eq( + vscode.debug.activeDebugSession, + undefined, + `attempt ${String(attempt)} left no session`, + ); + } assert.ok( - isAlive(running.pid), - `attempt ${String(attempt)} detached without killing the process`, - ); - eq( - vscode.debug.activeDebugSession, - undefined, - `attempt ${String(attempt)} left no session behind`, + ATTEMPTS - failures.length >= REQUIRED, + `${String(REQUIRED)} of ${String(ATTEMPTS)} attaches must succeed; failures: ${failures.join(' | ')}`, ); + eq(sessions.ours.length, ATTEMPTS, 'every attempt started exactly one session'); + eq(sessions.liveOurs.length, 0, 'and every one of them ended'); + const stops = recorder.stops(); + assert.ok(stops.length >= REQUIRED, 'every successful attach recorded its own pause'); + for (const stop of stops) assertStopReason(stop, 'pause', 'an attach pause'); + assertCleanSession(debuggee(), 'twenty consecutive attaches'); + } finally { + sessions.dispose(); } - - assert.ok( - ATTEMPTS - failures.length >= REQUIRED, - `${String(REQUIRED)} of ${String(ATTEMPTS)} attaches must succeed; failures: ${failures.join(' | ')}`, - ); - const stops = recorder.stops(); - assert.ok(stops.length >= REQUIRED, 'every successful attach recorded its own pause'); - for (const stop of stops) assertStopReason(stop, 'pause', 'an attach pause'); - await sleep(QUIET_MS); - assert.ok(isAlive(running.pid), 'twenty detaches later, the process is still the user’s'); - assertCleanSession(debuggee(), 'twenty consecutive attaches'); }); });