Skip to content

Commit 21d9c44

Browse files
author
Divjot Arora
authored
GODRIVER-1394 Require multiple hosts to exist if useMultipleMongoses=true (#530)
1 parent 171476d commit 21d9c44

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

mongo/integration/unified/client_entity_test.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,13 @@ func NewClientEntity(ctx context.Context, entityOptions *EntityOptions) (*Client
4949
return nil, fmt.Errorf("error parsing URI options: %v", err)
5050
}
5151
}
52-
// UseMultipleMongoses only requires options to be set if we're connected to a sharded cluster and it's set to
53-
// false. If it's unset or true, we make no changes because the cluster URI already includes all mongos nodes.
54-
if mtest.ClusterTopologyKind() == mtest.Sharded && !entityOptions.UseMultipleMongoses {
55-
clientOpts.SetHosts(mtest.ClusterConnString().Hosts[:1])
52+
// UseMultipleMongoses is only relevant if we're connected to a sharded cluster. Options changes and validation are
53+
// only required if the option is explicitly set. If it's unset, we make no changes because the cluster URI already
54+
// includes all nodes and we don't enforce any limits on the number of nodes.
55+
if mtest.ClusterTopologyKind() == mtest.Sharded && entityOptions.UseMultipleMongoses != nil {
56+
if err := evaluateUseMultipleMongoses(clientOpts, *entityOptions.UseMultipleMongoses); err != nil {
57+
return nil, err
58+
}
5659
}
5760
if entityOptions.ObserveEvents != nil {
5861
// Configure a command monitor that listens for the specified event types. We don't take the IgnoredCommands
@@ -183,3 +186,17 @@ func setClientOptionsFromURIOptions(clientOpts *options.ClientOptions, uriOpts b
183186
}
184187
return nil
185188
}
189+
190+
func evaluateUseMultipleMongoses(clientOpts *options.ClientOptions, useMultipleMongoses bool) error {
191+
hosts := mtest.ClusterConnString().Hosts
192+
193+
if !useMultipleMongoses {
194+
clientOpts.SetHosts(hosts[:1])
195+
return nil
196+
}
197+
198+
if len(hosts) < 2 {
199+
return fmt.Errorf("multiple mongoses required but cluster URI %q only contains one host", mtest.ClusterURI())
200+
}
201+
return nil
202+
}

mongo/integration/unified/entity_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ type EntityOptions struct {
2424

2525
// Options for client entities.
2626
URIOptions bson.M `bson:"uriOptions"`
27-
UseMultipleMongoses bool `bson:"useMultipleMongoses"`
27+
UseMultipleMongoses *bool `bson:"useMultipleMongoses"`
2828
ObserveEvents []string `bson:"observeEvents"`
2929
IgnoredCommands []string `bson:"ignoreCommandMonitoringEvents"`
3030

0 commit comments

Comments
 (0)