forked from Argus-Labs/world-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
world_test.go
379 lines (311 loc) · 10.1 KB
/
world_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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
package cardinal
import (
"context"
"errors"
"fmt"
"net"
"testing"
"github.com/alicebob/miniredis/v2"
"github.com/rotisserie/eris"
"pkg.world.dev/world-engine/assert"
"pkg.world.dev/world-engine/cardinal/iterators"
"pkg.world.dev/world-engine/cardinal/search/filter"
"pkg.world.dev/world-engine/cardinal/types"
"pkg.world.dev/world-engine/cardinal/worldstage"
)
type ScalarComponentStatic struct {
Val int
}
type ScalarComponentToggle struct {
Val int
}
func (ScalarComponentStatic) Name() string {
return "static"
}
func (ScalarComponentToggle) Name() string {
return "toggle"
}
func TestCanRecoverStateAfterFailedArchetypeChange(t *testing.T) {
miniRedis := miniredis.RunT(t)
t.Setenv("REDIS_ADDRESS", miniRedis.Addr())
ctx := context.Background()
for _, isFirstIteration := range []bool{true, false} {
world, err := NewWorld(WithPort(getOpenPort(t)))
assert.NilError(t, err)
assert.NilError(t, RegisterComponent[ScalarComponentStatic](world))
assert.NilError(t, RegisterComponent[ScalarComponentToggle](world))
wCtx := NewWorldContext(world)
errorToggleComponent := errors.New("problem with toggle component")
err = RegisterSystems(
world,
func(wCtx WorldContext) error {
// Get the one and only entity ID
q := NewSearch().Entity(filter.Contains(filter.Component[ScalarComponentStatic]()))
id, err := q.First(wCtx)
assert.NilError(t, err)
s, err := GetComponent[ScalarComponentStatic](wCtx, id)
assert.NilError(t, err)
s.Val++
assert.NilError(t, SetComponent[ScalarComponentStatic](wCtx, id, s))
if s.Val%2 == 1 {
assert.NilError(t, AddComponentTo[ScalarComponentToggle](wCtx, id))
} else {
assert.NilError(t, RemoveComponentFrom[ScalarComponentToggle](wCtx, id))
}
if isFirstIteration && s.Val == 5 {
return errorToggleComponent
}
return nil
},
)
assert.NilError(t, err)
go func() {
err = world.StartGame()
assert.NilError(t, err)
}()
<-world.worldStage.NotifyOnStage(worldstage.Running)
if isFirstIteration {
_, err := Create(wCtx, ScalarComponentStatic{})
assert.NilError(t, err)
}
q := NewSearch().Entity(filter.Contains(filter.Component[ScalarComponentStatic]()))
id, err := q.First(wCtx)
assert.NilError(t, err)
if isFirstIteration {
for i := 0; i < 4; i++ {
world.tickTheEngine(ctx, nil)
}
// After 4 ticks, static.Val should be 4 and toggle should have just been removed from the entity.
_, err = GetComponent[ScalarComponentToggle](wCtx, id)
assert.ErrorIs(t, iterators.ErrComponentNotOnEntity, eris.Cause(err))
s, err := GetComponent[ScalarComponentStatic](wCtx, id)
assert.NilError(t, err)
assert.Equal(t, 4, s.Val)
// Ticking again should result in an error
err = doTickCapturePanic(ctx, world)
assert.ErrorContains(t, err, errorToggleComponent.Error())
} else {
// At this second iteration, the errorToggleComponent bug has been fixed.
// It should recover at the last successful tick where toggle does not exist on the entity and val is 4
_, err = GetComponent[ScalarComponentToggle](wCtx, id)
assert.ErrorIs(t, iterators.ErrComponentNotOnEntity, eris.Cause(err))
s, err := GetComponent[ScalarComponentStatic](wCtx, id)
assert.NilError(t, err)
assert.Equal(t, 4, s.Val)
world.tickTheEngine(ctx, nil)
// After ticking again, static.Val should be 4 and toggle should have just been added to the entity.
_, err = GetComponent[ScalarComponentToggle](wCtx, id)
assert.NilError(t, err)
s, err = GetComponent[ScalarComponentStatic](wCtx, id)
assert.NilError(t, err)
assert.Equal(t, 5, s.Val)
}
assert.NilError(t, world.Shutdown())
CleanupViper(t)
}
miniRedis.Close()
}
type onePowerComponent struct {
Power int
}
func (onePowerComponent) Name() string {
return "onePower"
}
type twoPowerComponent struct {
Power int
}
func (twoPowerComponent) Name() string {
return "twoPower"
}
func TestCanIdentifyAndFixSystemError(t *testing.T) {
rs := miniredis.RunT(t)
t.Setenv("REDIS_ADDRESS", rs.Addr())
ctx := context.Background()
world, err := NewWorld(WithPort(getOpenPort(t)))
assert.NilError(t, err)
defer CleanupViper(t)
assert.NilError(t, RegisterComponent[onePowerComponent](world))
errorSystem := errors.New("3 power? That's too much, man")
// In this test, our "buggy" system fails once Power reaches 3
err = RegisterSystems(
world,
func(wCtx WorldContext) error {
searchObject := NewSearch().Entity(filter.Exact(filter.Component[onePowerComponent]()))
id := searchObject.MustFirst(wCtx)
p, err := GetComponent[onePowerComponent](wCtx, id)
if err != nil {
return err
}
p.Power++
if p.Power >= 3 {
return errorSystem
}
return SetComponent[onePowerComponent](wCtx, id, p)
},
)
assert.NilError(t, err)
go func() {
err = world.StartGame()
assert.NilError(t, err)
}()
<-world.worldStage.NotifyOnStage(worldstage.Running)
id, err := Create(NewWorldContext(world), onePowerComponent{})
assert.NilError(t, err)
// Power is set to 1
world.tickTheEngine(ctx, nil)
// Power is set to 2
world.tickTheEngine(ctx, nil)
// Power is set to 3, then the system fails
err = doTickCapturePanic(ctx, world)
assert.ErrorContains(t, err, errorSystem.Error())
assert.NilError(t, world.Shutdown())
// Set up a new engine using the same storage layer
world2, err := NewWorld(WithPort(getOpenPort(t)))
assert.NilError(t, err)
assert.NilError(t, RegisterComponent[onePowerComponent](world2))
assert.NilError(t, RegisterComponent[twoPowerComponent](world2))
// this is our fixed system that can handle Power levels of 3 and higher
err = RegisterSystems(
world2,
func(wCtx WorldContext) error {
p, err := GetComponent[onePowerComponent](wCtx, id)
if err != nil {
return err
}
p.Power++
return SetComponent[onePowerComponent](wCtx, id, p)
},
)
assert.NilError(t, err)
go func() {
err = world2.StartGame()
assert.NilError(t, err)
}()
<-world2.worldStage.NotifyOnStage(worldstage.Running)
// Loading a game state with the fixed system should start back from the last successful tick.
world2Ctx := NewWorldContext(world2)
world2.tickTheEngine(ctx, nil)
p1, err := GetComponent[onePowerComponent](world2Ctx, id)
assert.NilError(t, err)
assert.Equal(t, 3, p1.Power)
assert.NilError(t, world2.Shutdown())
}
type Foo struct{}
func (Foo) Name() string { return "foo" }
type Bar struct{}
func (Bar) Name() string { return "bar" }
type Qux struct{}
func (Qux) Name() string { return "qux" }
// TestSystemsPanicOnRedisError ensures systems panic when there is a problem connecting to redis. In general, Systems
// should panic on ANY fatal error, but this connection problem is how we'll simulate a non ecs state related error.
func TestSystemsPanicOnRedisError(t *testing.T) {
testCases := []struct {
name string
// the failFn will be called at a time when the ECB is empty of cached data and redis is down.
failFn func(wCtx WorldContext, goodID types.EntityID)
}{
{
name: "AddComponentTo",
failFn: func(wCtx WorldContext, goodID types.EntityID) {
_ = AddComponentTo[Qux](wCtx, goodID)
},
},
{
name: "RemoveComponentFrom",
failFn: func(wCtx WorldContext, goodID types.EntityID) {
_ = RemoveComponentFrom[Bar](wCtx, goodID)
},
},
{
name: "GetComponent",
failFn: func(wCtx WorldContext, goodID types.EntityID) {
_, _ = GetComponent[Foo](wCtx, goodID)
},
},
{
name: "SetComponent",
failFn: func(wCtx WorldContext, goodID types.EntityID) {
_ = SetComponent[Foo](wCtx, goodID, &Foo{})
},
},
{
name: "UpdateComponent",
failFn: func(wCtx WorldContext, goodID types.EntityID) {
_ = UpdateComponent[Foo](wCtx, goodID, func(f *Foo) *Foo {
return f
})
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
miniRedis := miniredis.RunT(t)
t.Setenv("REDIS_ADDRESS", miniRedis.Addr())
ctx := context.Background()
world, err := NewWorld(WithPort(getOpenPort(t)))
assert.NilError(t, err)
defer CleanupViper(t)
assert.NilError(t, RegisterComponent[Foo](world))
assert.NilError(t, RegisterComponent[Bar](world))
assert.NilError(t, RegisterComponent[Qux](world))
// This system will be called 2 times. The first time, a single entity is Created. The second time,
// the previously Created entity is fetched, and then miniRedis is closed. Subsequent attempts to access
// data should panic.
assert.NilError(t, RegisterSystems(world, func(wCtx WorldContext) error {
// Set up the entity in the first tick
if wCtx.CurrentTick() == 0 {
_, err := Create(wCtx, Foo{}, Bar{})
assert.Check(t, err == nil)
return nil
}
// Get the valid entity for the second tick
id, err := NewSearch().Entity(filter.Exact(filter.Component[Foo](),
filter.Component[Bar]())).First(wCtx)
assert.Check(t, err == nil)
assert.Check(t, id != iterators.BadID)
// Shut down redis. The testCase's failure function will now be able to fail
miniRedis.Close()
// Only set up this panic/recover expectation if we're in the second tick.
defer func() {
err := recover()
assert.Check(t, err != nil, "expected panic")
}()
tc.failFn(wCtx, id)
assert.Check(t, false, "should never reach here")
return nil
}))
go func() {
err = world.StartGame()
assert.NilError(t, err)
}()
<-world.worldStage.NotifyOnStage(worldstage.Running)
defer func() {
assert.NilError(t, world.Shutdown())
}()
// The first tick sets up the entity
world.tickTheEngine(ctx, nil)
// The second tick calls the test case's failure function.
err = doTickCapturePanic(ctx, world)
assert.IsError(t, err)
})
}
}
func doTickCapturePanic(ctx context.Context, world *World) (err error) {
defer func() {
if panicValue := recover(); panicValue != nil {
err = fmt.Errorf(panicValue.(string))
}
}()
world.tickTheEngine(ctx, nil)
return nil
}
func getOpenPort(t testing.TB) string {
l, err := net.Listen("tcp", "127.0.0.1:0")
defer func() {
assert.NilError(t, l.Close())
}()
assert.NilError(t, err)
tcpAddr, err := net.ResolveTCPAddr(l.Addr().Network(), l.Addr().String())
assert.NilError(t, err)
return fmt.Sprintf("%d", tcpAddr.Port)
}