Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add processNoTransitionStates property avoiding NoTransitionError when enabled #106

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion fsm.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ type FSM struct {
metadata map[string]interface{}

metadataMu sync.RWMutex

// processNoTransitionStates is used to allow events with no transitions
// to be performed
processNoTransitionStates bool
}

// EventDesc represents an event when initializing the FSM.
Expand Down Expand Up @@ -135,6 +139,7 @@ func NewFSM(initial string, events []EventDesc, callbacks map[string]Callback) *
transitions: make(map[eKey]string),
callbacks: make(map[cKey]Callback),
metadata: make(map[string]interface{}),
processNoTransitionStates: false,
}

// Build transition map and store sets of all events and states.
Expand Down Expand Up @@ -276,6 +281,12 @@ func (f *FSM) DeleteMetadata(key string) {
f.metadataMu.Unlock()
}

// SetProcessNoTransitionStates allow the users to enable events with no transitions
// to be performed
func (f *FSM) SetProcessNoTransitionStates(process bool) {
f.processNoTransitionStates = process
}

// Event initiates a state transition with the named event.
//
// The call takes a variable number of arguments that will be passed to the
Expand Down Expand Up @@ -332,7 +343,7 @@ func (f *FSM) Event(ctx context.Context, event string, args ...interface{}) erro
return err
}

if f.current == dst {
if f.current == dst && !f.processNoTransitionStates {
f.stateMu.RUnlock()
defer f.stateMu.RLock()
f.eventMu.Unlock()
Expand Down
24 changes: 23 additions & 1 deletion fsm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,32 @@ func TestSameState(t *testing.T) {
},
Callbacks{},
)
_ = fsm.Event(context.Background(), "run")
err := fsm.Event(context.Background(), "run")
if fsm.Current() != "start" {
t.Error("expected state to be 'start'")
}
if err == nil {
t.Error("expected no transition error")
}
}

func TestSameStateWithProcessNoTransitionStatesEnabled(t *testing.T) {
fsm := NewFSM(
"start",
Events{
{Name: "run", Src: []string{"start"}, Dst: "start"},
},
Callbacks{},
)
fsm.SetProcessNoTransitionStates(true)
err := fsm.Event(context.Background(), "run")
if fsm.Current() != "start" {
t.Error("expected state to be 'start'")
}

if err != nil {
t.Error("expected no errors")
}
}

func TestSetState(t *testing.T) {
Expand Down