-
Notifications
You must be signed in to change notification settings - Fork 19
/
msg_flow.go
executable file
·303 lines (258 loc) · 5.76 KB
/
msg_flow.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package mock
import (
"errors"
"fmt"
"io"
"strings"
"time"
)
// A Conn defines an abstract interface for connections used with a Flow.
type Conn interface {
Send(pkt interface{}) error
Receive() (interface{}, error)
Close() error
}
// The Pipe pipes packets from Send to Receive.
type Pipe struct {
pipe chan interface{}
close chan struct{}
}
// NewPipe returns a new Pipe.
func NewPipe() *Pipe {
return &Pipe{
pipe: make(chan interface{}),
close: make(chan struct{}),
}
}
// Send returns packet on next Receive call.
func (conn *Pipe) Send(pkt interface{}) error {
select {
case conn.pipe <- pkt:
return nil
case <-conn.close:
return errors.New("closed")
}
}
// Receive returns the packet being sent with Send.
func (conn *Pipe) Receive() (interface{}, error) {
select {
case pkt := <-conn.pipe:
return pkt, nil
case <-conn.close:
return nil, io.EOF
}
}
// Close will close the conn and let Send and Receive return errors.
func (conn *Pipe) Close() error {
close(conn.close)
return nil
}
// All available action types.
const (
actionSend byte = iota
actionReceive
actionSkip
actionRun
actionClose
actionEnd
)
// An Action is a step in a flow.
type action struct {
kind byte
packets []interface{}
fn func()
ch chan struct{}
duration time.Duration
}
// A Flow is a sequence of actions that can be tested against a connection.
type Flow struct {
debug bool
actions []action
}
// NewFlow returns a new flow.
func NewFlow() *Flow {
return &Flow{
actions: make([]action, 0),
}
}
// Debug will activate the debug mode.
func (f *Flow) Debug() *Flow {
f.debug = true
return f
}
// Send will send the specified packets.
func (f *Flow) Send(pkts ...interface{}) *Flow {
f.add(action{
kind: actionSend,
packets: pkts,
})
return f
}
// Receive will receive and match the specified packets out of order.
func (f *Flow) Receive(pkts ...interface{}) *Flow {
f.add(action{
kind: actionReceive,
packets: pkts,
})
return f
}
// Skip will receive the specified packets without matching out of order.
func (f *Flow) Skip(pkts ...interface{}) *Flow {
f.add(action{
kind: actionSkip,
packets: pkts,
})
return f
}
// Run will call the supplied function and wait until it returns.
func (f *Flow) Run(fn func()) *Flow {
f.add(action{
kind: actionRun,
fn: fn,
})
return f
}
// Close will immediately close the connection.
func (f *Flow) Close() *Flow {
f.add(action{
kind: actionClose,
})
return f
}
// End will match proper connection close.
func (f *Flow) End() *Flow {
f.add(action{
kind: actionEnd,
})
return f
}
// Test starts the flow on the given Conn and reports to the specified test.
func (f *Flow) Test(conn Conn) error {
// handle all actions
for _, action := range f.actions {
err := f.test(conn, action)
if err != nil {
return err
}
}
return nil
}
func (f *Flow) test(conn Conn, action action) error {
switch action.kind {
case actionSend:
// send all saved packets
for _, pkt := range action.packets {
if f.debug {
fmt.Println(time.Now(), "sending packet:", pkt, "...")
}
// send a single packet
err := conn.Send(pkt)
if err != nil {
return fmt.Errorf("error sending packet: %v", err)
}
}
case actionReceive:
// initialize store
store := make(map[int]string)
for i, pkt := range action.packets {
store[i] = fmt.Sprintf("%v", pkt)
}
receive:
// keep going until we have all packets
for len(store) > 0 {
if f.debug {
fmt.Println(time.Now(), "receiving packet ...")
}
// wait for next packet
pkt, err := conn.Receive()
if err != nil {
return fmt.Errorf("expected to receive a packet but got error: %v", err)
}
if f.debug {
fmt.Println(time.Now(), "received packet:", pkt)
}
// check packet
for i, p := range store {
if p == fmt.Sprintf("%v", pkt) {
delete(store, i)
continue receive
}
}
return fmt.Errorf("unexpected packet %q", pkt)
}
case actionSkip:
// initialize store
store := make(map[int]string)
for i, pkt := range action.packets {
store[i] = fmt.Sprintf("%T", pkt)
}
skip:
// keep going until we have all packets
for len(store) > 0 {
if f.debug {
fmt.Printf("skiping packet...\n")
}
// wait for next packet
pkt, err := conn.Receive()
if err != nil {
return fmt.Errorf("expected to skip over a received packet but got error: %v", err)
}
if f.debug {
fmt.Printf("received packet: %T", pkt)
}
// check packet
for i, p := range store {
if p == fmt.Sprintf("%T", pkt) {
delete(store, i)
continue skip
}
}
return fmt.Errorf("unexpected to receive a packet of type %T", pkt)
}
case actionRun:
if f.debug {
fmt.Printf("running...\n")
}
// run function
action.fn()
case actionClose:
if f.debug {
fmt.Printf("closing...\n")
}
// close connection
err := conn.Close()
if err != nil {
return fmt.Errorf("expected connection to close successfully but got error: %v", err)
}
case actionEnd:
if f.debug {
fmt.Printf("ending...\n")
}
// wait for end of file
pkt, err := conn.Receive()
if err != nil && !strings.Contains(err.Error(), "EOF") {
return fmt.Errorf("expected EOF but got %v", err)
}
if pkt != nil {
return fmt.Errorf("expected no packet but got %v", pkt)
}
}
return nil
}
// TestAsync starts the flow on the given Conn and reports to the specified test
// asynchronously.
func (f *Flow) TestAsync(conn Conn, timeout time.Duration) <-chan error {
errCh := make(chan error, 1)
go func() {
select {
case <-time.After(timeout):
errCh <- errors.New("timed out waiting for flow to complete")
case errCh <- f.Test(conn):
}
}()
return errCh
}
// add will add the specified action.
func (f *Flow) add(action action) {
f.actions = append(f.actions, action)
}