-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfsm_test.go
287 lines (228 loc) · 9.1 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package fsm
import (
"fmt"
"testing"
)
//the code is taken from https://github.com/arunma/AkkaFSM/blob/master/src/test/scala/me/rerun/akka/fsm/CoffeeSpec.scala
var Open State = "Open"
var ReadyToBuy State = "ReadyToBuy"
var PoweredOff State = "PoweredOff"
type MachineData struct {
currentTxTotal int
costOfCoffee int
coffeesLeft int
}
type Deposit struct {
value int
}
type Balance struct {
value int
}
type Cancel struct{}
type BrewCoffee struct{}
type GetCostOfCoffee struct{}
type ShutDownMachine struct{}
type StartUpMachine struct{}
type SetNumberOfCoffee struct {
quantity int
}
type SetCostOfCoffee struct {
price int
}
type GetNumberOfCoffee struct{}
func newMachineData(currentTxTotal int, costOfCoffee int, coffeesLeft int) *MachineData {
return &MachineData{currentTxTotal: currentTxTotal, costOfCoffee: costOfCoffee, coffeesLeft: coffeesLeft}
}
func newCoffeeMachine() *FSM {
cm := NewFSM()
cm.StartWith(Open, newMachineData(0, 5, 1))
cm.When(Open)(
func(event *Event) *NextState {
machineData, machineDataOk := event.Data.(*MachineData)
deposit, depositOk := event.Message.(*Deposit)
setNumberOfCoffee, setNumberOfCoffeeOk := event.Message.(*SetNumberOfCoffee)
_, getNumberOfCoffeeOk := event.Message.(*GetNumberOfCoffee)
setCostOfCoffee, setCostOfCoffeeOk := event.Message.(*SetCostOfCoffee)
_, getCostOfCoffeeOk := event.Message.(*GetCostOfCoffee)
switch {
case machineDataOk && machineData.coffeesLeft <= 0:
return cm.Goto(PoweredOff)
case depositOk && machineDataOk && (deposit.value+machineData.currentTxTotal >= machineData.costOfCoffee):
txTotal := machineData.currentTxTotal + deposit.value
newData := newMachineData(txTotal, machineData.costOfCoffee, machineData.coffeesLeft)
return cm.Goto(ReadyToBuy).With(newData)
case depositOk && machineDataOk && (deposit.value+machineData.currentTxTotal < machineData.costOfCoffee):
txTotal := machineData.currentTxTotal + deposit.value
newData := newMachineData(txTotal, machineData.costOfCoffee, machineData.coffeesLeft)
return cm.Stay().With(newData)
case setNumberOfCoffeeOk && machineDataOk:
fmt.Printf("Set new number of coffee: %d\n", setNumberOfCoffee.quantity)
newData := newMachineData(machineData.currentTxTotal, machineData.costOfCoffee, setNumberOfCoffee.quantity)
return cm.Stay().With(newData)
case getNumberOfCoffeeOk && machineDataOk:
fmt.Printf("Coffees left: %d\n", machineData.coffeesLeft)
return cm.Stay()
case setCostOfCoffeeOk && machineDataOk:
fmt.Printf("Set new coffee price: %d\n", setCostOfCoffee.price)
newData := newMachineData(machineData.currentTxTotal, setCostOfCoffee.price, machineData.coffeesLeft)
return cm.Stay().With(newData)
case getCostOfCoffeeOk && machineDataOk:
fmt.Printf("Cost of coffee: %d\n", machineData.costOfCoffee)
return cm.Stay()
}
return cm.DefaultHandler()(event)
})
cm.When(ReadyToBuy)(
func(event *Event) *NextState {
machineData, machineDataOk := event.Data.(*MachineData)
_, brewCoffeeOk := event.Message.(*BrewCoffee)
if brewCoffeeOk && machineDataOk {
balanceToBeDispensed := machineData.currentTxTotal - machineData.costOfCoffee
if balanceToBeDispensed > 0 {
fmt.Printf("Balance to be dispensed is %d\n", balanceToBeDispensed)
newData := newMachineData(0, machineData.costOfCoffee, machineData.coffeesLeft-1)
return cm.Goto(Open).With(newData)
}
newData := newMachineData(0, machineData.costOfCoffee, machineData.coffeesLeft-1)
return cm.Goto(Open).With(newData)
}
return cm.DefaultHandler()(event)
})
cm.When(PoweredOff)(
func(event *Event) *NextState {
_, startUpMachineOk := event.Message.(*StartUpMachine)
if startUpMachineOk {
return cm.Goto(Open)
}
fmt.Printf("Machine Powered down. Please start machine first with StartUpMachine")
return cm.Stay()
})
cm.SetDefaultHandler(
func(event *Event) *NextState {
_, shutDownMachineOk := event.Message.(*ShutDownMachine)
_, cancelOk := event.Message.(*Cancel)
machineData, machineDataOk := event.Data.(*MachineData)
switch {
case shutDownMachineOk && machineDataOk:
fmt.Printf("Balance is: %d\n", machineData.currentTxTotal)
newData := newMachineData(0, machineData.costOfCoffee, machineData.coffeesLeft)
return cm.Goto(PoweredOff).With(newData)
case cancelOk && machineDataOk:
fmt.Printf("Balance is: %d\n", machineData.currentTxTotal)
newData := newMachineData(0, machineData.costOfCoffee, machineData.coffeesLeft)
return cm.Goto(Open).With(newData)
}
panic("Something went wrong")
})
cm.OnTransition(
func(from State, to State) {
switch {
case from == Open && to == ReadyToBuy:
fmt.Println("From Transacting to ReadyToBuy")
case from == ReadyToBuy && to == Open:
fmt.Println("From ReadyToBuy to Open")
}
})
return cm
}
func TestCoffeeMachine(t *testing.T) {
//should allow setting and getting of price of coffee
coffeeMachine := newCoffeeMachine()
coffeeMachine.Send(&SetCostOfCoffee{7})
coffeeMachine.Send(&GetCostOfCoffee{})
machineData := coffeeMachine.CurrentData().(*MachineData)
if machineData.costOfCoffee != 7 {
t.Errorf("Cost of coffee is not 7. Price: %d", machineData.costOfCoffee)
}
//should allow setting and getting of maximum number of coffees
coffeeMachine = newCoffeeMachine()
coffeeMachine.Send(&SetNumberOfCoffee{10})
coffeeMachine.Send(&GetNumberOfCoffee{})
machineData = coffeeMachine.CurrentData().(*MachineData)
if machineData.coffeesLeft != 10 {
t.Errorf("Number of coffee is not 10. Number: %d", machineData.coffeesLeft)
}
//should stay at Transacting when the Deposit is less then the price of the coffee
coffeeMachine = newCoffeeMachine()
coffeeMachine.Send(&SetCostOfCoffee{5})
coffeeMachine.Send(&SetNumberOfCoffee{10})
if coffeeMachine.CurrentState() != Open {
t.Errorf("Current state is not Open. State: %s", coffeeMachine.CurrentState())
}
coffeeMachine.Send(&Deposit{2})
coffeeMachine.Send(&GetNumberOfCoffee{})
machineData = coffeeMachine.CurrentData().(*MachineData)
if machineData.coffeesLeft != 10 {
t.Errorf("Number of coffee is not 10. Number: %d", machineData.coffeesLeft)
}
if machineData.currentTxTotal != 2 {
t.Errorf("Deposit is not 2. Deposit: %d", machineData.currentTxTotal)
}
//should transition to ReadyToBuy and then Open when the Deposit is equal to the price of the coffee
coffeeMachine = newCoffeeMachine()
coffeeMachine.Send(&SetCostOfCoffee{5})
coffeeMachine.Send(&SetNumberOfCoffee{10})
if coffeeMachine.CurrentState() != Open {
t.Errorf("Current state is not Open. State: %s", coffeeMachine.CurrentState())
}
coffeeMachine.Send(&Deposit{5})
coffeeMachine.Send(&BrewCoffee{})
coffeeMachine.Send(&GetNumberOfCoffee{})
machineData = coffeeMachine.CurrentData().(*MachineData)
if machineData.coffeesLeft != 9 {
t.Errorf("Number of coffee is not 9. Number: %d", machineData.coffeesLeft)
}
//should transition to Open after flushing out all the deposit when the coffee is canceled
coffeeMachine = newCoffeeMachine()
coffeeMachine.Send(&SetCostOfCoffee{5})
coffeeMachine.Send(&SetNumberOfCoffee{10})
if coffeeMachine.CurrentState() != Open {
t.Errorf("Current state is not Open. State: %s", coffeeMachine.CurrentState())
}
coffeeMachine.Send(&Deposit{2})
coffeeMachine.Send(&Deposit{2})
coffeeMachine.Send(&Deposit{2})
machineData = coffeeMachine.CurrentData().(*MachineData)
if machineData.currentTxTotal != 6 {
t.Errorf("Deposit is not 6. Deposit: %d", machineData.currentTxTotal)
}
coffeeMachine.Send(&Cancel{})
machineData = coffeeMachine.CurrentData().(*MachineData)
if machineData.currentTxTotal != 0 {
t.Errorf("Deposit is not 0. Deposit: %d", machineData.currentTxTotal)
}
//should transition to PoweredOff state if the machine is shut down from ReadyToBuyState
coffeeMachine = newCoffeeMachine()
coffeeMachine.Send(&SetCostOfCoffee{5})
coffeeMachine.Send(&SetNumberOfCoffee{10})
if coffeeMachine.CurrentState() != Open {
t.Errorf("Current state is not Open. State: %s", coffeeMachine.CurrentState())
}
coffeeMachine.Send(&Deposit{2})
coffeeMachine.Send(&Deposit{2})
coffeeMachine.Send(&Deposit{2})
if coffeeMachine.CurrentState() != ReadyToBuy {
t.Errorf("Current state is not ReadyToBuy. State: %s", coffeeMachine.CurrentState())
}
coffeeMachine.Send(&ShutDownMachine{})
if coffeeMachine.CurrentState() != PoweredOff {
t.Errorf("Current state is not PoweredOff. State: %s", coffeeMachine.CurrentState())
}
machineData = coffeeMachine.CurrentData().(*MachineData)
if machineData.currentTxTotal != 0 {
t.Errorf("Deposit is not 0. Deposit: %d", machineData.currentTxTotal)
}
//should open the machine to operation if powered on
coffeeMachine = newCoffeeMachine()
coffeeMachine.Send(&SetCostOfCoffee{5})
coffeeMachine.Send(&SetNumberOfCoffee{10})
if coffeeMachine.CurrentState() != Open {
t.Errorf("Current state is not Open. State: %s", coffeeMachine.CurrentState())
}
coffeeMachine.Send(&Deposit{2})
coffeeMachine.Send(&Deposit{2})
coffeeMachine.Send(&ShutDownMachine{})
if coffeeMachine.CurrentState() != PoweredOff {
t.Errorf("Current state is not PoweredOff. State: %s", coffeeMachine.CurrentState())
}
}