-
Notifications
You must be signed in to change notification settings - Fork 9
/
any_test.go
294 lines (275 loc) · 5.6 KB
/
any_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
Copyright 2021 Joseph Cumines
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package behaviortree
import (
"errors"
"fmt"
"testing"
"time"
)
func ExampleAny_sequencePartialSuccess() {
fmt.Println(New(
Any(Sequence),
New(func(children []Node) (Status, error) {
fmt.Println(1)
return Success, nil
}),
New(func(children []Node) (Status, error) {
fmt.Println(2)
return Success, nil
}),
New(func(children []Node) (Status, error) {
fmt.Println(3)
return Success, nil
}),
New(func(children []Node) (Status, error) {
fmt.Println(4)
return Success, nil
}),
New(func(children []Node) (Status, error) {
fmt.Println(5)
return Failure, nil
}),
New(func(children []Node) (Status, error) {
panic(`wont reach here`)
}),
).Tick())
//output:
//1
//2
//3
//4
//5
//success <nil>
}
func ExampleAny_allPartialSuccess() {
fmt.Println(New(
Any(All),
New(func(children []Node) (Status, error) {
fmt.Println(1)
return Success, nil
}),
New(func(children []Node) (Status, error) {
fmt.Println(2)
return Success, nil
}),
New(func(children []Node) (Status, error) {
fmt.Println(3)
return Success, nil
}),
New(func(children []Node) (Status, error) {
fmt.Println(4)
return Success, nil
}),
New(func(children []Node) (Status, error) {
fmt.Println(5)
return Failure, nil
}),
New(func(children []Node) (Status, error) {
fmt.Println(6)
return Success, nil
}),
).Tick())
//output:
//1
//2
//3
//4
//5
//6
//success <nil>
}
func ExampleAny_resetBehavior() {
var (
status Status
err error
node = New(
Any(Sequence),
New(func(children []Node) (Status, error) {
fmt.Println(1)
return status, err
}),
New(func(children []Node) (Status, error) {
fmt.Println(2)
return Success, nil
}),
)
)
status = Success
err = nil
fmt.Println(node.Tick())
status = Failure
err = nil
fmt.Println(node.Tick())
status = Success
err = errors.New(`some_error`)
fmt.Println(node.Tick())
status = Success
err = nil
fmt.Println(node.Tick())
//output:
//1
//2
//success <nil>
//1
//failure <nil>
//1
//failure some_error
//1
//2
//success <nil>
}
func ExampleAny_running() {
status := Running
node := New(
Any(All),
New(func(children []Node) (Status, error) {
fmt.Printf("child ticked: %s\n", status)
return status, nil
}),
)
fmt.Println(node.Tick())
status = Failure
fmt.Println(node.Tick())
status = Running
fmt.Println(node.Tick())
status = Success
fmt.Println(node.Tick())
//output:
//child ticked: running
//running <nil>
//child ticked: failure
//failure <nil>
//child ticked: running
//running <nil>
//child ticked: success
//success <nil>
}
func ExampleAny_forkPartialSuccess() {
var (
c1 = make(chan struct{})
c2 = make(chan struct{})
c3 = make(chan struct{})
c4 = make(chan struct{})
c5 = make(chan struct{})
c6 = make(chan struct{})
status = Running
)
go func() {
time.Sleep(time.Millisecond * 100)
fmt.Println(`unblocking the forked nodes`)
close(c1)
time.Sleep(time.Millisecond * 100)
close(c2)
time.Sleep(time.Millisecond * 100)
close(c3)
time.Sleep(time.Millisecond * 100)
close(c4)
time.Sleep(time.Millisecond * 100)
close(c5)
time.Sleep(time.Millisecond * 100)
close(c6)
}()
node := New(
Any(Fork()),
New(func(children []Node) (Status, error) {
fmt.Println(`ready`)
<-c1
fmt.Println(1)
return Success, nil
}),
New(func(children []Node) (Status, error) {
fmt.Println(`ready`)
<-c2
fmt.Println(2)
return Success, nil
}),
New(func(children []Node) (Status, error) {
fmt.Println(`ready`)
<-c3
fmt.Println(3)
return status, nil
}),
New(func(children []Node) (Status, error) {
fmt.Println(`ready`)
<-c4
fmt.Println(4)
return Failure, nil
}),
New(func(children []Node) (Status, error) {
fmt.Println(`ready`)
<-c5
fmt.Println(5)
return Failure, nil
}),
New(func(children []Node) (Status, error) {
fmt.Println(`ready`)
<-c6
fmt.Println(6)
return Success, nil
}),
)
fmt.Println(node.Tick())
fmt.Println(`same running behavior as Fork`)
fmt.Println(node.Tick())
fmt.Println(`but the exit status is overridden`)
status = Failure
fmt.Println(node.Tick())
//output:
//ready
//ready
//ready
//ready
//ready
//ready
//unblocking the forked nodes
//1
//2
//3
//4
//5
//6
//running <nil>
//same running behavior as Fork
//ready
//3
//running <nil>
//but the exit status is overridden
//ready
//3
//success <nil>
}
func TestAny_nilTick(t *testing.T) {
if v := Any(nil); v != nil {
t.Error(v)
}
}
func TestAny_nilNode(t *testing.T) {
if v, err := Any(Sequence)([]Node{nil}); err == nil || v != Failure {
t.Error(v, err)
}
}
func TestAny_nilChildTick(t *testing.T) {
status, err := New(Any(Sequence), New(nil)).Tick()
if status != Failure {
t.Error(status)
}
if err == nil || err.Error() != `behaviortree.Node cannot tick a node with a nil tick` {
t.Error(err)
}
}
func TestAny_noChildren(t *testing.T) {
if status, err := New(Any(Sequence)).Tick(); err != nil || status != Failure {
t.Error(status, err)
}
}