-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexamples_state_test.go
51 lines (44 loc) · 1.28 KB
/
examples_state_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
package fsm
// Our special state type.
type SpecialState int
// Transition errors will contain helpful text if our State implements fmt.Stringer.
func (s SpecialState) String() string {
return specialStateStrings[s]
}
// Define our valid states.
const (
SpecialStateNotRunning SpecialState = iota
SpecialStateStarting
SpecialStateRunning
SpecialStateStopping
SpecialStateStopped
SpecialStateEnd
)
// Define string representations for each state.
var specialStateStrings = []string{
"NOT_RUNNING",
"STARTING",
"RUNNING",
"STOPPING",
"STOPPED",
"DONE",
}
func ExampleState() {
// Create an instance of the state machine with valid state transitions.
sm := New().
Allow(SpecialStateNotRunning, SpecialStateStarting).
Allow(SpecialStateStarting, SpecialStateRunning).
Allow(SpecialStateRunning, SpecialStateStopping).
Allow(SpecialStateStopping, SpecialStateStopped).
Allow(SpecialStateStopped, SpecialStateEnd).
Start(SpecialStateNotRunning)
// Create a new instance of the state machine.
smi, err := sm.NewInstance()
if err != nil {
panic(err)
}
// Attempt to transition through a series of states.
for _, s := range []SpecialState{SpecialStateStarting, SpecialStateRunning, SpecialStateStopping, SpecialStateStopped, SpecialStateEnd} {
smi.MustTransition(s)
}
}