Skip to content

Commit

Permalink
refactor(Sequence): treat invalid Status as Failure
Browse files Browse the repository at this point in the history
This change brings Sequence in line with the other implementations in this
package, such as All, and has no impact in normal operation / on Tick
implementations which are implemented correctly.
  • Loading branch information
joeycumines committed Apr 24, 2021
1 parent 1f0b8a9 commit 90b3d66
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
4 changes: 2 additions & 2 deletions behaviortree.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ import (
)

const (
_ = iota
_ Status = iota
// Running indicates that the Tick for a given Node is currently running
Running Status = iota
Running
// Success indicates that the Tick for a given Node completed successfully
Success
// Failure indicates that the Tick for a given Node failed to complete successfully
Expand Down
2 changes: 1 addition & 1 deletion sequence.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func Sequence(children []Node) (Status, error) {
if status == Running {
return Running, nil
}
if status == Failure {
if status != Success {
return Failure, nil
}
}
Expand Down
23 changes: 23 additions & 0 deletions sequence_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,3 +258,26 @@ func TestSequence_running(t *testing.T) {
t.Error("expected nil error but it was", err)
}
}

func TestSequence_invalidStatus(t *testing.T) {
var (
output []int
children []Node
)
for i := 0; i < 6; i++ {
i := i
children = append(children, New(func([]Node) (Status, error) {
output = append(output, i)
if i == 3 {
return 0, nil
}
return Success, nil
}))
}
if status, err := NewNode(Sequence, children).Tick(); err != nil || status != Failure {
t.Error(status, err)
}
if diff := deep.Equal(output, []int{0, 1, 2, 3}); diff != nil {
t.Errorf("%v\n%s", output, strings.Join(diff, "\n"))
}
}
4 changes: 2 additions & 2 deletions ticker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func TestNewTicker_runError(t *testing.T) {
func TestNewTicker_runCancel(t *testing.T) {
startGoroutines := runtime.NumGoroutine()
defer func() {
time.Sleep(time.Millisecond * 50)
time.Sleep(time.Millisecond * 100)

endGoroutines := runtime.NumGoroutine()

Expand All @@ -226,7 +226,7 @@ func TestNewTicker_runCancel(t *testing.T) {
startedAt := time.Now()
defer func() {
diff := time.Now().Sub(startedAt)
if diff > time.Millisecond*20 {
if diff > time.Millisecond*100 {
t.Error("unexpected diff", diff)
}
}()
Expand Down

0 comments on commit 90b3d66

Please sign in to comment.