testutil

package
v0.7.12 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2026 License: Apache-2.0 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 testutil holds helpers for tests that wait for something asynchronous.

It is for test files only. A production package must not import it. The test TestNoProductionPackageImportsTestutil enforces that.

Why it exists: a fixed wait such as "poll for 2 s" fails on a slow CI runner (2 cores, -race). WaitFor uses a long safety bound instead. A passing test still ends as soon as the condition holds, so the long bound costs nothing.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GoListDeps added in v0.7.11

func GoListDeps(t testing.TB, pkgs ...string) string

GoListDeps runs `go list -deps <pkgs>` and returns one import path per line.

It removes -json from GOFLAGS. The coverage step in .github/workflows/ci.yaml (line ~116), which heavy-ci.sh dispatches, sets GOFLAGS=-json to time each test. With that flag, `go list` prints JSON instead of import paths, and a dependency guard that reads the output proves nothing. The other GOFLAGS (for example -tags) stay.

func RequireTiming added in v0.7.8

func RequireTiming(t testing.TB)

RequireTiming skips the test unless CG_TIMING_TESTS=1.

Use it in a test whose result depends on the wall clock or on machine load: it asserts an elapsed time or a latency budget, or it races a rate limit or a timeout. Such a test fails on a busy machine with nothing wrong in the code. The integrator's checks (heavy CI, Part A) set the variable. scripts/dev/local-tests.sh sets it only for a PR that adds or changes a timing test. Do not use it for a test that only uses a fake clock, or a long timeout as a safety net.

A test that checks more than time can guard only its time check with TimingEnabled.

func SetBoundForTest

func SetBoundForTest(d time.Duration) (restore func())

SetBoundForTest sets the safety bound and returns a func that restores the old one. Only the tests of this package need it. The bound is process-wide: no test in this package may call t.Parallel.

func TimingEnabled added in v0.7.8

func TimingEnabled() bool

TimingEnabled reports whether the timing tests are on: CG_TIMING_TESTS=1.

func WaitFor

func WaitFor(t testing.TB, what string, cond func() bool)

WaitFor polls cond until it returns true. If it does not within the safety bound, WaitFor calls t.Fatalf with what and the elapsed time.

func WaitForState

func WaitForState(t testing.TB, what string, cond func() bool, state func() string)

WaitForState is WaitFor with a func that describes the state the test saw last. The failure message shows it, so a reader sees why the condition did not hold. state may be nil.

Types

type Call

type Call struct {
	Path   string
	Header http.Header
	Body   []byte
}

Call is one request a fake upstream received.

type Upstream

type Upstream struct {
	*httptest.Server
	// contains filtered or unexported fields
}

Upstream is a fake HTTP upstream. It records each request and answers it with the handler the test gave. A test uses WaitForCall to wait for a request that a background goroutine sends, in place of a check right after the client call returns.

func NewUpstream

func NewUpstream(t testing.TB, respond func(w http.ResponseWriter, r *http.Request)) *Upstream

NewUpstream starts a fake upstream. respond writes the reply; the body is already read and recorded. A nil respond replies 200 with the empty JSON object {}. The server closes when the test ends.

func (*Upstream) Calls

func (u *Upstream) Calls() []Call

Calls returns a copy of the requests received so far.

func (*Upstream) Matching

func (u *Upstream) Matching(match func(Call) bool) []Call

Matching returns the received requests for which match is true.

func (*Upstream) WaitForCall

func (u *Upstream) WaitForCall(t testing.TB, what string, n int, match func(Call) bool) []Call

WaitForCall waits until at least n received requests satisfy match. On timeout it fails with what and the number of requests seen, and returns the matching requests when it succeeds.

Directories

Path Synopsis
Package testdb opens SQLite files for tests the way production does.
Package testdb opens SQLite files for tests the way production does.

Jump to

Keyboard shortcuts

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