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 dd9c654
Showing 1 changed file with 83 additions and 0 deletions.
83 changes: 83 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,87 @@ 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()
stats := mgo.GetStats()
c.Logf("living socket: After queries: %d, before queris: %d", stats.SocketsAlive, oldSocket)

// give some time for shrink the pool, the tick is set to 1 minute
c.Log("Sleeping... 1 minutes to for pool shrinking")
time.Sleep(61 * time.Second)

stats = mgo.GetStats()
c.Logf("living socket: After shrinking: %d, at the beginning of the test: %d", stats.SocketsAlive, oldSocket)
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 dd9c654

Please sign in to comment.