Skip to content

Commit 5550d32

Browse files
author
Ryan
committed
added try/finally to close out mongo object.
1 parent 1d00ea1 commit 5550d32

1 file changed

Lines changed: 96 additions & 86 deletions

File tree

src/test/com/mongodb/SecondaryReadTest.java

Lines changed: 96 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -44,150 +44,160 @@ public void testSecondaryReads1() throws Exception {
4444

4545
final Mongo mongo = loadMongo();
4646

47-
final CommandResult result = serverStatusCmd(mongo);
47+
try {
48+
final CommandResult result = serverStatusCmd(mongo);
4849

49-
// If the result is null, this is not a replica set.
50-
if (result == null) return;
50+
// If the result is null, this is not a replica set.
51+
if (result == null) return;
5152

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

56-
final List<ObjectId> insertedIds = insertTestData(col);
57+
final List<ObjectId> insertedIds = insertTestData(col);
5758

58-
// Get the opcounter/query data for the hosts.
59-
loadQueryCount(testHosts, true);
59+
// Get the opcounter/query data for the hosts.
60+
loadQueryCount(testHosts, true);
6061

61-
final int secondaryCount = getSecondaryCount(testHosts);
62+
final int secondaryCount = getSecondaryCount(testHosts);
6263

63-
// Perform some reads on the secondaries
64-
col.setReadPreference(ReadPreference.SECONDARY);
64+
// Perform some reads on the secondaries
65+
col.setReadPreference(ReadPreference.SECONDARY);
6566

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");
67+
for (int idx=0; idx < ITERATION_COUNT; idx++) {
68+
for (ObjectId id : insertedIds) {
69+
final BasicDBObject doc = (BasicDBObject)col.findOne(new BasicDBObject("_id", id));
70+
if (doc == null) throw new IllegalStateException("Doc not found");
71+
if (!doc.getObjectId("_id").equals(id)) throw new IllegalStateException("Ids are off");
72+
}
7173
}
72-
}
7374

74-
loadQueryCount(testHosts, false);
75+
loadQueryCount(testHosts, false);
7576

76-
verifySecondaryCounts(secondaryCount, testHosts);
77+
verifySecondaryCounts(secondaryCount, testHosts);
78+
} finally { if (mongo != null) mongo.close(); }
7779
}
7880

7981
@Test(groups = {"basic"})
8082
public void testSecondaryReads2() throws Exception {
8183

8284
final Mongo mongo = loadMongo();
8385

84-
final CommandResult result = serverStatusCmd(mongo);
86+
try {
87+
88+
final CommandResult result = serverStatusCmd(mongo);
8589

86-
// If the result is null, this is not a replica set.
87-
if (result == null) return;
90+
// If the result is null, this is not a replica set.
91+
if (result == null) return;
8892

89-
final List<TestHost> testHosts = new ArrayList<TestHost>();
90-
final String primaryHostnameAndPort = extractHosts(result, testHosts);
91-
final DBCollection col = loadCleanDbCollection(mongo);
93+
final List<TestHost> testHosts = new ArrayList<TestHost>();
94+
final String primaryHostnameAndPort = extractHosts(result, testHosts);
95+
final DBCollection col = loadCleanDbCollection(mongo);
9296

93-
final List<ObjectId> insertedIds = insertTestData(col);
97+
final List<ObjectId> insertedIds = insertTestData(col);
9498

95-
// Get the opcounter/query data for the hosts.
96-
loadQueryCount(testHosts, true);
99+
// Get the opcounter/query data for the hosts.
100+
loadQueryCount(testHosts, true);
97101

98-
final int secondaryCount = getSecondaryCount(testHosts);
102+
final int secondaryCount = getSecondaryCount(testHosts);
99103

100-
// Perform some reads on the secondaries
101-
mongo.setReadPreference(ReadPreference.SECONDARY);
104+
// Perform some reads on the secondaries
105+
mongo.setReadPreference(ReadPreference.SECONDARY);
102106

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");
107+
for (int idx=0; idx < ITERATION_COUNT; idx++) {
108+
for (ObjectId id : insertedIds) {
109+
final BasicDBObject doc = (BasicDBObject)col.findOne(new BasicDBObject("_id", id));
110+
if (doc == null) throw new IllegalStateException("Doc not found");
111+
if (!doc.getObjectId("_id").equals(id)) throw new IllegalStateException("Ids are off");
112+
}
108113
}
109-
}
110114

111-
loadQueryCount(testHosts, false);
115+
loadQueryCount(testHosts, false);
112116

113-
verifySecondaryCounts(secondaryCount, testHosts);
117+
verifySecondaryCounts(secondaryCount, testHosts);
118+
} finally { if (mongo != null) mongo.close(); }
114119
}
115120

116121
@Test(groups = {"basic"})
117122
public void testSecondaryReads3() throws Exception {
118123

119124
final Mongo mongo = loadMongo();
120125

121-
final CommandResult result = serverStatusCmd(mongo);
126+
try {
127+
128+
final CommandResult result = serverStatusCmd(mongo);
122129

123-
// If the result is null, this is not a replica set.
124-
if (result == null) return;
130+
// If the result is null, this is not a replica set.
131+
if (result == null) return;
125132

126-
final List<TestHost> testHosts = new ArrayList<TestHost>();
127-
final String primaryHostnameAndPort = extractHosts(result, testHosts);
128-
final DBCollection col = loadCleanDbCollection(mongo);
133+
final List<TestHost> testHosts = new ArrayList<TestHost>();
134+
final String primaryHostnameAndPort = extractHosts(result, testHosts);
135+
final DBCollection col = loadCleanDbCollection(mongo);
129136

130-
final List<ObjectId> insertedIds = insertTestData(col);
137+
final List<ObjectId> insertedIds = insertTestData(col);
131138

132-
// Get the opcounter/query data for the hosts.
133-
loadQueryCount(testHosts, true);
139+
// Get the opcounter/query data for the hosts.
140+
loadQueryCount(testHosts, true);
134141

135-
final int secondaryCount = getSecondaryCount(testHosts);
142+
final int secondaryCount = getSecondaryCount(testHosts);
136143

137-
// Perform some reads on the secondaries
138-
col.getDB().setReadPreference(ReadPreference.SECONDARY);
144+
// Perform some reads on the secondaries
145+
col.getDB().setReadPreference(ReadPreference.SECONDARY);
139146

140-
for (int idx=0; idx < ITERATION_COUNT; idx++) {
141-
for (ObjectId id : insertedIds) {
142-
final BasicDBObject doc = (BasicDBObject)col.findOne(new BasicDBObject("_id", id));
143-
if (doc == null) throw new IllegalStateException("Doc not found");
144-
if (!doc.getObjectId("_id").equals(id)) throw new IllegalStateException("Ids are off");
147+
for (int idx=0; idx < ITERATION_COUNT; idx++) {
148+
for (ObjectId id : insertedIds) {
149+
final BasicDBObject doc = (BasicDBObject)col.findOne(new BasicDBObject("_id", id));
150+
if (doc == null) throw new IllegalStateException("Doc not found");
151+
if (!doc.getObjectId("_id").equals(id)) throw new IllegalStateException("Ids are off");
152+
}
145153
}
146-
}
147154

148-
loadQueryCount(testHosts, false);
155+
loadQueryCount(testHosts, false);
149156

150-
verifySecondaryCounts(secondaryCount, testHosts);
157+
verifySecondaryCounts(secondaryCount, testHosts);
158+
} finally { if (mongo != null) mongo.close(); }
151159
}
152160

153161
@Test(groups = {"basic"})
154162
public void testSecondaryReadCursor() throws Exception {
155-
156163
final Mongo mongo = loadMongo();
164+
try {
157165

158-
final CommandResult result = serverStatusCmd(mongo);
166+
final CommandResult result = serverStatusCmd(mongo);
159167

160-
// If the result is null, this is not a replica set.
161-
if (result == null) return;
168+
// If the result is null, this is not a replica set.
169+
if (result == null) return;
162170

163-
final List<TestHost> testHosts = new ArrayList<TestHost>();
164-
final String primaryHostnameAndPort = extractHosts(result, testHosts);
165-
final DBCollection col = loadCleanDbCollection(mongo);
171+
final List<TestHost> testHosts = new ArrayList<TestHost>();
172+
final String primaryHostnameAndPort = extractHosts(result, testHosts);
173+
final DBCollection col = loadCleanDbCollection(mongo);
166174

167-
final List<ObjectId> insertedIds = insertTestData(col);
175+
final List<ObjectId> insertedIds = insertTestData(col);
168176

169-
// Get the opcounter/query data for the hosts.
170-
loadQueryCount(testHosts, true);
177+
// Get the opcounter/query data for the hosts.
178+
loadQueryCount(testHosts, true);
171179

172-
final int secondaryCount = getSecondaryCount(testHosts);
180+
final int secondaryCount = getSecondaryCount(testHosts);
173181

174-
// Perform some reads on the secondaries
175-
col.setReadPreference(ReadPreference.SECONDARY);
182+
// Perform some reads on the secondaries
183+
col.setReadPreference(ReadPreference.SECONDARY);
176184

177-
final DBCursor cur = col.find();
185+
final DBCursor cur = col.find();
178186

179-
ServerAddress curServerAddress = cur.getServerAddress();
187+
ServerAddress curServerAddress = cur.getServerAddress();
180188

181-
assertEquals(true, serverIsSecondary(curServerAddress, testHosts));
189+
assertEquals(true, serverIsSecondary(curServerAddress, testHosts));
182190

183-
try {
184-
while (cur.hasNext()) {
185-
cur.next();
186-
assertEquals(true, serverIsSecondary(cur.getServerAddress(), testHosts));
187-
}
188-
} finally { cur.close(); }
191+
try {
192+
while (cur.hasNext()) {
193+
cur.next();
194+
assertEquals(true, serverIsSecondary(cur.getServerAddress(), testHosts));
195+
}
196+
} finally { cur.close(); }
197+
198+
loadQueryCount(testHosts, false);
189199

190-
loadQueryCount(testHosts, false);
200+
} finally { if (mongo != null) mongo.close(); }
191201
}
192202

193203
private boolean serverIsSecondary(final ServerAddress pServerAddr, final List<TestHost> pHosts) {
@@ -203,7 +213,7 @@ private boolean serverIsSecondary(final ServerAddress pServerAddr, final List<Te
203213
}
204214

205215
private Mongo loadMongo() throws Exception {
206-
return new Mongo(new MongoURI("mongodb://127.0.0.1:27017,127.0.0.1:27018/?connectTimeoutMS=20000;socketTimeoutMS=20000;maxpoolsize=50;autoconnectretry=true"));
216+
return new Mongo(new MongoURI("mongodb://127.0.0.1:27017,127.0.0.1:27018/?connectTimeoutMS=30000;socketTimeoutMS=30000;maxpoolsize=5;autoconnectretry=true"));
207217
}
208218

209219
private CommandResult serverStatusCmd(final Mongo pMongo) {
@@ -308,7 +318,7 @@ private void verifySecondaryCounts(final int pSecondaryCount, final List<TestHos
308318

309319
private static void loadQueryCount(final List<TestHost> pHosts, final boolean pBefore) throws Exception {
310320
for (final TestHost testHost : pHosts) {
311-
final Mongo mongoHost = new Mongo(new MongoURI("mongodb://"+testHost.hostnameAndPort+"/?connectTimeoutMS=20000;socketTimeoutMS=20000;maxpoolsize=50;autoconnectretry=true"));
321+
final Mongo mongoHost = new Mongo(new MongoURI("mongodb://"+testHost.hostnameAndPort+"/?connectTimeoutMS=30000;socketTimeoutMS=30000;maxpoolsize=5;autoconnectretry=true"));
312322
try {
313323
final CommandResult serverStatusResult
314324
= mongoHost.getDB("com_mongodb_unittest_SecondaryReadTest").command(new BasicDBObject("serverStatus", 1));
@@ -318,7 +328,7 @@ private static void loadQueryCount(final List<TestHost> pHosts, final boolean pB
318328
if (pBefore) testHost.queriesBefore = opcounters.getLong("query");
319329
else testHost.queriesAfter = opcounters.getLong("query");
320330

321-
} finally { mongoHost.close(); }
331+
} finally { if (mongoHost != null) mongoHost.close(); }
322332
}
323333
}
324334

0 commit comments

Comments
 (0)