-
Notifications
You must be signed in to change notification settings - Fork 19
/
example_test.go
103 lines (75 loc) · 3.7 KB
/
example_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
package statemachine_test
import (
"log"
"github.com/Gurpartap/statemachine-go"
)
var processStates = []string{
"unmonitored", "running", "stopped",
"starting", "stopping", "restarting",
}
type ExampleProcess struct {
statemachine.Machine
IsAutoStartOn bool
IsProcessRunning bool
}
func (p *ExampleProcess) GetIsAutoStartOn() bool {
return p.IsAutoStartOn
}
func (p *ExampleProcess) GetIsProcessRunning() bool {
return p.IsProcessRunning
}
func (ExampleProcess) Start() {}
func (ExampleProcess) Stop() {}
func (ExampleProcess) Restart() {}
func (ExampleProcess) NotifyTriggers(transition statemachine.Transition) {}
func (ExampleProcess) RecordTransition(transition statemachine.Transition) {}
func (ExampleProcess) LogFailure(err error) {
log.Println(err)
}
func Example_systemProcess() {
process := &ExampleProcess{}
process.Machine = statemachine.BuildNewMachine(func(machineBuilder statemachine.MachineBuilder) {
machineBuilder.States(processStates...)
machineBuilder.InitialState("unmonitored")
machineBuilder.Event("tick", func(eventBuilder statemachine.EventBuilder) {
eventBuilder.Transition().From("starting").To("running").If(process.GetIsProcessRunning)
eventBuilder.Transition().From("starting").To("stopped").Unless(process.GetIsProcessRunning)
// The process failed to die after entering the stopping state.
// Change the state to reflect reality.
eventBuilder.Transition().From("running").To("stopped").Unless(process.GetIsProcessRunning)
eventBuilder.Transition().From("stopping").To("running").If(process.GetIsProcessRunning)
eventBuilder.Transition().From("stopping").To("stopped").Unless(process.GetIsProcessRunning)
eventBuilder.Transition().From("stopped").To("running").If(process.GetIsProcessRunning)
eventBuilder.Transition().From("stopped").To("starting").If(func(transition statemachine.Transition) bool {
return process.GetIsAutoStartOn() && !process.GetIsProcessRunning()
})
eventBuilder.Transition().From("restarting").To("running").If(process.GetIsProcessRunning)
eventBuilder.Transition().From("restarting").To("stopped").Unless(process.GetIsProcessRunning)
})
machineBuilder.Event("monitor", func(eventBuilder statemachine.EventBuilder) {
eventBuilder.Transition().From("unmonitored").To("stopped")
})
machineBuilder.Event("start", func(eventBuilder statemachine.EventBuilder) {
eventBuilder.Transition().From("unmonitored", "stopped").To("starting")
})
machineBuilder.Event("stop", func(eventBuilder statemachine.EventBuilder) {
eventBuilder.Transition().From("running").To("stopping")
})
machineBuilder.Event("restart", func(eventBuilder statemachine.EventBuilder) {
eventBuilder.Transition().From("running", "stopped").To("restarting")
})
machineBuilder.Event("unmonitor", func(eventBuilder statemachine.EventBuilder) {
eventBuilder.Transition().FromAny().To("unmonitored")
})
machineBuilder.BeforeTransition().To("starting").Do(func() { process.IsAutoStartOn = true })
machineBuilder.AfterTransition().To("starting").Do(func() { process.Start() })
machineBuilder.BeforeTransition().To("stopping").Do(func() { process.IsAutoStartOn = false })
machineBuilder.AfterTransition().To("stopping").Do(func() { process.Stop() })
machineBuilder.BeforeTransition().To("restarting").Do(func() { process.IsAutoStartOn = true })
machineBuilder.AfterTransition().To("restarting").Do(func() { process.Restart() })
machineBuilder.BeforeTransition().To("unmonitored").Do(func() { process.IsAutoStartOn = false })
machineBuilder.BeforeTransition().Any().Do(process.NotifyTriggers)
machineBuilder.AfterTransition().Any().Do(process.RecordTransition)
machineBuilder.AfterFailure().OnAnyEvent().Do(process.LogFailure)
})
}