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 ¶
- Variables
- func Caller(ctx context.Context, caller backplane.Caller) context.Context
- func Config[C any](tb testing.TB, vars map[string]string) C
- func Find(h *Harness, name string) deps.Component
- func Live(h *Harness) error
- func LoadConfig[C any](ctx context.Context, vars map[string]string) (C, error)
- func Operator(ctx context.Context, session uuid.UUID) context.Context
- func Ready(h *Harness) error
- func SetLive[T any](l *config.Live[T], v T) error
- func Stop(h *Harness, scope deps.Scope)
- type Harness
- type Option
Constants ¶
This section is empty.
Variables ¶
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
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 ¶
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
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
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 ¶
LoadConfig is Config returning the error: to test what the configuration rejects.
func Operator ¶ added in v0.6.25
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 ¶
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 ¶
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
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 (*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 StartBudget ¶ added in v0.6.103
StartBudget bounds Start (default 2m): a required dependency not provided by then fails the test with its name and last error.
func StopBudget ¶
StopBudget bounds the stop at test cleanup (default 10s): a component whose goroutines outlive it fails the test.