-
Notifications
You must be signed in to change notification settings - Fork 7
/
channel_test.go
443 lines (360 loc) · 8.5 KB
/
channel_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
431
432
433
434
435
436
437
438
439
440
441
442
443
package connpool
import (
"context"
"errors"
"log"
"math/rand"
"net"
"sync"
"sync/atomic"
"testing"
"time"
)
var (
InitialCap = 5
MaximumCap = 30
network = "tcp"
address = "127.0.0.1:7777"
factory = func() (net.Conn, error) { return net.Dial(network, address) }
)
func init() {
// used for factory function
go simpleTCPServer()
time.Sleep(time.Millisecond * 300) // wait until tcp server has been settled
rand.Seed(time.Now().UTC().UnixNano())
}
func TestNew(t *testing.T) {
_, err := newChannelPool()
if err != nil {
t.Errorf("New error: %s", err)
}
}
func TestPool_Get_Impl(t *testing.T) {
p, _ := newChannelPool()
defer p.Close()
conn, err := p.Get(context.Background())
if err != nil {
t.Errorf("Get error: %s", err)
}
_, ok := conn.(*PoolConn)
if !ok {
t.Errorf("Conn is not of type poolConn")
}
}
func TestPool_Get(t *testing.T) {
p, _ := newChannelPool()
defer p.Close()
_, err := p.Get(context.Background())
if err != nil {
t.Errorf("Get error: %s", err)
}
// after one get, current capacity should be lowered by one.
if p.Len() != (InitialCap - 1) {
t.Errorf("Get error. Expecting %d, got %d",
(InitialCap - 1), p.Len())
}
// get them all
var wg sync.WaitGroup
for i := 0; i < (InitialCap - 1); i++ {
wg.Add(1)
go func() {
defer wg.Done()
_, err := p.Get(context.Background())
if err != nil {
t.Errorf("Get error: %s", err)
}
}()
}
wg.Wait()
if p.Len() != 0 {
t.Errorf("Get error. Expecting %d, got %d",
InitialCap-1, p.Len())
}
_, err = p.Get(context.Background())
if err != nil {
t.Errorf("Get error: %s", err)
}
}
func TestPool_Put(t *testing.T) {
p, err := NewChannelPool(0, 30, factory)
if err != nil {
t.Fatal(err)
}
defer p.Close()
// get/create from the pool
conns := make([]net.Conn, MaximumCap)
for i := 0; i < MaximumCap; i++ {
conn, _ := p.Get(context.Background())
conns[i] = conn
}
// now put them all back
for _, conn := range conns {
conn.Close()
}
if p.Len() != MaximumCap {
t.Errorf("Put error len. Expecting %d, got %d",
1, p.Len())
}
conn, _ := p.Get(context.Background())
p.Close() // close pool
conn.Close() // try to put into a full pool
if p.Len() != 0 {
t.Errorf("Put error. Closed pool shouldn't allow to put connections.")
}
}
func TestPool_PutUnusableConn(t *testing.T) {
p, _ := newChannelPool()
defer p.Close()
// ensure pool is not empty
conn, _ := p.Get(context.Background())
conn.Close()
poolSize := p.Len()
conn, _ = p.Get(context.Background())
conn.Close()
if p.Len() != poolSize {
t.Errorf("Pool size is expected to be equal to initial size")
}
conn, _ = p.Get(context.Background())
if pc, ok := conn.(*PoolConn); !ok {
t.Errorf("impossible")
} else {
pc.MarkUnusable()
}
conn.Close()
if p.Len() != poolSize-1 {
t.Errorf("Pool size is expected to be initial_size - 1 (%d), got:%d", poolSize-1, p.Len())
}
}
func TestPool_UsedCapacity(t *testing.T) {
p, _ := newChannelPool()
defer p.Close()
if p.Len() != InitialCap {
t.Errorf("InitialCap error. Expecting %d, got %d",
InitialCap, p.Len())
}
}
func TestPool_Close(t *testing.T) {
p, _ := newChannelPool()
// now close it and test all cases we are expecting.
p.Close()
c := p.(*channelPool)
if c.conns != nil {
t.Errorf("Close error, conns channel should be nil")
}
if c.factory != nil {
t.Errorf("Close error, factory should be nil")
}
_, err := p.Get(context.Background())
if err == nil {
t.Errorf("Close error, get conn should return an error")
}
if p.Len() != 0 {
t.Errorf("Close error used capacity. Expecting 0, got %d", p.Len())
}
}
func TestPoolConcurrent(t *testing.T) {
p, _ := newChannelPool()
pipe := make(chan net.Conn)
go func() {
p.Close()
}()
for i := 0; i < MaximumCap; i++ {
go func() {
conn, _ := p.Get(context.Background())
pipe <- conn
}()
go func() {
conn := <-pipe
if conn == nil {
return
}
conn.Close()
}()
}
}
func TestPoolWriteRead(t *testing.T) {
p, _ := NewChannelPool(0, 30, factory)
conn, _ := p.Get(context.Background())
msg := "hello"
_, err := conn.Write([]byte(msg))
if err != nil {
t.Error(err)
}
}
func TestPoolConcurrent2(t *testing.T) {
p, _ := NewChannelPool(0, 30, factory)
var wg sync.WaitGroup
go func() {
for i := 0; i < 10; i++ {
wg.Add(1)
go func(i int) {
conn, _ := p.Get(context.Background())
time.Sleep(time.Millisecond * time.Duration(rand.Intn(100)))
conn.Close()
wg.Done()
}(i)
}
}()
for i := 0; i < 10; i++ {
wg.Add(1)
go func(i int) {
conn, _ := p.Get(context.Background())
time.Sleep(time.Millisecond * time.Duration(rand.Intn(100)))
conn.Close()
wg.Done()
}(i)
}
wg.Wait()
}
func TestPoolConcurrent3(t *testing.T) {
p, _ := NewChannelPool(0, 1, factory)
var wg sync.WaitGroup
wg.Add(1)
go func() {
p.Close()
wg.Done()
}()
if conn, err := p.Get(context.Background()); err == nil {
conn.Close()
}
wg.Wait()
}
func newChannelPool() (Pool, error) {
return NewChannelPool(InitialCap, MaximumCap, factory)
}
func simpleTCPServer() {
l, err := net.Listen(network, address)
if err != nil {
log.Fatal(err)
}
defer l.Close()
for {
conn, err := l.Accept()
if err != nil {
panic(err)
}
go func() {
buffer := make([]byte, 256)
conn.Read(buffer)
}()
}
}
func TestPoolMaximumCapacity(t *testing.T) {
var successful int32
p, _ := NewChannelPool(InitialCap, MaximumCap, func() (net.Conn, error) {
return net.Dial(network, address)
})
defer p.Close()
var wg sync.WaitGroup
numWorkers := MaximumCap * 2
for i := 0; i < numWorkers; i++ {
wg.Add(1)
go func() {
defer wg.Done()
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(time.Second))
defer cancel()
_, err := p.Get(ctx)
if err != nil {
if errors.Is(err, context.DeadlineExceeded) {
return
}
t.Errorf("expected nil, got: %v", err)
return
}
atomic.AddInt32(&successful, 1)
}()
}
wg.Wait()
if atomic.LoadInt32(&successful) != int32(MaximumCap) {
t.Errorf("expected successfull Get calls: %d, got: %d", MaximumCap, successful)
}
if p.NumberOfConns() != MaximumCap {
t.Errorf("expected connection count %d, got %d", MaximumCap, p.NumberOfConns())
}
}
func TestPoolMaximumCapacity_Close(t *testing.T) {
var successful int32
p, _ := NewChannelPool(InitialCap, MaximumCap, func() (net.Conn, error) {
return net.Dial(network, address)
})
defer p.Close()
var wg sync.WaitGroup
numWorkers := MaximumCap * 2
for i := 0; i < numWorkers; i++ {
wg.Add(1)
go func() {
defer wg.Done()
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(time.Second))
defer cancel()
c, err := p.Get(ctx)
if err != nil {
if errors.Is(err, context.DeadlineExceeded) {
return
}
t.Errorf("expected nil, got: %v", err)
return
}
atomic.AddInt32(&successful, 1)
c.Close()
}()
}
wg.Wait()
if atomic.LoadInt32(&successful) != int32(numWorkers) {
t.Errorf("expected successful Get calls: %d, got: %d",
numWorkers, atomic.LoadInt32(&successful))
}
}
func TestPool_Get1(t *testing.T) {
p, _ := NewChannelPool(0, 1, factory)
defer p.Close()
var wg sync.WaitGroup
for i := 0; i < 2; i++ {
wg.Add(1)
go func() {
defer wg.Done()
conn, _ := p.Get(context.Background())
defer conn.Close()
time.Sleep(time.Millisecond * 100)
}()
}
wg.Wait()
}
func TestPool_ClosedConnectionsReplaced(t *testing.T) {
p, _ := NewChannelPool(1, 1, factory)
defer p.Close()
conn, _ := p.Get(context.Background())
conn.(*PoolConn).MarkUnusable()
conn.Close()
ctx, _ := context.WithTimeout(context.Background(), 10*time.Millisecond)
conn, err := p.Get(ctx)
if err != nil {
t.Errorf("expected nil, got: %v", err)
}
if conn == nil {
t.Errorf("expected non-nil conn")
}
}
func TestPool_FailedFactoryRestoresConnectionSlot(t *testing.T) {
// regression test to ensure failed factory doesn't permanently decrease connection max
// this happened previously due to semaphore not being released when factory failed
factory := func() (net.Conn, error) {
// a dial guaranteed to fail
return net.DialTimeout("tcp", "localhost:1234", time.Millisecond)
}
max := 2
pool, err := NewChannelPool(0, max, factory)
if err != nil {
t.Fatalf("error creating pool: %v", err)
}
for i := 0; i < max+1; i++ {
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond)
defer cancel()
conn, err := pool.Get(ctx)
if err == nil && conn != nil {
conn.Close()
}
}
if len(pool.(*channelPool).semaphore) != 0 {
t.Fatal("max connections decreased")
}
}