forked from ryanfaerman/fsm
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfsm_test.go
More file actions
185 lines (146 loc) · 5.52 KB
/
fsm_test.go
File metadata and controls
185 lines (146 loc) · 5.52 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
package fsm_test
import (
"errors"
"fmt"
"testing"
"time"
"github.com/nbio/st"
"github.com/processout/fsm"
)
var (
stateNone = fsm.NewState(fsm.String(""))
statePending = fsm.NewState(fsm.String("pending"))
stateStarted = fsm.NewState(fsm.String("started"))
stateFinished = fsm.NewState(fsm.String("finished"))
testError = errors.New("test error")
)
func TestRulesetTransitions(t *testing.T) {
rules := fsm.CreateRuleset(
fsm.NewTransition(statePending, stateStarted),
fsm.NewTransition(stateStarted, stateFinished),
)
examples := []struct {
start fsm.State
goal fsm.State
outcome bool
}{
// A Stater is responsible for setting its default state
{stateNone, stateStarted, false},
{stateNone, statePending, false},
{stateNone, stateFinished, false},
{statePending, stateStarted, true},
{statePending, statePending, false},
{statePending, stateFinished, false},
{stateStarted, stateStarted, false},
{stateStarted, statePending, false},
{stateStarted, stateFinished, true},
}
for i, ex := range examples {
err := rules.Permitted(ex.start, ex.goal)
out := err == nil
st.Expect(t, out, ex.outcome, i)
if out != ex.outcome {
fmt.Println("something wrong happened here", i, err)
}
}
}
func TestRulesetParallelGuarding(t *testing.T) {
rules := fsm.Ruleset{}
rules.AddTransition(fsm.NewTransition(statePending, stateStarted))
rules.AddTransition(fsm.NewTransition(stateStarted, stateFinished))
// Add two failing rules, the slow should be caught first
rules.AddRule(fsm.NewTransition(stateStarted, stateFinished), func(start fsm.State, goal fsm.State) error {
time.Sleep(1 * time.Second)
t.Error("Slow rule should have been short-circuited")
return testError
})
rules.AddRule(fsm.NewTransition(stateStarted, stateFinished), func(start fsm.State, goal fsm.State) error {
return testError
})
st.Expect(t, rules.Permitted(stateStarted, stateFinished).Error(),
"Guard failed from started to finished: "+testError.Error())
}
func TestMachineTransition(t *testing.T) {
rules := fsm.Ruleset{}
rules.AddTransition(fsm.NewTransition(statePending, stateStarted))
rules.AddTransition(fsm.NewTransition(stateStarted, stateFinished))
the_machine := fsm.Machine{
State: statePending,
Rules: &rules,
}
var err error
// should not be able to transition to the current state
err = the_machine.Transition(statePending)
st.Expect(t, err, errors.New("No rules found for pending to pending"))
st.Expect(t, the_machine.State, statePending)
// should not be able to skip states
err = the_machine.Transition(stateFinished)
st.Expect(t, err, errors.New("No rules found for pending to finished"))
st.Expect(t, the_machine.State, statePending)
// should be able to transition to the next valid state
err = the_machine.Transition(stateStarted)
st.Expect(t, err, nil)
st.Expect(t, the_machine.State, stateStarted)
}
func BenchmarkRulesetParallelGuarding(b *testing.B) {
rules := fsm.Ruleset{}
rules.AddTransition(fsm.NewTransition(statePending, stateStarted))
rules.AddTransition(fsm.NewTransition(stateStarted, stateFinished))
// Add two failing rules, one very slow and the other terribly fast
rules.AddRule(fsm.NewTransition(stateStarted, stateFinished), func(start fsm.State, goal fsm.State) error {
time.Sleep(1 * time.Second)
return testError
})
rules.AddRule(fsm.NewTransition(stateStarted, stateFinished), func(start fsm.State, goal fsm.State) error {
return testError
})
b.ResetTimer()
for i := 0; i < b.N; i++ {
rules.Permitted(stateStarted, stateFinished)
}
}
func BenchmarkRulesetTransitionPermitted(b *testing.B) {
// Permitted a transaction requires the transition to be valid and all of its
// guards to pass. Since we have to run every guard and there won't be any
// short-circuiting, this should actually be a little bit slower as a result,
// depending on the number of guards that must pass.
rules := fsm.Ruleset{}
rules.AddTransition(fsm.NewTransition(statePending, stateStarted))
rules.AddTransition(fsm.NewTransition(stateStarted, stateFinished))
some_thing := stateStarted
b.ResetTimer()
for i := 0; i < b.N; i++ {
rules.Permitted(some_thing, stateFinished)
}
}
func BenchmarkRulesetTransitionInvalid(b *testing.B) {
// This should be incredibly fast, since fsm.T{statePending, stateFinished}
// doesn't exist in the Ruleset. We expect some small overhead from creating
// the transition to check the internal map, but otherwise, we should be
// bumping up against the speed of a map lookup itself.
rules := fsm.Ruleset{}
rules.AddTransition(fsm.NewTransition(statePending, stateStarted))
rules.AddTransition(fsm.NewTransition(stateStarted, stateFinished))
some_thing := statePending
b.ResetTimer()
for i := 0; i < b.N; i++ {
rules.Permitted(some_thing, stateFinished)
}
}
func BenchmarkRulesetRuleForbids(b *testing.B) {
// Here, we explicity create a transition that is forbidden. This simulates an
// otherwise valid transition that would be denied based on a user role or the like.
// It should be slower than a standard invalid transition, since we have to
// actually execute a function to perform the check. The first guard to
// fail (returning false) will short circuit the execution, getting some some speed.
rules := fsm.Ruleset{}
rules.AddTransition(fsm.NewTransition(statePending, stateStarted))
rules.AddRule(fsm.NewTransition(stateStarted, stateFinished), func(start fsm.State, goal fsm.State) error {
return testError
})
some_thing := stateStarted
b.ResetTimer()
for i := 0; i < b.N; i++ {
rules.Permitted(some_thing, stateFinished)
}
}