4141import java .util .stream .Collectors ;
4242import 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 ;
4645import static java .util .Arrays .asList ;
4746import static java .util .Collections .emptyList ;
4847import 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 );
0 commit comments