diff --git a/cluster.go b/cluster.go index 8833da00f..53e52be0d 100644 --- a/cluster.go +++ b/cluster.go @@ -64,6 +64,7 @@ func newCluster(userSeeds []string, direct bool, dial dialer) *mongoCluster { } cluster.serverSynced.L = cluster.RWMutex.RLocker() cluster.sync = make(chan bool, 1) + stats.cluster(+1) go cluster.syncServersLoop() return cluster } @@ -91,6 +92,7 @@ func (cluster *mongoCluster) Release() { } // Wake up the sync loop so it can die. cluster.syncServers() + stats.cluster(-1) } cluster.Unlock() } diff --git a/stats.go b/stats.go index 4f9630107..59723e60c 100644 --- a/stats.go +++ b/stats.go @@ -58,6 +58,7 @@ func ResetStats() { old := stats stats = &Stats{} // These are absolute values: + stats.Clusters = old.Clusters stats.SocketsInUse = old.SocketsInUse stats.SocketsAlive = old.SocketsAlive stats.SocketRefs = old.SocketRefs @@ -66,6 +67,7 @@ func ResetStats() { } type Stats struct { + Clusters int MasterConns int SlaveConns int SentOps int @@ -76,6 +78,14 @@ type Stats struct { SocketRefs int } +func (stats *Stats) cluster(delta int) { + if stats != nil { + statsMutex.Lock() + stats.Clusters += delta + statsMutex.Unlock() + } +} + func (stats *Stats) conn(delta int, master bool) { if stats != nil { statsMutex.Lock() diff --git a/suite_test.go b/suite_test.go index c84bc7dd0..a846c5134 100644 --- a/suite_test.go +++ b/suite_test.go @@ -110,15 +110,29 @@ func (s *S) TearDownTest(c *C) { s.Thaw(host) } } - for i := 0; i < 20; i++ { - stats := mgo.GetStats() + var stats mgo.Stats + for i := 0; ; i++ { + stats = mgo.GetStats() if stats.SocketsInUse == 0 && stats.SocketsAlive == 0 { - return + break + } + if i == 20 { + c.Fatal("Test left sockets in a dirty state") } c.Logf("Waiting for sockets to die: %d in use, %d alive", stats.SocketsInUse, stats.SocketsAlive) time.Sleep(500 * time.Millisecond) } - c.Fatal("Test left sockets in a dirty state") + for i := 0; ; i++ { + stats = mgo.GetStats() + if stats.Clusters == 0 { + break + } + if i == 60 { + c.Fatal("Test left clusters alive") + } + c.Logf("Waiting for clusters to die: %d alive", stats.Clusters) + time.Sleep(1 * time.Second) + } } func (s *S) Stop(host string) {