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

Fix test error description and add cancel transaction method #77

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
25 changes: 25 additions & 0 deletions fsm.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
// transitioner is an interface for the FSM's transition function.
type transitioner interface {
transition(*FSM) error
cancelTransition(*FSM) error
}

// FSM is the state machine that holds the current state.
Expand Down Expand Up @@ -355,11 +356,23 @@ func (f *FSM) Transition() error {
return f.doTransition()
}

// CancelTransition wraps transitioner.cancelTransition.
func (f *FSM) CancelTransition() error {
f.eventMu.Lock()
defer f.eventMu.Unlock()
return f.cancelTransition()
}

// doTransition wraps transitioner.transition.
func (f *FSM) doTransition() error {
return f.transitionerObj.transition(f)
}

// cancelTransition wraps transitioner.cancelTransition.
func (f *FSM) cancelTransition() error {
return f.transitionerObj.cancelTransition(f)
}

// transitionerStruct is the default implementation of the transitioner
// interface. Other implementations can be swapped in for testing.
type transitionerStruct struct{}
Expand All @@ -377,6 +390,18 @@ func (t transitionerStruct) transition(f *FSM) error {
return nil
}

// CancelTransition cancels an asynchrounous state change.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is great! But it will never be visible as this is a private method on a private struct. Can you move it to the public CancelTransition() method? If you can also update the public Transition() comment to match this quality it would be a welcome bonus!

//
// The callback for leave_<STATE> must prviously have called Async on its
// event to have initiated an asynchronous state transition.
func (t transitionerStruct) cancelTransition(f *FSM) error {
if f.transition == nil {
return NotInTransitionError{}
}
f.transition = nil
return nil
}

// beforeEventCallbacks calls the before_ callbacks, first the named then the
// general version.
func (f *FSM) beforeEventCallbacks(e *Event) error {
Expand Down
74 changes: 73 additions & 1 deletion fsm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ func (t fakeTransitionerObj) transition(f *FSM) error {
return &InternalError{}
}

func (t fakeTransitionerObj) cancelTransition(f *FSM) error {
return nil
}

func TestSameState(t *testing.T) {
fsm := NewFSM(
"start",
Expand All @@ -53,7 +57,7 @@ func TestSetState(t *testing.T) {
)
fsm.SetState("start")
if fsm.Current() != "start" {
t.Error("expected state to be 'walking'")
t.Error("expected state to be 'start'")
}
err := fsm.Event("walk")
if err != nil {
Expand Down Expand Up @@ -471,6 +475,74 @@ func TestAsyncTransitionNotInProgress(t *testing.T) {
}
}

func TestCancelAsyncTransitionGenericState(t *testing.T) {
fsm := NewFSM(
"start",
Events{
{Name: "run", Src: []string{"start"}, Dst: "end"},
},
Callbacks{
"leave_state": func(e *Event) {
e.Async()
},
},
)
fsm.Event("run")
if fsm.Current() != "start" {
t.Error("expected state to be 'start'")
}
err := fsm.Event("run")
if e, ok := err.(InTransitionError); !ok && e.Event != "run" {
t.Error("expected 'InTransitionError' with correct state")
}
fsm.CancelTransition()
err = fsm.Event("run")
if _, ok := err.(AsyncError); !ok {
t.Error("expected 'AsyncError'")
}
}

func TestCancelAsyncTransitionSpecificState(t *testing.T) {
fsm := NewFSM(
"start",
Events{
{Name: "run", Src: []string{"start"}, Dst: "end"},
},
Callbacks{
"leave_start": func(e *Event) {
e.Async()
},
},
)
fsm.Event("run")
if fsm.Current() != "start" {
t.Error("expected state to be 'start'")
}
err := fsm.Event("run")
if e, ok := err.(InTransitionError); !ok && e.Event != "run" {
t.Error("expected 'InTransitionError' with correct state")
}
fsm.CancelTransition()
err = fsm.Event("run")
if _, ok := err.(AsyncError); !ok {
t.Error("expected 'AsyncError'")
}
}

func TestCancelAsyncTransitionNotInProgress(t *testing.T) {
fsm := NewFSM(
"start",
Events{
{Name: "run", Src: []string{"start"}, Dst: "end"},
},
Callbacks{},
)
err := fsm.CancelTransition()
if _, ok := err.(NotInTransitionError); !ok {
t.Error("expected 'NotInTransitionError'")
}
}

func TestCallbackNoError(t *testing.T) {
fsm := NewFSM(
"start",
Expand Down