-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient_pool_test.go
276 lines (233 loc) · 6.32 KB
/
client_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
/*
* Copyright (C) 2019, 2020, 2021, 2022. Genome Research Ltd. All rights
* reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License,
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @file client_pool_test.go
* @author Keith James <[email protected]>
*/
package extendo_test
import (
"time"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
ex "github.com/wtsi-npg/extendo/v2"
)
var _ = Describe("Make a client pool", func() {
var pool *ex.ClientPool
When("a pool is created", func() {
It("should be open", func() {
pool = ex.NewClientPool(ex.DefaultClientPoolParams)
Expect(pool.IsOpen()).To(BeTrue())
})
It("should be closeable", func() {
pool.Close()
Expect(pool.IsOpen()).To(BeFalse())
})
})
})
var _ = Describe("Get clients from the pool", func() {
var poolSize = uint8(10)
var poolTimout = time.Millisecond * 250
var pool *ex.ClientPool
var clients []*ex.Client
BeforeEach(func() {
params := ex.DefaultClientPoolParams
params.MaxSize = poolSize
params.GetTimeout = poolTimout
pool = ex.NewClientPool(params)
})
AfterEach(func() {
pool.Close()
})
When("a pool of size n is created", func() {
It("Get should supply n running clients before timing out", func() {
loop:
for timeout := time.After(time.Second * 10); ; {
select {
case <-timeout:
break loop // Fallback test timeout if something goes wrong
default:
c, err := pool.Get()
if err == nil {
clients = append(clients, c)
} else {
Expect(err).To(MatchError(MatchRegexp(`\d tries`)))
break loop // Test timeout if pool times out (as expected)
}
}
}
Expect(len(clients)).To(Equal(int(poolSize)))
for _, c := range clients {
Expect(c.IsRunning()).To(BeTrue())
}
})
})
When("a pool is closed", func() {
BeforeEach(func() {
pool.Close()
})
It("should not be possible to get clients from it", func() {
c, err := pool.Get()
Expect(c).To(BeNil())
Expect(err).To(HaveOccurred())
})
})
})
var _ = Describe("Return clients to the pool", func() {
var poolSize = uint8(10)
var poolTimout = time.Millisecond * 250
var pool *ex.ClientPool
var clients []*ex.Client
BeforeEach(func() {
params := ex.DefaultClientPoolParams
params.MaxSize = poolSize
params.GetTimeout = poolTimout
pool = ex.NewClientPool(params)
var newClients []*ex.Client
for i := 0; i < int(poolSize); i++ {
c, err := pool.Get()
Expect(err).NotTo(HaveOccurred())
if err == nil {
newClients = append(newClients, c)
}
}
clients = newClients
})
AfterEach(func() {
pool.Close()
})
When("a pool is open", func() {
It("should be possible to return running clients to it", func() {
for _, c := range clients {
Expect(c.IsRunning()).To(BeTrue())
err := pool.Return(c)
Expect(err).NotTo(HaveOccurred())
Expect(c.IsRunning()).To(BeTrue())
}
})
It("should be possible to return stopped clients to it", func() {
for _, c := range clients {
c.StopIgnoreError()
err := pool.Return(c)
Expect(err).NotTo(HaveOccurred())
}
})
})
When("a pool is closed", func() {
It("should be possible to return running clients to it", func() {
pool.Close()
for _, c := range clients {
Expect(c.IsRunning()).To(BeTrue())
err := pool.Return(c)
Expect(err).NotTo(HaveOccurred())
Expect(c.IsRunning()).To(BeFalse())
}
})
It("should be possible to return stopped clients to it", func() {
pool.Close()
for _, c := range clients {
c.StopIgnoreError()
err := pool.Return(c)
Expect(err).NotTo(HaveOccurred())
}
})
})
})
var _ = Describe("Pool client runtime timeout", func() {
var poolSize = uint8(10)
var poolTimout = time.Millisecond * 250
var pool *ex.ClientPool
var clients []*ex.Client
AfterEach(func() {
pool.Close()
})
When("a pool is open", func() {
When("clients have run longer than MaxClientRuntime", func() {
BeforeEach(func() {
params := ex.DefaultClientPoolParams
params.MaxSize = poolSize
params.GetTimeout = poolTimout
params.CheckClientFreq = time.Millisecond * 500
params.MaxClientRuntime = time.Millisecond * 500
params.MaxClientIdleTime = time.Minute
pool = ex.NewClientPool(params)
for i := 0; i < int(poolSize); i++ {
c, err := pool.Get()
Expect(err).NotTo(HaveOccurred())
if err == nil {
clients = append(clients, c)
}
}
for _, c := range clients {
err := pool.Return(c)
Expect(err).NotTo(HaveOccurred())
}
})
It("should stop those clients returned to it", func() {
allStopped := false
tries := 10
for i := 0; i < tries; i++ {
time.Sleep(time.Second * 1)
for _, c := range clients {
if c.IsRunning() {
continue
}
allStopped = true
break
}
}
Expect(allStopped).To(BeTrue())
})
})
When("clients have been idle longer than MaxClientIdleTime", func() {
BeforeEach(func() {
params := ex.DefaultClientPoolParams
params.MaxSize = poolSize
params.GetTimeout = poolTimout
params.CheckClientFreq = time.Millisecond * 500
params.MaxClientRuntime = time.Minute
params.MaxClientIdleTime = time.Millisecond * 500
pool = ex.NewClientPool(params)
for i := 0; i < int(poolSize); i++ {
c, err := pool.Get()
Expect(err).NotTo(HaveOccurred())
if err == nil {
clients = append(clients, c)
}
}
for _, c := range clients {
err := pool.Return(c)
Expect(err).NotTo(HaveOccurred())
}
})
It("should stop those clients returned to it", func() {
allStopped := false
tries := 10
for i := 0; i < tries; i++ {
time.Sleep(time.Second * 1)
for _, c := range clients {
if c.IsRunning() {
continue
}
allStopped = true
break
}
}
Expect(allStopped).To(BeTrue())
})
})
})
})