-
Notifications
You must be signed in to change notification settings - Fork 166
/
pool_test.go
303 lines (276 loc) · 6.58 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
package rueidis
import (
"errors"
"runtime"
"sync/atomic"
"testing"
"time"
)
var dead = deadFn()
//gocyclo:ignore
func TestPool(t *testing.T) {
defer ShouldNotLeaked(SetupLeakDetection())
setup := func(size int) (*pool, *int32) {
var count int32
return newPool(size, dead, 0, 0, func() wire {
atomic.AddInt32(&count, 1)
closed := false
return &mockWire{
CloseFn: func() {
closed = true
},
ErrorFn: func() error {
if closed {
return ErrClosing
}
return nil
},
}
}), &count
}
t.Run("DefaultPoolSize", func(t *testing.T) {
p := newPool(0, dead, 0, 0, func() wire { return nil })
if cap(p.list) == 0 {
t.Fatalf("DefaultPoolSize is not applied")
}
})
t.Run("Reuse", func(t *testing.T) {
pool, count := setup(100)
for i := 0; i < 1000; i++ {
pool.Store(pool.Acquire())
}
if atomic.LoadInt32(count) != 1 {
t.Fatalf("pool does not reuse connection")
}
})
t.Run("NotExceed", func(t *testing.T) {
conn := make([]wire, 100)
pool, count := setup(len(conn))
for i := 0; i < len(conn); i++ {
conn[i] = pool.Acquire()
}
if atomic.LoadInt32(count) != 100 {
t.Fatalf("unexpected acquire count")
}
go func() {
for i := 0; i < len(conn); i++ {
pool.Store(conn[i])
}
}()
for i := 0; i < len(conn); i++ {
pool.Acquire()
}
if atomic.LoadInt32(count) > 100 {
t.Fatalf("pool must not exceed the size limit")
}
})
t.Run("NoShare", func(t *testing.T) {
conn := make([]wire, 100)
pool, _ := setup(len(conn))
for i := 0; i < len(conn); i++ {
w := pool.Acquire()
go pool.Store(w)
}
for i := 0; i < len(conn); i++ {
conn[i] = pool.Acquire()
}
for i := 0; i < len(conn); i++ {
for j := i + 1; j < len(conn); j++ {
if conn[i] == conn[j] {
t.Fatalf("pool must not output acquired connection")
}
}
}
})
t.Run("Close", func(t *testing.T) {
pool, count := setup(2)
w1 := pool.Acquire()
w2 := pool.Acquire()
if w1.Error() != nil {
t.Fatalf("unexpected err %v", w1.Error())
}
if w2.Error() != nil {
t.Fatalf("unexpected err %v", w2.Error())
}
if atomic.LoadInt32(count) != 2 {
t.Fatalf("pool does not make new wire")
}
pool.Store(w1)
pool.Close()
if w1.Error() != ErrClosing {
t.Fatalf("pool does not close existing wire after Close()")
}
for i := 0; i < 100; i++ {
if rw := pool.Acquire(); rw != dead {
t.Fatalf("pool does not return the dead wire after Close()")
}
}
pool.Store(w2)
if w2.Error() != ErrClosing {
t.Fatalf("pool does not close stored wire after Close()")
}
})
t.Run("Close Empty", func(t *testing.T) {
pool, count := setup(2)
w1 := pool.Acquire()
if w1.Error() != nil {
t.Fatalf("unexpected err %v", w1.Error())
}
pool.Close()
w2 := pool.Acquire()
if w2.Error() != ErrClosing {
t.Fatalf("pool does not close wire after Close()")
}
if atomic.LoadInt32(count) != 1 {
t.Fatalf("pool should not make new wire")
}
for i := 0; i < 100; i++ {
if rw := pool.Acquire(); rw != dead {
t.Fatalf("pool does not return the dead wire after Close()")
}
}
pool.Store(w1)
if w1.Error() != ErrClosing {
t.Fatalf("pool does not close existing wire after Close()")
}
})
t.Run("Close Waiting", func(t *testing.T) {
pool, count := setup(1)
w1 := pool.Acquire()
if w1.Error() != nil {
t.Fatalf("unexpected err %v", w1.Error())
}
pending := int64(0)
for i := 0; i < 100; i++ {
go func() {
atomic.AddInt64(&pending, 1)
if rw := pool.Acquire(); rw != dead {
t.Errorf("pool does not return the dead wire after Close()")
}
atomic.AddInt64(&pending, -1)
}()
}
for atomic.LoadInt64(&pending) != 100 {
runtime.Gosched()
}
if atomic.LoadInt32(count) != 1 {
t.Fatalf("pool should not make new wire")
}
pool.Close()
for atomic.LoadInt64(&pending) != 0 {
runtime.Gosched()
}
pool.Store(w1)
if w1.Error() != ErrClosing {
t.Fatalf("pool does not close existing wire after Close()")
}
})
}
func TestPoolError(t *testing.T) {
defer ShouldNotLeaked(SetupLeakDetection())
setup := func(size int) (*pool, *int32) {
var count int32
return newPool(size, dead, 0, 0, func() wire {
w := &pipe{}
w.pshks.Store(emptypshks)
c := atomic.AddInt32(&count, 1)
if c%2 == 0 {
w.error.Store(&errs{error: errors.New("any")})
}
return w
}), &count
}
t.Run("NotStoreErrConn", func(t *testing.T) {
conn := make([]wire, 100)
pool, count := setup(len(conn))
for i := 0; i < len(conn); i++ {
conn[i] = pool.Acquire()
}
if atomic.LoadInt32(count) != int32(len(conn)) {
t.Fatalf("unexpected acquire count")
}
for i := 0; i < len(conn); i++ {
pool.Store(conn[i])
}
for i := 0; i < len(conn); i++ {
conn[i] = pool.Acquire()
}
if atomic.LoadInt32(count) != int32(len(conn)+len(conn)/2) {
t.Fatalf("unexpected acquire count")
}
})
}
func TestPoolWithIdleTTL(t *testing.T) {
defer ShouldNotLeaked(SetupLeakDetection())
setup := func(size int, ttl time.Duration, minSize int) *pool {
return newPool(size, dead, ttl, minSize, func() wire {
closed := false
return &mockWire{
CloseFn: func() {
closed = true
},
ErrorFn: func() error {
if closed {
return ErrClosing
}
return nil
},
}
})
}
t.Run("Removing idle conns. Min size is not 0", func(t *testing.T) {
minSize := 3
p := setup(0, time.Millisecond*50, minSize)
conns := make([]wire, 10)
for i := 0; i < 2; i++ {
for i := range conns {
w := p.Acquire()
conns[i] = w
}
for _, w := range conns {
p.Store(w)
}
time.Sleep(time.Millisecond * 60)
p.cond.Broadcast()
time.Sleep(time.Millisecond * 40)
p.cond.L.Lock()
if p.size != minSize {
defer p.cond.L.Unlock()
t.Fatalf("size must be equal to %d, actual: %d", minSize, p.size)
}
if len(p.list) != minSize {
defer p.cond.L.Unlock()
t.Fatalf("pool len must equal to %d, actual: %d", minSize, len(p.list))
}
p.cond.L.Unlock()
}
p.Close()
})
t.Run("Removing idle conns. Min size is 0", func(t *testing.T) {
p := setup(0, time.Millisecond*50, 0)
conns := make([]wire, 10)
for i := 0; i < 2; i++ {
for i := range conns {
w := p.Acquire()
conns[i] = w
}
for _, w := range conns {
p.Store(w)
}
time.Sleep(time.Millisecond * 60)
p.cond.Broadcast()
time.Sleep(time.Millisecond * 40)
p.cond.L.Lock()
if p.size != 0 {
defer p.cond.L.Unlock()
t.Fatalf("size must be equal to 0, actual: %d", p.size)
}
if len(p.list) != 0 {
defer p.cond.L.Unlock()
t.Fatalf("pool len must equal to 0, actual: %d", len(p.list))
}
p.cond.L.Unlock()
}
p.Close()
})
}