-
Notifications
You must be signed in to change notification settings - Fork 2
/
pool_test.go
70 lines (68 loc) · 1.14 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
package redisgo
import (
"context"
"testing"
"time"
)
func TestPoolConn(t *testing.T) {
dialfunc := func(ctx context.Context) (*Conn, error) {
return NewConn(&FakeConn{}), nil
}
var now time.Time
ctx := context.Background()
p := NewPool(dialfunc,
WithMaxIdle(1), WithMaxActive(2),
WithMaxIdleTime(time.Second), WithMaxConnTime(2*time.Second))
p.nowfunc = func() time.Time {
return now
}
c0, err := p.Get(ctx)
if err != nil {
t.Fatal(err)
}
c1, err := p.Get(ctx)
if err != nil {
t.Fatal(err)
}
_, err = p.Get(ctx)
if err != ErrMaxActive {
t.Fatal(err)
}
if p.Active() != 2 {
t.Fatal(p.Active())
}
c0.Close()
if p.Idle() != 1 {
t.Fatal(p.Idle())
}
if p.Active() != 2 {
t.Fatal(p.Active())
}
c1.Close()
if p.Idle() != 1 {
t.Fatal(p.Idle())
}
if p.Active() != 1 {
t.Fatal(p.Active())
}
c2, _ := p.Get(ctx)
if c2 != c0 {
t.Fatal(c2, c0)
}
now = now.Add(3 * time.Second)
c2.Close()
if p.Idle() != 0 {
t.Fatal(p.Idle())
}
if p.Active() != 0 {
t.Fatal(p.Active())
}
c0, _ = p.Get(ctx)
c0.Close()
now = now.Add(2 * time.Second)
c1, _ = p.Get(ctx)
if c0 == c1 {
t.Fatal(c0, c1)
}
c1.Close()
}