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 ¶
- type CompensationFixture
- type MinkSagaAdapter
- func (a *MinkSagaAdapter) HandleEvent(ctx context.Context, event mink.StoredEvent) ([]mink.Command, error)
- func (a *MinkSagaAdapter) IsComplete() bool
- func (a *MinkSagaAdapter) MinkSaga() mink.Saga
- func (a *MinkSagaAdapter) Name() string
- func (a *MinkSagaAdapter) State() interface{}
- func (a *MinkSagaAdapter) Status() mink.SagaStatus
- type Saga
- type SagaCompensator
- type SagaState
- type SagaStateMachineFixture
- type SagaTestFixture
- func (f *SagaTestFixture) Commands() []mink.Command
- func (f *SagaTestFixture) GivenEvents(events ...mink.StoredEvent) *SagaTestFixture
- func (f *SagaTestFixture) Saga() Saga
- func (f *SagaTestFixture) ThenCommandCount(expected int) *SagaTestFixture
- func (f *SagaTestFixture) ThenCommands(expected ...mink.Command) *SagaTestFixture
- func (f *SagaTestFixture) ThenCompleted() *SagaTestFixture
- func (f *SagaTestFixture) ThenContainsCommand(expected mink.Command) *SagaTestFixture
- func (f *SagaTestFixture) ThenError(expected error) *SagaTestFixture
- func (f *SagaTestFixture) ThenFirstCommand(expected mink.Command) *SagaTestFixture
- func (f *SagaTestFixture) ThenLastCommand(expected mink.Command) *SagaTestFixture
- func (f *SagaTestFixture) ThenNoCommands() *SagaTestFixture
- func (f *SagaTestFixture) ThenNotCompleted() *SagaTestFixture
- func (f *SagaTestFixture) ThenState(expected interface{}) *SagaTestFixture
- func (f *SagaTestFixture) WithContext(ctx context.Context) *SagaTestFixture
- type SagaWithTimeout
- type StateTransition
- type TB
- type TimeoutFixture
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 MinkSagaAdapter ¶
type MinkSagaAdapter struct {
// contains filtered or unexported fields
}
MinkSagaAdapter adapts a mink.Saga to the testing Saga interface.
func NewMinkSagaAdapter ¶
func NewMinkSagaAdapter(saga mink.Saga) *MinkSagaAdapter
NewMinkSagaAdapter creates an adapter for a mink.Saga.
func (*MinkSagaAdapter) HandleEvent ¶
func (a *MinkSagaAdapter) HandleEvent(ctx context.Context, event mink.StoredEvent) ([]mink.Command, error)
HandleEvent processes an event and returns commands to dispatch.
func (*MinkSagaAdapter) IsComplete ¶
func (a *MinkSagaAdapter) IsComplete() bool
IsComplete returns true if the saga has completed.
func (*MinkSagaAdapter) MinkSaga ¶
func (a *MinkSagaAdapter) MinkSaga() mink.Saga
MinkSaga returns the underlying mink.Saga.
func (*MinkSagaAdapter) Name ¶
func (a *MinkSagaAdapter) Name() string
Name returns the saga's unique identifier.
func (*MinkSagaAdapter) State ¶
func (a *MinkSagaAdapter) State() interface{}
State returns the current saga state as a map.
func (*MinkSagaAdapter) Status ¶
func (a *MinkSagaAdapter) Status() mink.SagaStatus
Status returns the saga status.
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. This interface is compatible with mink.Saga.
type SagaCompensator ¶
SagaCompensator is implemented by sagas that support compensation.
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) Run ¶ added in v1.0.25
func (f *SagaStateMachineFixture) Run(ctx context.Context) *SagaStateMachineFixture
Run drives the saga through the expected transitions in order: for each, it (optionally) asserts the current state matches From, applies a synthetic event of the transition's Event type, asserts the saga emits the expected Command, and asserts it lands in the To state. It fails the test on the first mismatch. This is what makes the fixture actually verify behavior rather than just record expectations.
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 ¶
SagaWithTimeout is implemented by sagas that support timeouts.
type StateTransition ¶
StateTransition represents a state transition in a saga.
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).