Public: anyone can read this; the crew can change it.

Checks run rule and scan tests first and stop on their red (a rule red in ~2 min, not 14) #16866

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

rules-first → main 0 commits

  • Merged. rules-first was merged into main as 372ce5e.

Review

Sign in to review this change.
3 files changed +120 −10
scripts/shards.ts +11
@@ -174,6 +174,17 @@ export function splitFiles(
174174 */
175175 export const FILES_PER_PROCESS = 40;
176176
177 /**
178 * The rule and scan tests of a run, apart from the rest: `first` the files `rules` names
179 * (repository invariants that read the source: seconds each, yet any diff can break one), `rest`
180 * the behaviour tests, each in the order given. A run that does not keep going runs `first` on its
181 * own and stops on its red, so a rule red is read in a minute or two, not after every shard.
182 */
183 export function rulesFirst(files: readonly string[], rules: ReadonlySet<string>): { first: string[]; rest: string[] } {
184 const isRule = (file: string): boolean => rules.has(file.replace(/^\.\//, ""));
185 return { first: files.filter(isRule), rest: files.filter((file) => !isRule(file)) };
186 }
187
177188 /**
178189 * {@link splitFiles}, with no slice over `max` files: enough slices that
179190 * every one fits, balanced by weight, and any still over (many light files
scripts/test-shards.ts +27 −10
@@ -23,7 +23,10 @@
2323 * shards that run side by side are written through whole as each
2424 * finishes, so the log reads one shard at a time. Either way the tail
2525 * names every failing test from every shard: every shard runs to the end,
26 * whatever goes red, so one red never hides another (#7788). A shard that
26 * whatever goes red, so one red never hides another (#7788). One exception:
27 * the rule and scan tests (`RULE_TESTS`, `PAGE_BUDGET_TESTS`, the tree scans)
28 * run first, on their own, and unless the run keeps going their red ends it
29 * before a behaviour test starts, the tally naming it as ever. A shard that
2730 * crashed outside any test (an unhandled error between tests, a file that
2831 * would not load, a process that died before its summary) is re-run once,
2932 * whole, and judged by that run; the log keeps the crashed run quoted
@@ -105,6 +108,7 @@ import {
105108 crashedOutside,
106109 quoteCrash,
107110 CRASH_RERUNS,
111 rulesFirst,
108112 type ShardResult,
109113 } from "./shards.ts";
110114 import { gateFiles, gateRefusal, looksTests, presentTests, SLOW, slowRefusal } from "./gate.ts";
@@ -119,7 +123,7 @@ import { changedLine, startChanged } from "./changed-pick.ts";
119123 import { ownTests, skipReport } from "../src/checks/own_skips.ts";
120124 import { docReadersIn, readersIn } from "./script-readers.ts";
121125 import { shardHome, spaceLine } from "../src/checks/disk_full.ts";
122 import { blindLine, fastSet, duration, reasonLine } from "../src/checks/tiers.ts";
126 import { blindLine, fastSet, duration, PAGE_BUDGET_TESTS, reasonLine, RULE_TESTS } from "../src/checks/tiers.ts";
123127 import { CHECK_CACHE } from "../src/checks/cache.ts";
124128 import { parseCheck } from "../src/apps/manifest.ts";
125129 import { FLAKY_ALONE, onlyFiles } from "../src/checks/verify.ts";
@@ -562,7 +566,15 @@ const slicesOf = (list: string[]): string[][] =>
562566 : perFile
563567 ? inOrder(list).map((file) => [file])
564568 : splitCapped(list, count.value, weight);
565 const slices = slicesOf(files);
569 // The rule and scan tests run first, on their own: a run that does not keep going stops on
570 // their red before a behaviour test starts. Recording, measuring and a run kept going (the
571 // verification) run every file as before.
572 const phased = !record && !measure && !keepGoing;
573 const { first: ruleFiles, rest: behaviourFiles } = phased
574 ? rulesFirst(files, new Set([...RULE_TESTS, ...PAGE_BUDGET_TESTS, ...scansIn(files, importsReader()).keys()]))
575 : { first: [], rest: files };
576 const ruleSlices = slicesOf(ruleFiles);
577 const slices = [...ruleSlices, ...slicesOf(behaviourFiles)];
566578 const work = files.reduce((sum, file) => sum + weight(file), 0);
567579
568580 const banner = record
@@ -579,6 +591,8 @@ const banner = record
579591 `expected ${slices.map((slice) => slice.reduce((sum, file) => sum + weight(file), 0).toFixed(0) + "s").join(" ")}`;
580592 console.log(banner);
581593 process.stderr.write(banner + "\n");
594 if (ruleFiles.length > 0)
595 console.log(`test-shards: ${ruleFiles.length} rule and scan files first, in ${ruleSlices.length} shard${ruleSlices.length === 1 ? "" : "s"}; their red ends the run`);
582596
583597 const children = new Set<Bun.Subprocess>();
584598 for (const signal of ["SIGINT", "SIGTERM"] as const)
@@ -1077,14 +1091,17 @@ const started = performance.now();
10771091 const busy: Busy[] = [];
10781092 // Serial one-file processes when SHARDS=1; record keeps its worker width;
10791093 // multi-shard still runs every slice side by side.
1094 // Never more side by side than the count: capped slices outnumber it.
1095 const width = (n: number): number => (serialFiles ? 1 : Math.max(1, Math.min(count.value, n)));
1096 const ruleResults = await pool(width(ruleSlices.length), 0, ruleSlices.length, busy);
1097 const ruleRed = ruleResults.some((result) => result.code !== 0);
1098 if (ruleRed)
1099 console.log(
1100 `test-shards: a rule or scan test is red, so the ${behaviourFiles.length} behaviour files were not run; fix it and push again`,
1101 );
10801102 const results = [
1081 ...(await pool(
1082 // Never more side by side than the count: capped slices outnumber it.
1083 serialFiles ? 1 : Math.max(1, Math.min(count.value, slices.length)),
1084 0,
1085 slices.length,
1086 busy,
1087 )),
1103 ...ruleResults,
1104 ...(ruleRed ? [] : await pool(width(slices.length - ruleSlices.length), ruleSlices.length, slices.length, busy)),
10881105 // Budget files, one at a time, once the rest are done and green.
10891106 ...(await pool(1, slices.length, slices.length)),
10901107 ];
test/check-rules-first.test.ts added +82
@@ -0,0 +1,82 @@
1 /**
2 * The rule and scan tests run first, and a run that does not keep going stops on their red before a
3 * behaviour test starts: a rule red is read in a minute or two, not after every shard.
4 */
5 import { describe, expect, test } from "bun:test";
6 import { existsSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
7 import { tmpdir } from "node:os";
8 import { join } from "node:path";
9 import { rulesFirst } from "../scripts/shards.ts";
10
11 describe("rulesFirst", () => {
12 test("splits the named rule files from the rest, each in the order given, with or without ./", () => {
13 const rules = new Set(["test/flake.test.ts", "test/hollow.test.ts"]);
14 expect(rulesFirst(["test/a.test.ts", "./test/hollow.test.ts", "test/b.test.ts", "test/flake.test.ts"], rules)).toEqual({
15 first: ["./test/hollow.test.ts", "test/flake.test.ts"],
16 rest: ["test/a.test.ts", "test/b.test.ts"],
17 });
18 expect(rulesFirst(["test/a.test.ts"], rules)).toEqual({ first: [], rest: ["test/a.test.ts"] });
19 });
20 });
21
22 describe("test-shards runs the rule tests first", () => {
23 const script = new URL("../scripts/test-shards.ts", import.meta.url).pathname;
24
25 /**
26 * A tree with one rule test (`test/flake.test.ts`, a RULE_TESTS name) and one behaviour test that
27 * leaves a mark when it runs.
28 */
29 const tree = (ruleGreen: boolean): string => {
30 const dir = mkdtempSync(join(tmpdir(), "shards-rules-"));
31 mkdirSync(join(dir, "test"));
32 writeFileSync(
33 join(dir, "test/flake.test.ts"),
34 `import { expect, test } from "bun:test";\ntest("a rule", () => { expect(${ruleGreen}).toBe(true); });\n`,
35 );
36 writeFileSync(
37 join(dir, "test/behaviour.test.ts"),
38 `import { test } from "bun:test";\nimport { writeFileSync } from "node:fs";\ntest("a behaviour", () => { writeFileSync("ran", ""); });\n`,
39 );
40 return dir;
41 };
42 const run = async (dir: string): Promise<{ code: number; out: string }> => {
43 // A bisection's list from the ambient environment would narrow the run under test.
44 const { FORGE_CHECK_ONLY: _, ...ambient } = process.env;
45 const child = Bun.spawn(["bun", script, "test/behaviour.test.ts", "test/flake.test.ts"], {
46 cwd: dir,
47 env: { ...ambient, FORGE_CHECK_SHARDS: "2", FORGE_CHECK_TIMES: "" },
48 stdin: "ignore",
49 stdout: "pipe",
50 stderr: "pipe",
51 });
52 const [out] = await Promise.all([new Response(child.stdout).text(), new Response(child.stderr).text()]);
53 return { code: await child.exited, out };
54 };
55
56 test("a rule red ends the run before the behaviour shards start, and the tally names it as today", async () => {
57 const dir = tree(false);
58 try {
59 const { code, out } = await run(dir);
60 expect(code).not.toBe(0);
61 expect(out).toContain("1 rule and scan files first");
62 expect(out).toContain("a rule or scan test is red, so the 1 behaviour files were not run");
63 expect(out).toMatch(/\(fail\) a rule/);
64 // The behaviour test never started.
65 expect(existsSync(join(dir, "ran"))).toBe(false);
66 } finally {
67 rmSync(dir, { recursive: true, force: true });
68 }
69 });
70
71 test("a green rule phase runs every behaviour file after it", async () => {
72 const dir = tree(true);
73 try {
74 const { code, out } = await run(dir);
75 expect(code).toBe(0);
76 expect(out.indexOf("rule and scan files first")).toBeGreaterThan(-1);
77 expect(existsSync(join(dir, "ran"))).toBe(true);
78 } finally {
79 rmSync(dir, { recursive: true, force: true });
80 }
81 });
82 });

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs