-
Notifications
You must be signed in to change notification settings - Fork 5
/
dialer_test.go
314 lines (284 loc) · 7.42 KB
/
dialer_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
//
//
// Tencent is pleased to support the open source community by making tRPC available.
//
// Copyright (C) 2023 THL A29 Limited, a Tencent company.
// 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_test
import (
"context"
"fmt"
"io"
"net"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"trpc.group/trpc-go/tnet"
)
func startNetTCPServer(t *testing.T, network, address string, ch chan string) {
ln, err := net.Listen(network, address)
require.Nil(t, err)
ch <- ln.Addr().String()
conn, err := ln.Accept()
require.Nil(t, err)
for {
req := make([]byte, 1024)
n, err := io.ReadAtLeast(conn, req, 1)
if err != nil {
return
}
m, err := conn.Write(req[:n])
require.Nil(t, err)
require.Equal(t, n, m)
}
}
func startTnetTCPServer(t *testing.T, network, address string, ch chan string) {
ln, err := tnet.Listen(network, address)
require.Nil(t, err)
ch <- ln.Addr().String()
opts := []tnet.Option{tnet.WithTCPKeepAlive(10 * time.Minute)}
handle := func(conn tnet.Conn) error {
req := make([]byte, 1024)
n, err := io.ReadAtLeast(conn, req, 1)
if err != nil {
return err
}
m, err := conn.Write(req[:n])
require.Nil(t, err)
require.Equal(t, n, m)
return nil
}
s, err := tnet.NewTCPService(ln, handle, opts...)
require.Nil(t, err)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
s.Serve(ctx)
}
func TestDialTCP_UnReach(t *testing.T) {
addr := getTestAddr()
_, err := tnet.DialTCP("tcp", addr, time.Millisecond*100)
require.NotNil(t, err)
}
func TestDialTCP_InvalidNetwork(t *testing.T) {
addr := getTestAddr()
_, err := tnet.DialTCP("unix", addr, time.Millisecond*100)
require.NotNil(t, err)
}
func TestDialTCP_Net_Sync(t *testing.T) {
waitCh := make(chan string)
addr := getTestAddr()
go startNetTCPServer(t, "tcp", addr, waitCh)
addr = <-waitCh
conn, err := tnet.DialTCP("tcp", addr, time.Millisecond*100)
require.Nil(t, err)
defer conn.Close()
for i := 0; i <= 1000; i++ {
_, err = conn.Write(helloWorld)
require.Nil(t, err)
rsp, err := conn.ReadN(len(helloWorld))
require.Nil(t, err)
require.Equal(t, helloWorld, rsp)
}
}
func TestDialTCP_Net_Async(t *testing.T) {
waitCh := make(chan string)
addr := getTestAddr()
go startNetTCPServer(t, "tcp", addr, waitCh)
addr = <-waitCh
conn, err := tnet.DialTCP("tcp", addr, time.Millisecond*100)
require.Nil(t, err)
defer conn.Close()
wg := sync.WaitGroup{}
onRequest := func(conn tnet.Conn) error {
rsp, err := conn.ReadN(len(helloWorld))
require.Nil(t, err)
require.Equal(t, helloWorld, rsp)
wg.Done()
return nil
}
assert.Nil(t, conn.SetOnRequest(onRequest))
for i := 0; i <= 1000; i++ {
wg.Add(1)
_, err = conn.Write(helloWorld)
require.Nil(t, err)
}
wg.Wait()
}
func TestDialTCP_Tnet_Sync(t *testing.T) {
waitCh := make(chan string)
addr := getTestAddr()
go startTnetTCPServer(t, "tcp", addr, waitCh)
addr = <-waitCh
conn, err := tnet.DialTCP("tcp", addr, time.Millisecond*100)
require.Nil(t, err)
defer conn.Close()
for i := 0; i <= 1000; i++ {
_, err = conn.Write(helloWorld)
require.Nil(t, err)
rsp, err := conn.ReadN(len(helloWorld))
require.Nil(t, err)
require.Equal(t, helloWorld, rsp)
}
}
func TestDialTCP_Tnet_Async(t *testing.T) {
waitCh := make(chan string)
addr := getTestAddr()
go startTnetTCPServer(t, "tcp", addr, waitCh)
addr = <-waitCh
conn, err := tnet.DialTCP("tcp", addr, time.Millisecond*100)
require.Nil(t, err)
defer conn.Close()
wg := sync.WaitGroup{}
onRequest := func(conn tnet.Conn) error {
rsp, err := conn.ReadN(len(helloWorld))
require.Nil(t, err)
require.Equal(t, helloWorld, rsp)
wg.Done()
return nil
}
assert.Nil(t, conn.SetOnRequest(onRequest))
for i := 0; i <= 10000; i++ {
wg.Add(1)
_, err = conn.Write(helloWorld)
require.Nil(t, err)
}
wg.Wait()
}
func startNetUDPServer(t *testing.T, network, address string, ch chan string) {
conn, err := net.ListenPacket(network, address)
require.Nil(t, err)
ch <- conn.LocalAddr().String()
for {
req := make([]byte, 1024)
n, addr, err := conn.ReadFrom(req)
if err != nil {
fmt.Println("收包错误")
return
}
m, err := conn.WriteTo(req[:n], addr)
require.Nil(t, err)
require.Equal(t, n, m)
}
}
func startTnetUDPServer(t *testing.T, network, address string, ch chan string) {
lns, err := tnet.ListenPackets(network, address, true)
require.Nil(t, err)
s, err := tnet.NewUDPService(lns, func(conn tnet.PacketConn) error {
req := make([]byte, 1024)
n, addr, err := conn.ReadFrom(req)
if err != nil {
return err
}
m, err := conn.WriteTo(req[:n], addr)
if err != nil {
fmt.Println("服务端写包失败:", err)
}
require.Nil(t, err)
require.Equal(t, n, m)
return nil
})
require.Nil(t, err)
ch <- lns[0].LocalAddr().String()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
s.Serve(ctx)
}
func TestDialUDP_InvalidNetwork(t *testing.T) {
addr := getTestAddr()
_, err := tnet.DialUDP("tcp", addr, time.Millisecond*100)
require.NotNil(t, err)
}
func TestDialUDP_Net_Sync(t *testing.T) {
waitCh := make(chan string)
addr := getTestAddr()
go startNetUDPServer(t, "udp", addr, waitCh)
addr = <-waitCh
conn, err := tnet.DialUDP("udp", addr, time.Millisecond*100)
require.Nil(t, err)
defer conn.Close()
for i := 0; i <= 1000; i++ {
_, err = conn.Write(helloWorld)
require.Nil(t, err)
rsp := make([]byte, 1024)
n, err := conn.Read(rsp)
require.Nil(t, err)
require.Equal(t, helloWorld, rsp[:n])
}
}
func TestDialUDP_Net_Async(t *testing.T) {
waitCh := make(chan string)
addr := getTestAddr()
go startNetUDPServer(t, "udp", addr, waitCh)
addr = <-waitCh
conn, err := tnet.DialUDP("udp", addr, time.Millisecond*100)
require.Nil(t, err)
defer conn.Close()
wg := sync.WaitGroup{}
onRequest := func(conn tnet.PacketConn) error {
rsp := make([]byte, 1024)
n, err := conn.Read(rsp)
require.Nil(t, err)
require.Equal(t, helloWorld, rsp[:n])
wg.Done()
return nil
}
assert.Nil(t, conn.SetOnRequest(onRequest))
for i := 0; i <= 100; i++ {
wg.Add(1)
_, err = conn.Write(helloWorld)
require.Nil(t, err)
time.Sleep(time.Microsecond)
}
wg.Wait()
}
func TestDialUDP_Tnet_Sync(t *testing.T) {
waitCh := make(chan string)
addr := getTestAddr()
go startTnetUDPServer(t, "udp", addr, waitCh)
addr = <-waitCh
conn, err := tnet.DialUDP("udp", addr, time.Millisecond*100)
require.Nil(t, err)
defer conn.Close()
for i := 0; i <= 1000; i++ {
_, err = conn.Write(helloWorld)
require.Nil(t, err)
rsp := make([]byte, 1024)
n, err := conn.Read(rsp)
require.Nil(t, err)
require.Equal(t, helloWorld, rsp[:n])
}
}
func TestDialUDP_Tnet_Async(t *testing.T) {
waitCh := make(chan string)
addr := getTestAddr()
go startTnetUDPServer(t, "udp", addr, waitCh)
addr = <-waitCh
conn, err := tnet.DialUDP("udp", addr, time.Millisecond*100)
require.Nil(t, err)
defer conn.Close()
wg := sync.WaitGroup{}
onRequest := func(conn tnet.PacketConn) error {
rsp := make([]byte, 1024)
n, err := conn.Read(rsp)
require.Nil(t, err)
require.Equal(t, helloWorld, rsp[:n])
wg.Done()
return nil
}
assert.Nil(t, conn.SetOnRequest(onRequest))
for i := 0; i <= 100; i++ {
wg.Add(1)
_, err = conn.Write(helloWorld)
require.Nil(t, err)
time.Sleep(time.Microsecond)
}
wg.Wait()
}