Skip to content

Commit 8a3cbc9

Browse files
committed
JAVA-2387: Mongo.getAddressList returns the seed list, as per the documentation, instead of the list of discovered servers
1 parent 365213f commit 8a3cbc9

File tree

4 files changed

+35
-6
lines changed

4 files changed

+35
-6
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@
2929
*/
3030
public interface Cluster extends Closeable{
3131

32+
/**
33+
* Gets the cluster settings with which this cluster was created.
34+
*
35+
* @return the cluster settings
36+
* @since 3.4
37+
*/
38+
ClusterSettings getSettings();
39+
3240
/**
3341
* Get the description of this cluster. This method will not return normally until the cluster type is known.
3442
*

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,19 @@ class BaseClusterSpecification extends Specification {
4949
private final List<ServerAddress> allServers = [firstServer, secondServer, thirdServer]
5050
private final TestClusterableServerFactory factory = new TestClusterableServerFactory()
5151

52+
def 'should get cluster settings'() {
53+
given:
54+
def clusterSettings = builder().mode(MULTIPLE)
55+
.hosts([firstServer, secondServer, thirdServer])
56+
.serverSelectionTimeout(1, SECONDS)
57+
.serverSelector(new ServerAddressSelector(firstServer))
58+
.build()
59+
def cluster = new MultiServerCluster(new ClusterId(), clusterSettings, factory)
60+
61+
expect:
62+
cluster.getSettings() == clusterSettings
63+
}
64+
5265
def 'should compose server selector passed to selectServer with server selector in cluster settings'() {
5366
given:
5467
def cluster = new MultiServerCluster(new ClusterId(),

driver/src/main/com/mongodb/Mongo.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,8 +390,7 @@ public ReadPreference getReadPreference() {
390390
* @throws MongoException if there's a failure
391391
*/
392392
public List<ServerAddress> getAllAddress() {
393-
//TODO It should return the address list without auto-discovered nodes. Not sure if it's required. Maybe users confused with name.
394-
return getServerAddressList();
393+
return cluster.getSettings().getHosts();
395394
}
396395

397396
/**

driver/src/test/unit/com/mongodb/MongoConstructorsTest.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@
2222
import java.net.UnknownHostException;
2323
import java.util.Arrays;
2424
import java.util.Collections;
25+
import java.util.List;
2526

2627
import static org.junit.Assert.assertEquals;
2728
import static org.junit.Assert.assertNotNull;
2829
import static org.junit.Assert.assertSame;
30+
import static org.junit.Assert.fail;
2931

3032
public class MongoConstructorsTest {
3133

@@ -52,11 +54,18 @@ public void shouldUseGivenHost() throws UnknownHostException {
5254
}
5355

5456
@Test
55-
@Ignore
56-
public void shouldUseGivenServerAddress() throws UnknownHostException {
57-
Mongo mongo = new MongoClient(new ServerAddress("localhost"));
57+
public void shouldGetSeedList() throws UnknownHostException {
58+
List<ServerAddress> seedList = Arrays.asList(new ServerAddress("localhost"), new ServerAddress("localhost:27018"));
59+
Mongo mongo = new MongoClient(seedList);
5860
try {
59-
assertEquals(Arrays.asList(new ServerAddress("localhost")), mongo.getServerAddressList());
61+
assertEquals(seedList, mongo.getAllAddress());
62+
try {
63+
mongo.getAllAddress().add(new ServerAddress("localhost:27019"));
64+
fail("Address list modification should be unsupported");
65+
} catch (UnsupportedOperationException e) {
66+
// all good
67+
}
68+
6069
} finally {
6170
mongo.close();
6271
}

0 commit comments

Comments
 (0)