Public: anyone can read this; the crew can change it.
Two order-dependent flakes made deterministic: tui-crawlers pins loop lag; job-alloc measures a kept buffer after a full collection #16875
Check · Review · Land · Live done — Landed and live in production.
fix/test-order-flakes → main 0 commits
2 files
changed +20
−16
test/job-alloc.test.ts +16 −15
| @@ -12,20 +12,24 @@ const MIB = 1024 * 1024; | ||
| 12 | 12 | /** A stopped clock, so the real heap's read time never spends the tally's budget here. */ |
| 13 | 13 | const { counted, take: takeAllocations } = createTally(undefined, () => 0); |
| 14 | 14 | const keep: unknown[] = []; |
| 15 | /** Grow the JS heap (typed arrays live off it) and keep it, so no collection undoes it. */ | |
| 16 | const grow = (n = 100_000): number => keep.push(Array.from({ length: n }, (_, i) => ({ i, s: `v${i}` }))); | |
| 17 | /** Run `fn` as `name` until the heap was seen to grow across it: a collection can hide one try. */ | |
| 15 | /** Grow the heap by 4 MiB and keep it: an ArrayBuffer is counted in heapSize() at its size. */ | |
| 16 | const grow = (): number => keep.push(new ArrayBuffer(4 * MIB)); | |
| 17 | /** | |
| 18 | * Run `fn` as `name` once, after a full collection. The test files before this one in a shard leave | |
| 19 | * garbage behind; a collection that ran inside the slice reclaimed it and read as the slice | |
| 20 | * shrinking the heap (-42 MiB measured), so small object arrays showed as no growth, or under the | |
| 21 | * 1 MiB a line names. Collected first, there is nothing for one inside the slice to reclaim, and | |
| 22 | * the kept buffer is counted whole. | |
| 23 | */ | |
| 18 | 24 | function grown(c: (name: string, fn: () => unknown) => unknown, name: string, fn: () => unknown = grow): void { |
| 19 | for (let i = 0; i < 40; i++) { | |
| 20 | const before = heapSize(); | |
| 21 | try { | |
| 22 | c(name, fn); | |
| 23 | } catch { | |
| 24 | // the caller's own assertion covers a throw | |
| 25 | } | |
| 26 | if (heapSize() - before > 0) return; | |
| 25 | Bun.gc(true); | |
| 26 | const before = heapSize(); | |
| 27 | try { | |
| 28 | c(name, fn); | |
| 29 | } catch { | |
| 30 | // the caller's own assertion covers a throw | |
| 27 | 31 | } |
| 28 | throw new Error(`the heap never grew across ${name}`); | |
| 32 | if (heapSize() - before <= 0) throw new Error(`the heap did not grow across ${name}`); | |
| 29 | 33 | } |
| 30 | 34 | |
| 31 | 35 | test("a job that keeps what it allocates is named with its size, and the count is emptied by reading it", () => { |
| @@ -107,9 +111,6 @@ afterAll(async () => { | ||
| 107 | 111 | |
| 108 | 112 | test("allocationsOf merges the unowned and this forge's jobs, most first, five at most", () => { |
| 109 | 113 | allocationsOf(app); |
| 110 | // An ArrayBuffer is counted in heapSize() at its size even when a collection runs inside the | |
| 111 | // slice; small object arrays are not (a collection reclaiming old garbage hides them), which | |
| 112 | // made a 20,000-object slice read as no growth on all forty tries in a loaded process. | |
| 113 | 114 | const duringGrown = (name: string, bytes: number, owner?: object) => |
| 114 | 115 | grown((nm, fn) => during(nm, fn as () => number, undefined, owner), name, () => keep.push(new ArrayBuffer(bytes))); |
| 115 | 116 | for (let i = 0; i < 5; i++) duringGrown(`own-${i}`, MIB, app); |
test/tui-crawlers.test.ts +4 −1
| @@ -6,7 +6,7 @@ import { tmpdir } from "node:os"; | ||
| 6 | 6 | import { join } from "node:path"; |
| 7 | 7 | import { createApp, type App } from "../src/app.ts"; |
| 8 | 8 | import { createUser, type User } from "../src/core/users.ts"; |
| 9 | import { setCrawlClock, takeCrawl } from "../src/http/crawl.ts"; | |
| 9 | import { pinCrawlLag, setCrawlClock, takeCrawl } from "../src/http/crawl.ts"; | |
| 10 | 10 | import { createForgeApp, forgeCommands } from "../src/tui/forge.ts"; |
| 11 | 11 | import { crawlersScreen } from "../src/tui/screens/crawlers.ts"; |
| 12 | 12 | import { headlessTerminal } from "../src/tui/term/terminal.ts"; |
| @@ -25,6 +25,9 @@ beforeAll(async () => { | ||
| 25 | 25 | dir = await mkdtemp(join(tmpdir(), "forge-tui-crawlers-")); |
| 26 | 26 | app = createApp({ dataDir: dir, port: 0 }); |
| 27 | 27 | quietWeek(app.db); |
| 28 | // The lag reading is the process's: a busy file before this one in the shard left it behind, the | |
| 29 | // budgets were divided by four, and the screen said "address 7" for 8 deep reads, not 2. | |
| 30 | pinCrawlLag(app, false); | |
| 28 | 31 | const mk = async (username: string, isAdmin: boolean) => { |
| 29 | 32 | const r = await createUser(app.db, { origin: "seed", trusted: true, username, email: `${username}@forge.local`, password: "a-good-passphrase", isAdmin }); |
| 30 | 33 | if (!r.ok) throw new Error(r.error); |
new above
offline