@@ -30,11 +30,13 @@ import (
30
30
"flag"
31
31
"fmt"
32
32
"math"
33
+ "math/rand"
33
34
"os"
34
35
"runtime"
35
36
"sort"
36
37
"strconv"
37
38
"strings"
39
+ "sync"
38
40
"testing"
39
41
"time"
40
42
@@ -166,6 +168,90 @@ func (s *S) TestURLInvalidReadPreference(c *C) {
166
168
}
167
169
}
168
170
171
+ func (s * S ) TestMinPoolSize (c * C ) {
172
+ tests := []struct {
173
+ url string
174
+ size int
175
+ fail bool
176
+ }{
177
+ {"localhost:40001?minPoolSize=0" , 0 , false },
178
+ {"localhost:40001?minPoolSize=1" , 1 , false },
179
+ {"localhost:40001?minPoolSize=-1" , - 1 , true },
180
+ {"localhost:40001?minPoolSize=-." , 0 , true },
181
+ }
182
+ for _ , test := range tests {
183
+ info , err := mgo .ParseURL (test .url )
184
+ if test .fail {
185
+ c .Assert (err , NotNil )
186
+ } else {
187
+ c .Assert (err , IsNil )
188
+ c .Assert (info .MinPoolSize , Equals , test .size )
189
+ }
190
+ }
191
+ }
192
+
193
+ func (s * S ) TestMaxIdleTimeMS (c * C ) {
194
+ tests := []struct {
195
+ url string
196
+ size int
197
+ fail bool
198
+ }{
199
+ {"localhost:40001?maxIdleTimeMS=0" , 0 , false },
200
+ {"localhost:40001?maxIdleTimeMS=1" , 1 , false },
201
+ {"localhost:40001?maxIdleTimeMS=-1" , - 1 , true },
202
+ {"localhost:40001?maxIdleTimeMS=-." , 0 , true },
203
+ }
204
+ for _ , test := range tests {
205
+ info , err := mgo .ParseURL (test .url )
206
+ if test .fail {
207
+ c .Assert (err , NotNil )
208
+ } else {
209
+ c .Assert (err , IsNil )
210
+ c .Assert (info .MaxIdleTimeMS , Equals , test .size )
211
+ }
212
+ }
213
+ }
214
+
215
+ func (s * S ) TestPoolShrink (c * C ) {
216
+ if * fast {
217
+ c .Skip ("-fast" )
218
+ }
219
+ oldSocket := mgo .GetStats ().SocketsAlive
220
+
221
+ session , err := mgo .Dial ("localhost:40001?minPoolSize=1&maxIdleTimeMS=1000" )
222
+ c .Assert (err , IsNil )
223
+ defer session .Close ()
224
+
225
+ parallel := 10
226
+ res := make (chan error , parallel + 1 )
227
+ wg := & sync.WaitGroup {}
228
+ for i := 1 ; i < parallel ; i ++ {
229
+ wg .Add (1 )
230
+ go func () {
231
+ s := session .Copy ()
232
+ defer s .Close ()
233
+ result := struct {}{}
234
+ err := s .Run ("ping" , & result )
235
+
236
+ //sleep random time to make the allocate and release in different sequence
237
+ time .Sleep (time .Duration (rand .Intn (parallel )* 100 ) * time .Millisecond )
238
+ res <- err
239
+ wg .Done ()
240
+ }()
241
+ }
242
+ wg .Wait ()
243
+ stats := mgo .GetStats ()
244
+ c .Logf ("living socket: After queries: %d, before queries: %d" , stats .SocketsAlive , oldSocket )
245
+
246
+ // give some time for shrink the pool, the tick is set to 1 minute
247
+ c .Log ("Sleeping... 1 minute to for pool shrinking" )
248
+ time .Sleep (60 * time .Second )
249
+
250
+ stats = mgo .GetStats ()
251
+ c .Logf ("living socket: After shrinking: %d, at the beginning of the test: %d" , stats .SocketsAlive , oldSocket )
252
+ c .Assert (stats .SocketsAlive - oldSocket > 1 , Equals , false )
253
+ }
254
+
169
255
func (s * S ) TestURLReadPreferenceTags (c * C ) {
170
256
type test struct {
171
257
url string
0 commit comments