-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathparallel_test.go
88 lines (80 loc) · 2.48 KB
/
parallel_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
package statemachine_test
import (
"errors"
"testing"
"github.com/dipeshdulal/statemachine"
"github.com/stretchr/testify/assert"
)
func TestParallelToggle(t *testing.T) {
times := 0
machineOne := statemachine.Machine{
ID: "machine-1",
Initial: "on",
States: statemachine.StateMap{
"on": statemachine.MachineState{
On: statemachine.TransitionMap{
"TOGGLE": statemachine.MachineTransition{
To: "off",
},
},
},
"off": statemachine.MachineState{
On: statemachine.TransitionMap{
"TOGGLE": statemachine.MachineTransition{
To: "on",
},
},
},
},
}
machineTwo := statemachine.Machine{
ID: "machine-2",
Initial: "on",
States: statemachine.StateMap{
"on": statemachine.MachineState{
On: statemachine.TransitionMap{
"TOGGLE": statemachine.MachineTransition{
To: "off",
},
},
},
"off": statemachine.MachineState{
On: statemachine.TransitionMap{
"TOGGLE": statemachine.MachineTransition{
To: "on",
},
},
},
},
}
parallel := statemachine.ParallelMachine{
Machines: statemachine.Machines{
"machine-1": &machineOne,
"machine-2": &machineTwo,
},
Subscribers: statemachine.ParallelSubscribers{
func(c, n statemachine.ParallelState) { times++ },
func(c, n statemachine.ParallelState) { times++ },
func(c, n statemachine.ParallelState) { times++ },
},
}
next, err := parallel.Transition("machine-1.TOGGLE")
assert.Equal(t, statemachine.ParallelState{"machine-1": "off", "machine-2": "on"}, next, "Transition should occur on toggle.")
assert.Equal(t, nil, err, "Error should not occurr in correct transition")
assert.Equal(t, 3, times, "Subscribers should be called on transition")
next, err = parallel.Transition("machine-one")
if assert.Error(t, err) {
assert.Equal(t, errors.New("event format doesn't match"), err)
assert.Equal(t, next, statemachine.ParallelState{"machine-1": "off", "machine-2": "on"}, "Transition should not occur on error.")
} else {
t.Error("error should occurr when key format doesn't match")
}
next, err = parallel.Transition("machine-one.TOGGLE")
if assert.Error(t, err) {
assert.Equal(t, errors.New("machine key doesnot match"), err)
assert.Equal(t, next, statemachine.ParallelState{"machine-1": "off", "machine-2": "on"}, "Transition should not occur on error.")
} else {
t.Error("error should occurr when machine key doesnot exist")
}
assert.Equal(t, 3, times, "Subscribers shouldnot be called on error on transition")
}