diff --git a/machine.go b/machine.go index 104e7b8..044b1e4 100644 --- a/machine.go +++ b/machine.go @@ -78,10 +78,6 @@ func (fsm *StateMachine) run() { } else { pendingEvents = nil } - if nextStep == nil { - atomic.StoreInt32(&fsm.busy, 0) - continue - } ctx := Context{ ctx: context.TODO(), @@ -91,13 +87,14 @@ func (fsm *StateMachine) run() { } go func() { - res := reflect.ValueOf(nextStep).Call([]reflect.Value{reflect.ValueOf(ctx), reflect.ValueOf(ustate).Elem()}) + if nextStep != nil { + res := reflect.ValueOf(nextStep).Call([]reflect.Value{reflect.ValueOf(ctx), reflect.ValueOf(ustate).Elem()}) - if res[0].Interface() != nil { - log.Errorf("executing step: %+v", res[0].Interface().(error)) // TODO: propagate top level - return + if res[0].Interface() != nil { + log.Errorf("executing step: %+v", res[0].Interface().(error)) // TODO: propagate top level + return + } } - atomic.StoreInt32(&fsm.busy, 0) fsm.stageDone <- struct{}{} }() diff --git a/machine_test.go b/machine_test.go index 71c1764..689154c 100644 --- a/machine_test.go +++ b/machine_test.go @@ -277,9 +277,13 @@ func TestNoStateCallback(t *testing.T) { defer cancel() ds := datastore.NewMapDatastore() - th := &testHandlerNoStateCB{t: t, done: make(chan struct{})} + th := &testHandlerNoStateCB{t: t, proceed: make(chan struct{}), done: make(chan struct{})} smm := New(ds, th, TestState{}) + if err := smm.Send(uint64(2), &TestEvent{A: "block"}); err != nil { + t.Fatalf("%+v", err) + } + if err := smm.Send(uint64(2), &TestEvent{A: "start"}); err != nil { t.Fatalf("%+v", err) } @@ -288,6 +292,7 @@ func TestNoStateCallback(t *testing.T) { t.Fatalf("%+v", err) } + close(th.proceed) select { case <-ctx.Done(): t.Fatal("Second event transition not processed") @@ -296,8 +301,9 @@ func TestNoStateCallback(t *testing.T) { } type testHandlerNoStateCB struct { - t *testing.T - done chan struct{} + t *testing.T + proceed chan struct{} + done chan struct{} } func (t *testHandlerNoStateCB) Plan(events []Event, state interface{}) (interface{}, uint64, error) { @@ -317,6 +323,8 @@ func (t *testHandlerNoStateCB) plan(events []Event, state *TestState) (func(Cont case "b": state.A = 2 state.B = e.Val + case "block": + state.A = 3 } switch state.A { @@ -324,12 +332,19 @@ func (t *testHandlerNoStateCB) plan(events []Event, state *TestState) (func(Cont return nil, uint64(1), nil case 2: return t.step1, uint64(1), nil + case 3: + return t.block, uint64(1), nil default: t.t.Fatal(state.A) } panic("how?") } +func (t *testHandlerNoStateCB) block(ctx Context, st TestState) error { + <-t.proceed + return nil +} + func (t *testHandlerNoStateCB) step1(ctx Context, st TestState) error { assert.Equal(t.t, uint64(2), st.A)