From c50bc8e3def90854625b3ba34cc7e94f46da80eb Mon Sep 17 00:00:00 2001 From: Wisdom Omuya Date: Tue, 9 Dec 2014 19:14:05 -0500 Subject: [PATCH] Don't use cluster.err --- cluster.go | 16 +++------------- cluster_test.go | 42 ++++++++++++++++++++---------------------- 2 files changed, 23 insertions(+), 35 deletions(-) diff --git a/cluster.go b/cluster.go index 681a39e04..320b5b986 100644 --- a/cluster.go +++ b/cluster.go @@ -54,17 +54,12 @@ type mongoCluster struct { direct bool failFast bool syncCount uint - err error setName string cachedIndex map[string]bool sync chan bool dial dialer } -var ( - ErrNoReachableServers = errors.New("no reachable servers") -) - func newCluster(userSeeds []string, direct, failFast bool, dial dialer, setName string) *mongoCluster { cluster := &mongoCluster{ userSeeds: userSeeds, @@ -208,8 +203,7 @@ func (cluster *mongoCluster) syncServer(server *mongoServer) (info *mongoServerI if cluster.setName != "" && result.SetName != cluster.setName { log("SYNC Server ", addr, " not a member of replica set ", cluster.setName) - cluster.err = errors.New(addr + " is not part of " + cluster.setName + " replica set") - return nil, nil, cluster.err + return nil, nil, errors.New(addr + " is not part of " + cluster.setName + " replica set") } if result.IsMaster { @@ -270,9 +264,8 @@ func (cluster *mongoCluster) addServer(server *mongoServer, info *mongoServerInf cluster.Unlock() server.Close() return - } else { - cluster.servers.Add(server) } + cluster.servers.Add(server) if info.Master { cluster.masters.Add(server) log("SYNC Adding ", server.Addr, " to cluster as a master.") @@ -561,10 +554,7 @@ func (cluster *mongoCluster) AcquireSocket(slaveOk bool, syncTimeout time.Durati syncCount = cluster.syncCount } else if syncTimeout != 0 && started.Before(time.Now().Add(-syncTimeout)) || cluster.failFast && cluster.syncCount != syncCount { cluster.RUnlock() - if cluster.err != nil { - return nil, cluster.err - } - return nil, ErrNoReachableServers + return nil, errors.New("no reachable servers") } log("Waiting for servers to synchronize...") cluster.syncServers() diff --git a/cluster_test.go b/cluster_test.go index a7ea49f13..6cad192ff 100644 --- a/cluster_test.go +++ b/cluster_test.go @@ -814,7 +814,7 @@ func (s *S) TestSyncTimeout(c *C) { // Do something. result := struct{ Ok bool }{} err = session.Run("getLastError", &result) - c.Assert(err, Equals, mgo.ErrNoReachableServers) + c.Assert(err, ErrorMatches, "no reachable servers") c.Assert(started.Before(time.Now().Add(-timeout)), Equals, true) c.Assert(started.After(time.Now().Add(-timeout*2)), Equals, true) } @@ -832,7 +832,7 @@ func (s *S) TestDialWithTimeout(c *C) { if session != nil { session.Close() } - c.Assert(err, Equals, mgo.ErrNoReachableServers) + c.Assert(err, ErrorMatches, "no reachable servers") c.Assert(session, IsNil) c.Assert(started.Before(time.Now().Add(-timeout)), Equals, true) c.Assert(started.After(time.Now().Add(-timeout*2)), Equals, true) @@ -875,7 +875,7 @@ func (s *S) TestSocketTimeoutOnDial(c *C) { started := time.Now() session, err := mgo.DialWithTimeout("localhost:40001", timeout) - c.Assert(err, Equals, mgo.ErrNoReachableServers) + c.Assert(err, ErrorMatches, "no reachable servers") c.Assert(session, IsNil) c.Assert(started.Before(time.Now().Add(-timeout)), Equals, true) @@ -963,8 +963,6 @@ func (s *S) TestDialWithKnownReplSecondary(c *C) { runTest(session, err) } -var foreignMemberErrorRegex = ".*not part of.*" - func (s *S) TestDialWithForeignReplPrimary(c *C) { if *fast { c.Skip("-fast") @@ -977,19 +975,19 @@ func (s *S) TestDialWithForeignReplPrimary(c *C) { ReplicaSetName: "rs1", } _, err := mgo.DialWithInfo(&info) - c.Assert(err, ErrorMatches, foreignMemberErrorRegex) + c.Assert(err, ErrorMatches, "no reachable servers") info.Direct = true _, err = mgo.DialWithInfo(&info) - c.Assert(err, ErrorMatches, foreignMemberErrorRegex) + c.Assert(err, ErrorMatches, "no reachable servers") connectionUrl := "mongodb://localhost:40021/?replicaSet=rs1" _, err = mgo.Dial(connectionUrl) - c.Assert(err, ErrorMatches, foreignMemberErrorRegex) + c.Assert(err, ErrorMatches, "no reachable servers") connectionUrl += "&connect=direct" _, err = mgo.Dial(connectionUrl) - c.Assert(err, ErrorMatches, foreignMemberErrorRegex) + c.Assert(err, ErrorMatches, "no reachable servers") } func (s *S) TestDialWithForeignReplSecondary(c *C) { @@ -1004,19 +1002,19 @@ func (s *S) TestDialWithForeignReplSecondary(c *C) { ReplicaSetName: "rs1", } _, err := mgo.DialWithInfo(&info) - c.Assert(err, ErrorMatches, foreignMemberErrorRegex) + c.Assert(err, ErrorMatches, "no reachable servers") info.Direct = true _, err = mgo.DialWithInfo(&info) - c.Assert(err, ErrorMatches, foreignMemberErrorRegex) + c.Assert(err, ErrorMatches, "no reachable servers") connectionUrl := "mongodb://localhost:40022/?replicaSet=rs1" _, err = mgo.Dial(connectionUrl) - c.Assert(err, ErrorMatches, foreignMemberErrorRegex) + c.Assert(err, ErrorMatches, "no reachable servers") connectionUrl += "&connect=direct" _, err = mgo.Dial(connectionUrl) - c.Assert(err, ErrorMatches, foreignMemberErrorRegex) + c.Assert(err, ErrorMatches, "no reachable servers") } func (s *S) TestDialWithMixedPrimaries(c *C) { @@ -1091,19 +1089,19 @@ func (s *S) TestDialWithForeignSeeds(c *C) { } _, err := mgo.DialWithInfo(&info) - c.Assert(err, ErrorMatches, foreignMemberErrorRegex) + c.Assert(err, ErrorMatches, "no reachable servers") info.Direct = true _, err = mgo.DialWithInfo(&info) - c.Assert(err, ErrorMatches, foreignMemberErrorRegex) + c.Assert(err, ErrorMatches, "no reachable servers") connectionUrl := "mongodb://localhost:40021,localhost:40022/?replicaSet=rs1" _, err = mgo.Dial(connectionUrl) - c.Assert(err, ErrorMatches, foreignMemberErrorRegex) + c.Assert(err, ErrorMatches, "no reachable servers") connectionUrl += "&connect=direct" _, err = mgo.Dial(connectionUrl) - c.Assert(err, ErrorMatches, foreignMemberErrorRegex) + c.Assert(err, ErrorMatches, "no reachable servers") } func (s *S) TestDialWithUnknownSeeds(c *C) { @@ -1118,11 +1116,11 @@ func (s *S) TestDialWithUnknownSeeds(c *C) { } _, err := mgo.DialWithInfo(&info) - c.Assert(err, Equals, mgo.ErrNoReachableServers) + c.Assert(err, ErrorMatches, "no reachable servers") connectionUrl := "mongodb://localhost:54321,localhost:12345/?replicaSet=rs1" _, err = mgo.Dial(connectionUrl) - c.Assert(err, Equals, mgo.ErrNoReachableServers) + c.Assert(err, ErrorMatches, "no reachable servers") } func (s *S) TestDirect(c *C) { @@ -1148,7 +1146,7 @@ func (s *S) TestDirect(c *C) { coll := session.DB("mydb").C("mycoll") err = coll.Insert(M{"test": 1}) - c.Assert(err, Equals, mgo.ErrNoReachableServers) + c.Assert(err, ErrorMatches, "no reachable servers") // Writing to the local database is okay. coll = session.DB("local").C("mycoll") @@ -1186,7 +1184,7 @@ func (s *S) TestDirectToUnknownStateMember(c *C) { coll := session.DB("mydb").C("mycoll") err = coll.Insert(M{"test": 1}) - c.Assert(err, Equals, mgo.ErrNoReachableServers) + c.Assert(err, ErrorMatches, "no reachable servers") // Slave is still reachable. result.Host = "" @@ -1205,7 +1203,7 @@ func (s *S) TestFailFast(c *C) { started := time.Now() _, err := mgo.DialWithInfo(&info) - c.Assert(err, Equals, mgo.ErrNoReachableServers) + c.Assert(err, ErrorMatches, "no reachable servers") c.Assert(started.After(time.Now().Add(-time.Second)), Equals, true) }