2424import java .util .List ;
2525
2626import static java .lang .String .format ;
27+ import static java .util .Arrays .asList ;
2728import static org .junit .Assert .assertTrue ;
2829
2930public class SecondaryReadTest extends TestCase {
@@ -163,8 +164,7 @@ private boolean serverIsSecondary(final ServerAddress pServerAddr, final List<Te
163164 }
164165
165166 private Mongo loadMongo () throws Exception {
166- return new MongoClient (new MongoClientURI (
167- "mongodb://127.0.0.1:27017,127.0.0.1:27018,127.0.0.1:27019/?connectTimeoutMS=30000;socketTimeoutMS=30000;maxpoolsize=5;autoconnectretry=true" ));
167+ return new MongoClient (getMongoClientURI ());
168168 }
169169
170170 @ SuppressWarnings ({"unchecked" })
@@ -180,8 +180,7 @@ private List<TestHost> extractHosts(Mongo mongo) {
180180 hostnameAndPort = hostnameAndPort + ":27017" ;
181181 }
182182
183- final String stateStr = member .getString ("stateStr" );
184-
183+ String stateStr = member .getString ("stateStr" );
185184 pHosts .add (new TestHost (hostnameAndPort , stateStr ));
186185 }
187186
@@ -237,19 +236,25 @@ private void verifySecondaryCounts(final int pSecondaryCount, final List<TestHos
237236 }
238237
239238 private static void loadQueryCount (final List <TestHost > pHosts , final boolean pBefore ) throws Exception {
240- for (final TestHost testHost : pHosts ) {
241- final Mongo mongoHost = new MongoClient (new MongoClientURI ("mongodb://" +testHost .hostnameAndPort
242- + "/?connectTimeoutMS=30000;socketTimeoutMS=30000;maxpoolsize=5;"
243- + "autoconnectretry=true" ));
239+ for (TestHost testHost : pHosts ) {
240+ Mongo mongoHost ;
241+ if (getMongoClientURI ().getCredentials () == null || testHost .stateStr .equals ("ARBITER" )) {
242+ mongoHost = new MongoClient (testHost .hostnameAndPort );
243+ } else {
244+ mongoHost = new MongoClient (new ServerAddress (testHost .hostnameAndPort ), asList (getMongoClientURI ().getCredentials ()));
245+ }
244246 mongoHost .setReadPreference (ReadPreference .nearest ());
245247 try {
246- final CommandResult serverStatusResult
247- = mongoHost . getDB ( "com_mongodb_unittest_SecondaryReadTest" ) .command (new BasicDBObject ("serverStatus" , 1 ));
248+ CommandResult serverStatusResult = mongoHost . getDB ( "com_mongodb_unittest_SecondaryReadTest" )
249+ .command (new BasicDBObject ("serverStatus" , 1 ));
248250
249- final BasicDBObject opcounters = (BasicDBObject )serverStatusResult .get ("opcounters" );
251+ BasicDBObject opcounters = (BasicDBObject ) serverStatusResult .get ("opcounters" );
250252
251- if (pBefore ) testHost .queriesBefore = opcounters .getLong ("query" );
252- else testHost .queriesAfter = opcounters .getLong ("query" );
253+ if (pBefore ) {
254+ testHost .queriesBefore = opcounters .getLong ("query" );
255+ } else {
256+ testHost .queriesAfter = opcounters .getLong ("query" );
257+ }
253258
254259 } finally {
255260 mongoHost .close ();
0 commit comments