Help us shape the future of Go! Take the 2026 Go Developer Survey

testutil

package
v0.81.0 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package testutil holds helpers for tests that must prove a concurrent interaction actually happened, rather than passing because the scheduler happened to keep the two sides apart (#658).

The rule these helpers encode: a concurrency test witnesses BOTH sides with positive counters, runs until every witness holds or a deadline fails it, and prints every counter in its failure message. A test that counts only one side, or stops when one side finishes, can pass vacuously on a broken implementation — which is exactly what the original TestParallelBranchSymlinkRace did.

SkipUnlessPermissionsEnforced and PermissionsEnforced serve another kind of test: one that needs the OS to refuse access because of file mode bits.

Lives in internal/ so it is not part of the public API surface.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Eventually

func Eventually(t testing.TB, deadline, poll time.Duration, cond func() bool, msg string)

Eventually polls cond every poll until it returns true. If deadline elapses first the test fails with msg and the deadline, so the record says WHAT never happened rather than just that something timed out.

func Overlap

func Overlap(t testing.TB, deadline time.Duration, step func() bool) (witnessed bool, iterations int)

Overlap calls step repeatedly until it returns true (every witness the test needs has been observed) or deadline elapses. It reports whether the overlap was witnessed and how many steps ran.

The caller decides what step does — typically one attempt of the racing operation plus a check of the counters — and what to do on a false return (fail with every counter printed). Overlap itself never fails the test: a deadline is a verdict for the caller to render with its own diagnostics, not a bare timeout.

func PermissionsEnforced added in v0.80.1

func PermissionsEnforced(t testing.TB) bool

PermissionsEnforced reports whether the OS refuses this process access that file mode bits deny. Root, a process holding CAP_DAC_OVERRIDE or CAP_DAC_READ_SEARCH, or a filesystem that ignores mode bits lets a write into a 0500 directory or a lookup in a 0600 one through, so a test that expects EACCES from a chmod-ed path fails there although the code under test is fine. A probe that cannot run fails the test instead of guessing.

func SkipUnlessPermissionsEnforced added in v0.80.1

func SkipUnlessPermissionsEnforced(t testing.TB)

SkipUnlessPermissionsEnforced skips the test when PermissionsEnforced reports false. Call it at the start of a test, or of the subtest, whose assertion needs the OS to refuse access because of file mode bits.

Types

type Witness

type Witness struct {
	// contains filtered or unexported fields
}

Witness tracks named counters ("sides" of an interaction). Declared names print first, in declaration order, so a failure message reads the same way every time; sides hit without being declared are appended.

func NewWitness

func NewWitness(names ...string) *Witness

NewWitness declares the sides the test intends to observe. Every declared side is printed by String even while its count is zero.

func (*Witness) Add

func (w *Witness) Add(name string, n int64)

Add records n observations of side name. Safe for concurrent use.

func (*Witness) Count

func (w *Witness) Count(name string) int64

Count returns how many times side name was observed (0 for an unknown side).

func (*Witness) Hit

func (w *Witness) Hit(name string)

Hit records one observation of side name.

func (*Witness) Require

func (w *Witness) Require(t testing.TB, min int64, sides ...string)

Require fails the test (one Fatalf) unless every named side was observed at least min times. The message names each side that fell short, its count, the minimum, and the full counter line.

func (*Witness) String

func (w *Witness) String() string

String renders every side as "name=count", space-separated, declared sides first. Use it in every failure message so the record shows all counters, not just the one that tripped.

Jump to

Keyboard shortcuts

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