Skip to content

Commit c3e8a03

Browse files
author
Ryan
committed
added another test and brokeup logic into reusable methods.
1 parent e1bc944 commit c3e8a03

File tree

1 file changed

+114
-39
lines changed

1 file changed

+114
-39
lines changed

src/test/com/mongodb/SecondaryReadTest.java

Lines changed: 114 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -40,89 +40,157 @@ public class SecondaryReadTest extends TestCase {
4040
private static final double MAX_DEVIATION_PERCENT = 1.0;
4141

4242
@Test(groups = {"basic"})
43-
@SuppressWarnings({"unchecked"})
44-
public void testSecondaryReads() throws Exception {
43+
public void testSecondaryReads1() throws Exception {
44+
45+
final Mongo mongo = loadMongo();
46+
47+
final CommandResult result = serverStatusCmd(mongo);
48+
49+
// If the result is null, this is not a replica set.
50+
if (result == null) return;
51+
52+
final List<TestHost> testHosts = new ArrayList<TestHost>();
53+
final String primaryHostnameAndPort = extractHosts(result, testHosts);
54+
final DBCollection col = loadCleanDbCollection(mongo);
55+
56+
final List<ObjectId> insertedIds = insertTestData(col);
57+
58+
// Get the opcounter/query data for the hosts.
59+
loadQueryCount(testHosts, true);
60+
61+
final int secondaryCount = getSecondaryCount(testHosts);
62+
63+
// Perform some reads on the secondaries
64+
col.setReadPreference(ReadPreference.SECONDARY);
65+
66+
for (int idx=0; idx < ITERATION_COUNT; idx++) {
67+
for (ObjectId id : insertedIds) {
68+
final BasicDBObject doc = (BasicDBObject)col.findOne(new BasicDBObject("_id", id));
69+
if (doc == null) throw new IllegalStateException("Doc not found");
70+
if (!doc.getObjectId("_id").equals(id)) throw new IllegalStateException("Ids are off");
71+
}
72+
}
73+
74+
loadQueryCount(testHosts, false);
75+
76+
verifySecondaryCounts(secondaryCount, testHosts);
77+
}
78+
79+
@Test(groups = {"basic"})
80+
public void testSecondaryReads2() throws Exception {
81+
82+
final Mongo mongo = loadMongo();
83+
84+
final CommandResult result = serverStatusCmd(mongo);
85+
86+
// If the result is null, this is not a replica set.
87+
if (result == null) return;
88+
89+
final List<TestHost> testHosts = new ArrayList<TestHost>();
90+
final String primaryHostnameAndPort = extractHosts(result, testHosts);
91+
final DBCollection col = loadCleanDbCollection(mongo);
92+
93+
final List<ObjectId> insertedIds = insertTestData(col);
94+
95+
// Get the opcounter/query data for the hosts.
96+
loadQueryCount(testHosts, true);
97+
98+
final int secondaryCount = getSecondaryCount(testHosts);
99+
100+
// Perform some reads on the secondaries
101+
mongo.setReadPreference(ReadPreference.SECONDARY);
102+
103+
for (int idx=0; idx < ITERATION_COUNT; idx++) {
104+
for (ObjectId id : insertedIds) {
105+
final BasicDBObject doc = (BasicDBObject)col.findOne(new BasicDBObject("_id", id));
106+
if (doc == null) throw new IllegalStateException("Doc not found");
107+
if (!doc.getObjectId("_id").equals(id)) throw new IllegalStateException("Ids are off");
108+
}
109+
}
110+
111+
loadQueryCount(testHosts, false);
45112

46-
final Mongo mongo = new Mongo(new MongoURI("mongodb://127.0.0.1:27017,127.0.0.1:27018"));
113+
verifySecondaryCounts(secondaryCount, testHosts);
114+
}
115+
116+
private Mongo loadMongo() throws Exception {
117+
return new Mongo(new MongoURI("mongodb://127.0.0.1:27017,127.0.0.1:27018"));
118+
}
47119

120+
private CommandResult serverStatusCmd(final Mongo pMongo) {
48121
// Check to see if this is a replica set... if not, get out of here.
49-
final CommandResult result = mongo.getDB("admin").command(new BasicDBObject("replSetGetStatus", 1));
122+
final CommandResult result = pMongo.getDB("admin").command(new BasicDBObject("replSetGetStatus", 1));
50123

51124
final String errorMsg = result.getErrorMessage();
52125

53126
if (errorMsg != null && errorMsg.indexOf("--replSet") != -1) {
54127
System.err.println("---- SecondaryReadTest: This is not a replica set - not testing secondary reads");
55-
return;
128+
return null;
56129
}
57130

58-
String primaryHostnameAndPort = null;
131+
return result;
132+
}
59133

134+
@SuppressWarnings({"unchecked"})
135+
private String extractHosts(final CommandResult pResult, final List<TestHost> pHosts) {
136+
String primaryHostnameAndPort = null;
60137
// Extract the repl set members.
61-
final List<TestHost> testHosts = new ArrayList<TestHost>();
62-
for (final BasicDBObject member : (List<BasicDBObject>)result.get("members")) {
138+
139+
for (final BasicDBObject member : (List<BasicDBObject>)pResult.get("members")) {
63140
String hostnameAndPort = member.getString("name");
64141
if (hostnameAndPort.indexOf(":") == -1) hostnameAndPort = hostnameAndPort + ":27017";
65142

66143
final String stateStr = member.getString("stateStr");
67144

68145
if (stateStr.equals("PRIMARY")) primaryHostnameAndPort = hostnameAndPort;
69146

70-
testHosts.add(new TestHost(hostnameAndPort, stateStr));
147+
pHosts.add(new TestHost(hostnameAndPort, stateStr));
71148
}
72149

73150
if (primaryHostnameAndPort == null) throw new IllegalStateException("No primary defined");
74151

75-
mongo.getDB("com_mongodb_unittest_SecondaryReadTest").dropDatabase();
76-
final DB db = mongo.getDB("com_mongodb_unittest_SecondaryReadTest");
77-
final DBCollection col = db.getCollection("testBalance");
152+
return primaryHostnameAndPort;
153+
}
154+
155+
private DBCollection loadCleanDbCollection(final Mongo pMongo) {
156+
pMongo.getDB("com_mongodb_unittest_SecondaryReadTest").dropDatabase();
157+
final DB db = pMongo.getDB("com_mongodb_unittest_SecondaryReadTest");
158+
return db.getCollection("testBalance");
159+
}
78160

161+
private List<ObjectId> insertTestData(final DBCollection pCol) throws Exception {
79162
final ArrayList<ObjectId> insertedIds = new ArrayList<ObjectId>();
80163

81164
// Insert some test data.
82165
for (int idx=0; idx < INSERT_COUNT; idx++) {
83166
final ObjectId id = ObjectId.get();
84-
WriteResult writeResult = col.insert(new BasicDBObject("_id", id), WriteConcern.REPLICAS_SAFE);
167+
WriteResult writeResult = pCol.insert(new BasicDBObject("_id", id), WriteConcern.REPLICAS_SAFE);
85168
writeResult.getLastError().throwOnError();
86169
insertedIds.add(id);
87170
}
88171

89172
// Make sure everything is inserted.
90173
while (true) {
91-
final long count = col.count();
174+
final long count = pCol.count();
92175
if (count == INSERT_COUNT) break;
93176
Thread.sleep(1000);
94177
}
95178

96-
// Get the opcounter/query data for the hosts.
97-
loadQueryCount(testHosts, true);
179+
return insertedIds;
180+
}
98181

182+
private int getSecondaryCount(final List<TestHost> pHosts) {
99183
int secondaryCount = 0;
184+
for (final TestHost testHost : pHosts) if (testHost.stateStr.equals("SECONDARY")) secondaryCount++;
185+
return secondaryCount;
186+
}
100187

101-
for (final TestHost testHost : testHosts) if (testHost.stateStr.equals("SECONDARY")) secondaryCount++;
102-
103-
// Perform some reads on the secondaries
104-
col.setReadPreference(ReadPreference.SECONDARY);
105-
106-
for (int idx=0; idx < ITERATION_COUNT; idx++) {
107-
for (ObjectId id : insertedIds) {
108-
final BasicDBObject doc = (BasicDBObject)col.findOne(new BasicDBObject("_id", id));
109-
if (doc == null) throw new IllegalStateException("Doc not found");
110-
if (!doc.getObjectId("_id").equals(id)) throw new IllegalStateException("Ids are off");
111-
}
112-
}
113-
114-
loadQueryCount(testHosts, false);
115-
116-
/*
117-
for (final TestHost testHost : testHosts) {
118-
System.out.println("--- host: " + testHost.hostnameAndPort + " - queries: " + testHost.queriesBefore + " - after: " + testHost.queriesAfter);
119-
}
120-
*/
188+
private void verifySecondaryCounts(final int pSecondaryCount, final List<TestHost> pHosts) {
121189

122190
// Verify the counts.
123-
final int expectedPerSecondary = TOTAL_COUNT / secondaryCount;
191+
final int expectedPerSecondary = TOTAL_COUNT / pSecondaryCount;
124192

125-
for (final TestHost testHost : testHosts) {
193+
for (final TestHost testHost : pHosts) {
126194

127195
if (!testHost.stateStr.equals("SECONDARY")) continue;
128196

@@ -140,7 +208,14 @@ public void testSecondaryReads() throws Exception {
140208
assertEquals(true, (deviation <= MAX_DEVIATION_PERCENT));
141209
}
142210
}
143-
}
211+
212+
/*
213+
for (final TestHost testHost : pHosts) {
214+
System.out.println("--- host: " + testHost.hostnameAndPort + " - queries: " + testHost.queriesBefore + " - after: " + testHost.queriesAfter);
215+
}
216+
*/
217+
218+
}
144219

145220
private static void loadQueryCount(final List<TestHost> pHosts, final boolean pBefore) throws Exception {
146221
for (final TestHost testHost : pHosts) {

0 commit comments

Comments
 (0)