Skip to content

Commit

Permalink
fix mutex unlock error on callback panic (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
nbaztec committed May 14, 2020
1 parent 54c5586 commit 86c5e2b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion fsm.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,8 @@ func (f *FSM) Event(event string, args ...interface{}) error {

// Perform the rest of the transition, if not asynchronous.
f.stateMu.RUnlock()
defer f.stateMu.RLock()
err = f.doTransition()
f.stateMu.RLock()
if err != nil {
return InternalError{}
}
Expand Down
25 changes: 25 additions & 0 deletions fsm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,31 @@ func TestCallbackArgs(t *testing.T) {
fsm.Event("run", "test")
}

func TestCallbackPanic(t *testing.T) {
panicMsg := "unexpected panic"
defer func() {
r := recover()
if r == nil || r != panicMsg {
t.Errorf("expected panic message to be '%s', got %v", panicMsg, r)
}
}()
fsm := NewFSM(
"start",
Events{
{Name: "run", Src: []string{"start"}, Dst: "end"},
},
Callbacks{
"run": func(e *Event) {
panic(panicMsg)
},
},
)
e := fsm.Event("run")
if e.Error() != "error" {
t.Error("expected error to be 'error'")
}
}

func TestNoDeadLock(t *testing.T) {
var fsm *FSM
fsm = NewFSM(
Expand Down

0 comments on commit 86c5e2b

Please sign in to comment.