-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpool_test.go
198 lines (151 loc) · 3.4 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
package riago
import (
"math/rand"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestPoolSingleConn(t *testing.T) {
assert := assert.New(t)
p := NewPool("127.0.0.1:8087", 1)
p.waitTimeout = 5 * time.Millisecond
// Get a single connection
conn, err := p.Get()
assert.Nil(err)
// The second get should fail
_, err = p.Get()
assert.Equal(ErrPoolWaitTimeout, err)
// Put back the first connection
p.Put(conn)
// The next get should succeed
conn, err = p.Get()
assert.Nil(err)
// Put back
p.Put(conn)
// Close the pool
err = p.Close()
assert.Nil(err)
}
func TestPoolManyConnections(t *testing.T) {
assert := assert.New(t)
n := 5
p := NewPool("127.0.0.1:8087", n)
p.waitTimeout = 5 * time.Millisecond
var err error
conns := make([]*Conn, n)
// Check out n
for i := 0; i < n; i++ {
conns[i], err = p.Get()
assert.Nil(err)
}
// The next get should fail
_, err = p.Get()
assert.Equal(ErrPoolWaitTimeout, err)
// Put back the first connection
p.Put(conns[0])
// The next get should succeed
conns[0], err = p.Get()
assert.Nil(err)
// Put back all
for _, conn := range conns {
p.Put(conn)
}
// Close the pool
err = p.Close()
assert.Nil(err)
}
func TestPoolManyConnectionsConcurrently(t *testing.T) {
assert := assert.New(t)
n := 5
p := NewPool("127.0.0.1:8087", n)
doStuff := func(p *Pool) {
// Check out a conn
conn, err := p.Get()
assert.Nil(err)
// Do some stuff
time.Sleep(time.Duration(rand.Int31n(100)) * time.Millisecond)
// Put it back
p.Put(conn)
}
// Do a bunch of stuff
for i := 0; i < n; i++ {
go doStuff(p)
}
// Let them spawn
time.Sleep(10 * time.Millisecond)
err := p.Close()
assert.Nil(err)
}
func TestPoolManyFailingConnectionsConcurrently(t *testing.T) {
assert := assert.New(t)
n := 5
p := NewPool("127.0.0.1:8087", n)
doStuffBadly := func(p *Pool) {
// Check out a conn
conn, err := p.Get()
assert.Nil(err)
// Do some stuff
time.Sleep(time.Duration(rand.Int31n(100)) * time.Millisecond)
// Sometimes things fail
if rand.Intn(10) < 5 {
p.Fail(conn)
} else {
p.Put(conn)
}
}
// Do a bunch of stuff, some of it will fail
for i := 0; i < n; i++ {
go doStuffBadly(p)
}
// Let them spawn
time.Sleep(10 * time.Millisecond)
err := p.Close()
assert.Nil(err)
}
func TestPoolConnectionClosing(t *testing.T) {
assert := assert.New(t)
n := 10
p := NewPool("127.0.0.1:8087", n)
// Get a single connection, wait and re-put
doStuff := func(p *Pool) {
conn, err := p.Get()
assert.Nil(err)
time.Sleep(100 * time.Millisecond)
p.Put(conn)
}
doFailDial := func(p *Pool) {
// Check out a conn
conn, err := p.Get()
assert.Nil(err)
// Do some stuff
time.Sleep(time.Duration(rand.Intn(10)) * time.Millisecond)
conn.addr = "127.0.0.1:999999"
p.Fail(conn)
}
doFailOther := func(p *Pool) {
conn, err := p.Get()
assert.Nil(err)
time.Sleep(time.Duration(rand.Intn(10)) * time.Millisecond)
p.Fail(conn)
}
// Spawn healthy connections
for i := 0; i < n/3; i++ {
go doStuff(p)
}
// Spawn conns that wont dial
for i := 0; i < n/3; i++ {
go doFailDial(p)
}
// Spawn conns that fail for another reason
for i := 0; i < n/3; i++ {
go doFailOther(p)
}
// Wait for things to happen
time.Sleep(500 * time.Millisecond)
// In the meantime, close the pool
err := p.Close()
assert.Nil(err)
// Try to get another connection
_, err = p.Get()
assert.Equal(ErrPoolClosing, err)
}