testutil

package
v0.7.5 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2026 License: Apache-2.0 Imports: 8 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 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 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