-
Notifications
You must be signed in to change notification settings - Fork 14
/
reuse_test.go
162 lines (157 loc) · 3.22 KB
/
reuse_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
// Copyright (c) 2023 Meng Huang (mhboy@outlook.com)
// This package is licensed under a MIT license that can be found in the LICENSE file.
//go:build darwin || dragonfly || freebsd || netbsd || openbsd || linux
// +build darwin dragonfly freebsd netbsd openbsd linux
package netpoll
import (
"github.com/hslam/reuse"
"net"
"sync"
"testing"
"time"
)
func TestReuseServerPort(t *testing.T) {
network := "tcp"
addr := ":9999"
msg := "Hello World"
wg := sync.WaitGroup{}
servers := make([]*Server, 2)
var handler = &DataHandler{
NoShared: false,
NoCopy: false,
BufferSize: 1024,
HandlerFunc: func(req []byte) (res []byte) {
res = req
return
},
}
for i := 0; i < 2; i++ {
server := &Server{
Network: network,
Address: addr,
Handler: handler,
NoAsync: false,
}
servers[i] = server
wg.Add(1)
go func() {
defer wg.Done()
server.ListenAndServe()
}()
}
time.Sleep(time.Millisecond * 100)
localPort := 8888
d := net.Dialer{LocalAddr: &net.TCPAddr{Port: localPort}, Control: reuse.Control}
conn, err := d.Dial(network, addr)
if err != nil {
t.Error("dial failed:", err)
return
}
if _, err := conn.Write([]byte(msg)); err != nil {
t.Error(err)
return
}
buf := make([]byte, 1024)
if n, err := conn.Read(buf); err != nil {
t.Error(err)
return
} else if n != len(msg) {
t.Errorf("%d %d", n, len(msg))
}
conn.Close()
for i := 0; i < 2; i++ {
servers[i].Close()
}
wg.Wait()
}
func TestReuseClientPort(t *testing.T) {
network := "tcp"
addr1 := ":9997"
addr2 := ":9998"
msg := "Hello World"
wg := sync.WaitGroup{}
var handler = &DataHandler{
NoShared: false,
NoCopy: false,
BufferSize: 1024,
HandlerFunc: func(req []byte) (res []byte) {
res = req
return
},
}
server1 := &Server{
Network: network,
Address: addr1,
Handler: handler,
NoAsync: false,
}
wg.Add(1)
go func() {
defer wg.Done()
server1.ListenAndServe()
}()
server2 := &Server{
Network: network,
Address: addr2,
Handler: handler,
NoAsync: false,
}
wg.Add(1)
go func() {
defer wg.Done()
server2.ListenAndServe()
}()
time.Sleep(time.Millisecond * 100)
localPort := 8888
d := net.Dialer{LocalAddr: &net.TCPAddr{Port: localPort}, Control: reuse.Control}
wg.Add(1)
go func() {
defer wg.Done()
conn, err := d.Dial(network, addr1)
time.Sleep(time.Millisecond * 100)
if err != nil {
t.Error("dial failed:", err)
return
}
if _, err := conn.Write([]byte(msg)); err != nil {
t.Error(err)
return
}
buf := make([]byte, 1024)
if n, err := conn.Read(buf); err != nil {
t.Error(err)
return
} else if n != len(msg) {
t.Errorf("%d %d", n, len(msg))
}
time.Sleep(time.Millisecond * 500)
conn.Close()
}()
wg.Add(1)
go func() {
defer wg.Done()
conn, err := d.Dial(network, addr2)
time.Sleep(time.Millisecond * 100)
if err != nil {
t.Error("dial failed:", err)
return
}
if _, err := conn.Write([]byte(msg)); err != nil {
t.Error(err)
return
}
buf := make([]byte, 1024)
if n, err := conn.Read(buf); err != nil {
t.Error(err)
return
} else if n != len(msg) {
t.Errorf("%d %d", n, len(msg))
}
time.Sleep(time.Millisecond * 500)
conn.Close()
}()
time.Sleep(time.Second)
server1.Close()
server2.Close()
wg.Wait()
}