testutil

package
v1.17.1 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2026 License: MIT Imports: 9 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 shared by tests across packages.

Index

Constants

View Source
const StubBaseURL = "http://stub.invalid"

StubBaseURL is the BaseURL to pin on a client whose transport is stubbed: it must parse, and it must never resolve.

Variables

This section is empty.

Functions

func CaptureOutput

func CaptureOutput(t *testing.T, fn func()) (stdout string, stderr string)

CaptureOutput returns what fn writes to os.Stdout and os.Stderr.

func CaptureStdout

func CaptureStdout(t *testing.T, fn func()) string

CaptureStdout returns what fn writes to os.Stdout.

func NewNoRequestServer added in v1.16.0

func NewNoRequestServer(t *testing.T) *httptest.Server

NewNoRequestServer returns a server that fails the test on any request, for asserting a call was refused before anything was sent.

func StubClient added in v1.12.0

func StubClient(fn func(*http.Request) (status int, body string)) *http.Client

StubClient returns an http.Client that answers from fn.

Types

type PollRecorder added in v1.12.0

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

PollRecorder collects the time of each poll a stubbed server sees, so a test can assert the schedule that produced them rather than the poll count alone. A count is a weak witness: a widened schedule and a fixed tick can land on the same number. Under testing/synctest the times come off the bubble's fake clock, which makes the gaps exact.

func (*PollRecorder) Record added in v1.12.0

func (r *PollRecorder) Record()

Record marks one poll as having arrived now. Safe to call from a handler goroutine.

func (*PollRecorder) WidestGap added in v1.12.0

func (r *PollRecorder) WidestGap() time.Duration

WidestGap returns the longest interval between consecutive polls, or 0 when fewer than two arrived. Only a schedule that widens with age can produce a gap above its own base tick.

type StubTransport added in v1.12.0

type StubTransport struct {
	Respond func(*http.Request) (status int, body string)
}

StubTransport answers every request from fn, with no socket and no server goroutine. A poll loop under testing/synctest needs that: the fake clock only advances while every goroutine in the bubble is durably blocked, and a real network read never counts as durably blocked.

func (StubTransport) RoundTrip added in v1.12.0

func (t StubTransport) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip implements http.RoundTripper.

Jump to

Keyboard shortcuts

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