Skip to content

Commit 4976da5

Browse files
committed
JAVA-2778: Test that sessions and bindings have been properly released after every functional test in driver
1 parent 0e90dd4 commit 4976da5

File tree

9 files changed

+82
-15
lines changed

9 files changed

+82
-15
lines changed

driver/src/main/com/mongodb/Mongo.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,10 @@ Cluster getCluster() {
782782
return cluster;
783783
}
784784

785+
ServerSessionPool getServerSessionPool() {
786+
return serverSessionPool;
787+
}
788+
785789
Bytes.OptionHolder getOptionHolder() {
786790
return optionHolder;
787791
}

driver/src/test/functional/com/mongodb/DBCollectionAggregationTest.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,10 @@ public void testDollarOutOnSecondary() throws UnknownHostException, InterruptedE
145145
AggregationOptions options = AggregationOptions.builder()
146146
.outputMode(AggregationOptions.OutputMode.CURSOR)
147147
.build();
148-
Cursor cursor = verify(pipeline, options, ReadPreference.secondary(), collection);
148+
ServerAddress serverAddress = verify(pipeline, options, ReadPreference.secondary(), collection);
149149
assertEquals(2, database.getCollection("aggCollection")
150150
.count());
151-
assertEquals(Fixture.getPrimary(), cursor.getServerAddress());
151+
assertEquals(Fixture.getPrimary(), serverAddress);
152152
}
153153

154154
public List<DBObject> prepareData() {
@@ -248,14 +248,20 @@ private void verify(final List<DBObject> pipeline, final AggregationOptions opti
248248
verify(pipeline, options, readPreference, collection);
249249
}
250250

251-
private Cursor verify(final List<DBObject> pipeline, final AggregationOptions options, final ReadPreference readPreference,
251+
private ServerAddress verify(final List<DBObject> pipeline, final AggregationOptions options, final ReadPreference readPreference,
252252
final DBCollection collection) {
253253
Cursor cursor = collection.aggregate(pipeline, options, readPreference);
254-
255-
Map<String, DBObject> results = new HashMap<String, DBObject>();
256-
while (cursor.hasNext()) {
257-
DBObject next = cursor.next();
258-
results.put((String) next.get("_id"), next);
254+
ServerAddress serverAddress = null;
255+
Map<String, DBObject> results;
256+
try {
257+
results = new HashMap<String, DBObject>();
258+
while (cursor.hasNext()) {
259+
DBObject next = cursor.next();
260+
results.put((String) next.get("_id"), next);
261+
}
262+
} finally {
263+
serverAddress = cursor.getServerAddress();
264+
cursor.close();
259265
}
260266

261267

@@ -269,6 +275,6 @@ private Cursor verify(final List<DBObject> pipeline, final AggregationOptions op
269275
assertEquals(1, barResult.get("docsPerName"));
270276
assertEquals(2, barResult.get("countPerName"));
271277

272-
return cursor;
278+
return serverAddress;
273279
}
274280
}

driver/src/test/functional/com/mongodb/DBCursorOldTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ public void testTailable() {
135135
assertEquals(null, cur.tryNext());
136136
assertEquals(secondDBObject, cur.curr());
137137
assertEquals(2, cur.numSeen());
138+
139+
cur.close();
138140
}
139141

140142
@Test
@@ -178,6 +180,8 @@ public Integer call() throws Exception {
178180
// this doc should unblock thread
179181
c.save(new BasicDBObject("x", 10), WriteConcern.ACKNOWLEDGED);
180182
assertEquals(10, (long) future.get(5, SECONDS));
183+
184+
cur.close();
181185
}
182186

183187
@Test
@@ -220,6 +224,8 @@ public Integer call() throws Exception {
220224
// this doc should unblock thread
221225
c.save(new BasicDBObject("x", 10), WriteConcern.ACKNOWLEDGED);
222226
assertEquals(10, (long) future.get(5, SECONDS));
227+
228+
cur.close();
223229
}
224230

225231
@Test
@@ -237,6 +243,8 @@ public void shouldSupportTryNextOnTailableCursors() {
237243
cur.tryNext();
238244
} catch (IllegalArgumentException e) {
239245
fail();
246+
} finally {
247+
cur.close();
240248
}
241249
}
242250

@@ -256,6 +264,8 @@ public void shouldSupportTryNextOnTailableAwaitCursors() {
256264
cur.tryNext();
257265
} catch (IllegalArgumentException e) {
258266
fail();
267+
} finally {
268+
cur.close();
259269
}
260270
}
261271

driver/src/test/functional/com/mongodb/DBCursorTest.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -207,14 +207,22 @@ public void testSkip() {
207207
@Test
208208
public void testGetCursorId() {
209209
DBCursor cursor = collection.find().batchSize(2);
210-
assertEquals(0, cursor.getCursorId());
211-
cursor.hasNext();
212-
assertThat(cursor.getCursorId(), is(not(0L)));
210+
try {
211+
assertEquals(0, cursor.getCursorId());
212+
cursor.hasNext();
213+
assertThat(cursor.getCursorId(), is(not(0L)));
214+
} finally {
215+
cursor.close();
216+
}
213217

214218
cursor = collection.find();
215-
assertEquals(0, cursor.getCursorId());
216-
cursor.hasNext();
217-
assertThat(cursor.getCursorId(), is(0L));
219+
try {
220+
assertEquals(0, cursor.getCursorId());
221+
cursor.hasNext();
222+
assertThat(cursor.getCursorId(), is(0L));
223+
} finally {
224+
cursor.close();
225+
}
218226
}
219227

220228
@Test

driver/src/test/functional/com/mongodb/DatabaseTestCase.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import static com.mongodb.Fixture.getDefaultDatabaseName;
2323
import static com.mongodb.Fixture.getMongoClient;
24+
import static com.mongodb.Fixture.getServerSessionPoolInUseCount;
2425

2526
public class DatabaseTestCase {
2627
//For ease of use and readability, in this specific case we'll allow protected variables
@@ -43,6 +44,10 @@ public void setUp() {
4344
@After
4445
public void tearDown() {
4546
collection.drop();
47+
48+
if (getServerSessionPoolInUseCount() != 0) {
49+
throw new IllegalStateException("Server session in use count is " + getServerSessionPoolInUseCount());
50+
}
4651
}
4752

4853
public MongoClient getClient() {

driver/src/test/functional/com/mongodb/Fixture.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ public static synchronized MongoClient getMongoClient() {
4444
return mongoClient;
4545
}
4646

47+
public static int getServerSessionPoolInUseCount() {
48+
return getMongoClient().getServerSessionPool().getInUseCount();
49+
}
50+
4751
@SuppressWarnings("deprecation") // This is for access to the old API, so it will use deprecated methods
4852
public static synchronized DB getDefaultDatabase() {
4953
if (defaultDatabase == null) {

driver/src/test/functional/com/mongodb/FunctionalSpecification.groovy

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import spock.lang.Specification
2020

2121
import static com.mongodb.Fixture.getDefaultDatabaseName
2222
import static com.mongodb.Fixture.getMongoClient
23+
import static com.mongodb.Fixture.getServerSessionPoolInUseCount
2324

2425
class FunctionalSpecification extends Specification {
2526
protected DB database;
@@ -35,6 +36,9 @@ class FunctionalSpecification extends Specification {
3536
if (collection != null) {
3637
collection.drop()
3738
}
39+
if (getServerSessionPoolInUseCount() != 0) {
40+
throw new IllegalStateException('Server session in use count is ' + getServerSessionPoolInUseCount());
41+
}
3842
}
3943

4044
String getDatabaseName() {

driver/src/test/functional/com/mongodb/MongoClientSessionSpecification.groovy

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ class MongoClientSessionSpecification extends FunctionalSpecification {
7070
clientSession.getClusterTime() == null
7171
clientSession.getOperationTime() == null
7272
clientSession.getServerSession() != null
73+
74+
cleanup:
75+
clientSession?.close()
7376
}
7477

7578
@IgnoreIf({ !serverVersionAtLeast(3, 6) })
@@ -111,6 +114,9 @@ class MongoClientSessionSpecification extends FunctionalSpecification {
111114

112115
then:
113116
clientSession.getClusterTime() == secondClusterTime
117+
118+
cleanup:
119+
clientSession?.close()
114120
}
115121

116122
@IgnoreIf({ !serverVersionAtLeast(3, 6) })
@@ -149,6 +155,9 @@ class MongoClientSessionSpecification extends FunctionalSpecification {
149155

150156
then:
151157
clientSession.getOperationTime() == secondOperationTime
158+
159+
cleanup:
160+
clientSession?.close()
152161
}
153162

154163
@IgnoreIf({ !serverVersionAtLeast(3, 6) })
@@ -175,6 +184,9 @@ class MongoClientSessionSpecification extends FunctionalSpecification {
175184

176185
then:
177186
thrown(IllegalStateException)
187+
188+
cleanup:
189+
clientSession?.close()
178190
}
179191

180192
@IgnoreIf({ !serverVersionAtLeast(3, 6) })
@@ -192,6 +204,9 @@ class MongoClientSessionSpecification extends FunctionalSpecification {
192204

193205
then:
194206
true
207+
208+
cleanup:
209+
clientSession?.close()
195210
}
196211

197212
@IgnoreIf({ !serverVersionAtLeast(3, 6) })
@@ -205,6 +220,9 @@ class MongoClientSessionSpecification extends FunctionalSpecification {
205220
clientSession != null
206221
clientSession.isCausallyConsistent() == causallyConsistent
207222

223+
cleanup:
224+
clientSession?.close()
225+
208226
where:
209227
causallyConsistent << [true, false]
210228
}
@@ -223,6 +241,9 @@ class MongoClientSessionSpecification extends FunctionalSpecification {
223241
identifier.get('id').isBinary()
224242
identifier.getBinary('id').getType() == BsonBinarySubType.UUID_STANDARD.value
225243
identifier.getBinary('id').data.length == 16
244+
245+
cleanup:
246+
clientSession?.close()
226247
}
227248

228249
@IgnoreIf({ !serverVersionAtLeast(3, 6) })

driver/src/test/functional/com/mongodb/client/DatabaseTestCase.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import static com.mongodb.Fixture.getDefaultDatabaseName;
3131
import static com.mongodb.Fixture.getMongoClient;
3232
import static com.mongodb.Fixture.getPrimary;
33+
import static com.mongodb.Fixture.getServerSessionPoolInUseCount;
3334

3435
public class DatabaseTestCase {
3536
//For ease of use and readability, in this specific case we'll allow protected variables
@@ -57,6 +58,10 @@ public void tearDown() {
5758
} catch (InterruptedException e) {
5859
// ignore
5960
}
61+
62+
if (getServerSessionPoolInUseCount() != 0) {
63+
throw new IllegalStateException("Server session in use count is " + getServerSessionPoolInUseCount());
64+
}
6065
}
6166

6267
protected String getDatabaseName() {

0 commit comments

Comments
 (0)