sagas

package
v0.4.8 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package sagas provides testing utilities for saga (process manager) development. It includes fixtures for testing saga state transitions, event handling, and command generation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CompensationFixture

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

CompensationFixture tests saga compensation (rollback) behavior.

func TestCompensation

func TestCompensation(t TB, saga Saga) *CompensationFixture

TestCompensation creates a new compensation test fixture.

func (*CompensationFixture) GivenFailureAfter

func (f *CompensationFixture) GivenFailureAfter(events ...mink.StoredEvent) *CompensationFixture

GivenFailureAfter simulates a failure after certain events.

func (*CompensationFixture) ThenCompensates

func (f *CompensationFixture) ThenCompensates(expected ...mink.Command)

ThenCompensates asserts the expected compensation commands.

type Saga

type Saga interface {
	// Name returns the saga's unique identifier.
	Name() string

	// HandleEvent processes an event and returns commands to dispatch.
	HandleEvent(ctx context.Context, event mink.StoredEvent) ([]mink.Command, error)

	// IsComplete returns true if the saga has completed.
	IsComplete() bool

	// State returns the current saga state.
	State() interface{}
}

Saga represents a saga or process manager interface. Sagas coordinate long-running processes by listening to events and dispatching commands.

type SagaCompensator

type SagaCompensator interface {
	GetCompensationCommands() []mink.Command
}

SagaCompensator is implemented by sagas that support compensation.

type SagaState

type SagaState string

SagaState represents the state of a saga.

const (
	SagaStateNotStarted   SagaState = "NotStarted"
	SagaStateInProgress   SagaState = "InProgress"
	SagaStateCompleted    SagaState = "Completed"
	SagaStateFailed       SagaState = "Failed"
	SagaStateCompensating SagaState = "Compensating"
)

Common saga states.

type SagaStateMachineFixture

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

SagaStateMachineFixture tests saga state machine behavior.

func TestSagaStateMachine

func TestSagaStateMachine(t TB, saga Saga) *SagaStateMachineFixture

TestSagaStateMachine creates a new state machine test fixture.

func (*SagaStateMachineFixture) ExpectTransition

func (f *SagaStateMachineFixture) ExpectTransition(from, to SagaState, event, command string) *SagaStateMachineFixture

ExpectTransition defines an expected state transition.

func (*SagaStateMachineFixture) Transitions

func (f *SagaStateMachineFixture) Transitions() []StateTransition

Transitions returns the expected transitions for verification.

type SagaTestFixture

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

SagaTestFixture provides BDD-style testing for sagas.

func TestSaga

func TestSaga(t TB, saga Saga) *SagaTestFixture

TestSaga creates a new saga test fixture.

func (*SagaTestFixture) Commands

func (f *SagaTestFixture) Commands() []mink.Command

Commands returns the collected commands for additional assertions.

func (*SagaTestFixture) GivenEvents

func (f *SagaTestFixture) GivenEvents(events ...mink.StoredEvent) *SagaTestFixture

GivenEvents applies events to the saga and collects commands.

func (*SagaTestFixture) Saga

func (f *SagaTestFixture) Saga() Saga

Saga returns the underlying saga for additional assertions.

func (*SagaTestFixture) ThenCommandCount

func (f *SagaTestFixture) ThenCommandCount(expected int) *SagaTestFixture

ThenCommandCount asserts the number of commands produced.

func (*SagaTestFixture) ThenCommands

func (f *SagaTestFixture) ThenCommands(expected ...mink.Command) *SagaTestFixture

ThenCommands asserts that the saga produced the expected commands.

func (*SagaTestFixture) ThenCompleted

func (f *SagaTestFixture) ThenCompleted() *SagaTestFixture

ThenCompleted asserts that the saga has completed.

func (*SagaTestFixture) ThenContainsCommand

func (f *SagaTestFixture) ThenContainsCommand(expected mink.Command) *SagaTestFixture

ThenContainsCommand asserts that a command exists in the produced commands.

func (*SagaTestFixture) ThenError

func (f *SagaTestFixture) ThenError(expected error) *SagaTestFixture

ThenError asserts that an error occurred during event handling.

func (*SagaTestFixture) ThenFirstCommand

func (f *SagaTestFixture) ThenFirstCommand(expected mink.Command) *SagaTestFixture

ThenFirstCommand asserts the first command matches.

func (*SagaTestFixture) ThenLastCommand

func (f *SagaTestFixture) ThenLastCommand(expected mink.Command) *SagaTestFixture

ThenLastCommand asserts the last command matches.

func (*SagaTestFixture) ThenNoCommands

func (f *SagaTestFixture) ThenNoCommands() *SagaTestFixture

ThenNoCommands asserts that no commands were produced.

func (*SagaTestFixture) ThenNotCompleted

func (f *SagaTestFixture) ThenNotCompleted() *SagaTestFixture

ThenNotCompleted asserts that the saga has not completed.

func (*SagaTestFixture) ThenState

func (f *SagaTestFixture) ThenState(expected interface{}) *SagaTestFixture

ThenState asserts the saga's current state.

func (*SagaTestFixture) WithContext

func (f *SagaTestFixture) WithContext(ctx context.Context) *SagaTestFixture

WithContext sets a custom context for event handling.

type SagaWithTimeout

type SagaWithTimeout interface {
	Saga
	OnTimeout(ctx context.Context) ([]mink.Command, error)
}

SagaWithTimeout is implemented by sagas that support timeouts.

type StateTransition

type StateTransition struct {
	From    SagaState
	To      SagaState
	Event   string
	Command string
}

StateTransition represents a state transition in a saga.

type TB

type TB = testing.TB

TB is an alias for testing.TB to enable easier mocking in tests.

type TimeoutFixture

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

TimeoutFixture tests saga timeout behavior.

func TestTimeout

func TestTimeout(t TB, saga Saga) *TimeoutFixture

TestTimeout creates a new timeout test fixture.

func (*TimeoutFixture) ThenOnTimeout

func (f *TimeoutFixture) ThenOnTimeout(expected ...mink.Command)

ThenOnTimeout simulates a timeout and checks commands.

func (*TimeoutFixture) WithContext

func (f *TimeoutFixture) WithContext(ctx context.Context) *TimeoutFixture

WithContext sets a custom context (can include timeout).

Jump to

Keyboard shortcuts

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