Skip to content

Commit baf0d8c

Browse files
committed
Refactored replica set tests
1 parent 408b356 commit baf0d8c

2 files changed

Lines changed: 53 additions & 51 deletions

File tree

src/test/com/mongodb/SecondaryReadTest.java

Lines changed: 26 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package com.mongodb;
1818

1919
// Mongo
20-
import com.mongodb.*;
2120
import org.bson.types.*;
2221
import com.mongodb.util.*;
2322

@@ -26,7 +25,6 @@
2625

2726
// Java
2827
import java.util.*;
29-
import java.util.concurrent.*;
3028

3129
public class SecondaryReadTest extends TestCase {
3230

@@ -45,13 +43,12 @@ public void testSecondaryReads1() throws Exception {
4543
final Mongo mongo = loadMongo();
4644

4745
try {
48-
final CommandResult result = serverStatusCmd(mongo);
46+
if (isStandalone(mongo)) {
47+
return;
48+
}
4949

50-
// If the result is null, this is not a replica set.
51-
if (result == null) return;
50+
final List<TestHost> testHosts = extractHosts(mongo);
5251

53-
final List<TestHost> testHosts = new ArrayList<TestHost>();
54-
final String primaryHostnameAndPort = extractHosts(result, testHosts);
5552
final DBCollection col = loadCleanDbCollection(mongo);
5653

5754
final List<ObjectId> insertedIds = insertTestData(col);
@@ -84,14 +81,12 @@ public void testSecondaryReads2() throws Exception {
8481
final Mongo mongo = loadMongo();
8582

8683
try {
84+
if (isStandalone(mongo)) {
85+
return;
86+
}
8787

88-
final CommandResult result = serverStatusCmd(mongo);
89-
90-
// If the result is null, this is not a replica set.
91-
if (result == null) return;
88+
final List<TestHost> testHosts = extractHosts(mongo);
9289

93-
final List<TestHost> testHosts = new ArrayList<TestHost>();
94-
final String primaryHostnameAndPort = extractHosts(result, testHosts);
9590
final DBCollection col = loadCleanDbCollection(mongo);
9691

9792
final List<ObjectId> insertedIds = insertTestData(col);
@@ -124,14 +119,12 @@ public void testSecondaryReads3() throws Exception {
124119
final Mongo mongo = loadMongo();
125120

126121
try {
122+
if (isStandalone(mongo)) {
123+
return;
124+
}
127125

128-
final CommandResult result = serverStatusCmd(mongo);
129-
130-
// If the result is null, this is not a replica set.
131-
if (result == null) return;
126+
final List<TestHost> testHosts = extractHosts(mongo);
132127

133-
final List<TestHost> testHosts = new ArrayList<TestHost>();
134-
final String primaryHostnameAndPort = extractHosts(result, testHosts);
135128
final DBCollection col = loadCleanDbCollection(mongo);
136129

137130
final List<ObjectId> insertedIds = insertTestData(col);
@@ -162,23 +155,19 @@ public void testSecondaryReads3() throws Exception {
162155
public void testSecondaryReadCursor() throws Exception {
163156
final Mongo mongo = loadMongo();
164157
try {
158+
if (isStandalone(mongo)) {
159+
return;
160+
}
165161

166-
final CommandResult result = serverStatusCmd(mongo);
167-
168-
// If the result is null, this is not a replica set.
169-
if (result == null) return;
162+
final List<TestHost> testHosts = extractHosts(mongo);
170163

171-
final List<TestHost> testHosts = new ArrayList<TestHost>();
172-
final String primaryHostnameAndPort = extractHosts(result, testHosts);
173164
final DBCollection col = loadCleanDbCollection(mongo);
174165

175-
final List<ObjectId> insertedIds = insertTestData(col);
166+
insertTestData(col);
176167

177168
// Get the opcounter/query data for the hosts.
178169
loadQueryCount(testHosts, true);
179170

180-
final int secondaryCount = getSecondaryCount(testHosts);
181-
182171
// Perform some reads on the secondaries
183172
col.setReadPreference(ReadPreference.SECONDARY);
184173

@@ -221,39 +210,25 @@ private Mongo loadMongo() throws Exception {
221210
return new Mongo(new MongoURI("mongodb://127.0.0.1:27017,127.0.0.1:27018/?connectTimeoutMS=30000;socketTimeoutMS=30000;maxpoolsize=5;autoconnectretry=true"));
222211
}
223212

224-
private CommandResult serverStatusCmd(final Mongo pMongo) {
225-
// Check to see if this is a replica set... if not, get out of here.
226-
final CommandResult result = pMongo.getDB("admin").command(new BasicDBObject("replSetGetStatus", 1));
227-
228-
final String errorMsg = result.getErrorMessage();
229-
230-
if (errorMsg != null && errorMsg.indexOf("--replSet") != -1) {
231-
System.err.println("---- SecondaryReadTest: This is not a replica set - not testing secondary reads");
232-
return null;
233-
}
213+
@SuppressWarnings({"unchecked"})
214+
private List<TestHost> extractHosts(Mongo mongo) {
215+
CommandResult result = runReplicaSetStatusCommand(mongo);
234216

235-
return result;
236-
}
217+
List<TestHost> pHosts = new ArrayList<TestHost>();
237218

238-
@SuppressWarnings({"unchecked"})
239-
private String extractHosts(final CommandResult pResult, final List<TestHost> pHosts) {
240-
String primaryHostnameAndPort = null;
241219
// Extract the repl set members.
242-
243-
for (final BasicDBObject member : (List<BasicDBObject>)pResult.get("members")) {
220+
for (final BasicDBObject member : (List<BasicDBObject>) result.get("members")) {
244221
String hostnameAndPort = member.getString("name");
245-
if (hostnameAndPort.indexOf(":") == -1) hostnameAndPort = hostnameAndPort + ":27017";
222+
if (!hostnameAndPort.contains(":")) {
223+
hostnameAndPort = hostnameAndPort + ":27017";
224+
}
246225

247226
final String stateStr = member.getString("stateStr");
248227

249-
if (stateStr.equals("PRIMARY")) primaryHostnameAndPort = hostnameAndPort;
250-
251228
pHosts.add(new TestHost(hostnameAndPort, stateStr));
252229
}
253230

254-
if (primaryHostnameAndPort == null) throw new IllegalStateException("No primary defined");
255-
256-
return primaryHostnameAndPort;
231+
return pHosts;
257232
}
258233

259234
private DBCollection loadCleanDbCollection(final Mongo pMongo) {

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import java.util.ArrayList;
2727
import java.util.List;
2828

29+
import com.mongodb.BasicDBObject;
30+
import com.mongodb.CommandResult;
2931
import com.mongodb.Mongo;
3032

3133
public class TestCase extends MyAsserts {
@@ -241,6 +243,31 @@ protected boolean serverIsAtLeastVersion(double version) {
241243
return Double.parseDouble(serverVersion.substring(0, 3)) >= version;
242244
}
243245

246+
/**
247+
*
248+
* @param mongo the connection
249+
* @return true if connected to a standalone server
250+
*/
251+
protected boolean isStandalone(Mongo mongo) {
252+
return runReplicaSetStatusCommand(mongo) == null;
253+
}
254+
255+
protected CommandResult runReplicaSetStatusCommand(final Mongo pMongo) {
256+
// Check to see if this is a replica set... if not, get out of here.
257+
final CommandResult result = pMongo.getDB("admin").command(new BasicDBObject("replSetGetStatus", 1));
258+
259+
final String errorMsg = result.getErrorMessage();
260+
261+
if (errorMsg != null && errorMsg.indexOf("--replSet") != -1) {
262+
System.err.println("---- SecondaryReadTest: This is not a replica set - not testing secondary reads");
263+
return null;
264+
}
265+
266+
return result;
267+
}
268+
269+
270+
244271
public static void main( String args[] )
245272
throws Exception {
246273

0 commit comments

Comments
 (0)