Skip to content

Commit a69ed65

Browse files
committed
Add unit tests.
1 parent 0ba949c commit a69ed65

6 files changed

Lines changed: 48 additions & 18 deletions

File tree

driver-core/src/main/com/mongodb/ConnectionString.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1482,7 +1482,7 @@ public Boolean getRetryWritesValue() {
14821482
/**
14831483
* <p>Gets whether reads should be retried if they fail due to a network error</p>
14841484
*
1485-
* @return the retryWrites value
1485+
* @return the retryReads value
14861486
* @since 3.11
14871487
* @mongodb.server.release 3.6
14881488
*/

driver-core/src/test/unit/com/mongodb/ConnectionStringUnitTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,15 @@ void serverMonitoringMode() {
9494
);
9595
}
9696

97+
@Test
98+
void enableOverloadRetargeting() {
99+
assertAll(
100+
() -> assertNull(new ConnectionString("mongodb://localhost/").getEnableOverloadRetargeting()),
101+
() -> assertEquals(false, new ConnectionString(DEFAULT_OPTIONS + "enableOverloadRetargeting=false").getEnableOverloadRetargeting()),
102+
() -> assertEquals(true, new ConnectionString(DEFAULT_OPTIONS + "enableOverloadRetargeting=true").getEnableOverloadRetargeting()),
103+
() -> assertNull(new ConnectionString(DEFAULT_OPTIONS + "enableOverloadRetargeting=foos").getEnableOverloadRetargeting())
104+
);
105+
}
97106

98107
@ParameterizedTest
99108
@ValueSource(strings = {"mongodb://foo:bar/@hostname/java?", "mongodb://foo:bar?@hostname/java/",

driver-core/src/test/unit/com/mongodb/MongoClientSettingsSpecification.groovy

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,7 @@ class MongoClientSettingsSpecification extends Specification {
552552
def actual = MongoClientSettings.Builder.declaredFields.grep { !it.synthetic } *.name.sort()
553553
def expected = ['applicationName', 'autoEncryptionSettings', 'clusterSettingsBuilder', 'codecRegistry', 'commandListeners',
554554
'compressorList', 'connectionPoolSettingsBuilder', 'contextProvider', 'credential', 'dnsClient',
555+
'enableOverloadRetargeting',
555556
'heartbeatConnectTimeoutMS', 'heartbeatSocketTimeoutMS', 'inetAddressResolver', 'loggerSettingsBuilder',
556557
'observabilitySettings',
557558
'readConcern', 'readPreference', 'retryReads',
@@ -571,6 +572,7 @@ class MongoClientSettingsSpecification extends Specification {
571572
'applyToConnectionPoolSettings', 'applyToLoggerSettings', 'applyToServerSettings', 'applyToSocketSettings',
572573
'applyToSslSettings', 'autoEncryptionSettings', 'build', 'codecRegistry', 'commandListenerList',
573574
'compressorList', 'contextProvider', 'credential', 'dnsClient',
575+
'enableOverloadRetargeting',
574576
'heartbeatConnectTimeoutMS',
575577
'heartbeatSocketTimeoutMS', 'inetAddressResolver', 'observabilitySettings', 'readConcern',
576578
'readPreference',

driver-core/src/test/unit/com/mongodb/internal/connection/ServerDeprioritizationTest.java

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@
4141
import java.util.stream.Collectors;
4242
import java.util.stream.Stream;
4343

44-
import static com.mongodb.ClusterFixture.TIMEOUT_SETTINGS;
45-
import static com.mongodb.ClusterFixture.createOperationContext;
44+
import static java.lang.String.format;
4645
import static java.util.Arrays.asList;
4746
import static java.util.Collections.emptyList;
4847
import static java.util.Collections.singletonList;
@@ -62,11 +61,13 @@ final class ServerDeprioritizationTest {
6261
private static final ClusterDescription SHARDED_CLUSTER = multipleModeClusterDescription(ClusterType.SHARDED);
6362
private static final ClusterDescription UNKNOWN_CLUSTER = multipleModeClusterDescription(ClusterType.UNKNOWN);
6463
private static final List<ClusterDescription> CLUSTERS = asList(SHARDED_CLUSTER, REPLICA_SET_CLUSTER, UNKNOWN_CLUSTER);
64+
private static final RuntimeException RUNTIME_EXCEPTION = new RuntimeException();
65+
private static final MongoException MONGO_EXCEPTION_NO_LABEL = new MongoException(0, "test");
6566
private ServerDeprioritization serverDeprioritization;
6667

6768
@BeforeEach
6869
void beforeEach() {
69-
serverDeprioritization = createOperationContext(TIMEOUT_SETTINGS).getServerDeprioritization();
70+
serverDeprioritization = new OperationContext.ServerDeprioritization(true);
7071
}
7172

7273
private static Stream<Arguments> selectNoneDeprioritized() {
@@ -105,8 +106,8 @@ void selectNoneDeprioritizedSingleServerCluster(final ClusterType clusterType) {
105106

106107
private static Stream<Arguments> deprioritizableClusters() {
107108
return Stream.of(
108-
of(SHARDED_CLUSTER, new RuntimeException()),
109-
of(SHARDED_CLUSTER, new MongoException(0, "test")),
109+
of(SHARDED_CLUSTER, RUNTIME_EXCEPTION),
110+
of(SHARDED_CLUSTER, MONGO_EXCEPTION_NO_LABEL),
110111
of(REPLICA_SET_CLUSTER, createSystemOverloadedError()),
111112
of(UNKNOWN_CLUSTER, createSystemOverloadedError())
112113
);
@@ -204,7 +205,7 @@ void onAttemptFailureIgnoresIfPoolClearedException() {
204205

205206
@Test
206207
void onAttemptFailureDoesNotThrowIfNoCandidate() {
207-
assertDoesNotThrow(() -> serverDeprioritization.onAttemptFailure(new RuntimeException()));
208+
assertDoesNotThrow(() -> serverDeprioritization.onAttemptFailure(RUNTIME_EXCEPTION));
208209
}
209210

210211
@ParameterizedTest
@@ -214,20 +215,30 @@ void onAttemptFailureIgnoresIfNonShardedWithoutOverloadError(final ClusterType c
214215
ServerSelector selector = createAssertingSelector(ALL_SERVERS, singletonList(SERVER_A));
215216

216217
assertAll(() -> {
217-
serverDeprioritization.updateCandidate(SERVER_B.getAddress(), clusterType);
218-
serverDeprioritization.onAttemptFailure(new RuntimeException());
218+
deprioritize(clusterType, RUNTIME_EXCEPTION, SERVER_B);
219219
assertEquals(singletonList(SERVER_A), serverDeprioritization.apply(selector).select(cluster),
220-
"Expected no deprioritization for " + clusterType + " with RuntimeException");
221-
}, () -> {
222-
serverDeprioritization = createOperationContext(TIMEOUT_SETTINGS).getServerDeprioritization();
223-
serverDeprioritization.updateCandidate(SERVER_B.getAddress(), clusterType);
224-
serverDeprioritization.onAttemptFailure(new MongoException(1, "error"));
220+
format("Expected no deprioritization for %s with RuntimeException", clusterType));
221+
},
222+
() -> {
223+
deprioritize(clusterType, MONGO_EXCEPTION_NO_LABEL, SERVER_B);
225224
assertEquals(singletonList(SERVER_A), serverDeprioritization.apply(selector).select(cluster),
226-
"Expected no deprioritization for " + clusterType + " with no SystemOverloadedError MongoException");
225+
format("Expected no deprioritization for %s with MongoException without SystemOverloadedError", clusterType));
227226
}
228227
);
229228
}
230229

230+
@ParameterizedTest
231+
@EnumSource(value = ClusterType.class, names = "SHARDED", mode = EnumSource.Mode.EXCLUDE)
232+
void onAttemptFailureIgnoresIfNonShardedWithOverloadErrorAndDisabledOverloadRetargeting(final ClusterType clusterType) {
233+
ClusterDescription cluster = multipleModeClusterDescription(clusterType);
234+
ServerSelector selector = createAssertingSelector(ALL_SERVERS, singletonList(SERVER_A));
235+
236+
ServerDeprioritization serverDeprioritization = new OperationContext.ServerDeprioritization(false);
237+
deprioritize(clusterType, createSystemOverloadedError(), SERVER_B);
238+
assertEquals(singletonList(SERVER_A), serverDeprioritization.apply(selector).select(cluster),
239+
format("Expected no deprioritization when overloadRetargeting is disabled for %s with SystemOverloadedError", clusterType));
240+
}
241+
231242
private void deprioritize(final ClusterType clusterType, final Throwable exception, final ServerDescription... serverDescriptions) {
232243
for (ServerDescription serverDescription : serverDescriptions) {
233244
serverDeprioritization.updateCandidate(serverDescription.getAddress(), clusterType);

driver-core/src/test/unit/com/mongodb/internal/connection/ServerSelectionSelectionTest.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@
3535
import com.mongodb.connection.ServerSettings;
3636
import com.mongodb.connection.ServerType;
3737
import com.mongodb.event.ServerDescriptionChangedEvent;
38+
import com.mongodb.internal.IgnorableRequestContext;
3839
import com.mongodb.internal.TimeoutContext;
3940
import com.mongodb.internal.mockito.MongoMockito;
41+
import com.mongodb.internal.observability.micrometer.TracingManager;
4042
import com.mongodb.internal.selector.ReadPreferenceServerSelector;
4143
import com.mongodb.internal.selector.WritableServerSelector;
4244
import com.mongodb.internal.time.Timeout;
@@ -297,8 +299,14 @@ private static List<ServerAddress> extractDeprioritizedServerAddresses(final Bso
297299

298300
private OperationContext createOperationContext() {
299301
OperationContext operationContext =
300-
OperationContext.simpleOperationContext(
301-
new TimeoutContext(TIMEOUT_SETTINGS.withServerSelectionTimeoutMS(0)));
302+
new OperationContext(
303+
IgnorableRequestContext.INSTANCE,
304+
NoOpSessionContext.INSTANCE,
305+
new TimeoutContext(TIMEOUT_SETTINGS.withServerSelectionTimeoutMS(0)),
306+
TracingManager.NO_OP,
307+
null,
308+
null,
309+
new OperationContext.ServerDeprioritization(true));
302310
OperationContext.ServerDeprioritization serverDeprioritization = operationContext.getServerDeprioritization();
303311
for (ServerAddress address : extractDeprioritizedServerAddresses(definition)) {
304312
serverDeprioritization.updateCandidate(address, clusterDescription.getType());

driver-sync/src/test/unit/com/mongodb/client/internal/MongoClusterSpecification.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ class MongoClusterSpecification extends Specification {
259259
MongoClusterImpl createMongoCluster(final MongoClientSettings settings, final OperationExecutor operationExecutor) {
260260
new MongoClusterImpl(null, cluster, settings.codecRegistry, null, null,
261261
originator, operationExecutor, settings.readConcern, settings.readPreference, settings.retryReads, settings.retryWrites,
262-
null, serverSessionPool, TimeoutSettings.create(settings), settings.uuidRepresentation,
262+
settings.enableOverloadRetargeting, null, serverSessionPool, TimeoutSettings.create(settings), settings.uuidRepresentation,
263263
settings.writeConcern, TracingManager.NO_OP)
264264
}
265265
}

0 commit comments

Comments
 (0)