forked from alexcesaro/statsd
-
Notifications
You must be signed in to change notification settings - Fork 4
/
safeconn_test.go
224 lines (202 loc) · 4.34 KB
/
safeconn_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
package statsd
import (
"errors"
"io"
"net"
"testing"
"time"
)
type mockNetConn struct {
read func(p []byte) (n int, err error)
write func(p []byte) (n int, err error)
close func() error
localAddr func() net.Addr
remoteAddr func() net.Addr
setDeadline func(t time.Time) error
setReadDeadline func(t time.Time) error
setWriteDeadline func(t time.Time) error
}
func (m *mockNetConn) Read(p []byte) (n int, err error) {
if m.read != nil {
return m.read(p)
}
panic("implement me")
}
func (m *mockNetConn) Write(p []byte) (n int, err error) {
if m.write != nil {
return m.write(p)
}
panic("implement me")
}
func (m *mockNetConn) Close() error {
if m.close != nil {
return m.close()
}
panic("implement me")
}
func (m *mockNetConn) LocalAddr() net.Addr {
if m.localAddr != nil {
return m.localAddr()
}
panic("implement me")
}
func (m *mockNetConn) RemoteAddr() net.Addr {
if m.remoteAddr != nil {
return m.remoteAddr()
}
panic("implement me")
}
func (m *mockNetConn) SetDeadline(t time.Time) error {
if m.setDeadline != nil {
return m.setDeadline(t)
}
panic("implement me")
}
func (m *mockNetConn) SetReadDeadline(t time.Time) error {
if m.setReadDeadline != nil {
return m.setReadDeadline(t)
}
panic("implement me")
}
func (m *mockNetConn) SetWriteDeadline(t time.Time) error {
if m.setWriteDeadline != nil {
return m.setWriteDeadline(t)
}
panic("implement me")
}
func TestSafeConn_FailsToWriteIfCannotRead(t *testing.T) {
c := &mockNetConn{
setReadDeadline: func(t time.Time) error {
return nil
},
read: func(b []byte) (int, error) {
return 0, io.EOF
},
}
s := SafeConn{
netConn: c,
}
p := []byte("test_key:1|c\n")
n, err := s.Write(p)
if n != 0 {
t.Error("Write() did not return 0 bytes when it failed")
}
if err == nil {
t.Error("Error should have been connection is closed")
}
}
func TestSafeConn_SuccessfullyWritesWhenConnectionOpen(t *testing.T) {
c := &mockNetConn{
setReadDeadline: func(t time.Time) error {
return nil
},
read: func(b []byte) (int, error) {
return 1, nil
},
write: func(b []byte) (int, error) {
return len(b), nil
},
}
s := SafeConn{
netConn: c,
}
p := []byte("test_key:1|c\n")
_, err := s.Write(p)
if err != nil {
t.Errorf("Error should have been nil, but instead it was: %v", err)
}
}
func TestNewSafeConnWithDefaultTimeouts(t *testing.T) {
for _, tc := range [...]struct {
Name string
Conn net.Conn
Err error
}{
{
Name: `failure`,
Err: errors.New(`some error`),
},
{
Name: `success`,
Conn: new(mockNetConn),
},
} {
t.Run(tc.Name, func(t *testing.T) {
type (
DialIn struct {
Network string
Address string
Timeout time.Duration
}
DialOut struct {
Conn net.Conn
Err error
}
)
var (
dialIn = make(chan DialIn)
dialOut = make(chan DialOut)
)
defer close(dialIn)
defer close(dialOut)
defer func() func() {
old := dialTimeout
dialTimeout = func(network, address string, timeout time.Duration) (net.Conn, error) {
dialIn <- DialIn{network, address, timeout}
v := <-dialOut
return v.Conn, v.Err
}
return func() { dialTimeout = old }
}()()
const (
expectedNetwork = `tcp`
expectedAddress = `127.0.0.1:21969`
)
done := make(chan struct{})
go func() {
defer close(done)
if v := <-dialIn; v != (DialIn{Network: expectedNetwork, Address: expectedAddress, Timeout: defaultConnTimeout}) {
t.Errorf("%+v", v)
}
dialOut <- DialOut{tc.Conn, tc.Err}
}()
conn, err := NewSafeConnWithDefaultTimeouts(expectedNetwork, expectedAddress)
if err != tc.Err {
t.Error(conn, err)
}
if (tc.Conn == nil) != (conn == nil) {
t.Error(conn)
} else if conn != nil {
if conn.netConn != tc.Conn {
t.Error(conn.netConn)
}
if conn.connTimeout != defaultConnTimeout {
t.Error(conn.connTimeout)
}
if conn.readTimeout != defaultReadTimeout {
t.Error(conn.readTimeout)
}
}
<-done
})
}
}
func TestSafeConn_Close(t *testing.T) {
a, b := net.Pipe()
conn := SafeConn{
netConn: a,
readTimeout: 1,
}
if conn.connIsClosed() {
t.Error()
}
if err := (&SafeConn{netConn: b}).Close(); err != nil {
t.Error(err)
}
if !conn.connIsClosed() {
t.Error()
}
if err := conn.Close(); err != nil {
t.Error(err)
}
}