Skip to content

Commit

Permalink
adding new test for invalid state exits
Browse files Browse the repository at this point in the history
  • Loading branch information
gaffo committed Jul 9, 2024
1 parent b78626a commit 580b0e3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
3 changes: 3 additions & 0 deletions processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ func (p *Processor[AC, OC, JC]) init() error {
if !s.Terminal && s.Exec == nil {
return fmt.Errorf("State %s is non-terminal but has no Exec function", s.TriggerState)
}
if !s.Terminal && p.ValidateExitStates && len(s.ExitStates) == 0 {
return fmt.Errorf("ValidateExitStates: invalid State machine, state %s is non-terminal but has no ExitStates", s.TriggerState)
}
}

if p.serializer == nil {
Expand Down
27 changes: 26 additions & 1 deletion processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,7 @@ func TestProcessor_ValidateExits_NonTerminal_NoExitStates(t *testing.T) {
p.ValidateExitStates = true
r := NewRun[MyOverallContext, MyJobContext]("name", MyOverallContext{})
err := p.Exec(context.Background(), r)
require.Errorf(t, err, "ValidateExitStates: invalid State machine, state %s is non-terminal but has no ExitStates")
require.ErrorContains(t, err, fmt.Sprintf("ValidateExitStates: invalid State machine, state %s is non-terminal but has no ExitStates", TRIGGER_STATE_NEW))
}

func TestProcessor_NonTerminal_NoExitFunction(t *testing.T) {
Expand All @@ -740,3 +740,28 @@ func TestProcessor_NonTerminal_NoExitFunction(t *testing.T) {
err := p.Exec(context.Background(), r)
require.ErrorContains(t, err, fmt.Sprintf("State %s is non-terminal but has no Exec function", TRIGGER_STATE_NEW))
}

func TestProcessor_ValidateExitStates_InvalidExit(t *testing.T) {
t.Parallel()
uknownState := "UNKNOWN_EXIT_STATE"
states := []State[MyAppContext, MyOverallContext, MyJobContext]{
{
TriggerState: TRIGGER_STATE_NEW,
Exec: func(ctx context.Context, ac MyAppContext, oc MyOverallContext, jc MyJobContext) (MyJobContext, string, []KickRequest[MyJobContext], error) {
return jc, uknownState, nil, nil
},
Terminal: false,
ExitStates: []string{TRIGGER_STATE_NEW},
},
{
TriggerState: uknownState,
Terminal: true,
},
}
p := NewProcessor[MyAppContext, MyOverallContext, MyJobContext](MyAppContext{}, states, nil, nil)
p.ValidateExitStates = true
r := NewRun[MyOverallContext, MyJobContext]("name", MyOverallContext{})
r.AddJob(MyJobContext{})
err := p.Exec(context.Background(), r)
require.ErrorContains(t, err, fmt.Sprintf("invalid exit state [%s] for state %s", uknownState, TRIGGER_STATE_NEW))
}

0 comments on commit 580b0e3

Please sign in to comment.