-
Notifications
You must be signed in to change notification settings - Fork 2
/
connPool_test.go
350 lines (277 loc) · 6.87 KB
/
connPool_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
package vconnpool
import (
"context"
"net"
"testing"
"time"
"github.com/456vv/vconn"
"github.com/456vv/x/tcptest"
"github.com/issue9/assert/v2"
)
// 判断池中空闲连接数量
func Test_ConnPool_1(t *testing.T) {
as := assert.New(t, true)
tcptest.D2S("127.0.0.1:0", func(c net.Conn) {
defer c.Close()
<-vconn.New(c).CloseNotify()
}, func(raddr net.Addr) {
cp := &ConnPool{
IdeConn: 5,
MaxConn: 2,
}
defer cp.Close()
// 创建连接
conn1, err := cp.Dial(raddr.Network(), raddr.String())
as.NotError(err)
conn2, err := cp.Dial(raddr.Network(), raddr.String())
as.NotError(err)
// 回收到池里
conn1.Close()
conn2.Close()
d := cp.ConnNum()
as.Equal(d, 2)
time.Sleep(time.Millisecond)
d = cp.ConnNumIde(raddr.Network(), raddr.String())
as.Equal(d, 2)
cp.CloseIdleConnections()
time.Sleep(time.Millisecond)
d = cp.ConnNum()
as.Equal(d, 0)
d = cp.ConnNumIde(raddr.Network(), raddr.String())
as.Equal(d, 0)
})
}
// 检查池中的数量
func Test_ConnPool_2(t *testing.T) {
as := assert.New(t, true)
tcptest.D2S("127.0.0.1:0", func(c net.Conn) {
<-vconn.New(c).CloseNotify()
c.Close()
}, func(raddr net.Addr) {
cp := &ConnPool{
IdeConn: 5,
}
defer cp.Close()
// 创建连接
conn, err := cp.Dial(raddr.Network(), raddr.String())
as.NotError(err)
conn.Close()
d := cp.ConnNum()
as.Equal(d, 1)
time.Sleep(time.Millisecond)
d = cp.ConnNumIde(raddr.Network(), raddr.String())
as.Equal(d, 1)
d = cp.ConnNum()
as.Equal(d, 1)
})
}
// 读取原始连接,并关闭
func Test_ConnPool_4(t *testing.T) {
as := assert.New(t, true)
tcptest.D2S("127.0.0.1:0", func(c net.Conn) {
<-vconn.New(c).CloseNotify()
c.Close()
}, func(raddr net.Addr) {
cp := &ConnPool{
IdeConn: 5,
}
defer cp.Close()
// 创建连接
conn, err := cp.Dial(raddr.Network(), raddr.String())
as.NotError(err)
conn.Close()
d := cp.ConnNum()
as.Equal(d, 1)
time.Sleep(time.Millisecond)
d = cp.ConnNumIde(raddr.Network(), raddr.String())
as.Equal(d, 1)
// 从池中读出
conn, err = cp.Dial(raddr.Network(), raddr.String())
as.NotError(err)
// 原始连接
netConn_2 := conn.(Conn).RawConn()
netConn_2.Close()
// 不会被回收,因为已经使用RawConn读取连接
conn.Close()
d = cp.ConnNum()
as.Equal(d, 0)
time.Sleep(time.Millisecond)
d = cp.ConnNumIde(raddr.Network(), raddr.String())
as.Equal(d, 0)
})
}
// 使用GET读取连接,池中的当前连接数量有变化
func Test_ConnPool_5(t *testing.T) {
as := assert.New(t, true)
tcptest.D2S("127.0.0.1:0", func(c net.Conn) {
<-vconn.New(c).CloseNotify()
c.Close()
}, func(raddr net.Addr) {
cp := &ConnPool{
IdeConn: 5,
}
defer cp.Close()
// 创建连接
conn, err := cp.Dial(raddr.Network(), raddr.String())
as.NotError(err)
defer conn.Close()
// 加入池中
err = cp.Add(conn)
as.NotError(err)
d := cp.ConnNum()
as.Equal(d, 1)
time.Sleep(time.Millisecond)
d = cp.ConnNumIde(raddr.Network(), raddr.String())
as.Equal(d, 1)
// 从池中读取
tconn, err := cp.Get(conn.RemoteAddr())
as.NotError(err)
tconn.Close()
d = cp.ConnNum()
as.Equal(d, 0)
time.Sleep(time.Millisecond)
d = cp.ConnNumIde(raddr.Network(), raddr.String())
as.Equal(d, 0)
cp.Close()
cp.Close()
cp.CloseIdleConnections()
cp.CloseIdleConnections()
cp.Close()
})
}
// 废弃连接,不入池
func Test_ConnPool_6(t *testing.T) {
as := assert.New(t, true)
tcptest.D2S("127.0.0.1:0", func(c net.Conn) {
<-vconn.New(c).CloseNotify()
c.Close()
}, func(raddr net.Addr) {
cp := &ConnPool{
IdeConn: 5,
MaxConn: 2,
}
defer cp.Close()
conn, err := cp.Dial(raddr.Network(), raddr.String())
as.NotError(err)
conn.Close()
conn, err = cp.Dial(raddr.Network(), raddr.String())
as.NotError(err)
c1, ok := conn.(Conn)
as.True(ok)
d := cp.ConnNum()
as.Equal(d, 1)
time.Sleep(time.Millisecond)
d = cp.ConnNumIde(raddr.Network(), raddr.String())
as.Equal(d, 0)
// 废弃这个连接,不让他进入池内
c1.Discard()
c1.Close()
d = cp.ConnNum()
as.Equal(d, 0)
})
}
// 检查连接数量和空闲数量
func Test_ConnPool_7(t *testing.T) {
as := assert.New(t, true)
tcptest.D2S("127.0.0.1:0", func(c net.Conn) {
<-vconn.New(c).CloseNotify()
c.Close()
}, func(raddr net.Addr) {
cp := &ConnPool{
IdeConn: 5,
MaxConn: 2,
}
defer cp.Close()
conn, err := cp.Dial(raddr.Network(), raddr.String())
as.NotError(err)
conn.Close()
conn, err = cp.Dial(raddr.Network(), raddr.String())
as.NotError(err)
time.Sleep(5 * time.Millisecond)
// 正在使用的连接数量
as.Equal(cp.ConnNum(), 1)
// 池中的连接数量
d := cp.ConnNumIde(raddr.Network(), raddr.String())
as.Equal(d, 0)
conn.Close()
as.Equal(cp.ConnNum(), 1)
d = cp.ConnNumIde(raddr.Network(), raddr.String())
as.Equal(d, 1)
})
}
// 判断连接池中的数量是否正确
func Test_ConnPool_8(t *testing.T) {
as := assert.New(t, true)
tcptest.D2S("127.0.0.1:0", func(c net.Conn) {
<-vconn.New(c).CloseNotify()
c.Close()
}, func(raddr net.Addr) {
cp := &ConnPool{
IdeConn: 5,
MaxConn: 2,
}
defer cp.Close()
conn, err := cp.Dial(raddr.Network(), raddr.String())
as.NotError(err)
conn.Close()
ctx := context.WithValue(context.Background(), PriorityContextKey, true) // 新创建连接
conn, err = cp.DialContext(ctx, raddr.Network(), raddr.String())
as.NotError(err)
conn.Close()
as.Equal(cp.ConnNum(), 2)
d := cp.ConnNumIde(raddr.Network(), raddr.String())
as.Equal(d, 2)
cp.CloseIdleConnections()
time.Sleep(time.Millisecond)
as.Equal(cp.ConnNum(), 0)
d = cp.ConnNumIde(raddr.Network(), raddr.String())
as.Equal(d, 0)
})
}
// 已经关闭的连接禁止加入
func Test_pools_1(t *testing.T) {
as := assert.New(t, true)
tcptest.C2S("127.0.0.1:0", func(c net.Conn) {
time.Sleep(time.Second)
c.Close()
}, func(c net.Conn) {
conn := vconn.New(c)
ps := &pools{
cp: &ConnPool{
IdeConn: 10,
},
occupy: make(map[net.Conn]int), // 占据位置
vacancy: make(map[int]struct{}), // 空缺位置
conns: make([]*connMan, 0, 10), // 存在
}
conn.Close()
ps.put(conn, 5*time.Second)
as.Equal(ps.length(), 0)
conn1, err := ps.get()
as.ErrorIs(err, ErrConnNotAvailable).Nil(conn1)
})
}
// 连接加入池之后,关闭连接
func Test_pools_2(t *testing.T) {
as := assert.New(t, true)
tcptest.C2S("127.0.0.1:0", func(c net.Conn) {
time.Sleep(time.Second)
c.Close()
}, func(c net.Conn) {
conn := vconn.New(c)
ps := &pools{
cp: &ConnPool{
IdeConn: 10,
},
occupy: make(map[net.Conn]int), // 占据位置
vacancy: make(map[int]struct{}), // 空缺位置
conns: make([]*connMan, 0, 10), // 存在
}
ps.put(conn, 5*time.Second)
conn.Close()
time.Sleep(10 * time.Millisecond)
as.Equal(ps.length(), 0)
conn1, err := ps.get()
as.ErrorIs(err, ErrConnNotAvailable).Nil(conn1)
})
}