-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpool_test.go
430 lines (394 loc) · 8.15 KB
/
pool_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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
package connpool
import (
"fmt"
"log"
"net"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
)
var (
cfg = Config{
MinSize: 1,
MaxSize: 10,
Increment: 1,
IdleTimeout: 10 * time.Minute,
}
fakeFactory = func() (net.Conn, error) {
return nil, nil
}
fakeFactorySrv = func(s *server) Factory {
return func() (net.Conn, error) {
return net.Dial(s.l.Addr().Network(), s.l.Addr().String())
}
}
)
type server struct {
t *testing.T
host string
port uint
l net.Listener
connSize counter
terminate chan struct{}
}
func newTestSrv() *server {
return &server{port: 0, terminate: make(chan struct{}, 1), connSize: newCounter()}
}
func (s *server) start() error {
l, err := net.Listen("tcp", fmt.Sprintf(":%d", s.port))
if err != nil {
return err
}
s.l = l
go s.listen()
return nil
}
func (s *server) listen() {
for {
select {
case <-s.terminate:
s.l.Close()
return
default:
_, err := s.l.Accept()
if err != nil {
log.Fatal(err)
}
}
}
}
func (s *server) stop() {
s.terminate <- struct{}{}
}
func TestNew(t *testing.T) {
tests := []struct {
desc string
cfg Config
name string
wantErr bool
}{
{
desc: "empty pool",
cfg: Config{},
name: "empty pool",
wantErr: true,
},
{
desc: "min is grater than max",
cfg: Config{
MinSize: 2,
MaxSize: 1,
Increment: 1,
IdleTimeout: time.Minute,
},
wantErr: true,
},
{
desc: "invalid increment",
cfg: Config{
MinSize: 6,
MaxSize: 10,
Increment: 5,
IdleTimeout: time.Minute,
},
wantErr: true,
},
{
desc: "with conn factory",
cfg: Config{
MinSize: 5,
MaxSize: 30,
Increment: 1,
IdleTimeout: time.Minute,
},
wantErr: false,
},
{
desc: "with address",
cfg: Config{
MinSize: 5,
MaxSize: 30,
Increment: 1,
IdleTimeout: time.Minute,
},
wantErr: false,
},
}
for _, tc := range tests {
tc := tc
t.Run(tc.desc, func(t *testing.T) {
_, err := New(tc.cfg, fakeFactory)
if (err != nil) != tc.wantErr {
t.Fatalf("New error, -wantErr: %v, +gotErr: %v, err: %v", tc.wantErr, err != nil, err)
}
})
}
}
func TestPool_Get(t *testing.T) {
tests := []struct {
desc string
cfg Config
demanding int
wantErr bool
}{
{
desc: "get",
demanding: 10,
cfg: Config{
MinSize: 1,
MaxSize: 10,
Increment: 1,
IdleTimeout: 100 * time.Minute,
},
},
{
desc: "with increment 2",
demanding: 10,
cfg: Config{
MinSize: 5,
MaxSize: 10,
Increment: 2,
IdleTimeout: 100 * time.Millisecond,
},
},
{
desc: "demanding more than max",
demanding: 11,
cfg: Config{
MinSize: 5,
MaxSize: 10,
Increment: 3,
IdleTimeout: 100 * time.Millisecond,
},
wantErr: true,
},
{
desc: "demanding less than max",
demanding: 6,
cfg: Config{
MinSize: 5,
MaxSize: 10,
Increment: 3,
IdleTimeout: 100 * time.Millisecond,
},
wantErr: true,
},
}
for _, tc := range tests {
tc := tc
t.Run(tc.desc, func(t *testing.T) {
srv := newTestSrv()
if err := srv.start(); err != nil {
log.Fatalf("Failed to start test server, err: %v.", err)
}
defer srv.stop()
p, err := New(tc.cfg, fakeFactorySrv(srv))
if err != nil {
t.Fatalf("new error, err: %v", err)
}
defer p.Stop()
wg := sync.WaitGroup{}
for i := 0; i < tc.demanding; i++ {
conn, err := p.Get()
if err != nil {
if tc.wantErr {
continue
}
t.Fatalf("get error, err: %v", err)
}
wg.Add(1)
go func() {
defer wg.Done()
time.Sleep(5 * time.Millisecond)
conn.Close()
}()
}
wg.Wait()
if p.Stats().Active() > tc.cfg.MaxSize {
t.Fatalf("conn management err, active: %d, maxSize: %d", p.Stats().Active(), tc.cfg.MaxSize)
}
})
}
}
func TestPool_Start(t *testing.T) {
tests := []struct {
desc string
name string
autoStart bool
}{
{
desc: "start",
name: "test_pool",
autoStart: false,
},
{
desc: "auto start",
name: "test_pool",
autoStart: true,
},
}
for _, tc := range tests {
tc := tc
t.Run(tc.desc, func(t *testing.T) {
p, err := New(cfg, fakeFactory)
if err != nil {
t.Fatalf("new err, err: %v", err)
}
if tc.autoStart && atomic.LoadInt32(&p.(*pool).running) == 0 {
t.Fatal("start err, it is not running though it is configured to auto start.")
}
if atomic.LoadInt32(&p.(*pool).running) == 0 {
t.Fatal("start err, it is not running.")
}
})
}
}
func TestPool_Stop(t *testing.T) {
tests := []struct {
desc string
name string
}{
{
desc: "start",
name: "test_pool",
},
}
for _, tc := range tests {
tc := tc
t.Run(tc.desc, func(t *testing.T) {
p, err := New(cfg, fakeFactory)
if err != nil {
t.Fatalf("new err, err: %v", err)
}
_ = p.Stop()
if atomic.LoadInt32(&p.(*pool).running) == 1 {
t.Fatal("stop err, it is still running.")
}
})
}
}
func TestPool_Name(t *testing.T) {
tests := []struct {
desc string
name string
}{
{
desc: " name",
name: "test_pool",
},
{
desc: "default name",
},
}
for _, tc := range tests {
tc := tc
var opts []Option
if tc.name != "" {
opts = append(opts, WithName(tc.name))
}
p, err := New(cfg, fakeFactory, opts...)
if err != nil {
t.Fatalf("new err, err: %v", err)
}
if tc.name == "" && !strings.HasPrefix(p.Name(), defaultNamePrefix) {
t.Fatalf("Name error, -want:%s*, +got:%s", defaultNamePrefix, p.Name())
}
if tc.name != "" && tc.name != p.Name() {
t.Fatalf("Name error, -name: %v, +name: %v", tc.name, p.Name())
}
}
}
func TestConcurrency(t *testing.T) {
tests := []struct {
desc string
workersSize int
reqCount int
}{
{
desc: "with 10 consumers",
workersSize: 10,
reqCount: 10,
},
{
desc: "with 30 consumers",
workersSize: 30,
reqCount: 50,
},
{
desc: "with 100 consumers",
workersSize: 100,
reqCount: 250,
},
}
for _, tc := range tests {
tc := tc
t.Run(tc.desc, func(t *testing.T) {
srv := newTestSrv()
if err := srv.start(); err != nil {
log.Fatalf("Failed to start test server, err: %v.", err)
}
defer srv.stop()
cfg := Config{
MinSize: 1,
MaxSize: 5,
Increment: 3,
IdleTimeout: time.Second,
}
p, err := New(cfg, fakeFactorySrv(srv))
if err != nil {
log.Fatalf("new error, err: %v.", err)
}
var successCount int64
wg := sync.WaitGroup{}
wg.Add(tc.workersSize)
for i := 0; i < tc.workersSize; i++ {
go func() {
defer wg.Done()
for i := 0; i < tc.reqCount; i++ {
stats := p.Stats()
active, available := stats.Active(), stats.Available()
if active+available > cfg.MaxSize {
t.Fatalf("conn management error, active: %d, available: %d, maxConn: %d", active, available, cfg.MaxSize)
}
c, _ := p.Get()
if c == nil {
continue
}
atomic.AddInt64(&successCount, 1)
time.Sleep(20 * time.Millisecond)
_ = c.Close()
}
}()
}
wg.Wait()
if p.Stats().Active() > cfg.MaxSize {
t.Fatalf("active conn count failure, avtive: %d should not be larger than maxSize: %d", p.Stats().Active(), cfg.MaxSize)
}
wantReqCount := tc.workersSize * tc.reqCount
if p.Stats().Request() != wantReqCount {
t.Fatalf("request count failure, -wantedReqCount: %d, +gotReqCount: %d", wantReqCount, p.Stats().Request())
}
if p.Stats().Success() != int(successCount) {
t.Fatalf("request count failurer, -wantedSuccessCount: %d, +gotSuccessCount: %d", successCount, p.Stats().Success())
}
if p.Stats().Available() != cfg.MaxSize {
t.Fatalf("available conn count failure, -wantedAvailableCount: %d, +gotAvailableCount: %d", cfg.MaxSize, p.Stats().Available())
}
})
}
}
func TestPool_MarkUnusable(t *testing.T) {
p, _ := New(cfg, fakeFactory)
c, _ := p.Get()
defer c.Close()
p.MarkUnusable(c)
pc, ok := c.(*conn)
if !ok {
t.Fatalf("unexpected type %T", pc)
}
if !pc.isUnUsable() {
t.Fatal("conn must be unusable")
}
}