-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparallel_test.go
More file actions
495 lines (433 loc) · 13.4 KB
/
parallel_test.go
File metadata and controls
495 lines (433 loc) · 13.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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
package statekit
import (
"strings"
"testing"
"go.klarlabs.de/statekit/export"
)
// TestParallelState_Basic tests basic parallel state entry
func TestParallelState_Basic(t *testing.T) {
t.Parallel()
machine, err := NewMachine[struct{}]("parallel_basic").
WithInitial("active").
State("active").Parallel().
Region("region1").
WithInitial("r1_idle").
State("r1_idle").EndState().
EndRegion().
Region("region2").
WithInitial("r2_idle").
State("r2_idle").EndState().
EndRegion().
Done().
State("done").Final().
Done().
Build()
if err != nil {
t.Fatalf("Failed to build machine: %v", err)
}
interp := NewInterpreter(machine)
interp.Start()
// Should be in the parallel state
if interp.State().Value != "active" {
t.Errorf("Expected state 'active', got %s", interp.State().Value)
}
// Both regions should have their initial states active
if len(interp.State().ActiveInParallel) != 2 {
t.Errorf("Expected 2 active regions, got %d", len(interp.State().ActiveInParallel))
}
// Check specific region states
if interp.State().ActiveInParallel["region1"] != "r1_idle" {
t.Errorf("Expected region1 state 'r1_idle', got %s", interp.State().ActiveInParallel["region1"])
}
if interp.State().ActiveInParallel["region2"] != "r2_idle" {
t.Errorf("Expected region2 state 'r2_idle', got %s", interp.State().ActiveInParallel["region2"])
}
interp.Stop()
}
// TestParallelState_Matches tests the Matches function with parallel states
func TestParallelState_Matches(t *testing.T) {
t.Parallel()
machine, err := NewMachine[struct{}]("parallel_matches").
WithInitial("active").
State("active").Parallel().
Region("region1").
WithInitial("r1_idle").
State("r1_idle").EndState().
State("r1_working").EndState().
EndRegion().
Region("region2").
WithInitial("r2_idle").
State("r2_idle").EndState().
EndRegion().
Done().
Build()
if err != nil {
t.Fatalf("Failed to build machine: %v", err)
}
interp := NewInterpreter(machine)
interp.Start()
// Should match the parallel state
if !interp.Matches("active") {
t.Error("Expected to match 'active'")
}
// Should match states in regions
if !interp.Matches("r1_idle") {
t.Error("Expected to match 'r1_idle'")
}
if !interp.Matches("r2_idle") {
t.Error("Expected to match 'r2_idle'")
}
// Should not match non-active states
if interp.Matches("r1_working") {
t.Error("Should not match 'r1_working'")
}
interp.Stop()
}
// TestParallelState_EventBroadcast tests event broadcasting to regions
func TestParallelState_EventBroadcast(t *testing.T) {
t.Parallel()
type Context struct {
Region1Events int
Region2Events int
}
machine, err := NewMachine[Context]("parallel_broadcast").
WithInitial("active").
WithAction("incR1", func(ctx *Context, e Event) {
ctx.Region1Events++
}).
WithAction("incR2", func(ctx *Context, e Event) {
ctx.Region2Events++
}).
State("active").Parallel().
Region("region1").
WithInitial("r1_idle").
State("r1_idle").
On("GO").Target("r1_working").Do("incR1").
EndState().
State("r1_working").EndState().
EndRegion().
Region("region2").
WithInitial("r2_idle").
State("r2_idle").
On("GO").Target("r2_working").Do("incR2").
EndState().
State("r2_working").EndState().
EndRegion().
Done().
Build()
if err != nil {
t.Fatalf("Failed to build machine: %v", err)
}
interp := NewInterpreter(machine)
interp.Start()
// Both regions should be in idle
if interp.State().ActiveInParallel["region1"] != "r1_idle" {
t.Errorf("Expected region1 'r1_idle', got %s", interp.State().ActiveInParallel["region1"])
}
if interp.State().ActiveInParallel["region2"] != "r2_idle" {
t.Errorf("Expected region2 'r2_idle', got %s", interp.State().ActiveInParallel["region2"])
}
// Send event - should be broadcast to both regions
interp.Send(Event{Type: "GO"})
// Both regions should transition
if interp.State().ActiveInParallel["region1"] != "r1_working" {
t.Errorf("Expected region1 'r1_working', got %s", interp.State().ActiveInParallel["region1"])
}
if interp.State().ActiveInParallel["region2"] != "r2_working" {
t.Errorf("Expected region2 'r2_working', got %s", interp.State().ActiveInParallel["region2"])
}
// Both actions should have executed
if interp.State().Context.Region1Events != 1 {
t.Errorf("Expected Region1Events 1, got %d", interp.State().Context.Region1Events)
}
if interp.State().Context.Region2Events != 1 {
t.Errorf("Expected Region2Events 1, got %d", interp.State().Context.Region2Events)
}
interp.Stop()
}
// TestParallelState_IndependentTransitions tests regions transitioning independently
func TestParallelState_IndependentTransitions(t *testing.T) {
t.Parallel()
machine, err := NewMachine[struct{}]("parallel_independent").
WithInitial("active").
State("active").Parallel().
Region("region1").
WithInitial("r1_idle").
State("r1_idle").
On("R1_GO").Target("r1_working").
EndState().
State("r1_working").EndState().
EndRegion().
Region("region2").
WithInitial("r2_idle").
State("r2_idle").
On("R2_GO").Target("r2_working").
EndState().
State("r2_working").EndState().
EndRegion().
Done().
Build()
if err != nil {
t.Fatalf("Failed to build machine: %v", err)
}
interp := NewInterpreter(machine)
interp.Start()
// Send event only to region1
interp.Send(Event{Type: "R1_GO"})
// Only region1 should transition
if interp.State().ActiveInParallel["region1"] != "r1_working" {
t.Errorf("Expected region1 'r1_working', got %s", interp.State().ActiveInParallel["region1"])
}
if interp.State().ActiveInParallel["region2"] != "r2_idle" {
t.Errorf("Expected region2 still 'r2_idle', got %s", interp.State().ActiveInParallel["region2"])
}
// Now send event to region2
interp.Send(Event{Type: "R2_GO"})
// Both should now be working
if interp.State().ActiveInParallel["region1"] != "r1_working" {
t.Errorf("Expected region1 still 'r1_working', got %s", interp.State().ActiveInParallel["region1"])
}
if interp.State().ActiveInParallel["region2"] != "r2_working" {
t.Errorf("Expected region2 'r2_working', got %s", interp.State().ActiveInParallel["region2"])
}
interp.Stop()
}
// TestParallelState_ExitOnParentTransition tests exiting parallel via parent transition
func TestParallelState_ExitOnParentTransition(t *testing.T) {
t.Parallel()
type Context struct {
EntryCount int
ExitCount int
}
machine, err := NewMachine[Context]("parallel_exit").
WithInitial("active").
WithAction("incEntry", func(ctx *Context, e Event) {
ctx.EntryCount++
}).
WithAction("incExit", func(ctx *Context, e Event) {
ctx.ExitCount++
}).
State("active").Parallel().
OnEntry("incEntry").
OnExit("incExit").
On("CANCEL").Target("cancelled").End().
Region("region1").
WithInitial("r1_working").
State("r1_working").
OnEntry("incEntry").
OnExit("incExit").
EndState().
EndRegion().
Region("region2").
WithInitial("r2_working").
State("r2_working").
OnEntry("incEntry").
OnExit("incExit").
EndState().
EndRegion().
Done().
State("cancelled").Final().
Done().
Build()
if err != nil {
t.Fatalf("Failed to build machine: %v", err)
}
interp := NewInterpreter(machine)
interp.Start()
// Entry actions: parallel + r1_working + r2_working = 3
// (regions are compound states but only have their children's entry actions)
if interp.State().Context.EntryCount != 3 {
t.Errorf("Expected EntryCount 3, got %d", interp.State().Context.EntryCount)
}
// Send CANCEL to exit parallel state
interp.Send(Event{Type: "CANCEL"})
// Should now be in cancelled
if interp.State().Value != "cancelled" {
t.Errorf("Expected state 'cancelled', got %s", interp.State().Value)
}
// Exit actions: r1_working + region1 + r2_working + region2 = 4, then parallel exit is not in the filtered list
// The exitRegion filters to states within the region, which includes region1/region2 themselves
// Total: r1_working + r2_working + parallel = 4 (regions don't have exit actions)
if interp.State().Context.ExitCount != 4 {
t.Errorf("Expected ExitCount 4, got %d", interp.State().Context.ExitCount)
}
// Parallel tracking should be cleared
if len(interp.State().ActiveInParallel) != 0 {
t.Errorf("Expected empty ActiveInParallel, got %d entries", len(interp.State().ActiveInParallel))
}
interp.Stop()
}
// TestParallelState_EntryOrder tests entry action ordering
func TestParallelState_EntryOrder(t *testing.T) {
t.Parallel()
type Context struct {
Order []string
}
machine, err := NewMachine[Context]("parallel_entry_order").
WithInitial("active").
WithAction("enterActive", func(ctx *Context, e Event) {
ctx.Order = append(ctx.Order, "active")
}).
WithAction("enterR1", func(ctx *Context, e Event) {
ctx.Order = append(ctx.Order, "region1")
}).
WithAction("enterR1Idle", func(ctx *Context, e Event) {
ctx.Order = append(ctx.Order, "r1_idle")
}).
WithAction("enterR2", func(ctx *Context, e Event) {
ctx.Order = append(ctx.Order, "region2")
}).
WithAction("enterR2Idle", func(ctx *Context, e Event) {
ctx.Order = append(ctx.Order, "r2_idle")
}).
State("active").Parallel().
OnEntry("enterActive").
Region("region1").
WithInitial("r1_idle").
State("r1_idle").OnEntry("enterR1Idle").EndState().
EndRegion().
Region("region2").
WithInitial("r2_idle").
State("r2_idle").OnEntry("enterR2Idle").EndState().
EndRegion().
Done().
Build()
if err != nil {
t.Fatalf("Failed to build machine: %v", err)
}
interp := NewInterpreter(machine)
interp.Start()
// Parallel state entry should come first
if len(interp.State().Context.Order) < 1 || interp.State().Context.Order[0] != "active" {
t.Errorf("Expected 'active' to be first entry, got %v", interp.State().Context.Order)
}
interp.Stop()
}
// TestParallelState_NativeExport tests Native JSON export of parallel states
func TestParallelState_NativeExport(t *testing.T) {
t.Parallel()
machine, err := NewMachine[struct{}]("parallel").
WithInitial("active").
State("active").Parallel().
Region("r1").WithInitial("s1").
State("s1").EndState().
EndRegion().
Region("r2").WithInitial("s2").
State("s2").EndState().
EndRegion().
Done().
Build()
if err != nil {
t.Fatalf("failed to build machine: %v", err)
}
exporter := export.NewNativeExporter(machine)
jsonStr, err := exporter.ExportJSONIndent("", " ")
if err != nil {
t.Fatalf("failed to export: %v", err)
}
if !strings.Contains(jsonStr, `"type": "parallel"`) {
t.Error("expected type: parallel")
}
}
// TestParallelState_Validation tests validation rules for parallel states
func TestParallelState_Validation(t *testing.T) {
t.Parallel()
t.Run("parallel with no regions fails", func(t *testing.T) {
_, err := NewMachine[struct{}]("no_regions").
WithInitial("active").
State("active").Parallel().
Done().
Build()
if err == nil {
t.Error("Expected validation error for parallel state with no regions")
}
})
t.Run("parallel with valid regions succeeds", func(t *testing.T) {
_, err := NewMachine[struct{}]("valid_parallel").
WithInitial("active").
State("active").Parallel().
Region("r1").
WithInitial("s1").
State("s1").EndState().
EndRegion().
Done().
Build()
if err != nil {
t.Errorf("Expected no error, got: %v", err)
}
})
}
// TestParallelState_TransitionToParallel tests transitioning into a parallel state
func TestParallelState_TransitionToParallel(t *testing.T) {
t.Parallel()
machine, err := NewMachine[struct{}]("transition_to_parallel").
WithInitial("idle").
State("idle").
On("START").Target("active").
Done().
State("active").Parallel().
Region("region1").
WithInitial("r1_working").
State("r1_working").EndState().
EndRegion().
Region("region2").
WithInitial("r2_working").
State("r2_working").EndState().
EndRegion().
Done().
Build()
if err != nil {
t.Fatalf("Failed to build machine: %v", err)
}
interp := NewInterpreter(machine)
interp.Start()
// Should start in idle
if interp.State().Value != "idle" {
t.Errorf("Expected state 'idle', got %s", interp.State().Value)
}
// Transition to parallel state
interp.Send(Event{Type: "START"})
// Should now be in parallel state
if interp.State().Value != "active" {
t.Errorf("Expected state 'active', got %s", interp.State().Value)
}
// Both regions should be active
if interp.State().ActiveInParallel["region1"] != "r1_working" {
t.Errorf("Expected region1 'r1_working', got %s", interp.State().ActiveInParallel["region1"])
}
if interp.State().ActiveInParallel["region2"] != "r2_working" {
t.Errorf("Expected region2 'r2_working', got %s", interp.State().ActiveInParallel["region2"])
}
interp.Stop()
}
// TestParallelState_SimpleWithTransitions tests parallel state with basic transitions
func TestParallelState_SimpleWithTransitions(t *testing.T) {
t.Parallel()
machine, err := NewMachine[struct{}]("parallel_simple").
WithInitial("active").
State("active").Parallel().
Region("region1").
WithInitial("r1_a").
State("r1_a").
On("ADVANCE").Target("r1_b").
EndState().
State("r1_b").EndState().
EndRegion().
Done().
Build()
if err != nil {
t.Fatalf("Failed to build machine: %v", err)
}
interp := NewInterpreter(machine)
interp.Start()
// Should be in the initial state
if interp.State().ActiveInParallel["region1"] != "r1_a" {
t.Errorf("Expected region1 'r1_a', got %s", interp.State().ActiveInParallel["region1"])
}
// Transition within region
interp.Send(Event{Type: "ADVANCE"})
if interp.State().ActiveInParallel["region1"] != "r1_b" {
t.Errorf("Expected region1 'r1_b', got %s", interp.State().ActiveInParallel["region1"])
}
interp.Stop()
}