Skip to content

Commit

Permalink
PR fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
katcharov committed Sep 26, 2024
1 parent 238590b commit d7d7a11
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,20 @@
*/
package com.mongodb.connection;

import com.mongodb.annotations.Immutable;
import com.mongodb.lang.Nullable;

import java.nio.channels.AsynchronousChannelGroup;
import java.util.concurrent.ExecutorService;

import static com.mongodb.assertions.Assertions.notNull;

/**
* {@link TransportSettings} for a non-<a href="http://netty.io/">Netty</a>-based async transport implementation.
*
* @since 5.2
*/
@Immutable
public final class AsyncTransportSettings extends TransportSettings {

private final ExecutorService executorService;
Expand All @@ -49,15 +54,16 @@ private Builder() {
}

/**
* Sets the executor service
* Sets the executor service. This executor service will not be shut
* down by the driver code, and must be shut down by application code.
*
* @param executorService the executor service
* @return this
* @see #getExecutorService()
* @see AsynchronousChannelGroup#withThreadPool(ExecutorService)
*/
public Builder executorService(final ExecutorService executorService) {
this.executorService = executorService;
this.executorService = notNull("executorService", executorService);
return this;
}

Expand All @@ -80,4 +86,11 @@ public AsyncTransportSettings build() {
public ExecutorService getExecutorService() {
return executorService;
}

@Override
public String toString() {
return "AsyncTransportSettings{" +
"executorService=" + executorService +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static StreamFactory getSyncStreamFactory(final MongoClientSettings setti
if (transportSettings == null) {
return new SocketStreamFactory(inetAddressResolver, socketSettings, settings.getSslSettings());
} else if (transportSettings instanceof AsyncTransportSettings) {
throw new MongoClientException("Unsupported async transport settings: " + transportSettings.getClass().getName());
throw new MongoClientException("Unsupported transport settings in sync: " + transportSettings.getClass().getName());
} else if (transportSettings instanceof NettyTransportSettings) {
return getNettyStreamFactoryFactory(inetAddressResolver, (NettyTransportSettings) transportSettings)
.create(socketSettings, settings.getSslSettings());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2008-present MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.mongodb.connection;

import org.junit.jupiter.api.Test;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

class AsyncTransportSettingsTest {

@Test
public void shouldDefaultAllValuesToNull() {
AsyncTransportSettings settings = TransportSettings.asyncBuilder().build();

assertNull(settings.getExecutorService());
}

@Test
public void shouldApplySettingsFromBuilder() {
ExecutorService executorService = Executors.newFixedThreadPool(1);
AsyncTransportSettings settings = TransportSettings.asyncBuilder()
.executorService(executorService)
.build();

assertEquals(executorService, settings.getExecutorService());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,44 +27,44 @@
import java.util.concurrent.Executors;

import static com.mongodb.client.Fixture.getMongoClientSettingsBuilder;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;

class AsyncTransportSettingsTest {
@Test
public void shouldDefaultAllValuesToNull() {
AsyncTransportSettings settings = TransportSettings.asyncBuilder().build();

assertNull(settings.getExecutorService());
}

@Test
public void shouldApplySettingsFromBuilder() {
ExecutorService executorService = Executors.newFixedThreadPool(1);
AsyncTransportSettings settings = TransportSettings.asyncBuilder()
void testAsyncTransportSettings() {
ExecutorService executorService = spy(Executors.newFixedThreadPool(5));
AsyncTransportSettings asyncTransportSettings = TransportSettings.asyncBuilder()
.executorService(executorService)
.build();
MongoClientSettings mongoClientSettings = getMongoClientSettingsBuilder()
.transportSettings(asyncTransportSettings)
.build();

assertEquals(executorService, settings.getExecutorService());
try (MongoClient client = new SyncMongoClient(MongoClients.create(mongoClientSettings))) {
client.listDatabases().first();
}
verify(executorService, atLeastOnce()).execute(any());
}

@Test
void testAsyncTransportSettings() {
void testExternalExecutorNotShutDown() {
ExecutorService executorService = spy(Executors.newFixedThreadPool(5));
AsyncTransportSettings asyncTransportSettings = TransportSettings.asyncBuilder()
.executorService(executorService)
.build();
MongoClientSettings mongoClientSettings = getMongoClientSettingsBuilder()
.applyToSslSettings(builder -> builder.enabled(true))
.transportSettings(asyncTransportSettings)
.build();

try (MongoClient client = new SyncMongoClient(MongoClients.create(mongoClientSettings))) {
client.listDatabases().first();
try (MongoClient ignored = new SyncMongoClient(MongoClients.create(mongoClientSettings))) {
// ignored
}
verify(executorService, atLeastOnce()).execute(any());
verify(executorService, never()).shutdown();
}
}

0 comments on commit d7d7a11

Please sign in to comment.