forked from ryanfaerman/fsm
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfsm_test.go
208 lines (158 loc) · 5.72 KB
/
fsm_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
package fsm_test
import (
"errors"
"testing"
"time"
"github.com/nbio/st"
"github.com/nozim/fsm"
)
// add sample comment here for sake or PR
// test comment here
type Thing struct {
State fsm.State
}
func (t *Thing) CurrentState() fsm.State { return t.State }
func (t *Thing) SetState(s fsm.State) { t.State = s }
func TestFooBaz2(t *testing.T) {
st.Expect(t, 40,10*4)
}
func TestFooBaz(t *testing.T) {
st.Expect(t, 40,10*4)
}
func TestSomeNewFancyTest(t *testing.T) {
st.Expect(t, 40,10*4)
}
func TestIt(t *testing.T) {
st.Expect(t, 4,4)
}
func TestSomething(t *testing.T) {
st.Expect(t,2+2,4)
}
func TestSomeOtherThing(t *testing.T) {
st.Expect(t, 5, 10-5)
}
func TestSomeOtherThingTwo(t *testing.T) {
st.Expect(t, 5+5, 10)
}
func TestRulesetTransitions(t *testing.T) {
rules := fsm.CreateRuleset(
fsm.T{"pending", "started"},
fsm.T{"started", "finished"},
)
examples := []struct {
subject fsm.Stater
goal fsm.State
outcome error
}{
// A Stater is responsible for setting its default state
{&Thing{}, "started", fsm.InvalidTransition},
{&Thing{}, "pending", fsm.InvalidTransition},
{&Thing{}, "finished", fsm.InvalidTransition},
{&Thing{State: "pending"}, "started", nil},
{&Thing{State: "pending"}, "pending", fsm.InvalidTransition},
{&Thing{State: "pending"}, "finished", fsm.InvalidTransition},
{&Thing{State: "started"}, "started", fsm.InvalidTransition},
{&Thing{State: "started"}, "pending", fsm.InvalidTransition},
{&Thing{State: "started"}, "finished", nil},
}
for i, ex := range examples {
st.Expect(t, rules.Permitted(ex.subject, ex.goal), ex.outcome, i)
}
}
func TestRulesetParallelGuarding(t *testing.T) {
rules := fsm.Ruleset{}
rules.AddTransition(fsm.T{"pending", "started"})
rules.AddTransition(fsm.T{"started", "finished"})
// Add two failing rules, the slow should be caught first
rules.AddRule(fsm.T{"started", "finished"}, func(subject fsm.Stater, goal fsm.State) error {
time.Sleep(1 * time.Second)
t.Error("Slow rule should have been short-circuited")
return errors.New("Slow guard")
})
rules.AddRule(fsm.T{"started", "finished"}, func(subject fsm.Stater, goal fsm.State) error {
return errors.New("Always reject guard")
})
st.Expect(t, rules.Permitted(&Thing{State: "started"}, "finished"), errors.New("Always reject guard"))
}
func TestMachineTransition(t *testing.T) {
rules := fsm.Ruleset{}
rules.AddTransition(fsm.T{"pending", "started"})
rules.AddTransition(fsm.T{"started", "finished"})
some_thing := Thing{State: "pending"}
the_machine := fsm.New(fsm.WithRules(rules), fsm.WithSubject(&some_thing))
var err error
// should not be able to transition to the current state
err = the_machine.Transition("pending")
st.Expect(t, err, fsm.InvalidTransition)
st.Expect(t, some_thing.State, fsm.State("pending"))
// should not be able to skip states
err = the_machine.Transition("finished")
st.Expect(t, err, fsm.InvalidTransition)
st.Expect(t, some_thing.State, fsm.State("pending"))
// should be able to transition to the next valid state
err = the_machine.Transition("started")
st.Expect(t, err, nil)
st.Expect(t, some_thing.State, fsm.State("started"))
}
func BenchmarkRulesetParallelGuarding(b *testing.B) {
rules := fsm.Ruleset{}
rules.AddTransition(fsm.T{"pending", "started"})
rules.AddTransition(fsm.T{"started", "finished"})
// Add two failing rules, one very slow and the other terribly fast
rules.AddRule(fsm.T{"started", "finished"}, func(subject fsm.Stater, goal fsm.State) error {
time.Sleep(1 * time.Second)
return errors.New("Slow guard")
})
rules.AddRule(fsm.T{"started", "finished"}, func(subject fsm.Stater, goal fsm.State) error {
return errors.New("Failing guard")
})
b.ResetTimer()
for i := 0; i < b.N; i++ {
rules.Permitted(&Thing{State: "started"}, "finished")
}
}
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.T{"pending", "started"})
rules.AddTransition(fsm.T{"started", "finished"})
some_thing := &Thing{State: "started"}
b.ResetTimer()
for i := 0; i < b.N; i++ {
rules.Permitted(some_thing, "finished")
}
}
func BenchmarkRulesetTransitionInvalid(b *testing.B) {
// This should be incredibly fast, since fsm.T{"pending", "finished"}
// 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.T{"pending", "started"})
rules.AddTransition(fsm.T{"started", "finished"})
some_thing := &Thing{State: "pending"}
b.ResetTimer()
for i := 0; i < b.N; i++ {
rules.Permitted(some_thing, "finished")
}
}
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.T{"pending", "started"})
rules.AddRule(fsm.T{"started", "finished"}, func(subject fsm.Stater, goal fsm.State) error {
return errors.New("Failing guard")
})
some_thing := &Thing{State: "started"}
b.ResetTimer()
for i := 0; i < b.N; i++ {
rules.Permitted(some_thing, "finished")
}
}