workflowtest

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2026 License: MIT Imports: 14 Imported by: ? 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 workflowtest tests durable workflows by faking starts with Fake, driving runs step by step with Start and Run, and verifying custom workflow stores with StoreContract.

Install a fake on a test client with New to record workflow starts without persisting or executing them:

func TestRegisteringStartsOnboarding(t *testing.T) {
	client := laraveltest.New(t, bootstrap.Options())
	workflows := workflowtest.New(client)

	client.Post("/register", form).AssertRedirect("/dashboard")

	workflows.AssertStarted(func(w workflows.OnboardUser) bool { return w.UserID == 1 })
}

To test a workflow's own steps, sleeps, and tokens, start it with Start and advance it with Run.Advance or Run.Resume:

func TestOnboardingCompletesAfterDelay(t *testing.T) {
	client := laraveltest.New(t, bootstrap.Options(), laraveltest.RefreshDatabase)
	run := workflowtest.Start(client, workflows.OnboardUser{UserID: user.ID})
	run.AssertSleeping()

	client.Travel(3 * 24 * time.Hour)
	run.Advance().AssertCompleted()
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func StoreContract

func StoreContract(t testing.TB, ctx context.Context, store workflow.Store)

StoreContract verifies that store implements the full workflow.Store contract: exclusive run leases, token ownership by one unfinished run, resumes that reach only the token's owner, ordered inbox numbering, unique keys for active runs, filtering, retries, cancellation, pruning, and purging sensitive run data. Pass an empty store to test a custom implementation:

func TestDynamoStore(t *testing.T) {
	workflowtest.StoreContract(t, t.Context(), dynamo.NewStore(testTable(t)))
}

Every store method receives ctx, such as a test client context bound to a test database.

Types

type Fake

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

Fake records workflow runs started by an application instead of persisting and executing them. Create and install one with New. Assertions report failures on the test associated with the client passed to New. Fake is safe for concurrent use.

func New

func New(client interface{ Context() context.Context }, with ...Option) *Fake

New installs a Fake on the application that client tests and returns it. Inside a database transaction, a workflow start is recorded only after the transaction commits.

Assertions on the returned fake report failures to the client's test. New fails the test if a workflow fake is already installed, and panics if client.Context does not carry a test client.

func (*Fake) AssertNotStarted

func (f *Fake) AssertNotStarted[W any](match ...func(W) bool)

AssertNotStarted fails the test if any workflow of type W was started. When match functions are provided, it fails only if a started run of type W satisfies every match function.

func (*Fake) AssertNothingStarted

func (f *Fake) AssertNothingStarted()

AssertNothingStarted fails the test if any workflow was started.

func (*Fake) AssertStarted

func (f *Fake) AssertStarted[W any](match ...func(W) bool)

AssertStarted fails the test unless a workflow of type W was started. When match functions are provided, at least one started run of type W must satisfy every match function:

workflows.AssertStarted[workflows.OnboardUser]()
workflows.AssertStarted(func(w workflows.OnboardUser) bool { return w.UserID == 1 })

func (*Fake) AssertStartedTimes

func (f *Fake) AssertStartedTimes[W any](times int)

AssertStartedTimes fails the test unless workflows of type W were started the exact number of times given by times.

func (*Fake) Record

func (f *Fake) Record(start workflow.Recorded)

Record stores a started workflow run in the fake.

func (*Fake) Records

func (f *Fake) Records(workflow any) bool

Records reports whether f intercepts and records runs of workflow's concrete type, based on any Only and Except options.

func (*Fake) Started

func (f *Fake) Started[W any]() []W

Started returns all recorded workflow values of type W in the order they started.

func (*Fake) Starts

func (f *Fake) Starts() []workflow.Recorded

Starts returns a copy of every recorded workflow start, in the order they started.

type Option

type Option func(f *Fake)

Option configures which workflow types a Fake records.

func Except

func Except(workflows ...any) Option

Except excludes the concrete types of the given values from being recorded. Excluded workflows start and run as usual, while all other workflows are recorded.

func Only

func Only(workflows ...any) Option

Only restricts a Fake to record only runs matching the concrete types of the given values, such as workflowtest.Only(workflows.OnboardUser{}). Other workflows start and run as usual.

type Run

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

Run represents a workflow execution driven inside a test. Create one with Start. Assertion methods inspect the state captured by the most recent Run.Advance or Run.Resume call, report failures on the test, and return r for chaining.

func Start

func Start(client interface{ Context() context.Context }, w any, options ...RunOption) *Run

Start persists a new run of workflow w on the application that client tests and advances it until it completes, fails, or suspends, as a worker would. Start fails the test if saving or advancing the run returns an error, and panics if client.Context does not carry a test client.

func (*Run) Advance

func (r *Run) Advance() *Run

Advance executes the next turn of the workflow run, such as after moving the clock past a sleep with client.Travel, and returns r for chaining. Advance fails the test if advancing or reloading the run returns an error.

func (*Run) AssertCancelled

func (r *Run) AssertCancelled() *Run

AssertCancelled fails the test unless the workflow run was cancelled.

func (*Run) AssertCompleted

func (r *Run) AssertCompleted() *Run

AssertCompleted fails the test unless the workflow run completed without error.

func (*Run) AssertCompletedWith

func (r *Run) AssertCompletedWith(expected any) *Run

AssertCompletedWith fails the test unless the workflow run completed and its output matches expected when both are compared as canonical JSON. It fails the test immediately if expected cannot be encoded as JSON.

func (*Run) AssertFailed

func (r *Run) AssertFailed() *Run

AssertFailed fails the test unless the workflow run ended in workflow.Failed.

func (*Run) AssertFailedWith

func (r *Run) AssertFailedWith(message string) *Run

AssertFailedWith fails the test unless the workflow run failed with an error message containing message.

func (*Run) AssertRetrying

func (r *Run) AssertRetrying(name string) *Run

AssertRetrying fails the test unless the step named name failed and is waiting to be retried.

func (*Run) AssertSleeping

func (r *Run) AssertSleeping() *Run

AssertSleeping fails the test unless the workflow run is currently waiting on a pending sleep.

func (*Run) AssertStepNotRan

func (r *Run) AssertStepNotRan(name string) *Run

AssertStepNotRan fails the test if the workflow run ever reached the step named name.

func (*Run) AssertStepRan

func (r *Run) AssertStepRan(name string) *Run

AssertStepRan fails the test unless the step named name has completed.

func (*Run) AssertStepTries

func (r *Run) AssertStepTries(name string, tries int) *Run

AssertStepTries fails the test unless the step named name was attempted the exact number of times given by tries. A step stubbed with WithStep has 0 tries.

func (*Run) AssertWaitingFor

func (r *Run) AssertWaitingFor[S ~string](token S) *Run

AssertWaitingFor fails the test unless the workflow run is currently waiting on token via a wait or an inbox. The token may be a string or a workflow.Token.

func (*Run) ID

func (r *Run) ID() workflow.RunID

ID returns the unique identifier of the workflow run.

func (*Run) Info

func (r *Run) Info() workflow.RunInfo

Info returns the workflow.RunInfo snapshot captured after the most recent advance.

func (*Run) Resume

func (r *Run) Resume[T any](token workflow.Token[T], value T) *Run

Resume delivers value to token, as workflow.Resume does, advances the run, and returns r for chaining. For a workflow reading from an inbox, call Resume once per message in order. Resume fails the test if value cannot be encoded as JSON, if the run doesn't own the token, or if resuming or advancing the run returns an error. A run owns a token once it reaches a wait for it, or from its start with ClaimToken.

func (*Run) String

func (r *Run) String() string

String formats the run's status, error, and step history as shown in assertion failure messages.

type RunOption

type RunOption func(r *Run)

RunOption configures how Start executes a workflow run.

func ClaimToken

func ClaimToken[S ~string](token S) RunOption

ClaimToken makes the run own token from its start, as workflow.PendingStart.ClaimToken does, so the test can resume the token before the run reaches its wait:

run := workflowtest.Start(client, workflows.ChargeOrder{OrderID: 42}, workflowtest.ClaimToken(workflows.Charged(42)))

func WithStep

func WithStep(name string, output any) RunOption

WithStep substitutes output for the step named name instead of running that step, so a test does not need to fake the external service the step calls:

run := workflowtest.Start(client, workflows.Checkout{OrderID: 1}, workflowtest.WithStep("charge card", Receipt{ID: "re_1"}))

The replaced step is recorded as completed with 0 tries, which you can check with run.AssertStepTries("charge card", 0). WithStep fails the test if output cannot be encoded as JSON.

Jump to

Keyboard shortcuts

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