-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.go
268 lines (227 loc) · 5.02 KB
/
server.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
package redimock
import (
"context"
"fmt"
"io"
"net"
"strings"
"sync"
"time"
)
// Command is a single
type Command struct {
command string
argCompare func(...string) bool
responses []interface{}
count int
terminate bool
delay time.Duration
lock sync.RWMutex
called int
}
// Result is the function that can be used for advanced result value
type Result = func(...string) []interface{}
// Server is the mock server used for handling the connections
type Server struct {
listener net.Listener
expectList []*Command
lock sync.RWMutex
unexpectedCommands [][]string
}
// NewServer makes a server listening on addr. Close with .Close().
func NewServer(ctx context.Context, addr string) (*Server, error) {
s := Server{}
l, err := net.Listen("tcp", addr)
if err != nil {
return nil, err
}
s.listener = l
go s.serve()
go func() {
<-ctx.Done()
_ = l.Close()
}()
return &s, nil
}
func (s *Server) serve() {
for {
conn, err := s.listener.Accept()
if err != nil {
return
}
go func() {
// TODO : add this err to log?
_ = s.serveConn(conn)
}()
}
}
// ServeConn handles a connection
func (s *Server) serveConn(conn io.ReadWriteCloser) error {
defer func() {
_ = conn.Close()
}()
for {
args, err := readArray(conn)
if err != nil {
// Close the connection and return, error in client should not break the server
return err
}
var cmd *Command
for i := range s.expectList {
if s.expectList[i].compare(args) {
cmd = s.expectList[i]
cmd.increase()
break
}
}
if cmd == nil {
// Return error *and continue?*
if err := write(conn, Error("command not expected")); err != nil {
return err // this means the write was not successful , close the connection
}
s.lock.Lock()
s.unexpectedCommands = append(s.unexpectedCommands, args)
s.lock.Unlock()
continue
}
if cmd.delay > 0 {
time.Sleep(cmd.delay)
}
rsp := cmd.responses
if len(rsp) == 1 {
fn, ok := rsp[0].(Result)
if ok {
rsp = fn(args[1:]...)
}
}
if err := write(conn, rsp...); err != nil {
// write failed, return and close the connection
return err
}
if cmd.terminate {
return nil
}
}
}
// Addr has the net.Addr struct
func (s *Server) Addr() *net.TCPAddr {
return s.listener.Addr().(*net.TCPAddr)
}
// ExpectationsWereMet return nil if the all expects match or error if not
func (s *Server) ExpectationsWereMet() error {
s.lock.RLock()
var all []error
for i := range s.expectList {
if err := s.expectList[i].error(); err != nil {
all = append(all, err)
}
}
for i := range s.unexpectedCommands {
all = append(all, fmt.Errorf(
"command %s is called but not expected",
strings.Join(s.unexpectedCommands[i], " ")),
)
}
s.lock.RUnlock()
var str string
if len(all) > 0 {
for i := range all {
str += all[i].Error() + "\n"
}
return fmt.Errorf(str)
}
return nil
}
// Expect return a command
func (s *Server) Expect(command string) *Command {
c := &Command{
command: strings.ToUpper(command),
}
s.expectList = append(s.expectList, c)
return c
}
// WithArgs add array as arguments
func (c *Command) WithArgs(args ...string) *Command {
return c.WithFnArgs(func(s ...string) bool {
if len(s) != len(args) {
return false
}
return equalArgs(s, args)
})
}
// WithAnyArgs if any argument is ok
func (c *Command) WithAnyArgs() *Command {
return c.WithFnArgs(func(...string) bool {
return true
})
}
// WithFnArgs is advanced function compare for arguments
func (c *Command) WithFnArgs(f func(...string) bool) *Command {
// TODO : may be panic() if the function already set
c.argCompare = f
return c
}
// WillReturn set the return value for this command
func (c *Command) WillReturn(ret ...interface{}) *Command {
c.responses = ret
return c
}
// WillReturnFn is a helper function for advance return value
func (c *Command) WillReturnFn(r Result) *Command {
return c.WillReturn(r)
}
// WithDelay return command with delay
func (c *Command) WithDelay(d time.Duration) *Command {
c.delay = d
return c
}
// Once means it should be called once
func (c *Command) Once() *Command {
c.count = 1
return c
}
// Any means this can be called 0 to n time
func (c *Command) Any() *Command {
c.count = -1
return c
}
// Times this should be called n times
func (c *Command) Times(n int) *Command {
c.count = n
return c
}
// CloseConnection should close connection after this command
func (c *Command) CloseConnection() *Command {
c.terminate = true
return c
}
func (c *Command) compare(input []string) bool {
if len(input) < 1 {
return false
}
if strings.ToUpper(input[0]) != c.command {
return false
}
if c.argCompare != nil {
return c.argCompare(input[1:]...)
}
return len(input) == 1
}
func (c *Command) error() error {
c.lock.RLock()
defer c.lock.RUnlock()
if c.count < 0 || c.count == c.called {
return nil
}
return fmt.Errorf(
`command "%s" expected %d time called %d times`,
c.command,
c.count,
c.called,
)
}
func (c *Command) increase() {
c.lock.Lock()
c.called++
c.lock.Unlock()
}