-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathtcplistener_test.go
More file actions
208 lines (185 loc) · 5.17 KB
/
Copy pathtcplistener_test.go
File metadata and controls
208 lines (185 loc) · 5.17 KB
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
//
//
// Tencent is pleased to support the open source community by making tRPC available.
//
// Copyright (C) 2023 Tencent.
// All rights reserved.
//
// If you have downloaded a copy of the tRPC source code from Tencent,
// please note that tRPC source code is licensed under the Apache 2.0 License,
// A copy of the Apache 2.0 License is included in this file.
//
//
package tnet
import (
"fmt"
"net"
"testing"
"time"
"unsafe"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"trpc.group/trpc-go/tnet/internal/iovec"
"trpc.group/trpc-go/tnet/internal/netutil"
)
const (
listenerAcceptTimeout = time.Second
listenerAcceptInterval = 10 * time.Millisecond
)
func TestListen(t *testing.T) {
tests := []struct {
name string
network string
address string
expected bool
}{
{"tcp", "tcp", ":0", true},
{"tcp4", "tcp4", "127.0.0.1:0", true},
{"tcp6", "tcp6", "[::1]:0", true},
{"udp", "udp", ":0", false},
{"udp4", "udp4", "127.0.0.1:0", false},
{"udp6", "udp6", "[::1]:0", false},
{"unix", "unix", "/tmp/test.sock", false},
}
for _, test := range tests {
if !netutil.TestableNetwork(test.network) {
t.Logf("skipping %s test", test.name)
continue
}
t.Run(test.name, func(t *testing.T) {
ln, err := Listen(test.network, test.address)
defer func() {
if err == nil {
ln.Close()
}
}()
require.Equal(t, test.expected, err == nil)
if err == nil {
assert.NotEqual(t, -1, ln.(*tcpListener).FD())
assert.NotEmpty(t, ln.Addr())
}
})
}
}
func TestListenerAccept(t *testing.T) {
tests := []struct {
name string
network string
address string
}{
{"tcp normal accept", "tcp", ":0"},
{"tcp4 normal accept", "tcp4", "127.0.0.1:0"},
{"tcp6 normal accept", "tcp6", "[::1]:0"},
}
for _, test := range tests {
if !netutil.TestableNetwork(test.network) {
t.Logf("skipping %s test", test.name)
continue
}
t.Run(test.network, func(t *testing.T) {
MassiveConnections.Store(true)
DefaultCleanUpThrottle = 0
ln, err := Listen(test.network, test.address)
require.Nil(t, err)
defer ln.Close()
_, err = ln.Accept()
assert.NotNil(t, err)
ne, ok := err.(net.Error)
require.Equal(t, true, ok)
require.Equal(t, true, ne.Temporary())
assert.Equal(t, false, ne.Timeout())
client, err := net.Dial(ln.Addr().Network(), ln.Addr().String())
require.Nil(t, err)
defer client.Close()
var conn net.Conn
require.Eventually(t, func() bool {
conn, err = ln.Accept()
return err == nil && conn != nil
}, listenerAcceptTimeout, listenerAcceptInterval, "accept connection failed: %v", err)
require.NotNil(t, conn)
defer conn.Close()
c := conn.(*tcpconn)
t.Logf("conn size: %v\n", unsafe.Sizeof(*c))
t.Logf("c.writevData.IsNil(): %v\n", c.writevData.IsNil())
t.Logf("IOData size: %v\n", unsafe.Sizeof(iovec.IOData{}))
t.Logf("conn.writevData size: %v\n", unsafe.Sizeof(c.writevData))
t.Logf("conn.postponeWrite size: %v\n", unsafe.Sizeof(c.postpone))
t.Logf("alignOf: %v\n", unsafe.Alignof(c)) // 8
assert.Nil(t, err)
assert.NotNil(t, conn)
assert.NotEmpty(t, conn.LocalAddr())
assert.NotEmpty(t, conn.RemoteAddr())
})
}
}
func TestListenerAcceptAfterClose(t *testing.T) {
tests := []struct {
name string
network string
address string
}{
{"tcp close before accept", "tcp", ":0"},
{"tcp4 close before accept", "tcp4", "127.0.0.1:0"},
{"tcp6 close before accept", "tcp6", "[::1]:0"},
}
for _, test := range tests {
if !netutil.TestableNetwork(test.network) {
t.Logf("skipping %s test", test.name)
continue
}
t.Run(test.network, func(t *testing.T) {
ln, err := Listen(test.network, test.address)
require.Nil(t, err)
client, err := net.Dial(ln.Addr().Network(), ln.Addr().String())
require.Nil(t, err)
defer client.Close()
ln.Close()
_, err = ln.Accept()
assert.NotNil(t, err)
})
}
}
func TestListenerLocalAddr(t *testing.T) {
ln, err := Listen("tcp", ":0") // Use random port.
if err != nil {
t.Fatalf("Failed to listen: %v", err)
}
defer ln.Close()
port := ln.Addr().(*net.TCPAddr).Port
// Start server in goroutine.
addrChan := make(chan string, 1)
go func() {
time.Sleep(100 * time.Millisecond)
conn, err := ln.Accept()
if err != nil {
t.Logf("Accept error: %v", err)
addrChan <- ""
return
}
defer conn.Close()
// Return the actual local address as string.
addrChan <- conn.LocalAddr().String()
}()
// Connect to server.
client, err := net.Dial("tcp", fmt.Sprintf("127.0.0.1:%d", port))
if err != nil {
t.Fatalf("Failed to connect: %v", err)
}
defer client.Close()
// Get the actual connection local address from server.
localAddr := <-addrChan
if localAddr == "" {
t.Fatal("Failed to get local address from server")
}
t.Logf("Server connection reports LocalAddr: %s", localAddr)
// Verify it's not reporting 0.0.0.0.
addr, err := net.ResolveTCPAddr("tcp", localAddr)
if err != nil {
t.Fatalf("Failed to resolve TCP address %s: %v", localAddr, err)
}
if addr.IP.String() == "0.0.0.0" {
t.Errorf("LocalAddr still reports 0.0.0.0 instead of actual IP: %s", addr.IP.String())
} else {
t.Logf("LocalAddr reports correct IP: %s", addr.IP.String())
}
}