-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuilder_test.go
More file actions
460 lines (370 loc) · 11.4 KB
/
builder_test.go
File metadata and controls
460 lines (370 loc) · 11.4 KB
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
package statechartx_test
import (
"context"
"sync/atomic"
"testing"
"time"
. "github.com/comalice/statechartx"
)
func TestBuilderTrafficLight(t *testing.T) {
b := NewMachineBuilder("traffic", "green")
b.State("green").Atomic().On("timer", "yellow", nil, nil)
b.State("yellow").Atomic().On("timer", "red", nil, nil)
b.State("red").Atomic().On("timer", "green", nil, nil)
machine, err := b.Build()
if err != nil {
t.Fatal(err)
}
rt := NewRuntime(machine, nil)
ctx := context.Background()
if err := rt.Start(ctx); err != nil {
t.Fatal(err)
}
defer rt.Stop()
// Test initial state
greenID := b.GetID("green")
if !rt.IsInState(greenID) {
t.Error("should start in green")
}
// Test transitions
timerEventID := EventID(b.GetID("event:timer"))
rt.SendEvent(ctx, Event{ID: timerEventID})
time.Sleep(50 * time.Millisecond)
yellowID := b.GetID("yellow")
if !rt.IsInState(yellowID) {
t.Error("should transition to yellow")
}
rt.SendEvent(ctx, Event{ID: timerEventID})
time.Sleep(50 * time.Millisecond)
redID := b.GetID("red")
if !rt.IsInState(redID) {
t.Error("should transition to red")
}
rt.SendEvent(ctx, Event{ID: timerEventID})
time.Sleep(50 * time.Millisecond)
if !rt.IsInState(greenID) {
t.Error("should cycle back to green")
}
}
func TestBuilderCompoundStates(t *testing.T) {
b := NewMachineBuilder("app", "off")
b.State("off").Atomic().On("power", "on.idle", nil, nil)
b.State("on").Compound("on.idle")
b.State("on.idle").Atomic().On("work", "on.working", nil, nil)
b.State("on.working").Atomic().On("done", "on.idle", nil, nil)
b.State("on").On("power", "off", nil, nil)
machine, err := b.Build()
if err != nil {
t.Fatal(err)
}
rt := NewRuntime(machine, nil)
ctx := context.Background()
if err := rt.Start(ctx); err != nil {
t.Fatal(err)
}
defer rt.Stop()
// Start in off
offID := b.GetID("off")
if !rt.IsInState(offID) {
t.Error("should start in off")
}
// Power on -> should enter on.idle
powerEventID := EventID(b.GetID("event:power"))
rt.SendEvent(ctx, Event{ID: powerEventID})
time.Sleep(50 * time.Millisecond)
onIdleID := b.GetID("on.idle")
if !rt.IsInState(onIdleID) {
t.Error("should be in on.idle after power on")
}
// Start working
workEventID := EventID(b.GetID("event:work"))
rt.SendEvent(ctx, Event{ID: workEventID})
time.Sleep(50 * time.Millisecond)
onWorkingID := b.GetID("on.working")
if !rt.IsInState(onWorkingID) {
t.Error("should be in on.working")
}
// Power off from nested state
rt.SendEvent(ctx, Event{ID: powerEventID})
time.Sleep(50 * time.Millisecond)
if !rt.IsInState(offID) {
t.Error("should be off after power event from nested state")
}
}
func TestBuilderParallelStates(t *testing.T) {
b := NewMachineBuilder("app", "running")
b.State("running").Parallel()
b.State("running.audio").Compound("running.audio.playing")
b.State("running.audio.playing").Atomic().On("pause_audio", "running.audio.paused", nil, nil)
b.State("running.audio.paused").Atomic().On("play_audio", "running.audio.playing", nil, nil)
b.State("running.video").Compound("running.video.playing")
b.State("running.video.playing").Atomic().On("pause_video", "running.video.paused", nil, nil)
b.State("running.video.paused").Atomic().On("play_video", "running.video.playing", nil, nil)
machine, err := b.Build()
if err != nil {
t.Fatal(err)
}
rt := NewRuntime(machine, nil)
ctx := context.Background()
if err := rt.Start(ctx); err != nil {
t.Fatal(err)
}
defer rt.Stop()
time.Sleep(100 * time.Millisecond) // Let parallel regions initialize
// Both regions should be active
audioPlayingID := b.GetID("running.audio.playing")
videoPlayingID := b.GetID("running.video.playing")
if !rt.IsInState(audioPlayingID) {
t.Error("audio should be playing")
}
if !rt.IsInState(videoPlayingID) {
t.Error("video should be playing")
}
// Pause audio only
pauseAudioID := EventID(b.GetID("event:pause_audio"))
rt.SendEvent(ctx, Event{ID: pauseAudioID})
time.Sleep(50 * time.Millisecond)
audioPausedID := b.GetID("running.audio.paused")
if !rt.IsInState(audioPausedID) {
t.Error("audio should be paused")
}
if !rt.IsInState(videoPlayingID) {
t.Error("video should still be playing")
}
}
func TestBuilderFinalState(t *testing.T) {
b := NewMachineBuilder("workflow", "start")
b.State("start").Atomic().On("begin", "processing", nil, nil)
b.State("processing").Atomic().On("complete", "done", nil, nil)
b.State("done").Final("success")
machine, err := b.Build()
if err != nil {
t.Fatal(err)
}
rt := NewRuntime(machine, nil)
ctx := context.Background()
if err := rt.Start(ctx); err != nil {
t.Fatal(err)
}
defer rt.Stop()
// Transition to final state
beginID := EventID(b.GetID("event:begin"))
rt.SendEvent(ctx, Event{ID: beginID})
time.Sleep(50 * time.Millisecond)
completeID := EventID(b.GetID("event:complete"))
rt.SendEvent(ctx, Event{ID: completeID})
time.Sleep(50 * time.Millisecond)
doneID := b.GetID("done")
if !rt.IsInState(doneID) {
t.Error("should be in done (final) state")
}
}
func TestBuilderActions(t *testing.T) {
b := NewMachineBuilder("counter", "idle")
var entryCount, exitCount, transitionCount int32
entryAction := func(ctx context.Context, evt *Event, from StateID, to StateID) error {
atomic.AddInt32(&entryCount, 1)
return nil
}
exitAction := func(ctx context.Context, evt *Event, from StateID, to StateID) error {
atomic.AddInt32(&exitCount, 1)
return nil
}
transitionAction := func(ctx context.Context, evt *Event, from StateID, to StateID) error {
atomic.AddInt32(&transitionCount, 1)
return nil
}
b.State("idle").Atomic().
Entry(entryAction).
Exit(exitAction).
On("start", "active", nil, transitionAction)
b.State("active").Atomic().
Entry(entryAction).
On("stop", "idle", nil, nil)
machine, err := b.Build()
if err != nil {
t.Fatal(err)
}
rt := NewRuntime(machine, nil)
ctx := context.Background()
if err := rt.Start(ctx); err != nil {
t.Fatal(err)
}
defer rt.Stop()
time.Sleep(50 * time.Millisecond)
// Entry action should have fired for idle
if atomic.LoadInt32(&entryCount) != 1 {
t.Errorf("expected 1 entry, got %d", entryCount)
}
// Transition to active
startID := EventID(b.GetID("event:start"))
rt.SendEvent(ctx, Event{ID: startID})
time.Sleep(50 * time.Millisecond)
// Should have exit(idle) + transition + entry(active)
if atomic.LoadInt32(&exitCount) != 1 {
t.Errorf("expected 1 exit, got %d", exitCount)
}
if atomic.LoadInt32(&transitionCount) != 1 {
t.Errorf("expected 1 transition action, got %d", transitionCount)
}
if atomic.LoadInt32(&entryCount) != 2 {
t.Errorf("expected 2 entries, got %d", entryCount)
}
}
func TestBuilderGuards(t *testing.T) {
b := NewMachineBuilder("guarded", "start")
allowTransition := true
guard := func(ctx context.Context, evt *Event, from StateID, to StateID) (bool, error) {
return allowTransition, nil
}
b.State("start").Atomic().On("next", "end", guard, nil)
b.State("end").Atomic()
machine, err := b.Build()
if err != nil {
t.Fatal(err)
}
rt := NewRuntime(machine, nil)
ctx := context.Background()
if err := rt.Start(ctx); err != nil {
t.Fatal(err)
}
defer rt.Stop()
startID := b.GetID("start")
endID := b.GetID("end")
nextEventID := EventID(b.GetID("event:next"))
// Guard blocks transition
allowTransition = false
rt.SendEvent(ctx, Event{ID: nextEventID})
time.Sleep(50 * time.Millisecond)
if !rt.IsInState(startID) {
t.Error("should still be in start (guard blocked)")
}
// Guard allows transition
allowTransition = true
rt.SendEvent(ctx, Event{ID: nextEventID})
time.Sleep(50 * time.Millisecond)
if !rt.IsInState(endID) {
t.Error("should be in end (guard allowed)")
}
}
func TestBuilderInternalTransition(t *testing.T) {
b := NewMachineBuilder("app", "running")
var actionCount int32
internalAction := func(ctx context.Context, evt *Event, from StateID, to StateID) error {
atomic.AddInt32(&actionCount, 1)
return nil
}
var entryCount, exitCount int32
entryAction := func(ctx context.Context, evt *Event, from StateID, to StateID) error {
atomic.AddInt32(&entryCount, 1)
return nil
}
exitAction := func(ctx context.Context, evt *Event, from StateID, to StateID) error {
atomic.AddInt32(&exitCount, 1)
return nil
}
b.State("running").Atomic().
Entry(entryAction).
Exit(exitAction).
OnInternal("update", nil, internalAction)
machine, err := b.Build()
if err != nil {
t.Fatal(err)
}
rt := NewRuntime(machine, nil)
ctx := context.Background()
if err := rt.Start(ctx); err != nil {
t.Fatal(err)
}
defer rt.Stop()
time.Sleep(50 * time.Millisecond)
// Entry should fire once
if atomic.LoadInt32(&entryCount) != 1 {
t.Errorf("expected 1 entry, got %d", entryCount)
}
// Send internal transition event
updateEventID := EventID(b.GetID("event:update"))
rt.SendEvent(ctx, Event{ID: updateEventID})
time.Sleep(50 * time.Millisecond)
// Internal action should fire, but not entry/exit
if atomic.LoadInt32(&actionCount) != 1 {
t.Errorf("expected 1 internal action, got %d", actionCount)
}
if atomic.LoadInt32(&exitCount) != 0 {
t.Errorf("expected 0 exits (internal transition), got %d", exitCount)
}
if atomic.LoadInt32(&entryCount) != 1 {
t.Errorf("expected still 1 entry (internal transition), got %d", entryCount)
}
runningID := b.GetID("running")
if !rt.IsInState(runningID) {
t.Error("should still be in running after internal transition")
}
}
func TestBuilderDeterministicIDs(t *testing.T) {
// Build same machine twice
b1 := NewMachineBuilder("app", "idle")
b1.State("idle").Atomic().On("start", "active", nil, nil)
b1.State("active").Atomic().On("stop", "idle", nil, nil)
b2 := NewMachineBuilder("app", "idle")
b2.State("idle").Atomic().On("start", "active", nil, nil)
b2.State("active").Atomic().On("stop", "idle", nil, nil)
// IDs should match
if b1.GetID("idle") != b2.GetID("idle") {
t.Error("idle IDs should match across builds")
}
if b1.GetID("active") != b2.GetID("active") {
t.Error("active IDs should match across builds")
}
if b1.GetID("event:start") != b2.GetID("event:start") {
t.Error("start event IDs should match across builds")
}
}
func TestBuilderGetNameReverseLookup(t *testing.T) {
b := NewMachineBuilder("app", "state1")
b.State("state1").Atomic()
b.State("state2").Atomic()
id1 := b.GetID("state1")
id2 := b.GetID("state2")
if b.GetName(id1) != "state1" {
t.Error("GetName should return state1")
}
if b.GetName(id2) != "state2" {
t.Error("GetName should return state2")
}
if b.GetName(999) != "" {
t.Error("GetName for unknown ID should return empty string")
}
}
func TestBuilderValidationMissingInitial(t *testing.T) {
b := NewMachineBuilder("app", "parent")
b.State("parent").Compound("child") // Declares initial but doesn't create child
_, err := b.Build()
if err == nil {
t.Error("should error on missing initial state")
}
}
func TestBuilderContextIntegration(t *testing.T) {
b := NewMachineBuilder("app", "idle")
action := func(ctx context.Context, evt *Event, from StateID, to StateID) error {
// Access runtime context (would need to pass runtime in real usage)
return nil
}
b.State("idle").Atomic().On("start", "active", nil, action)
b.State("active").Atomic()
machine, err := b.Build()
if err != nil {
t.Fatal(err)
}
// Auto-created Context
rt := NewRuntime(machine, nil)
ctx := rt.Ctx()
if ctx == nil {
t.Fatal("expected auto-created Context")
}
// Should be able to use context
ctx.Set("test_key", "test_value")
if ctx.Get("test_key") != "test_value" {
t.Error("Context should work with builder-created machine")
}
}