Documentation
¶
Overview ¶
Package backplanetest runs components without Open, Run, ports or Consul: build them under Root, Start, and inspect what they published, answer their hooks, invoke their activities and reactors, and check their readiness. Native workflow tests use the optional workflows/workflowtest package.
h := backplanetest.New(t, backplanetest.Name("hello"))
g, _ := greeter.New(h.Root(), &cfg, deps.Static(db))
h.Start()
backplanetest.Events(h, g.Greeted()) // what g published
Transports are replaced by a recorder: Publish is recorded (FailPublish and Unavailable make it fail), Call is answered by Answer, reactors run through React and activities through Activity, both with the metadata a transport puts on the handler's context (event.DeliveryOf, activity.InfoOf). The optional workflows/workflowtest package builds a Temporal testsuite environment with the same registrations and hook answers.
Index ¶
- Variables
- func Activity[Req, Res any](ctx context.Context, h *Harness, name string, in Req) (Res, error)
- func Answer[Req, Res any](h *Harness, ref hook.Ref[Req, Res], ...)
- func Available(h *Harness)
- func CallKey(ctx context.Context) string
- func Config[C any](tb testing.TB, vars map[string]string) C
- func Events[T any](h *Harness, ref event.Ref[T]) []T
- func FailPublish(h *Harness, err error)
- func LoadConfig[C any](ctx context.Context, vars map[string]string) (C, error)
- func React[T any](ctx context.Context, h *Harness, path, name string, v T, ...) error
- func ReactConsumer[T any](ctx context.Context, h *Harness, consumer string, v T, opts ...DeliveryOption) error
- func Ready(h *Harness) error
- func SetLive[T any](l *config.Live[T], v T)
- func Unavailable(h *Harness)
- type DeliveryOption
- type Harness
- type Option
- type Published
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNotDeclared: no activity or reactor is declared under that name. ErrNotDeclared = errors.New("backplanetest: not declared") // ErrNotReady: the tree's readiness is down (Ready). ErrNotReady = errors.New("backplanetest: not ready") )
Errors.
Functions ¶
func Activity ¶
Activity invokes a declared activity as a binding would, once: the handler's ctx carries activity.InfoOf with Attempt 1, a Key unique to this call ("backplanetest/<name>/<uuid>/1") and ctx's deadline; Heartbeat does nothing.
func Answer ¶
func Answer[Req, Res any](h *Harness, ref hook.Ref[Req, Res], fn func(ctx context.Context, in Req) (Res, error))
Answer makes fn answer ref's calls, from Call and, in Workflows, from workflows.CallHook; unanswered hooks fail with hook.ErrUnavailable as in production without a transport (workflows.CallHook: hook.ErrNoBinding).
func CallKey ¶
CallKey is the hook.Key of the call an Answer function serves; "" for a call without one.
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 FailPublish ¶
FailPublish makes every Publish fail with err (wrapped as the transport wraps its errors) until FailPublish(h, nil); nothing is recorded meanwhile.
func LoadConfig ¶
LoadConfig is Config returning the error: to test what the configuration rejects.
func React ¶
func React[T any](ctx context.Context, h *Harness, path, name string, v T, opts ...DeliveryOption) error
React delivers v to the reactor declared for the event name (full, "iam.UserRegistered") on the scope at path ("" for the root), as the broker would: once, with the reactor's event.Timeout and a full event.Delivery on ctx (a new ID, Attempt 1, the reactor's Consumer, extensions instance and version; opts change them). A reactor pinned with event.Consumer is found by its event when it is the only pinned reactor of it; else use ReactConsumer. The error is the handler's; nothing is redelivered or dead-lettered.
func ReactConsumer ¶
func ReactConsumer[T any](ctx context.Context, h *Harness, consumer string, v T, opts ...DeliveryOption) error
ReactConsumer delivers v to the reactor whose consumer is consumer (as the manifest's subscriptions list it, pinned or derived), as React does.
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 Unavailable ¶
func Unavailable(h *Harness)
Unavailable takes both transports away, as while NATS and Temporal are down: Publish fails with event.ErrUnavailable, Call with hook.ErrUnavailable (in Workflows, a workflows.CallHook finds no binding), until Available.
Types ¶
type DeliveryOption ¶
DeliveryOption sets what event.DeliveryOf reports to a reactor run by React or ReactConsumer.
func WithAttempt ¶
func WithAttempt(n int) DeliveryOption
WithAttempt is the delivery's attempt (Delivery.Attempt), 1 by default.
func WithHeader ¶
func WithHeader(name, value string) DeliveryOption
WithHeader adds an extension (Delivery.Extensions) as event.Header sets it.
func WithID ¶
func WithID(id string) DeliveryOption
WithID is the event's id (Delivery.ID), by default a new UUID: deliver the same id twice to test idempotency.
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) Manifest ¶
func (h *Harness) Manifest() *backplanev1.Manifest
Manifest is what the components declared so far.
type Option ¶
type Option func(o *options)
Option configures New.
func Name ¶
Name is the service's name (default "test"): events are published as <name>.<Event>, hooks called as <name>.<Name>, and a reactor to the service's own event subscribes to "<name>.<Event>". It panics on a name that is not a service name ([a-z][a-z0-9-]*).
func StopBudget ¶
StopBudget bounds the stop at test cleanup (default 10s): a component whose goroutines outlive it fails the test.
type Published ¶
type Published[T any] struct { Value T // Key is the event.Key it was published with ("" without). Key string // ID is event.ID, else the new UUID the transport would assign. ID string // Time is event.Time, else the publish time. Time time.Time // Headers are the event.Header values by name. Headers map[string]string }
Published is one recorded event with what Publish set on it.