|
| 1 | +import { afterEach, beforeEach } from 'bun:test' |
| 2 | + |
| 3 | +/** |
| 4 | + * bun:test lifecycle hooks keep their own 5s timeout, independent of any |
| 5 | + * per-test timeout. In isolation a real in-process realm starts and stops in a |
| 6 | + * few ms, but under a full-parallel `bun run check` the starved event loop can |
| 7 | + * stretch that setup/teardown past 5s — and then the HOOK, not the test body, |
| 8 | + * times out, landing the failure on whichever test the hook happened to be |
| 9 | + * wrapping (comms-xwqm). 30s gives that contention wide headroom while still |
| 10 | + * failing if a hook genuinely wedges. |
| 11 | + * |
| 12 | + * This is headroom, not a cure-all: the long-poll teardown tests (the |
| 13 | + * gap-replay / scope-close-interrupt cases that drive an infinite server |
| 14 | + * handler and rely on AbortSignal unwinding a forked drain fiber) can still |
| 15 | + * exceed even this under aggressive contention. That residual is a fiber/scope |
| 16 | + * lifecycle problem, not a too-small-number problem, and is tracked for the |
| 17 | + * Effect-Scope-based realm rework (comms-4lz5 / comms-30hq) — running the whole |
| 18 | + * test, realm acquisition included, inside one scope with guaranteed finalizers |
| 19 | + * removes the leftover-fiber-starves-teardown failure mode structurally. |
| 20 | + */ |
| 21 | +export const REALM_HOOK_TIMEOUT_MS = 30_000 |
| 22 | + |
| 23 | +/** |
| 24 | + * Register per-test realm lifecycle on the bun:test hooks — a fresh instance |
| 25 | + * before each test, stopped after — with the contention-proof timeout above. |
| 26 | + * The caller keeps its own directly-referenced binding via `assign`, so |
| 27 | + * existing `realm.` / `fixture.` call sites stay untouched. |
| 28 | + */ |
| 29 | +export const registerRealmHooks = <T extends { readonly stop: () => Promise<void> }>( |
| 30 | + start: () => T, |
| 31 | + assign: (instance: T) => void, |
| 32 | +): void => { |
| 33 | + let instance: T |
| 34 | + beforeEach(() => { |
| 35 | + instance = start() |
| 36 | + assign(instance) |
| 37 | + }, REALM_HOOK_TIMEOUT_MS) |
| 38 | + afterEach(() => instance.stop(), REALM_HOOK_TIMEOUT_MS) |
| 39 | +} |
0 commit comments