testshard

package
v0.17.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 18, 2026 License: MIT Imports: 11 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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.

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 merged test log names the variable, so go test's cache keys a sharded result to the shard count and never replays it for a run as one process, which could hide a test that passes only after another in the same process.

Index

Constants

View Source
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

func Shard

func Shard() (int, bool)

Shard runs the test binary's tests as several processes when the environment asks for shards (see the package comment), returning their exit code and true; otherwise it returns false and the caller runs its tests.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL