Skip to content

Commit dd9c654

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

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

session_test.go

Lines changed: 83 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,87 @@ 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+
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+
169252
func (s *S) TestURLReadPreferenceTags(c *C) {
170253
type test struct {
171254
url string

0 commit comments

Comments
 (0)