Skip to content

Commit cebb534

Browse files
committed
tests for shrink the socks pool
Signed-off-by: Wang Xu <[email protected]>
1 parent 506d402 commit cebb534

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

session_test.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,13 @@ import (
3030
"flag"
3131
"fmt"
3232
"math"
33+
"math/rand"
3334
"os"
3435
"runtime"
3536
"sort"
3637
"strconv"
3738
"strings"
39+
"sync"
3840
"testing"
3941
"time"
4042

@@ -166,6 +168,90 @@ func (s *S) TestURLInvalidReadPreference(c *C) {
166168
}
167169
}
168170

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+
169255
func (s *S) TestURLReadPreferenceTags(c *C) {
170256
type test struct {
171257
url string

0 commit comments

Comments
 (0)