Skip to content

Commit f2a1301

Browse files
author
Ryan
committed
added another test - this time setting read preference on the db object.
1 parent a8b837f commit f2a1301

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

src/test/com/mongodb/SecondaryReadTest.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,44 @@ public void testSecondaryReads2() throws Exception {
113113
verifySecondaryCounts(secondaryCount, testHosts);
114114
}
115115

116+
@Test(groups = {"basic"})
117+
public void testSecondaryReads3() throws Exception {
118+
119+
final Mongo mongo = loadMongo();
120+
121+
final CommandResult result = serverStatusCmd(mongo);
122+
123+
// If the result is null, this is not a replica set.
124+
if (result == null) return;
125+
126+
final List<TestHost> testHosts = new ArrayList<TestHost>();
127+
final String primaryHostnameAndPort = extractHosts(result, testHosts);
128+
final DBCollection col = loadCleanDbCollection(mongo);
129+
130+
final List<ObjectId> insertedIds = insertTestData(col);
131+
132+
// Get the opcounter/query data for the hosts.
133+
loadQueryCount(testHosts, true);
134+
135+
final int secondaryCount = getSecondaryCount(testHosts);
136+
137+
// Perform some reads on the secondaries
138+
col.getDB().setReadPreference(ReadPreference.SECONDARY);
139+
140+
for (int idx=0; idx < ITERATION_COUNT; idx++) {
141+
for (ObjectId id : insertedIds) {
142+
final BasicDBObject doc = (BasicDBObject)col.findOne(new BasicDBObject("_id", id));
143+
if (doc == null) throw new IllegalStateException("Doc not found");
144+
if (!doc.getObjectId("_id").equals(id)) throw new IllegalStateException("Ids are off");
145+
}
146+
}
147+
148+
loadQueryCount(testHosts, false);
149+
150+
verifySecondaryCounts(secondaryCount, testHosts);
151+
}
152+
153+
116154
private Mongo loadMongo() throws Exception {
117155
return new Mongo(new MongoURI("mongodb://127.0.0.1:27017,127.0.0.1:27018"));
118156
}

0 commit comments

Comments
 (0)