xtest

package
v0.17.0 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package xtest provides fatal-by-default test helpers.

Naming convention:

Require*  Fatal. Calls tb.Fatalf and stops the current test (or subtest).
          The default. Use when continuing past a failure would test
          nothing real (failed setup, nil response, blown budget).

Assert*   Soft. Calls tb.Errorf and continues, so one run reports every
          violation. Combine with t.Run subtests. Use only when the
          checks are genuinely independent.

Eventually / Never / WaitFor*   Polling helpers; fail on timeout.
Golden*                         Golden-file helpers.

Parameters of type testing.TB are named tb (not t).

Index

Constants

View Source
const DefaultPollInterval = 2 * time.Millisecond

Variables

View Source
var UpdateGolden = flag.Bool("xtest.update", false, "rewrite golden files")

Functions

func AllocsPerRun

func AllocsPerRun(runs int, fn func()) float64

func Eventually

func Eventually(tb testing.TB, timeout time.Duration, cond func() bool)

func EventuallyE

func EventuallyE(tb testing.TB, timeout time.Duration, fn func() error) error

func EventuallyEvery

func EventuallyEvery(tb testing.TB, timeout, interval time.Duration, cond func() bool)

func EventuallyEveryE

func EventuallyEveryE(tb testing.TB, timeout, interval time.Duration, fn func() error) error

func ExpectFatal

func ExpectFatal(t *testing.T, testName, envVar, wantSubstr string, fn func(t *testing.T))

ExpectFatal isolates subprocess crash testing. It runs fn in a child process and asserts that it fails with wantSubstr in its output. It is used to test test-helpers (verifying that they correctly call t.Fatal).

func GoldenJSON

func GoldenJSON(tb testing.TB, name string, got any)

func GoldenJSONWithUpdate

func GoldenJSONWithUpdate(tb testing.TB, name string, got any, update bool)

func Never

func Never(tb testing.TB, duration time.Duration, cond func() bool)

func RequireAllocsBetween

func RequireAllocsBetween(tb testing.TB, runs int, minimum, maximum float64, fn func())

func RequireGoroutinesAtMost

func RequireGoroutinesAtMost(tb testing.TB, baseline int, timeout time.Duration, allowedDelta int)

RequireGoroutinesAtMost waits for goroutines to settle at or below baseline+allowedDelta. The allowance is useful because the testing runtime and unrelated parallel tests may transiently create goroutines.

func RequireMaxAlloc

func RequireMaxAlloc(tb testing.TB, runs int, limit float64, fn func())

func RequireNoGoroutineLeak

func RequireNoGoroutineLeak(tb testing.TB, baseline int, timeout time.Duration)

RequireNoGoroutineLeak waits until the goroutine count returns to baseline.

func RequireZeroAlloc

func RequireZeroAlloc(tb testing.TB, runs int, fn func())

func RunParallel

func RunParallel(tb testing.TB, count int, fn func(index int) error)

RunParallel starts count workers behind one barrier, waits for all of them, and reports the first worker error after every worker has stopped.

The callback must return an error instead of calling Fatal from a worker; this keeps failure reporting deterministic and guarantees the parent test does not continue while worker goroutines are still running.

func WaitForAtLeast

func WaitForAtLeast(tb testing.TB, counter *atomic.Int32, minimum int32, timeout time.Duration)

func WaitForAtMost

func WaitForAtMost(tb testing.TB, counter *atomic.Int32, maximum int32, timeout time.Duration)

func WaitForSignal

func WaitForSignal(tb testing.TB, signal <-chan struct{}, timeout time.Duration)

WaitForSignal waits for a one-shot signal and fails with a useful timeout.

func WaitForValue

func WaitForValue[T comparable](tb testing.TB, timeout time.Duration, want T, get func() T) T

Types

type Gate

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

Gate provides a concurrent barrier for thundering-herd tests.

func NewGate

func NewGate() *Gate

NewGate returns a new synchronization barrier.

func (*Gate) Active

func (g *Gate) Active() int32

Active returns the number of goroutines that have called Enter.

func (*Gate) Enter

func (g *Gate) Enter()

Enter blocks until Release is called.

func (*Gate) Release

func (g *Gate) Release()

Release unblocks all goroutines waiting in Enter.

type Latch

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

Latch is a one-shot signal for tests. Signal is idempotent and safe for concurrent use; Wait blocks until Signal has been called or the timeout elapses.

func NewLatch

func NewLatch() *Latch

func (*Latch) Signal

func (l *Latch) Signal()

func (*Latch) Wait

func (l *Latch) Wait(tb testing.TB, timeout time.Duration)

Directories

Path Synopsis
Package ktest provides high-level test helpers for the kernel action runtime.
Package ktest provides high-level test helpers for the kernel action runtime.

Jump to

Keyboard shortcuts

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