Skip to content

Commit a47471f

Browse files
committed
Merge branch 'master' of github.com:mongodb/mongo-java-driver
2 parents 1101a2d + f142244 commit a47471f

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

src/test/com/mongodb/SecondaryReadTest.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,57 @@ public void testSecondaryReads3() throws Exception {
150150
verifySecondaryCounts(secondaryCount, testHosts);
151151
}
152152

153+
@Test(groups = {"basic"})
154+
public void testSecondaryReadCursor() throws Exception {
155+
156+
final Mongo mongo = loadMongo();
157+
158+
final CommandResult result = serverStatusCmd(mongo);
159+
160+
// If the result is null, this is not a replica set.
161+
if (result == null) return;
162+
163+
final List<TestHost> testHosts = new ArrayList<TestHost>();
164+
final String primaryHostnameAndPort = extractHosts(result, testHosts);
165+
final DBCollection col = loadCleanDbCollection(mongo);
166+
167+
final List<ObjectId> insertedIds = insertTestData(col);
168+
169+
// Get the opcounter/query data for the hosts.
170+
loadQueryCount(testHosts, true);
171+
172+
final int secondaryCount = getSecondaryCount(testHosts);
173+
174+
// Perform some reads on the secondaries
175+
col.setReadPreference(ReadPreference.SECONDARY);
176+
177+
final DBCursor cur = col.find();
178+
179+
ServerAddress curServerAddress = cur.getServerAddress();
180+
181+
assertEquals(true, serverIsSecondary(curServerAddress, testHosts));
182+
183+
try {
184+
while (cur.hasNext()) {
185+
cur.next();
186+
assertEquals(true, serverIsSecondary(cur.getServerAddress(), testHosts));
187+
}
188+
} finally { cur.close(); }
189+
190+
loadQueryCount(testHosts, false);
191+
}
192+
193+
private boolean serverIsSecondary(final ServerAddress pServerAddr, final List<TestHost> pHosts) {
194+
for (final TestHost h : pHosts) {
195+
if (!h.stateStr.equals("SECONDARY")) continue;
196+
final int portIdx = h.hostnameAndPort.indexOf(":");
197+
final int port = Integer.parseInt(h.hostnameAndPort.substring(portIdx+1, h.hostnameAndPort.length()));
198+
final String hostname = h.hostnameAndPort.substring(0, portIdx);
199+
if (pServerAddr.getPort() == port && hostname.equals(pServerAddr.getHost())) return true;
200+
}
201+
202+
return false;
203+
}
153204

154205
private Mongo loadMongo() throws Exception {
155206
return new Mongo(new MongoURI("mongodb://127.0.0.1:27017,127.0.0.1:27018"));

0 commit comments

Comments
 (0)