Skip to content

Commit 30805e4

Browse files
GODRIVER-2333 Assert that Ping op succeeds initial DNS spec tests (#1124)
1 parent 19f3fb9 commit 30805e4

File tree

55 files changed

+196
-24
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+196
-24
lines changed

mongo/integration/initial_dns_seedlist_discovery_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

4246
func 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+
6694
func 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

136168
func 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

testdata/initial-dns-seedlist-discovery/load-balanced/loadBalanced-directConnection.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@
1010
"loadBalanced": true,
1111
"ssl": true,
1212
"directConnection": false
13-
}
13+
},
14+
"ping": true
1415
}

testdata/initial-dns-seedlist-discovery/load-balanced/loadBalanced-directConnection.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ options:
1111
loadBalanced: true
1212
ssl: true
1313
directConnection: false
14+
ping: true

testdata/initial-dns-seedlist-discovery/load-balanced/loadBalanced-true-txt.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@
99
"options": {
1010
"loadBalanced": true,
1111
"ssl": true
12-
}
12+
},
13+
"ping": true
1314
}

testdata/initial-dns-seedlist-discovery/load-balanced/loadBalanced-true-txt.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ hosts:
88
options:
99
loadBalanced: true
1010
ssl: true
11+
ping: true

testdata/initial-dns-seedlist-discovery/load-balanced/srvMaxHosts-zero-txt.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@
1010
"loadBalanced": true,
1111
"srvMaxHosts": 0,
1212
"ssl": true
13-
}
13+
},
14+
"ping": true
1415
}

testdata/initial-dns-seedlist-discovery/load-balanced/srvMaxHosts-zero-txt.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ options:
88
loadBalanced: true
99
srvMaxHosts: 0
1010
ssl: true
11+
ping: true

testdata/initial-dns-seedlist-discovery/load-balanced/srvMaxHosts-zero.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@
1010
"loadBalanced": true,
1111
"srvMaxHosts": 0,
1212
"ssl": true
13-
}
13+
},
14+
"ping": true
1415
}

testdata/initial-dns-seedlist-discovery/load-balanced/srvMaxHosts-zero.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ options:
88
loadBalanced: true
99
srvMaxHosts: 0
1010
ssl: true
11+
ping: true

testdata/initial-dns-seedlist-discovery/replica-set/direct-connection-false.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@
1111
"options": {
1212
"ssl": true,
1313
"directConnection": false
14-
}
14+
},
15+
"ping": true
1516
}

0 commit comments

Comments
 (0)