backplanetest

package
v0.7.1 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2026 License: MIT Imports: 20 Imported by: 1 module, 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 backplanetest starts and stops a service's component tree without listeners or Consul. Use Root to assemble components, Config to load isolated settings, and Ready or Live to inspect health.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotReady: the tree's readiness is down (Ready).
	ErrNotReady = errors.New("backplanetest: not ready")
	// ErrNotLive: the tree's liveness is down (Live).
	ErrNotLive = errors.New("backplanetest: not live")
)

Errors.

Functions

func Caller added in v0.6.29

func Caller(ctx context.Context, caller backplane.Caller) context.Context

Caller is ctx as a request from caller over mutual TLS carries it: backplane.CallerOf reports caller, so a test calls a handler served with api.MTLS directly, or over bufconn with Caller in a server interceptor.

ctx := backplanetest.Caller(t.Context(), backplane.Caller{Service: "sync"})
_, err := rooms.Join(ctx, req) // authorizes caller sync

func Config

func Config[C any](tb testing.TB, vars map[string]string) C

Config loads a configuration of type C (a section such as greeter.Config or a whole service Config) as the service would, from the schema defaults and vars only: no file, no process environment, no Consul. vars are named as in the service's environment without its prefix: {"SUFFIX": "?"} for a section, {"GREETER_SUFFIX": "?"} for the whole Config; a name the schema does not know fails. Every Validate of C and its sections runs, as Open runs them. A failure fails the test.

func Find added in v0.6.19

func Find(h *Harness, name string) deps.Component

Find is the node of h's tree named name — its Name, or its Path under the root ("sync/fanout") — as a scope Stop takes: a test reaches a component the service does not export. It fails the test when no node or more than one matches (the failure lists the paths).

func Live added in v0.6.84

func Live(h *Harness) error

Live evaluates the tree's liveness as the service's liveness probe does (the same composite, bounded like Ready): nil while every component and dependency can still recover; otherwise an error matching ErrNotLive with the failing probes' reasons, e.g. a dependency whose probe reported deps.ErrTerminal (a NATS connection closed for good). A dependency that is only down (reconnecting) fails Ready, not Live. Dependencies learn they are terminal from their probes, which the service runs every interval: Live runs them first too.

func LoadConfig

func LoadConfig[C any](ctx context.Context, vars map[string]string) (C, error)

LoadConfig is Config returning the error: to test what the configuration rejects.

func Operator added in v0.6.25

func Operator(ctx context.Context, session uuid.UUID) context.Context

Operator is ctx as a call the console relays for session carries it: backplane.OperatorOf reports that session, so a test calls an internal API handler directly as an operator would.

ctx := backplanetest.Operator(t.Context(), session)
_, err := admin.CloseRoom(ctx, req) // audits actor console:<session>

func Ready

func Ready(h *Harness) error

Ready evaluates the tree's readiness as the service's readiness probe does: every required dependency provided and passing its probe. nil when ready; otherwise an error matching ErrNotReady that names each dependency that is not and why. Optional dependencies never count.

func SetLive

func SetLive[T any](l *config.Live[T], v T) error

SetLive changes a Live field as the console would: its appliers (Live.Apply) run first and their error is returned with the field left as it was; otherwise the value is published and watchers fire.

func Stop added in v0.6.17

func Stop(h *Harness, scope deps.Scope)

Stop stops one component's subtree now — a projection, a consumer, a worker killed in the middle of a test — while the rest of the tree runs: its goroutines are cancelled (their context ends wherever they are) and waited for, its children and then its own OnStop hooks run, within the StopBudget. A failure fails the test. It stays stopped: what it served is gone for the components that use it, as in a crash; the tree's cleanup skips it. scope must be a component created under h.Root.

Types

type Harness

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

Harness is one service's tree, started by Start and stopped at cleanup.

func New

func New(tb testing.TB, opts ...Option) *Harness

New creates a harness for a service named "test", or as Name says.

func (*Harness) Root

func (h *Harness) Root() deps.Component

Root is where components and dependencies under test are created.

func (*Harness) Service

func (h *Harness) Service() string

Service is the harness's service name.

func (*Harness) Start

func (h *Harness) Start()

Start starts the tree (dependencies are provided, goroutines run) within the StartBudget and stops it at test cleanup within the StopBudget; a failure fails the test, a start out of budget naming each required dependency not provided and its last error.

func (*Harness) Stop added in v0.6.17

func (h *Harness) Stop()

Stop stops the whole tree now, as the cleanup would: goroutines are cancelled and waited for and OnStop hooks run in reverse start order, within the StopBudget; a failure fails the test. The cleanup then has nothing left to stop. A restarted instance is a new Harness.

type Option

type Option func(o *options)

Option configures New.

func Name

func Name(service string) Option

Name sets the service name (default "test"); it must match [a-z][a-z0-9-]*.

func StartBudget added in v0.6.103

func StartBudget(d time.Duration) Option

StartBudget bounds Start (default 2m): a required dependency not provided by then fails the test with its name and last error.

func StopBudget

func StopBudget(d time.Duration) Option

StopBudget bounds the stop at test cleanup (default 10s): a component whose goroutines outlive it fails the test.

Jump to

Keyboard shortcuts

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