Skip to content

Commit 1d90607

Browse files
committed
JAVA-535: Respecting read preference on DB for DB.getCollectionNames
1 parent 3b12c2a commit 1d90607

File tree

3 files changed

+36
-7
lines changed

3 files changed

+36
-7
lines changed

src/main/com/mongodb/DB.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,8 +295,7 @@ public Set<String> getCollectionNames()
295295
if (namespaces == null)
296296
throw new RuntimeException("this is impossible");
297297

298-
// TODO - Is ReadPreference OK for collection Names?
299-
Iterator<DBObject> i = namespaces.__find(new BasicDBObject(), null, 0, 0, 0, getOptions(), null, null);
298+
Iterator<DBObject> i = namespaces.__find(new BasicDBObject(), null, 0, 0, 0, getOptions(), getReadPreference(), null);
300299
if (i == null)
301300
return new HashSet<String>();
302301

src/test/com/mongodb/DBTests.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,15 +123,35 @@ public void testCommandToSecondary() throws MongoException, UnknownHostException
123123

124124
DB db = mongo.getDB("secondaryTest");
125125
db.setReadPreference(ReadPreference.SECONDARY);
126-
// CommandResult result = db.command("ping");
127-
// assertNotEquals(primary, result.get("serverUsed"));
126+
CommandResult result = db.command("ping");
127+
assertNotEquals(primary, result.get("serverUsed"));
128+
} finally {
129+
mongo.close();
130+
}
131+
}
128132

129-
db.getCollection("secondaryTest").findOne();
133+
@Test
134+
public void testGetCollectionNamesToSecondary() throws MongoException, UnknownHostException {
135+
Mongo mongo = new Mongo(Arrays.asList(new ServerAddress("127.0.0.1"), new ServerAddress("127.0.0.1", 27018)));
136+
137+
try {
138+
if (isStandalone(mongo)) {
139+
return;
140+
}
141+
142+
String secondary = getASecondaryAsString(mongo);
143+
mongo.close();
144+
mongo = new Mongo(secondary);
145+
DB db = mongo.getDB("secondaryTest");
146+
db.setReadPreference(ReadPreference.SECONDARY);
147+
db.getCollectionNames();
130148
} finally {
131149
mongo.close();
132150
}
133151
}
134152

153+
154+
135155
@Test
136156
@SuppressWarnings("deprecation")
137157
public void testTurnOffSlaveOk() throws MongoException, UnknownHostException {

src/test/com/mongodb/util/TestCase.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,16 @@ protected boolean isStandalone(Mongo mongo) {
259259

260260
@SuppressWarnings({"unchecked"})
261261
protected String getPrimaryAsString(Mongo mongo) {
262+
return getMemberNameByState(mongo, "primary");
263+
}
264+
265+
@SuppressWarnings({"unchecked"})
266+
protected String getASecondaryAsString(Mongo mongo) {
267+
return getMemberNameByState(mongo, "secondary");
268+
}
269+
270+
@SuppressWarnings({"unchecked"})
271+
protected String getMemberNameByState(Mongo mongo, String stateStrToMatch) {
262272
CommandResult replicaSetStatus = runReplicaSetStatusCommand(mongo);
263273

264274
for (final BasicDBObject member : (List<BasicDBObject>) replicaSetStatus.get("members")) {
@@ -268,11 +278,11 @@ protected String getPrimaryAsString(Mongo mongo) {
268278

269279
final String stateStr = member.getString("stateStr");
270280

271-
if (stateStr.equals("PRIMARY"))
281+
if (stateStr.equalsIgnoreCase(stateStrToMatch))
272282
return hostnameAndPort;
273283
}
274284

275-
throw new IllegalStateException("No primary defined");
285+
throw new IllegalStateException("No member found in state " + stateStrToMatch);
276286
}
277287

278288
@SuppressWarnings("unchecked")

0 commit comments

Comments
 (0)