@@ -15,12 +15,15 @@ import (
15
15
"runtime"
16
16
"strings"
17
17
"testing"
18
+ "time"
18
19
19
20
"go.mongodb.org/mongo-driver/bson"
20
21
"go.mongodb.org/mongo-driver/internal/testutil/assert"
22
+ "go.mongodb.org/mongo-driver/mongo"
21
23
"go.mongodb.org/mongo-driver/mongo/description"
22
24
"go.mongodb.org/mongo-driver/mongo/integration/mtest"
23
25
"go.mongodb.org/mongo-driver/mongo/options"
26
+ "go.mongodb.org/mongo-driver/mongo/readpref"
24
27
"go.mongodb.org/mongo-driver/x/mongo/driver/connstring"
25
28
"go.mongodb.org/mongo-driver/x/mongo/driver/topology"
26
29
)
@@ -37,19 +40,26 @@ type seedlistTest struct {
37
40
NumHosts * int `bson:"numHosts"`
38
41
Error bool `bson:"error"`
39
42
Options bson.Raw `bson:"options"`
43
+ Ping * bool `bson:"ping"`
40
44
}
41
45
42
46
func TestInitialDNSSeedlistDiscoverySpec (t * testing.T ) {
43
47
mt := mtest .New (t , noClientOpts )
44
48
defer mt .Close ()
45
49
46
50
mt .RunOpts ("replica set" , mtest .NewOptions ().Topologies (mtest .ReplicaSet ).CreateClient (false ), func (mt * mtest.T ) {
51
+ mt .Parallel ()
52
+
47
53
runSeedlistDiscoveryDirectory (mt , "replica-set" )
48
54
})
49
55
mt .RunOpts ("sharded" , mtest .NewOptions ().Topologies (mtest .Sharded ).CreateClient (false ), func (mt * mtest.T ) {
56
+ mt .Parallel ()
57
+
50
58
runSeedlistDiscoveryDirectory (mt , "sharded" )
51
59
})
52
60
mt .RunOpts ("load balanced" , mtest .NewOptions ().Topologies (mtest .LoadBalanced ).CreateClient (false ), func (mt * mtest.T ) {
61
+ mt .Parallel ()
62
+
53
63
runSeedlistDiscoveryDirectory (mt , "load-balanced" )
54
64
})
55
65
}
@@ -63,6 +73,24 @@ func runSeedlistDiscoveryDirectory(mt *mtest.T, subdirectory string) {
63
73
}
64
74
}
65
75
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
+
66
94
func runSeedlistDiscoveryTest (mt * mtest.T , file string ) {
67
95
content , err := ioutil .ReadFile (file )
68
96
assert .Nil (mt , err , "ReadFile error for %v: %v" , file , err )
@@ -131,6 +159,10 @@ func runSeedlistDiscoveryTest(mt *mtest.T, file string) {
131
159
_ , err := getServerByAddress (host , topo )
132
160
assert .Nil (mt , err , "error finding host %q: %v" , host , err )
133
161
}
162
+
163
+ if ping := test .Ping ; ping == nil || * ping {
164
+ runSeedlistDiscoveryPingTest (mt , opts )
165
+ }
134
166
}
135
167
136
168
func buildSet (list []string ) map [string ]struct {} {
@@ -230,6 +262,14 @@ func getServerByAddress(address string, topo *topology.Topology) (description.Se
230
262
if err != nil {
231
263
return description.Server {}, err
232
264
}
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
+
233
273
selectedServerConnection , err := selectedServer .Connection (context .Background ())
234
274
if err != nil {
235
275
return description.Server {}, err
0 commit comments