forked from keimoon/gore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pool_test.go
104 lines (99 loc) · 1.79 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
package gore
import (
"testing"
)
func TestPool(t *testing.T) {
conn, err := Dial("localhost:6379")
if err != nil {
t.Fatal(err)
}
defer conn.Close()
pool := &Pool{
InitialConn: 5,
MaximumConn: 5,
}
err = pool.Dial("localhost:6379")
if err != nil {
t.Fatal(err)
}
c := make(chan bool, 20)
for i := 0; i < 10000; i++ {
go func(pool *Pool, c chan bool, x int64) {
defer func() {
c <- true
}()
conn, err := pool.Acquire()
if err != nil || conn == nil {
t.Fatal(err, "nil")
}
defer pool.Release(conn)
rep, err := NewCommand("SET", x, x).Run(conn)
if err != nil || !rep.IsOk() {
t.Fatal(err, "not ok")
}
}(pool, c, int64(i))
}
for i := 0; i < 10000; i++ {
<-c
}
for i := 0; i < 10000; i++ {
go func(pool *Pool, c chan bool, x int64) {
defer func() {
c <- true
}()
conn, err := pool.Acquire()
if err != nil || conn == nil {
t.Fatal(err, "nil")
}
defer pool.Release(conn)
rep, err := NewCommand("GET", x).Run(conn)
if err != nil {
t.Fatal(err)
}
y, err := rep.Int()
if err != nil || y != x {
t.Fatal(err, x, y)
}
}(pool, c, int64(i))
}
for i := 0; i < 10000; i++ {
<-c
}
rep, err := NewCommand("FLUSHALL").Run(conn)
if err != nil || !rep.IsOk() {
t.Fatal(err, "not ok")
}
}
func TestPoolClose(t *testing.T) {
pool := &Pool{
InitialConn: 20,
MaximumConn: 20,
}
err := pool.Dial("localhost:6379")
if err != nil {
t.Fatal(err)
}
c := make(chan bool, 20)
ready := make(chan bool, 10)
for i := 0; i < 1000; i++ {
go func() {
defer func() {
c <- true
}()
conn, err := pool.Acquire()
if err != nil {
t.Fatal(err)
}
if conn != nil {
ready <- true
}
}()
}
for i := 0; i < 20; i++ {
<- ready
}
pool.Close()
for i := 0; i < 1000; i++ {
<-c
}
}