Skip to content

Commit d7d7a11

Browse files
committed
PR fixes
1 parent 238590b commit d7d7a11

File tree

4 files changed

+77
-19
lines changed

4 files changed

+77
-19
lines changed

Diff for: driver-core/src/main/com/mongodb/connection/AsyncTransportSettings.java

+15-2
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,20 @@
1717
*/
1818
package com.mongodb.connection;
1919

20+
import com.mongodb.annotations.Immutable;
2021
import com.mongodb.lang.Nullable;
2122

23+
import java.nio.channels.AsynchronousChannelGroup;
2224
import java.util.concurrent.ExecutorService;
2325

26+
import static com.mongodb.assertions.Assertions.notNull;
27+
2428
/**
2529
* {@link TransportSettings} for a non-<a href="http://netty.io/">Netty</a>-based async transport implementation.
2630
*
2731
* @since 5.2
2832
*/
33+
@Immutable
2934
public final class AsyncTransportSettings extends TransportSettings {
3035

3136
private final ExecutorService executorService;
@@ -49,15 +54,16 @@ private Builder() {
4954
}
5055

5156
/**
52-
* Sets the executor service
57+
* Sets the executor service. This executor service will not be shut
58+
* down by the driver code, and must be shut down by application code.
5359
*
5460
* @param executorService the executor service
5561
* @return this
5662
* @see #getExecutorService()
5763
* @see AsynchronousChannelGroup#withThreadPool(ExecutorService)
5864
*/
5965
public Builder executorService(final ExecutorService executorService) {
60-
this.executorService = executorService;
66+
this.executorService = notNull("executorService", executorService);
6167
return this;
6268
}
6369

@@ -80,4 +86,11 @@ public AsyncTransportSettings build() {
8086
public ExecutorService getExecutorService() {
8187
return executorService;
8288
}
89+
90+
@Override
91+
public String toString() {
92+
return "AsyncTransportSettings{" +
93+
"executorService=" + executorService +
94+
'}';
95+
}
8396
}

Diff for: driver-core/src/main/com/mongodb/internal/connection/StreamFactoryHelper.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public static StreamFactory getSyncStreamFactory(final MongoClientSettings setti
3838
if (transportSettings == null) {
3939
return new SocketStreamFactory(inetAddressResolver, socketSettings, settings.getSslSettings());
4040
} else if (transportSettings instanceof AsyncTransportSettings) {
41-
throw new MongoClientException("Unsupported async transport settings: " + transportSettings.getClass().getName());
41+
throw new MongoClientException("Unsupported transport settings in sync: " + transportSettings.getClass().getName());
4242
} else if (transportSettings instanceof NettyTransportSettings) {
4343
return getNettyStreamFactoryFactory(inetAddressResolver, (NettyTransportSettings) transportSettings)
4444
.create(socketSettings, settings.getSslSettings());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2008-present MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.mongodb.connection;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import java.util.concurrent.ExecutorService;
22+
import java.util.concurrent.Executors;
23+
24+
import static org.junit.jupiter.api.Assertions.assertEquals;
25+
import static org.junit.jupiter.api.Assertions.assertNull;
26+
27+
class AsyncTransportSettingsTest {
28+
29+
@Test
30+
public void shouldDefaultAllValuesToNull() {
31+
AsyncTransportSettings settings = TransportSettings.asyncBuilder().build();
32+
33+
assertNull(settings.getExecutorService());
34+
}
35+
36+
@Test
37+
public void shouldApplySettingsFromBuilder() {
38+
ExecutorService executorService = Executors.newFixedThreadPool(1);
39+
AsyncTransportSettings settings = TransportSettings.asyncBuilder()
40+
.executorService(executorService)
41+
.build();
42+
43+
assertEquals(executorService, settings.getExecutorService());
44+
}
45+
}

Diff for: driver-reactive-streams/src/test/functional/com/mongodb/reactivestreams/client/AsyncTransportSettingsTest.java

+16-16
Original file line numberDiff line numberDiff line change
@@ -27,44 +27,44 @@
2727
import java.util.concurrent.Executors;
2828

2929
import static com.mongodb.client.Fixture.getMongoClientSettingsBuilder;
30-
import static org.junit.jupiter.api.Assertions.assertEquals;
31-
import static org.junit.jupiter.api.Assertions.assertNull;
3230
import static org.mockito.ArgumentMatchers.any;
3331
import static org.mockito.Mockito.atLeastOnce;
32+
import static org.mockito.Mockito.never;
3433
import static org.mockito.Mockito.spy;
3534
import static org.mockito.Mockito.verify;
3635

3736
class AsyncTransportSettingsTest {
38-
@Test
39-
public void shouldDefaultAllValuesToNull() {
40-
AsyncTransportSettings settings = TransportSettings.asyncBuilder().build();
41-
42-
assertNull(settings.getExecutorService());
43-
}
4437

4538
@Test
46-
public void shouldApplySettingsFromBuilder() {
47-
ExecutorService executorService = Executors.newFixedThreadPool(1);
48-
AsyncTransportSettings settings = TransportSettings.asyncBuilder()
39+
void testAsyncTransportSettings() {
40+
ExecutorService executorService = spy(Executors.newFixedThreadPool(5));
41+
AsyncTransportSettings asyncTransportSettings = TransportSettings.asyncBuilder()
4942
.executorService(executorService)
5043
.build();
44+
MongoClientSettings mongoClientSettings = getMongoClientSettingsBuilder()
45+
.transportSettings(asyncTransportSettings)
46+
.build();
5147

52-
assertEquals(executorService, settings.getExecutorService());
48+
try (MongoClient client = new SyncMongoClient(MongoClients.create(mongoClientSettings))) {
49+
client.listDatabases().first();
50+
}
51+
verify(executorService, atLeastOnce()).execute(any());
5352
}
5453

5554
@Test
56-
void testAsyncTransportSettings() {
55+
void testExternalExecutorNotShutDown() {
5756
ExecutorService executorService = spy(Executors.newFixedThreadPool(5));
5857
AsyncTransportSettings asyncTransportSettings = TransportSettings.asyncBuilder()
5958
.executorService(executorService)
6059
.build();
6160
MongoClientSettings mongoClientSettings = getMongoClientSettingsBuilder()
61+
.applyToSslSettings(builder -> builder.enabled(true))
6262
.transportSettings(asyncTransportSettings)
6363
.build();
6464

65-
try (MongoClient client = new SyncMongoClient(MongoClients.create(mongoClientSettings))) {
66-
client.listDatabases().first();
65+
try (MongoClient ignored = new SyncMongoClient(MongoClients.create(mongoClientSettings))) {
66+
// ignored
6767
}
68-
verify(executorService, atLeastOnce()).execute(any());
68+
verify(executorService, never()).shutdown();
6969
}
7070
}

0 commit comments

Comments
 (0)