Skip to content

Commit

Permalink
tests for shrink the socks pool
Browse files Browse the repository at this point in the history
Signed-off-by: Wang Xu <[email protected]>
  • Loading branch information
gnawux committed Feb 27, 2018
1 parent 9743bfa commit 2930169
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ import (
"flag"
"fmt"
"math"
"math/rand"
"os"
"runtime"
"sort"
"strconv"
"strings"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -166,6 +168,82 @@ func (s *S) TestURLInvalidReadPreference(c *C) {
}
}

func (s *S) TestMinPoolSize(c *C) {
tests := []struct {
url string
size int
fail bool
}{
{"localhost:40001?minPoolSize=0", 0, false},
{"localhost:40001?minPoolSize=1", 1, false},
{"localhost:40001?minPoolSize=-1", -1, true},
{"localhost:40001?minPoolSize=-.", 0, true},
}
for _, test := range tests {
info, err := mgo.ParseURL(test.url)
if test.fail {
c.Assert(err, NotNil)
} else {
c.Assert(err, IsNil)
c.Assert(info.MinPoolSize, Equals, test.size)
}
}
}

func (s *S) TestMaxIdleTimeMS(c *C) {
tests := []struct {
url string
size int
fail bool
}{
{"localhost:40001?maxIdleTimeMS=0", 0, false},
{"localhost:40001?maxIdleTimeMS=1", 1, false},
{"localhost:40001?maxIdleTimeMS=-1", -1, true},
{"localhost:40001?maxIdleTimeMS=-.", 0, true},
}
for _, test := range tests {
info, err := mgo.ParseURL(test.url)
if test.fail {
c.Assert(err, NotNil)
} else {
c.Assert(err, IsNil)
c.Assert(info.MaxIdleTimeMS, Equals, test.size)
}
}
}

func (s *S) TestPoolShrink(c *C) {
oldSocket := mgo.GetStats().SocketsAlive

session, err := mgo.Dial("localhost:40001?minPoolSize=1&maxIdleTimeMS=1000")
c.Assert(err, IsNil)
defer session.Close()

parallel := 10
res := make(chan error, parallel+1)
wg := &sync.WaitGroup{}
for i := 1; i < parallel; i++ {
wg.Add(1)
go func() {
s := session.Copy()
defer s.Close()
result := struct{}{}
err := s.Run("ping", &result)

//sleep random time to make the allocate and release in different sequence
time.Sleep(time.Duration(rand.Intn(parallel)*100) * time.Millisecond)
res <- err
wg.Done()
}()
}
wg.Wait()

// give some time for shrink the pool
time.Sleep(2 * time.Second)
stats := mgo.GetStats()
c.Assert(stats.SocketsAlive-oldSocket > 1, Equals, false)
}

func (s *S) TestURLReadPreferenceTags(c *C) {
type test struct {
url string
Expand Down

0 comments on commit 2930169

Please sign in to comment.