Skip to content

Commit 90ec0aa

Browse files
committed
Set connection mode to MULTIPLE when srvHost is specified in ClusterSettings
Also: * Fix the check that the srvHost has three parts * Throw exception if the mode is not compatible with SRV * Beef up the unit tests for SRV in ClusterSettings JAVA-3573
1 parent d3a7353 commit 90ec0aa

File tree

2 files changed

+56
-10
lines changed

2 files changed

+56
-10
lines changed

driver-core/src/main/com/mongodb/connection/ClusterSettings.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package com.mongodb.connection;
1818

1919
import com.mongodb.ConnectionString;
20-
import com.mongodb.MongoClientException;
2120
import com.mongodb.ServerAddress;
2221
import com.mongodb.annotations.Immutable;
2322
import com.mongodb.annotations.NotThreadSafe;
@@ -519,14 +518,13 @@ public String getShortDescription() {
519518
}
520519

521520
private ClusterSettings(final Builder builder) {
522-
// TODO: Unit test this
523521
if (builder.srvHost != null) {
524522
if (builder.srvHost.contains(":")) {
525523
throw new IllegalArgumentException("The srvHost can not contain a host name that specifies a port");
526524
}
527525

528-
if (builder.hosts.get(0).getHost().split("\\.").length < 3) {
529-
throw new MongoClientException(format("An SRV host name '%s' was provided that does not contain at least three parts. "
526+
if (builder.srvHost.split("\\.").length < 3) {
527+
throw new IllegalArgumentException(format("An SRV host name '%s' was provided that does not contain at least three parts. "
530528
+ "It must contain a hostname, domain name and a top level domain.", builder.hosts.get(0).getHost()));
531529
}
532530
}
@@ -535,10 +533,6 @@ private ClusterSettings(final Builder builder) {
535533
throw new IllegalArgumentException("Multiple hosts cannot be specified when using ClusterType.STANDALONE.");
536534
}
537535

538-
if (builder.mode != null && builder.mode == ClusterConnectionMode.SINGLE && builder.hosts.size() > 1) {
539-
throw new IllegalArgumentException("Can not directly connect to more than one server");
540-
}
541-
542536
if (builder.requiredReplicaSetName != null) {
543537
if (builder.requiredClusterType == ClusterType.UNKNOWN) {
544538
builder.requiredClusterType = ClusterType.REPLICA_SET;
@@ -550,7 +544,18 @@ private ClusterSettings(final Builder builder) {
550544

551545
srvHost = builder.srvHost;
552546
hosts = builder.hosts;
553-
mode = builder.mode != null ? builder.mode : hosts.size() == 1 ? ClusterConnectionMode.SINGLE : ClusterConnectionMode.MULTIPLE;
547+
if (srvHost != null) {
548+
if (builder.mode == ClusterConnectionMode.SINGLE) {
549+
throw new IllegalArgumentException("An SRV host name was provided but the connection mode is not MULTIPLE");
550+
}
551+
mode = ClusterConnectionMode.MULTIPLE;
552+
} else {
553+
if (builder.mode == ClusterConnectionMode.SINGLE && builder.hosts.size() > 1) {
554+
throw new IllegalArgumentException("Can not directly connect to more than one server");
555+
}
556+
557+
mode = builder.mode != null ? builder.mode : hosts.size() == 1 ? ClusterConnectionMode.SINGLE : ClusterConnectionMode.MULTIPLE;
558+
}
554559
requiredReplicaSetName = builder.requiredReplicaSetName;
555560
requiredClusterType = builder.requiredClusterType;
556561
localThresholdMS = builder.localThresholdMS;

driver-core/src/test/unit/com/mongodb/connection/ClusterSettingsSpecification.groovy

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import spock.lang.Specification
2727

2828
import java.util.concurrent.TimeUnit
2929

30-
// TODO: add SRV tests
3130
class ClusterSettingsSpecification extends Specification {
3231
def hosts = [new ServerAddress('localhost'), new ServerAddress('localhost', 30000)]
3332
def serverSelector = new WritableServerSelector()
@@ -97,6 +96,48 @@ class ClusterSettingsSpecification extends Specification {
9796
ClusterSettings.builder(customSettings).applySettings(defaultSettings).build() == defaultSettings
9897
}
9998

99+
def 'when hosts contains more than one element and mode is SINGLE, should throw IllegalArgumentException'() {
100+
when:
101+
def builder = ClusterSettings.builder()
102+
builder.hosts([new ServerAddress('host1'), new ServerAddress('host2')])
103+
builder.mode(ClusterConnectionMode.SINGLE)
104+
builder.build()
105+
106+
then:
107+
thrown(IllegalArgumentException)
108+
}
109+
110+
def 'when srvHost is specified, should set mode to MULTIPLE'() {
111+
when:
112+
def builder = ClusterSettings.builder()
113+
builder.srvHost('foo.bar.com')
114+
def settings = builder.build()
115+
116+
then:
117+
settings.getSrvHost() == 'foo.bar.com'
118+
settings.getMode() == ClusterConnectionMode.MULTIPLE
119+
}
120+
121+
def 'when srvHost contains a colon, should throw IllegalArgumentException'() {
122+
when:
123+
def builder = ClusterSettings.builder()
124+
builder.srvHost('foo.bar.com:27017')
125+
builder.build()
126+
127+
then:
128+
thrown(IllegalArgumentException)
129+
}
130+
131+
def 'when srvHost contains less than three parts (host, domain, top-level domain, should throw IllegalArgumentException'() {
132+
when:
133+
def builder = ClusterSettings.builder()
134+
builder.srvHost('foo.bar')
135+
builder.build()
136+
137+
then:
138+
thrown(IllegalArgumentException)
139+
}
140+
100141
def 'should allow configure serverSelectors correctly'() {
101142
given:
102143
def latMinServerSelector = new LatencyMinimizingServerSelector(10, TimeUnit.MILLISECONDS)

0 commit comments

Comments
 (0)