-
Notifications
You must be signed in to change notification settings - Fork 36
/
memconn_test.go
369 lines (323 loc) · 9.28 KB
/
memconn_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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
package memconn_test
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"math/rand"
"net"
"os"
"strconv"
"strings"
"testing"
"time"
"github.com/akutz/memconn"
)
// TestMemuRace is for verifying there is no race condition when
// calling Dial and Listen concurrently for the same address. The
// Provider.Dial function did not used to initialize the Provider.cnxns
// map since the function only queries it, and querying a nil map is
// allowed. However, Go detected a race condition when the field was
// both assigned and queried at the same time.
//
// Many thanks to @vburenin for spotting this!
//
// $ go test -race -run TestMemuRace
func TestMemuRace(t *testing.T) {
for i := 0; i < 1000; i++ {
p := &memconn.Provider{}
addr := strconv.Itoa(i)
go func(p *memconn.Provider, addr string) {
if c, err := p.Listen("memu", addr); err == nil {
go c.Accept()
}
}(p, addr)
go func(p *memconn.Provider, addr string) {
if c, err := p.Dial("memu", addr); err == nil {
c.Close()
}
}(p, addr)
}
}
func TestMembRace(t *testing.T) {
for i := 0; i < 1000; i++ {
p := &memconn.Provider{}
addr := strconv.Itoa(i)
go func(p *memconn.Provider, addr string) {
if c, err := p.Listen("memb", addr); err == nil {
go c.Accept()
}
}(p, addr)
go func(p *memconn.Provider, addr string) {
if c, err := p.Dial("memb", addr); err == nil {
c.Close()
}
}(p, addr)
}
}
// TestMemuNoDeadline validates that the memu connection properly implements
// the net.Conn interface's deadline semantics.
func TestMemuNoDeadline(t *testing.T) {
testMemConnDeadline(t, "memu", 0, 0)
}
func TestMembNoDeadline(t *testing.T) {
testMemConnDeadline(t, "memb", 0, 0)
}
// TestMemuDeadline validates that the memu connection properly implements
// the net.Conn interface's deadline semantics.
func TestMemuDeadline(t *testing.T) {
testMemConnDeadline(
t, "memu", time.Duration(3)*time.Second, time.Duration(3)*time.Second)
}
func TestMembDeadline(t *testing.T) {
testMemConnDeadline(
t, "memb", time.Duration(3)*time.Second, time.Duration(3)*time.Second)
}
// TestMemuWriteDeadline validates that the memu connection properly implements
// the net.Conn interface's write deadline semantics.
func TestMemuWriteDeadline(t *testing.T) {
testMemConnDeadline(t, "memu", time.Duration(3)*time.Second, 0)
}
func TestMembWriteDeadline(t *testing.T) {
testMemConnDeadline(t, "memb", time.Duration(3)*time.Second, 0)
}
// TestMemuReadDeadline validates that the memu connection properly implements
// the net.Conn interface's read deadline semantics.
func TestMemuReadDeadline(t *testing.T) {
testMemConnDeadline(t, "memu", 0, time.Duration(3)*time.Second)
}
func TestMembReadDeadline(t *testing.T) {
testMemConnDeadline(t, "memb", 0, time.Duration(3)*time.Second)
}
func testMemConnDeadline(
t *testing.T, network string, write, read time.Duration) {
var (
serverReadDeadline time.Duration
serverWriteDeadline time.Duration
)
if write > 0 {
serverReadDeadline = time.Duration(1) * time.Minute
}
if read > 0 {
serverWriteDeadline = time.Duration(1) * time.Minute
}
lis := serve(
t, memconn.Listen, "memu", t.Name(),
serverReadDeadline, serverWriteDeadline, read > 0)
client, err := memconn.Dial(lis.Addr().Network(), lis.Addr().String())
if err != nil {
t.Fatal(err)
}
defer client.Close()
// Set the deadline(s)
if read == write && read > 0 {
client.SetDeadline(time.Now().Add(read))
} else {
now := time.Now()
if read > 0 {
client.SetReadDeadline(now.Add(read))
}
if write > 0 {
client.SetWriteDeadline(now.Add(write))
}
}
// Write data to the server. If an error occurs check to see
// if a write deadline was specified. It would have been small of
// enough a window to force a timeout error. If the error is not
// ErrTimeout then fail the write test.
if _, e := client.Write(fixedData); e == nil && write > 0 {
t.Fatal("write timeout should have occurred")
} else if e != nil && write > 0 {
if opErr, ok := e.(*net.OpError); !ok || !opErr.Timeout() {
t.Fatalf("write timeout should have occurred: %v", e)
}
} else if e != nil {
t.Fatalf("%[1]T %+[1]v", e)
}
// Only perform the read test if a read deadline was set.
// Read data from the server. If an error occurs check to see
// if a read deadline was specified. It would have been small of
// enough a window to force a timeout error. If the error is not
// ErrTimeout then fail the read test.
if read > 0 {
buf := make([]byte, dataLen)
if _, e := client.Read(buf); e == nil {
t.Fatal("read timeout should have occurred")
} else if opErr, ok := e.(*net.OpError); !ok || !opErr.Timeout() {
t.Fatalf("read timeout should have occurred: %v", e)
}
}
}
const parallelTests = 100
func TestMemb(t *testing.T) {
lis := serve(t, memconn.Listen, "memb", t.Name(), 0, 0, true)
testNetConnParallel(t, lis, memconn.Dial)
}
func TestMemu(t *testing.T) {
lis := serve(t, memconn.Listen, "memu", t.Name(), 0, 0, true)
testNetConnParallel(t, lis, memconn.Dial)
}
func TestTCP(t *testing.T) {
lis := serve(t, net.Listen, "tcp", "127.0.0.1:", 0, 0, true)
testNetConnParallel(t, lis, net.Dial)
}
func TestUNIX(t *testing.T) {
sockFile := getTempSockFile(t)
defer os.RemoveAll(sockFile)
lis := serve(t, net.Listen, "unix", sockFile, 0, 0, true)
testNetConnParallel(t, lis, dialUNIX)
}
const (
errConnRefused = "connect: connection refused"
errConnTmpUnavai = "connect: resource temporarily unavailable"
)
var oneMillisecond = time.Duration(1) * time.Millisecond
func getTempSockFile(logger hasLoggerFuncs) string {
// Get a temp file name to use for the socket file.
f, err := ioutil.TempFile("", "")
if err != nil {
logger.Fatalf("error creating temp sock file: %v", err)
}
fileName := f.Name()
f.Close()
os.RemoveAll(fileName)
return fmt.Sprintf("%s.sock", fileName)
}
// dialUNIX is a custom dialer that keeps trying to connect in case
// of ECONNREFUSED or EAGAIN
func dialUNIX(network, addr string) (net.Conn, error) {
for {
client, err := net.Dial(network, addr)
// If there is no error then return the client
if err == nil {
return client, nil
}
msg := err.Error()
// If the error is ECONNREFUSED then keep trying to connect.
if strings.Contains(msg, errConnRefused) {
time.Sleep(oneMillisecond)
continue
}
// If the error is EAGAIN then keep trying to connect.
if strings.Contains(msg, errConnTmpUnavai) {
time.Sleep(oneMillisecond)
continue
}
return nil, err
}
}
func testNetConnParallel(
t *testing.T,
lis net.Listener,
dial func(string, string) (net.Conn, error)) {
defer lis.Close()
network, addr := lis.Addr().Network(), lis.Addr().String()
t.Run("Parallel", func(t *testing.T) {
for i := 0; i < parallelTests; i++ {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
t.Parallel()
writeAndReadTestData(t, network, addr, dial)
})
}
})
}
const dataLen = 8
type hasLoggerFuncs interface {
Logf(string, ...interface{})
Fatal(...interface{})
Fatalf(string, ...interface{})
}
func serve(
logger hasLoggerFuncs,
listen func(string, string) (net.Listener, error),
network, addr string,
readDeadline, writeDeadline time.Duration,
writeBack bool) net.Listener {
lis, err := listen(network, addr)
if err != nil {
logger.Fatalf("error serving %s:%s: %v", network, addr, err)
}
if testing.Verbose() {
logger.Logf("serving %s:%s",
lis.Addr().Network(),
lis.Addr().String())
}
go func() {
for {
c, err := lis.Accept()
if err != nil {
return
}
go func(conn net.Conn) {
if readDeadline > 0 {
time.Sleep(readDeadline)
}
buf := make([]byte, dataLen)
_, err := c.Read(buf)
if err != nil {
fatal := true
if e, ok := err.(*net.OpError); ok {
if e.Err == io.EOF || e.Err == io.ErrClosedPipe {
fatal = false
}
}
if fatal {
logger.Fatal(err)
}
}
//if n != dataLen {
// logger.Fatalf("read != %d bytes: %d", dataLen, n)
//}
if writeBack {
if writeDeadline > 0 {
time.Sleep(writeDeadline)
}
_, err := c.Write(buf)
if err != nil {
logger.Fatal(err)
}
//if n != dataLen {
// logger.Fatalf("write != %d bytes: %d", dataLen, n)
//}
}
if err := c.Close(); err != nil {
logger.Fatal(err)
}
}(c)
}
}()
return lis
}
func writeAndReadTestData(
logger hasLoggerFuncs,
network, addr string,
dial func(string, string) (net.Conn, error)) {
client, err := dial(network, addr)
if err != nil {
logger.Fatal(err)
}
defer client.Close()
// Create the buffer to write to the server and fill it with random data.
wbuf := make([]byte, dataLen)
if n, err := rand.Read(wbuf); err != nil {
logger.Fatal(err)
} else if n != dataLen {
logger.Fatalf("rand != %d bytes: %d", dataLen, n)
}
// Write the buffer to the server and assert that dataLen bytes were
// successfully written.
if n, err := client.Write(wbuf); err != nil {
logger.Fatal(err)
} else if n != dataLen {
logger.Fatalf("wrote != %d bytes: %d", dataLen, n)
}
// Read the response and assert that it matches what was sent.
rbuf := &bytes.Buffer{}
if n, err := io.CopyN(rbuf, client, dataLen); err != nil {
logger.Fatal(err)
} else if n != dataLen {
logger.Fatalf("read != %d bytes: %d", dataLen, n)
} else if rbytes := rbuf.Bytes(); !bytes.Equal(rbytes, wbuf) {
logger.Fatalf("read != write: rbuf=%v, wbuf=%v", rbytes, wbuf)
}
}