-
Notifications
You must be signed in to change notification settings - Fork 8
/
state_machine_test.go
168 lines (160 loc) · 3.22 KB
/
state_machine_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
package gobayeux
import "testing"
func TestNewConnectionStateMachineDefaults(t *testing.T) {
csm := NewConnectionStateMachine()
if csm.IsConnected() == true {
t.Error("expected IsConnected() to be false, got true")
}
*csm.currentState = connected
if csm.IsConnected() != true {
t.Error("expected IsConnected() to be true, got false")
}
}
func TestProcessEvent(t *testing.T) {
testCases := []struct {
name string
startingState int32
event Event
shouldErr bool
endingState int32
}{
{
"unconnected state machine gets handshake request sent event",
unconnected,
handshakeSent,
false,
connecting,
},
{
"connected state machine gets handshake request sent event",
connected,
handshakeSent,
true,
connected,
},
{
"unconnected state machine gets successful connect response",
unconnected,
successfullyConnected,
true,
unconnected,
},
{
"unconnected state machine gets unknown event",
unconnected,
"random",
true,
unconnected,
},
{
"unconnected state machine gets timeout",
unconnected,
timeout,
false,
unconnected,
},
{
"connecting state machine gets successfully connected response",
connecting,
successfullyConnected,
false,
connected,
},
{
"connecting state machine gets timeout",
connecting,
timeout,
false,
unconnected,
},
{
"connecting state machine gets disconnect request sent",
connecting,
disconnectSent,
false,
unconnected,
},
{
"connecting state machine gets unknown event",
connecting,
"random",
true,
unconnected,
},
{
"connected state machine gets timeout",
connected,
timeout,
false,
unconnected,
},
{
"connected state machine gets disconnect request sent",
connected,
disconnectSent,
false,
unconnected,
},
{
"connected state machine gets unknown event",
connected,
"random",
true,
unconnected,
},
}
for _, testCase := range testCases {
tc := testCase
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
startingState := tc.startingState
csm := &ConnectionStateMachine{&startingState}
err := csm.ProcessEvent(tc.event)
if tc.shouldErr && err == nil {
t.Error("expected ProcessEvent to error but it didn't")
}
if !tc.shouldErr && err != nil {
t.Errorf("didn't expect ProcessEvent to error but it did: %q", err)
}
if tc.shouldErr && err != nil {
return
}
if tc.endingState != *csm.currentState {
t.Errorf("unexpected ending state: want %d, got %d", tc.endingState, *csm.currentState)
}
})
}
}
func TestCurrentState(t *testing.T) {
testCases := []struct {
name string
state int32
want StateRepresentation
}{
{
name: "connecting",
state: connecting,
want: connectingRepr,
},
{
name: "connected",
state: connected,
want: connectedRepr,
},
{
name: "unconnected",
state: unconnected,
want: unconnectedRepr,
},
}
for _, testCase := range testCases {
tc := testCase
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
csm := &ConnectionStateMachine{&tc.state}
if got := csm.CurrentState(); got != tc.want {
t.Errorf("expected CurrentState() == %s, got %s", tc.want, got)
}
})
}
}