Skip to content

Commit 0e90dd4

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

File tree

6 files changed

+57
-0
lines changed

6 files changed

+57
-0
lines changed

driver-async/src/main/com/mongodb/async/client/MongoClientImpl.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,4 +161,7 @@ Cluster getCluster() {
161161
return cluster;
162162
}
163163

164+
ServerSessionPool getServerSessionPool() {
165+
return serverSessionPool;
166+
}
164167
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import static com.mongodb.async.client.Fixture.drop;
2828
import static com.mongodb.async.client.Fixture.getDefaultDatabaseName;
2929
import static com.mongodb.async.client.Fixture.getMongoClient;
30+
import static com.mongodb.async.client.Fixture.waitForLastServerSessionPoolRelease;
3031

3132
public class DatabaseTestCase {
3233
//For ease of use and readability, in this specific case we'll allow protected variables
@@ -49,6 +50,8 @@ public void tearDown() {
4950
if (collection != null) {
5051
drop(collection.getNamespace());
5152
}
53+
54+
waitForLastServerSessionPoolRelease();
5255
}
5356

5457
public abstract class MongoOperation<TResult> {

driver-async/src/test/functional/com/mongodb/async/client/Fixture.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@
1616

1717
package com.mongodb.async.client;
1818

19+
import com.mongodb.ClusterFixture;
1920
import com.mongodb.ConnectionString;
2021
import com.mongodb.MongoCommandException;
22+
import com.mongodb.MongoInterruptedException;
2123
import com.mongodb.MongoNamespace;
24+
import com.mongodb.MongoTimeoutException;
2225
import com.mongodb.async.FutureResultCallback;
2326
import com.mongodb.connection.ClusterSettings;
2427
import com.mongodb.connection.ConnectionPoolSettings;
@@ -28,6 +31,7 @@
2831
import org.bson.Document;
2932

3033
import static com.mongodb.connection.ClusterType.SHARDED;
34+
import static java.lang.Thread.sleep;
3135
import static java.util.concurrent.TimeUnit.SECONDS;
3236

3337
/**
@@ -152,6 +156,29 @@ public static void drop(final MongoNamespace namespace) {
152156
}
153157
}
154158

159+
public static synchronized void waitForLastServerSessionPoolRelease() {
160+
if (mongoClient != null) {
161+
long startTime = System.currentTimeMillis();
162+
int sessionInUseCount = getSessionInUseCount();
163+
while (sessionInUseCount > 0) {
164+
try {
165+
if (System.currentTimeMillis() > startTime + ClusterFixture.TIMEOUT * 1000) {
166+
throw new MongoTimeoutException("Timed out waiting for server session pool in use count to drop to 0. Now at: "
167+
+ sessionInUseCount);
168+
}
169+
sleep(10);
170+
sessionInUseCount = getSessionInUseCount();
171+
} catch (InterruptedException e) {
172+
throw new MongoInterruptedException("Interrupted", e);
173+
}
174+
}
175+
}
176+
}
177+
178+
private static int getSessionInUseCount() {
179+
return mongoClient.getServerSessionPool().getInUseCount();
180+
}
181+
155182
static class ShutdownHook extends Thread {
156183
@Override
157184
public void run() {

driver-async/src/test/functional/com/mongodb/async/client/FunctionalSpecification.groovy

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import spock.lang.Specification
2222

2323
import static Fixture.getDefaultDatabase
2424
import static Fixture.initializeCollection
25+
import static com.mongodb.async.client.Fixture.waitForLastServerSessionPoolRelease
2526

2627
class FunctionalSpecification extends Specification {
2728
protected MongoDatabase database;
@@ -32,6 +33,10 @@ class FunctionalSpecification extends Specification {
3233
collection = initializeCollection(new MongoNamespace(database.getName(), getClass().getName()))
3334
}
3435

36+
def cleanup() {
37+
waitForLastServerSessionPoolRelease();
38+
}
39+
3540
String getDatabaseName() {
3641
database.getName();
3742
}

driver-async/src/test/functional/com/mongodb/async/client/MongoClientSessionSpecification.groovy

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ class MongoClientSessionSpecification extends FunctionalSpecification {
7474
clientSession.getClusterTime() == null
7575
clientSession.getOperationTime() == null
7676
clientSession.getServerSession() != null
77+
78+
cleanup:
79+
clientSession.close()
7780
}
7881

7982
@IgnoreIf({ !serverVersionAtLeast(3, 6) })
@@ -115,6 +118,9 @@ class MongoClientSessionSpecification extends FunctionalSpecification {
115118

116119
then:
117120
clientSession.getClusterTime() == secondClusterTime
121+
122+
cleanup:
123+
clientSession.close()
118124
}
119125

120126
@IgnoreIf({ !serverVersionAtLeast(3, 6) })
@@ -153,6 +159,9 @@ class MongoClientSessionSpecification extends FunctionalSpecification {
153159

154160
then:
155161
clientSession.getOperationTime() == secondOperationTime
162+
163+
cleanup:
164+
clientSession.close()
156165
}
157166

158167
@IgnoreIf({ !serverVersionAtLeast(3, 6) })
@@ -179,6 +188,9 @@ class MongoClientSessionSpecification extends FunctionalSpecification {
179188

180189
then:
181190
thrown(IllegalStateException)
191+
192+
cleanup:
193+
clientSession.close()
182194
}
183195

184196
@IgnoreIf({ !serverVersionAtLeast(3, 6) })
@@ -207,6 +219,9 @@ class MongoClientSessionSpecification extends FunctionalSpecification {
207219
clientSession != null
208220
clientSession.isCausallyConsistent() == causallyConsistent
209221

222+
cleanup:
223+
clientSession.close()
224+
210225
where:
211226
causallyConsistent << [true, false]
212227
}
@@ -225,6 +240,9 @@ class MongoClientSessionSpecification extends FunctionalSpecification {
225240
identifier.get('id').isBinary()
226241
identifier.getBinary('id').getType() == BsonBinarySubType.UUID_STANDARD.value
227242
identifier.getBinary('id').data.length == 16
243+
244+
cleanup:
245+
clientSession.close()
228246
}
229247

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

driver-async/src/test/functional/com/mongodb/async/client/SmokeTestSpecification.groovy

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ class SmokeTestSpecification extends FunctionalSpecification {
114114

115115
then:
116116
batchCursor.getBatchSize() == 0
117+
batchCursor.close()
117118

118119
when: 'The collection name should be in the collections list'
119120
def collectionNames = run(database.listCollections().&into, [])

0 commit comments

Comments
 (0)