Files
orca/src/main/updater-test-module-loader.ts
T
Neil aa658d28e3 test(updater): stop a slow module import from failing the next test (#17726)
* test(updater): stop a slow module import from failing the next test

`updater.ts` is 2.4k lines. Its first transform in a worker costs ~1.4s idle
but 45s+ when the machine is oversubscribed, which is past the 30s
`testTimeout`. Vitest cannot cancel the timed-out test body, so the abandoned
continuation went on to call `setupAutoUpdater` during the *next* test — with
the harness already reset — and failed it with:

    AssertionError: expected "vi.fn()" to be called 1 times, but got 2 times

That is the exact signature of the abandoned-instance timer flake fixed in
#17649/#17663, so a machine-load timeout reads as that regression returning and
sends the reader hunting in the wrong place.

Two changes, in `updater-test-module-loader.ts`:

- `loadUpdaterModule()` replaces every `await import('./updater')` in the suite.
  It records the test that asked for the module and throws if the import
  resolves after that test ended, stranding the continuation so the timeout
  stays the only reported failure. This removes the trap.
- `warmUpdaterModule()` imports the module once in `beforeAll`. The transform is
  cached across `vi.resetModules()` — only a file's first import pays it — so
  warming moves that one slow import onto the 60s `hookTimeout` and leaves every
  in-test import at re-evaluation cost (~25ms idle).

Measured on a 16-core mac, first vs later import in one file: 1439ms / 25ms
idle, 8339ms / 149ms under 40 CPU hogs, 45521ms / 15182ms under 400.

Under 400 hogs the suite went from 15 files and 22 tests failing (15 timeouts
plus 7 misleading assertion failures) to 23/23 files and 269/269 passing. Under
900 hogs it degrades into 14 plain `Hook timed out in 60000ms` failures and zero
assertion failures.

* fix: tighten the fence, surface its warning, stop patching timers on warm-up

Review findings on the loader:

Drop trackRealTimers() from warmUpdaterModule(). It was inert — updater.ts
arms no timers at module scope — and actively harmful for the 5 files that
build their own mocks and never call clearTrackedRealTimers(). Those files
previously had pristine timer globals; the warm-up installed a wrapper that
was never restored and whose armed-handle set grew unbounded.

Key the fence on TestRunner.getCurrentTest() instead of currentTestName.
Nothing ever clears currentTestName, so the fence only fired once the *next*
test had started; a continuation resolving during the timed-out test's own
teardown, or after the file's last test, was still handed the module. The
last-test case mattered: the harness afterAll has already cleared timer
tracking by then.

Emit the diagnostic through process.emitWarning. The throw lands on a promise
vitest already settled, so the message explaining why the continuation was
stranded was discarded and reached nobody — which was the entire payoff.

Widen the loader test's race margin 50ms -> 500ms. It gated on the
test-to-test transition completing in 50ms, so the regression test for a
contention bug could itself fail under contention.
2026-08-31 13:22:27 -07:00

42 lines
2.0 KiB
TypeScript

import { beforeAll, TestRunner } from 'vitest'
import type * as UpdaterModule from './updater'
/**
* Pays `updater.ts`'s transform cost once per file, against `hookTimeout` instead of `testTimeout`.
*
* Why: the module is ~2.4k lines and pulls in a wide graph, so a worker's first import of it costs
* ~1.4s idle but 45s+ on an oversubscribed machine — past the 30s `testTimeout`. `vi.resetModules()`
* re-evaluates the module without re-transforming it, so only a file's *first* import is exposed;
* warming it in a hook moves that one slow import onto the 60s hook budget and leaves every in-test
* import at re-evaluation cost (~25ms idle).
*/
export function warmUpdaterModule(): void {
beforeAll(async () => {
await import('./updater')
})
}
/**
* Imports `./updater`, refusing to hand the module to a test that has already ended.
*
* Why: vitest cannot cancel a timed-out test body. When the import outran `testTimeout` the
* continuation went on to call `setupAutoUpdater` during the *next* test, failing it with
* "expected 1 times, but got 2 times" — the exact signature of the abandoned-instance timer flake
* fixed in #17649/#17663, so the timeout read as that regression returning. Throwing here strands the
* continuation and leaves the timeout as the only reported failure.
*/
export async function loadUpdaterModule(): Promise<typeof UpdaterModule> {
const owner = TestRunner.getCurrentTest()
const module = await import('./updater')
if (owner !== undefined && TestRunner.getCurrentTest() !== owner) {
// Why: vitest has already settled the timed-out test's promise, so this throw is swallowed —
// warn separately or the reason the continuation was stranded reaches nobody.
process.emitWarning(
`updater import requested by "${owner.name}" resolved after that test ended. The test timed ` +
`out mid-import; fix that timeout, not the assertions.`
)
throw new Error(`updater import for "${owner.name}" resolved after that test ended`)
}
return module
}