Skip to content
Merged
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
15 changes: 6 additions & 9 deletions machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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{}{}
}()
Expand Down
21 changes: 18 additions & 3 deletions machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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")
Expand All @@ -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) {
Expand All @@ -317,19 +323,28 @@ 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 {
case 1:
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)

Expand Down