Skip to content

Commit d21ff56

Browse files
committed
In tests, use TlsChannel when possible, otherwise use Netty
JAVA-3038
1 parent f4b6274 commit d21ff56

File tree

4 files changed

+49
-8
lines changed

4 files changed

+49
-8
lines changed

.evergreen/run-tests.sh

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ provision_ssl () {
4949

5050
# We add extra gradle arguments for SSL
5151
export GRADLE_EXTRA_VARS="-Pssl.enabled=true -Pssl.keyStoreType=pkcs12 -Pssl.keyStore=`pwd`/client.pkc -Pssl.keyStorePassword=bithere -Pssl.trustStoreType=jks -Pssl.trustStore=`pwd`/mongo-truststore -Pssl.trustStorePassword=hithere"
52-
export ASYNC_TYPE="-Dorg.mongodb.async.type=netty"
53-
5452
# Arguments for auth + SSL
5553
if [ "$AUTH" != "noauth" ] || [ "$TOPOLOGY" == "replica_set" ]; then
5654
export MONGODB_URI="${MONGODB_URI}&ssl=true&sslInvalidHostNameAllowed=true"

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,22 @@
1919
import com.mongodb.Block;
2020
import com.mongodb.ClusterFixture;
2121
import com.mongodb.ConnectionString;
22+
import com.mongodb.MongoClientSettings;
2223
import com.mongodb.MongoCommandException;
2324
import com.mongodb.MongoInterruptedException;
2425
import com.mongodb.MongoNamespace;
2526
import com.mongodb.MongoTimeoutException;
2627
import com.mongodb.async.FutureResultCallback;
28+
import com.mongodb.connection.AsynchronousSocketChannelStreamFactoryFactory;
2729
import com.mongodb.connection.SslSettings;
30+
import com.mongodb.connection.StreamFactoryFactory;
31+
import com.mongodb.connection.TlsChannelStreamFactoryFactory;
32+
import com.mongodb.connection.netty.NettyStreamFactoryFactory;
2833
import org.bson.Document;
2934

35+
import static com.mongodb.ClusterFixture.getSslSettings;
36+
import static com.mongodb.ClusterFixture.isNotAtLeastJava7;
37+
import static com.mongodb.ClusterFixture.isNotAtLeastJava8;
3038
import static com.mongodb.connection.ClusterType.SHARDED;
3139
import static java.lang.Thread.sleep;
3240
import static java.util.concurrent.TimeUnit.SECONDS;
@@ -51,6 +59,10 @@ public static synchronized MongoClient getMongoClient() {
5159
return mongoClient;
5260
}
5361

62+
public static MongoClientSettings getMongoClientSettings() {
63+
return getMongoClientBuilderFromConnectionString().build();
64+
}
65+
5466
public static com.mongodb.MongoClientSettings.Builder getMongoClientBuilderFromConnectionString() {
5567
com.mongodb.MongoClientSettings.Builder builder = com.mongodb.MongoClientSettings.builder()
5668
.applyConnectionString(getConnectionString());
@@ -62,9 +74,26 @@ public void apply(final SslSettings.Builder builder) {
6274
}
6375
});
6476
}
77+
builder.streamFactoryFactory(getStreamFactoryFactory());
6578
return builder;
6679
}
6780

81+
public static StreamFactoryFactory getStreamFactoryFactory() {
82+
if (getSslSettings().isEnabled()) {
83+
if (isNotAtLeastJava8()) {
84+
return NettyStreamFactoryFactory.builder().build();
85+
} else {
86+
return new TlsChannelStreamFactoryFactory();
87+
}
88+
} else {
89+
if (isNotAtLeastJava7()) {
90+
return NettyStreamFactoryFactory.builder().build();
91+
} else {
92+
return AsynchronousSocketChannelStreamFactoryFactory.builder().build();
93+
}
94+
}
95+
}
96+
6897
public static synchronized ConnectionString getConnectionString() {
6998
return ClusterFixture.getConnectionString();
7099
}

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454

5555
import static com.mongodb.ClusterFixture.getSslSettings;
5656
import static com.mongodb.ClusterFixture.isDiscoverableReplicaSet;
57+
import static com.mongodb.async.client.Fixture.getStreamFactoryFactory;
5758
import static org.junit.Assert.assertEquals;
5859
import static org.junit.Assert.assertTrue;
5960
import static org.junit.Assert.fail;
@@ -170,11 +171,11 @@ public void shouldDiscover() throws InterruptedException {
170171
assumeTrue("SSL settings don't match", getSslSettings().isEnabled() == sslSettings.isEnabled());
171172

172173
com.mongodb.MongoClientSettings settings = com.mongodb.MongoClientSettings.builder()
173-
.streamFactoryFactory(NettyStreamFactoryFactory.builder().build()) // TODO: why wasn't this necessary before? Is it now?
174+
.streamFactoryFactory(getStreamFactoryFactory())
174175
.applyToClusterSettings(new Block<ClusterSettings.Builder>() {
175-
@Override
176-
public void apply(final ClusterSettings.Builder builder) {
177-
builder.applyConnectionString(connectionString)
176+
@Override
177+
public void apply(final ClusterSettings.Builder builder) {
178+
builder.applyConnectionString(connectionString)
178179
.addClusterListener(new ClusterListener() {
179180
@Override
180181
public void clusterOpening(final ClusterOpeningEvent event) {

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import com.mongodb.connection.SocketStreamFactory;
4747
import com.mongodb.connection.SslSettings;
4848
import com.mongodb.connection.StreamFactory;
49+
import com.mongodb.connection.TlsChannelStreamFactoryFactory;
4950
import com.mongodb.connection.netty.NettyStreamFactory;
5051
import com.mongodb.operation.AsyncReadOperation;
5152
import com.mongodb.operation.AsyncWriteOperation;
@@ -330,10 +331,22 @@ public static StreamFactory getStreamFactory() {
330331
public static StreamFactory getAsyncStreamFactory() {
331332
String streamType = System.getProperty("org.mongodb.async.type", "nio2");
332333

333-
if (streamType.equals("netty") || getSslSettings().isEnabled()) {
334+
if (streamType.equals("netty")) {
334335
return new NettyStreamFactory(getSocketSettings(), getSslSettings());
335336
} else if (streamType.equals("nio2")) {
336-
return new AsynchronousSocketChannelStreamFactory(getSocketSettings(), getSslSettings());
337+
if (getSslSettings().isEnabled()) {
338+
if (isNotAtLeastJava8()) {
339+
return new NettyStreamFactory(getSocketSettings(), getSslSettings());
340+
} else {
341+
return new TlsChannelStreamFactoryFactory().create(getSocketSettings(), getSslSettings());
342+
}
343+
} else {
344+
if (isNotAtLeastJava7()) {
345+
return new NettyStreamFactory(getSocketSettings(), getSslSettings());
346+
} else {
347+
return new AsynchronousSocketChannelStreamFactory(getSocketSettings(), getSslSettings());
348+
}
349+
}
337350
} else {
338351
throw new IllegalArgumentException("Unsupported stream type " + streamType);
339352
}

0 commit comments

Comments
 (0)