Skip to content

Commit 357ffae

Browse files
committed
JAVA-2691: Ensure that all driver-core integration tests run with sessions enabled on MongoDB 3.6+
1 parent fc875e0 commit 357ffae

File tree

5 files changed

+48
-29
lines changed

5 files changed

+48
-29
lines changed

driver-core/src/test/functional/com/mongodb/ClusterFixture.java

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@
2323
import com.mongodb.binding.AsyncConnectionSource;
2424
import com.mongodb.binding.AsyncReadBinding;
2525
import com.mongodb.binding.AsyncReadWriteBinding;
26+
import com.mongodb.binding.AsyncSessionBinding;
2627
import com.mongodb.binding.AsyncSingleConnectionBinding;
2728
import com.mongodb.binding.AsyncWriteBinding;
2829
import com.mongodb.binding.ClusterBinding;
2930
import com.mongodb.binding.ReadWriteBinding;
31+
import com.mongodb.binding.SessionBinding;
3032
import com.mongodb.binding.SingleConnectionBinding;
3133
import com.mongodb.connection.AsyncConnection;
3234
import com.mongodb.connection.AsynchronousSocketChannelStreamFactory;
@@ -62,6 +64,7 @@
6264

6365
import java.util.ArrayList;
6466
import java.util.Collections;
67+
import java.util.HashMap;
6568
import java.util.List;
6669
import java.util.Map;
6770

@@ -90,6 +93,8 @@ public final class ClusterFixture {
9093
private static ConnectionString connectionString;
9194
private static Cluster cluster;
9295
private static Cluster asyncCluster;
96+
private static Map<ReadPreference, ReadWriteBinding> bindingMap = new HashMap<ReadPreference, ReadWriteBinding>();
97+
private static Map<ReadPreference, AsyncReadWriteBinding> asyncBindingMap = new HashMap<ReadPreference, AsyncReadWriteBinding>();
9398

9499
static {
95100
String mongoURIProperty = System.getProperty(MONGODB_URI_SYSTEM_PROPERTY_NAME);
@@ -201,20 +206,27 @@ public static synchronized ConnectionString getConnectionString() {
201206
return connectionString;
202207
}
203208

209+
public static ReadWriteBinding getBinding(final Cluster cluster) {
210+
return new ClusterBinding(cluster, ReadPreference.primary());
211+
}
212+
204213
public static ReadWriteBinding getBinding() {
205-
return getBinding(getCluster());
214+
return getBinding(getCluster(), ReadPreference.primary());
206215
}
207216

208217
public static ReadWriteBinding getBinding(final ReadPreference readPreference) {
209218
return getBinding(getCluster(), readPreference);
210219
}
211220

212-
public static ReadWriteBinding getBinding(final Cluster cluster) {
213-
return getBinding(cluster, ReadPreference.primary());
214-
}
215-
216221
private static ReadWriteBinding getBinding(final Cluster cluster, final ReadPreference readPreference) {
217-
return new ClusterBinding(cluster, readPreference);
222+
if (!bindingMap.containsKey(readPreference)) {
223+
ReadWriteBinding binding = new ClusterBinding(cluster, readPreference);
224+
if (serverVersionAtLeast(3, 6)) {
225+
binding = new SessionBinding(binding);
226+
}
227+
bindingMap.put(readPreference, binding);
228+
}
229+
return bindingMap.get(readPreference);
218230
}
219231

220232
public static SingleConnectionBinding getSingleConnectionBinding() {
@@ -229,20 +241,27 @@ public static AsyncSingleConnectionBinding getAsyncSingleConnectionBinding(final
229241
return new AsyncSingleConnectionBinding(cluster, 20, SECONDS);
230242
}
231243

244+
public static AsyncReadWriteBinding getAsyncBinding(final Cluster cluster) {
245+
return new AsyncClusterBinding(cluster, ReadPreference.primary());
246+
}
247+
232248
public static AsyncReadWriteBinding getAsyncBinding() {
233-
return getAsyncBinding(getAsyncCluster());
249+
return getAsyncBinding(getAsyncCluster(), ReadPreference.primary());
234250
}
235251

236252
public static AsyncReadWriteBinding getAsyncBinding(final ReadPreference readPreference) {
237253
return getAsyncBinding(getAsyncCluster(), readPreference);
238254
}
239255

240-
public static AsyncReadWriteBinding getAsyncBinding(final Cluster cluster) {
241-
return getAsyncBinding(cluster, ReadPreference.primary());
242-
}
243-
244256
public static AsyncReadWriteBinding getAsyncBinding(final Cluster cluster, final ReadPreference readPreference) {
245-
return new AsyncClusterBinding(cluster, readPreference);
257+
if (!asyncBindingMap.containsKey(readPreference)) {
258+
AsyncReadWriteBinding binding = new AsyncClusterBinding(cluster, readPreference);
259+
if (serverVersionAtLeast(3, 6)) {
260+
binding = new AsyncSessionBinding(binding);
261+
}
262+
asyncBindingMap.put(readPreference, binding);
263+
}
264+
return asyncBindingMap.get(readPreference);
246265
}
247266

248267
public static synchronized Cluster getCluster() {

driver-core/src/test/functional/com/mongodb/binding/AsyncSessionBinding.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public final class AsyncSessionBinding implements AsyncReadWriteBinding {
2929
private final AsyncReadWriteBinding wrapped;
3030
private final SessionContext sessionContext;
3131

32-
AsyncSessionBinding(final AsyncReadWriteBinding wrapped) {
32+
public AsyncSessionBinding(final AsyncReadWriteBinding wrapped) {
3333
this.wrapped = notNull("wrapped", wrapped);
3434
this.sessionContext = new SimpleSessionContext();
3535
}

driver-core/src/test/functional/com/mongodb/binding/SessionBinding.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@
2323

2424
import static org.bson.assertions.Assertions.notNull;
2525

26-
class SessionBinding implements ReadWriteBinding {
26+
public class SessionBinding implements ReadWriteBinding {
2727
private final ReadWriteBinding wrapped;
2828
private final SessionContext sessionContext;
2929

30-
SessionBinding(final ReadWriteBinding wrapped) {
30+
public SessionBinding(final ReadWriteBinding wrapped) {
3131
this.wrapped = notNull("wrapped", wrapped);
3232
this.sessionContext = new SimpleSessionContext();
3333
}

driver-core/src/test/functional/com/mongodb/operation/AsyncQueryBatchCursorFunctionalSpecification.groovy

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -365,10 +365,9 @@ class AsyncQueryBatchCursorFunctionalSpecification extends OperationFunctionalSp
365365
}
366366

367367
def futureResultCallback = new FutureResultCallback<BsonDocument>();
368-
connection.commandAsync(getDatabaseName(), findCommand,
369-
NO_OP_FIELD_NAME_VALIDATOR, ReadPreference.secondaryPreferred(),
370-
CommandResultDocumentCodec.create(new DocumentCodec(), 'firstBatch'), binding.sessionContext,
371-
futureResultCallback)
368+
connection.commandAsync(getDatabaseName(), findCommand, NO_OP_FIELD_NAME_VALIDATOR, ReadPreference.primary(),
369+
CommandResultDocumentCodec.create(new DocumentCodec(), 'firstBatch'),
370+
connectionSource.sessionContext, futureResultCallback)
372371
def response = futureResultCallback.get(60, SECONDS)
373372
cursorDocumentToQueryResult(response.getDocument('cursor'), connection.getDescription().getServerAddress())
374373
} else {

driver-core/src/test/functional/com/mongodb/operation/QueryBatchCursorFunctionalSpecification.groovy

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -512,11 +512,11 @@ class QueryBatchCursorFunctionalSpecification extends OperationFunctionalSpecifi
512512
given:
513513
connectionSource = getBinding(ReadPreference.secondary()).getReadConnectionSource()
514514

515-
def firstBatch = executeQuery(2, true)
515+
def firstBatch = executeQuery(2, ReadPreference.secondary())
516516

517517
// wait for replication
518518
while (firstBatch.cursor == null ) {
519-
firstBatch = executeQuery(2, true)
519+
firstBatch = executeQuery(2, ReadPreference.secondary())
520520
}
521521

522522
when:
@@ -533,24 +533,24 @@ class QueryBatchCursorFunctionalSpecification extends OperationFunctionalSpecifi
533533
}
534534

535535
private QueryResult<Document> executeQuery(int batchSize) {
536-
executeQuery(new BsonDocument(), 0, batchSize, false, false, false)
536+
executeQuery(new BsonDocument(), 0, batchSize, false, false, ReadPreference.primary())
537537
}
538538

539-
private QueryResult<Document> executeQuery(int batchSize, boolean slaveOk) {
540-
executeQuery(new BsonDocument(), 0, batchSize, false, false, slaveOk)
539+
private QueryResult<Document> executeQuery(int batchSize, ReadPreference readPreference) {
540+
executeQuery(new BsonDocument(), 0, batchSize, false, false, readPreference)
541541
}
542542

543543
private QueryResult<Document> executeQuery(int limit, int batchSize) {
544-
executeQuery(new BsonDocument(), limit, batchSize, false, false, false)
544+
executeQuery(new BsonDocument(), limit, batchSize, false, false, ReadPreference.primary())
545545
}
546546

547547

548548
private QueryResult<Document> executeQuery(BsonDocument filter, int limit, int batchSize, boolean tailable, boolean awaitData) {
549-
executeQuery(filter, limit, batchSize, tailable, awaitData, false)
549+
executeQuery(filter, limit, batchSize, tailable, awaitData, ReadPreference.primary())
550550
}
551551

552552
private QueryResult<Document> executeQuery(BsonDocument filter, int limit, int batchSize, boolean tailable, boolean awaitData,
553-
boolean slaveOk) {
553+
ReadPreference readPreference) {
554554
def connection = connectionSource.getConnection()
555555
try {
556556
if (serverIsAtLeastVersionThreeDotTwo(connection.getDescription())) {
@@ -570,8 +570,9 @@ class QueryBatchCursorFunctionalSpecification extends OperationFunctionalSpecifi
570570
}
571571

572572
def response = connection.command(getDatabaseName(), findCommand,
573-
slaveOk, NO_OP_FIELD_NAME_VALIDATOR,
574-
CommandResultDocumentCodec.create(new DocumentCodec(), 'firstBatch'))
573+
NO_OP_FIELD_NAME_VALIDATOR, readPreference,
574+
CommandResultDocumentCodec.create(new DocumentCodec(), 'firstBatch'),
575+
connectionSource.sessionContext)
575576
cursorDocumentToQueryResult(response.getDocument('cursor'), connection.getDescription().getServerAddress())
576577
} else {
577578
connection.query(getNamespace(), filter, null, 0, limit, batchSize,

0 commit comments

Comments
 (0)