Documentation
¶
Overview ¶
Package testshard lets one package's tests run as several test processes, so a package too long for go test's per-binary -timeout as one process fits it as several (iss-2610251004378066).
A package opts in from its TestMain, after any branch that re-executes the test binary as something else and before the set-up its tests need:
if code, ok := testshard.Shard(); ok {
os.Exit(code)
}
Shard does nothing unless the environment asks for shards: ShardsEnv set to a count above one, and go test running the whole package with nothing but the flags Run knows to be safe to repeat (-test.timeout, -test.count, -test.v, -test.short, -test.failfast, -test.paniconexit0, and go test's own -test.testlogfile, which each shard writes apart and Shard merges, so go test's cache reads the inputs of every shard). Then it lists the package's tests, deals them round-robin into that many shares, runs that many copies of its own binary at once, each over one share under -test.run and each with its own -test.timeout, and writes each copy's output whole, in order, once all have ended. The result is a failure when any copy failed. Any other flag (a -test.run of the caller's own, a profile, -json's test2json stream, a fuzz or bench run) runs the package as one process, as without the variable. Each shard runs the TestMain again, as a shard, and so makes the set-up its share needs.
PartEnv narrows a run to one part of the package's tests, for a package too long for one runner (iss-2612021244212147): set to i/k, Shard deals the package's test list round-robin into k parts, the deal it shares out its processes by, and runs only the i-th, as the processes ShardsEnv asks for, or as one. The k runs of a package, one per part, run each of its tests once between them. A part that is not i/k with 1 <= i <= k is refused, exit 2, before any test runs. Where Shard would run the package as one process anyway, for a flag it cannot repeat or a list it cannot read, the part runs the whole package: a part never runs fewer tests than it was dealt.
make preflight exports the variable, and so do CI's race passes and its plain pass on an inert change (ci.yml), with the same count (iss-2610290720087336, iss-2610302058222905). The macOS suite's race step exports the part cmd/ci-shard gives its shard, and only a shard that holds the one package cmd/ci-shard splits, alone, has one. The merged test log names both variables, so go test's cache keys a sharded result to the shard count and the part, and never replays it for a run as one process, which could hide a test that passes only after another in the same process, or for another part.
Index ¶
Constants ¶
const PartEnv = "ABCD_TEST_PART"
PartEnv is the part of a package's tests a run tests, as i/k: the i-th of k parts. Unset or empty, the run tests the whole package.
const ShardsEnv = "ABCD_TEST_SHARDS"
ShardsEnv is the number of test processes a package that opts in runs as.
Variables ¶
This section is empty.
Functions ¶
Types ¶
This section is empty.