@@ -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,87 @@ 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
+ oldSocket := mgo .GetStats ().SocketsAlive
217
+
218
+ session , err := mgo .Dial ("localhost:40001?minPoolSize=1&maxIdleTimeMS=1000" )
219
+ c .Assert (err , IsNil )
220
+ defer session .Close ()
221
+
222
+ parallel := 10
223
+ res := make (chan error , parallel + 1 )
224
+ wg := & sync.WaitGroup {}
225
+ for i := 1 ; i < parallel ; i ++ {
226
+ wg .Add (1 )
227
+ go func () {
228
+ s := session .Copy ()
229
+ defer s .Close ()
230
+ result := struct {}{}
231
+ err := s .Run ("ping" , & result )
232
+
233
+ //sleep random time to make the allocate and release in different sequence
234
+ time .Sleep (time .Duration (rand .Intn (parallel )* 100 ) * time .Millisecond )
235
+ res <- err
236
+ wg .Done ()
237
+ }()
238
+ }
239
+ wg .Wait ()
240
+ stats := mgo .GetStats ()
241
+ c .Logf ("living socket: After queries: %d, before queris: %d" , stats .SocketsAlive , oldSocket )
242
+
243
+ // give some time for shrink the pool, the tick is set to 1 minute
244
+ c .Log ("Sleeping... 1 minutes to for pool shrinking" )
245
+ time .Sleep (61 * time .Second )
246
+
247
+ stats = mgo .GetStats ()
248
+ c .Logf ("living socket: After shrinking: %d, at the beginning of the test: %d" , stats .SocketsAlive , oldSocket )
249
+ c .Assert (stats .SocketsAlive - oldSocket > 1 , Equals , false )
250
+ }
251
+
169
252
func (s * S ) TestURLReadPreferenceTags (c * C ) {
170
253
type test struct {
171
254
url string
0 commit comments