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 ¶
- func StoreContract(t testing.TB, ctx context.Context, store workflow.Store)
- type Fake
- func (f *Fake) AssertNotStarted[W any](match ...func(W) bool)
- func (f *Fake) AssertNothingStarted()
- func (f *Fake) AssertStarted[W any](match ...func(W) bool)
- func (f *Fake) AssertStartedTimes[W any](times int)
- func (f *Fake) Record(start workflow.Recorded)
- func (f *Fake) Records(workflow any) bool
- func (f *Fake) Started[W any]() []W
- func (f *Fake) Starts() []workflow.Recorded
- type Option
- type Run
- func (r *Run) Advance() *Run
- func (r *Run) AssertCancelled() *Run
- func (r *Run) AssertCompleted() *Run
- func (r *Run) AssertCompletedWith(expected any) *Run
- func (r *Run) AssertFailed() *Run
- func (r *Run) AssertFailedWith(message string) *Run
- func (r *Run) AssertRetrying(name string) *Run
- func (r *Run) AssertSleeping() *Run
- func (r *Run) AssertStepNotRan(name string) *Run
- func (r *Run) AssertStepRan(name string) *Run
- func (r *Run) AssertStepTries(name string, tries int) *Run
- func (r *Run) AssertWaitingFor[S ~string](token S) *Run
- func (r *Run) ID() workflow.RunID
- func (r *Run) Info() workflow.RunInfo
- func (r *Run) Resume[T any](token workflow.Token[T], value T) *Run
- func (r *Run) String() string
- type RunOption
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func StoreContract ¶
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 ¶
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 ¶
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 ¶
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 ¶
AssertStartedTimes fails the test unless workflows of type W were started the exact number of times given by times.
func (*Fake) Records ¶
Records reports whether f intercepts and records runs of workflow's concrete type, based on any Only and Except options.
type Option ¶
type Option func(f *Fake)
Option configures which workflow types a Fake records.
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 ¶
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 ¶
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 ¶
AssertCancelled fails the test unless the workflow run was cancelled.
func (*Run) AssertCompleted ¶
AssertCompleted fails the test unless the workflow run completed without error.
func (*Run) AssertCompletedWith ¶
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 ¶
AssertFailed fails the test unless the workflow run ended in workflow.Failed.
func (*Run) AssertFailedWith ¶
AssertFailedWith fails the test unless the workflow run failed with an error message containing message.
func (*Run) AssertRetrying ¶
AssertRetrying fails the test unless the step named name failed and is waiting to be retried.
func (*Run) AssertSleeping ¶
AssertSleeping fails the test unless the workflow run is currently waiting on a pending sleep.
func (*Run) AssertStepNotRan ¶
AssertStepNotRan fails the test if the workflow run ever reached the step named name.
func (*Run) AssertStepRan ¶
AssertStepRan fails the test unless the step named name has completed.
func (*Run) AssertStepTries ¶
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 ¶
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) Info ¶
Info returns the workflow.RunInfo snapshot captured after the most recent advance.
func (*Run) Resume ¶
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.
type RunOption ¶
type RunOption func(r *Run)
RunOption configures how Start executes a workflow run.
func ClaimToken ¶
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 ¶
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.