-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtcpkeepalive_test.go
99 lines (84 loc) · 1.53 KB
/
tcpkeepalive_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
package netutil
import (
"bytes"
"io"
"net"
"os"
"testing"
"time"
)
const (
testAddress = "localhost:15345"
)
func TestTCPKeepAlive(t *testing.T) {
t.Parallel()
addr, err := net.ResolveTCPAddr("tcp", testAddress)
if err != nil {
t.Fatal(err)
}
tl, err := net.ListenTCP("tcp", addr)
if err != nil {
t.Skip(err)
}
go func(l net.Listener) {
c, err := l.Accept()
l.Close()
if err != nil {
t.Error(err)
return
}
c.Write([]byte("abc"))
c.Close()
}(TCPKeepAliveListener{tl})
c, err := net.DialTimeout("tcp", testAddress, 1*time.Second)
if err != nil {
t.Fatal(err)
}
defer c.Close()
SetKeepAlive(c)
data, err := io.ReadAll(c)
if err != nil {
t.Error(err)
}
if !bytes.Equal(data, []byte("abc")) {
t.Error(`!bytes.Equal(data, []byte("abc"))`)
}
}
func TestKeepAliveListener(t *testing.T) {
t.Parallel()
f, err := os.CreateTemp("", "gotest")
if err != nil {
t.Fatal(err)
}
f.Close()
os.Remove(f.Name())
l, err := net.Listen("unix", f.Name())
if err != nil {
t.Fatal(err)
}
if l != KeepAliveListener(l) {
t.Error(`l != KeepAliveListener(l)`)
}
go func() {
c, err := l.Accept()
l.Close()
if err != nil {
t.Error(err)
}
c.Write([]byte("xyz"))
c.Close()
}()
c, err := net.DialTimeout("unix", f.Name(), 1*time.Second)
if err != nil {
t.Fatal(err)
}
defer c.Close()
SetKeepAlive(c) // noop for UNIX-domain socket
data, err := io.ReadAll(c)
if err != nil {
t.Error(err)
}
if !bytes.Equal(data, []byte("xyz")) {
t.Error(`!bytes.Equal(data, []byte("xyz"))`)
}
}