processtest

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2026 License: Apache-2.0 Imports: 15 Imported by: 2

Documentation

Overview

Package processtest is the integration toolkit for a program built on the process package. It runs the program as the binary and drives it only through the interfaces a terminal or an orchestrator uses: its captured output, the signals process.SignalContext answers, and its exit code. Nothing in the runtime exists for the tests' sake.

The package exports:

Index

Constants

View Source
const Failsafe = 15 * time.Second

Failsafe bounds every wait for an event that should occur, so a broken composition fails a test instead of hanging the run.

Variables

This section is empty.

Functions

func FreePort

func FreePort(t testing.TB) int

FreePort reserves an ephemeral loopback port and releases it for the process to bind. The window between release and bind is the usual one of a port-based harness; a lost race fails the bind, and Await reports the exit with the output.

func Main

func Main(m *testing.M, pkg string)

Main is the suite's TestMain: it builds the main package pkg once, with the race detector so the program runs under it too, runs the tests, and removes the build. pkg is a package path relative to the module root, such as ./cmd/server. A build failure ends the run before any test starts.

func WaitFor

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

WaitFor polls fn until it returns true, failing the test with what if Failsafe elapses first. It is Await for a condition no single process owns.

Types

type Cmd added in v0.6.0

type Cmd struct {
	// Args are the program's arguments, without the program name.
	Args []string
	// Stdin is the program's standard input; nil is empty.
	Stdin io.Reader
	// Env holds KEY=VALUE overrides added to the parent's environment.
	Env []string
}

Cmd is one invocation of the program for Run. Its zero value runs the program with no arguments, an empty standard input, and the parent's environment.

type Forwarder

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

Forwarder is a loopback TCP relay between the process and a backing service, the connection a test severs to inject an outage.

func Forward

func Forward(t testing.TB, target string) *Forwarder

Forward starts relaying to target, host:port, on an ephemeral loopback port; the relay is severed at test cleanup.

func (*Forwarder) Addr

func (f *Forwarder) Addr() string

Addr is the address the process connects to, host:port. It is stable across Sever and Restore.

func (*Forwarder) Restore

func (f *Forwarder) Restore(t testing.TB)

Restore listens again on the forwarder's address. It is a no-op while the forwarder is listening.

func (*Forwarder) Sever

func (f *Forwarder) Sever()

Sever refuses new connections and drops the relayed ones; a relay still dialing the target finishes within Failsafe.

type Process

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

Process is one running program: its captured output and its exit.

func Launch

func Launch(t testing.TB, env ...string) *Process

Launch starts the binary Main built in the module root, with the parent's environment plus env's KEY=VALUE overrides, and returns without waiting. At test cleanup, a process still running is interrupted, then killed after Failsafe.

func (*Process) Await

func (p *Process) Await(t testing.TB, what string, cond func() bool)

Await polls cond until it holds, failing the test with the captured output if the process exits or Failsafe elapses first. A harness waits for readiness this way: on the condition a client observes, never on the output.

func (*Process) Exited

func (p *Process) Exited() bool

Exited reports whether the process has ended.

func (*Process) Output

func (p *Process) Output() string

Output is everything the process has written so far, stdout and stderr interleaved as they arrived.

func (*Process) Stop

func (p *Process) Stop(t testing.TB) int

Stop interrupts the process, the signal a terminal or an orchestrator sends, waits for it to exit, and returns its exit code. A process still running after Failsafe is killed, and the test fails.

func (*Process) Wait

func (p *Process) Wait(t testing.TB) int

Wait blocks until the process exits and returns its exit code, failing the test if Failsafe elapses first.

type Result added in v0.6.0

type Result struct {
	// Stdout and Stderr are the program's two output streams, each
	// captured whole and apart from the other.
	Stdout, Stderr string
	// Code is the program's exit code.
	Code int
}

Result is what one run of the program produced.

func Run added in v0.6.0

func Run(t testing.TB, cmd Cmd) Result

Run executes the program Main built, once, in the module root, with the environment Launch gives it plus cmd.Env; it waits for the program to exit and returns its output and exit code. A nonzero code is a result, not a failure. A run still going after Failsafe is a stall: it is interrupted, killed if it has not exited within Failsafe more, and fails the test with its output. Run logs the run through t.Log as a shell line a reader can paste, with the output after it.

Directories

Path Synopsis
internal
testprog command
Command testprog is the program the processtest package's own tests run: it answers on a loopback address, waits for the interrupt, and exits with the code its environment names, so the runner is proved against a real subprocess without a service behind it.
Command testprog is the program the processtest package's own tests run: it answers on a loopback address, waits for the interrupt, and exits with the code its environment names, so the runner is proved against a real subprocess without a service behind it.

Jump to

Keyboard shortcuts

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