1717package com .mongodb ;
1818
1919// Mongo
20- import com .mongodb .*;
2120import org .bson .types .*;
2221import com .mongodb .util .*;
2322
2625
2726// Java
2827import java .util .*;
29- import java .util .concurrent .*;
3028
3129public 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 ) {
0 commit comments