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 RequireTiming ¶ added in v0.7.8
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 ¶
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.
Types ¶
type Upstream ¶
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 ¶
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) WaitForCall ¶
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.