@@ -15,12 +15,15 @@ import (
1515 "runtime"
1616 "strings"
1717 "testing"
18+ "time"
1819
1920 "go.mongodb.org/mongo-driver/bson"
2021 "go.mongodb.org/mongo-driver/internal/testutil/assert"
22+ "go.mongodb.org/mongo-driver/mongo"
2123 "go.mongodb.org/mongo-driver/mongo/description"
2224 "go.mongodb.org/mongo-driver/mongo/integration/mtest"
2325 "go.mongodb.org/mongo-driver/mongo/options"
26+ "go.mongodb.org/mongo-driver/mongo/readpref"
2427 "go.mongodb.org/mongo-driver/x/mongo/driver/connstring"
2528 "go.mongodb.org/mongo-driver/x/mongo/driver/topology"
2629)
@@ -37,19 +40,26 @@ type seedlistTest struct {
3740 NumHosts * int `bson:"numHosts"`
3841 Error bool `bson:"error"`
3942 Options bson.Raw `bson:"options"`
43+ Ping * bool `bson:"ping"`
4044}
4145
4246func TestInitialDNSSeedlistDiscoverySpec (t * testing.T ) {
4347 mt := mtest .New (t , noClientOpts )
4448 defer mt .Close ()
4549
4650 mt .RunOpts ("replica set" , mtest .NewOptions ().Topologies (mtest .ReplicaSet ).CreateClient (false ), func (mt * mtest.T ) {
51+ mt .Parallel ()
52+
4753 runSeedlistDiscoveryDirectory (mt , "replica-set" )
4854 })
4955 mt .RunOpts ("sharded" , mtest .NewOptions ().Topologies (mtest .Sharded ).CreateClient (false ), func (mt * mtest.T ) {
56+ mt .Parallel ()
57+
5058 runSeedlistDiscoveryDirectory (mt , "sharded" )
5159 })
5260 mt .RunOpts ("load balanced" , mtest .NewOptions ().Topologies (mtest .LoadBalanced ).CreateClient (false ), func (mt * mtest.T ) {
61+ mt .Parallel ()
62+
5363 runSeedlistDiscoveryDirectory (mt , "load-balanced" )
5464 })
5565}
@@ -63,6 +73,24 @@ func runSeedlistDiscoveryDirectory(mt *mtest.T, subdirectory string) {
6373 }
6474}
6575
76+ // runSeedlistDiscoveryPingTest will create a new connection using the test URI and attempt to "ping" the server.
77+ func runSeedlistDiscoveryPingTest (mt * mtest.T , clientOpts * options.ClientOptions ) {
78+ ctx := context .Background ()
79+
80+ client , err := mongo .Connect (ctx , clientOpts )
81+ assert .Nil (mt , err , "Connect error: %v" , err )
82+
83+ defer func () { _ = client .Disconnect (ctx ) }()
84+
85+ // Create a context with a timeout to prevent the ping operation from blocking indefinitely.
86+ pingCtx , cancel := context .WithTimeout (ctx , 1 * time .Second )
87+ defer cancel ()
88+
89+ // Ping the server.
90+ err = client .Ping (pingCtx , readpref .Nearest ())
91+ assert .Nil (mt , err , "Ping error: %v" , err )
92+ }
93+
6694func runSeedlistDiscoveryTest (mt * mtest.T , file string ) {
6795 content , err := ioutil .ReadFile (file )
6896 assert .Nil (mt , err , "ReadFile error for %v: %v" , file , err )
@@ -131,6 +159,10 @@ func runSeedlistDiscoveryTest(mt *mtest.T, file string) {
131159 _ , err := getServerByAddress (host , topo )
132160 assert .Nil (mt , err , "error finding host %q: %v" , host , err )
133161 }
162+
163+ if ping := test .Ping ; ping == nil || * ping {
164+ runSeedlistDiscoveryPingTest (mt , opts )
165+ }
134166}
135167
136168func buildSet (list []string ) map [string ]struct {} {
@@ -230,6 +262,14 @@ func getServerByAddress(address string, topo *topology.Topology) (description.Se
230262 if err != nil {
231263 return description.Server {}, err
232264 }
265+
266+ // If the selected server is a topology.SelectedServer, then we can get the description without creating a
267+ // connect pool.
268+ topologySelectedServer , ok := selectedServer .(* topology.SelectedServer )
269+ if ok {
270+ return topologySelectedServer .Description ().Server , nil
271+ }
272+
233273 selectedServerConnection , err := selectedServer .Connection (context .Background ())
234274 if err != nil {
235275 return description.Server {}, err
0 commit comments