1616
1717package com .mongodb .internal .connection ;
1818
19+ import com .mongodb .ClusterFixture ;
1920import com .mongodb .ConnectionString ;
2021import com .mongodb .MongoClientSettings ;
2122import com .mongodb .MongoCommandException ;
4142import org .bson .Document ;
4243import org .junit .jupiter .api .AfterEach ;
4344import org .junit .jupiter .api .BeforeEach ;
45+ import org .junit .jupiter .api .DisplayName ;
4446import org .junit .jupiter .api .Test ;
4547import org .junit .jupiter .params .ParameterizedTest ;
4648import org .junit .jupiter .params .provider .Arguments ;
4749import org .junit .jupiter .params .provider .MethodSource ;
48- import org .junit .jupiter .params .provider .ValueSource ;
4950
5051import java .io .IOException ;
5152import java .lang .reflect .Field ;
7980import static com .mongodb .MongoCredential .TOKEN_RESOURCE_KEY ;
8081import static com .mongodb .assertions .Assertions .assertNotNull ;
8182import static com .mongodb .testing .MongoAssertions .assertCause ;
82- import static java .lang .Math .min ;
8383import static java .lang .String .format ;
8484import static java .lang .System .getenv ;
8585import static java .util .Arrays .asList ;
@@ -215,9 +215,9 @@ public void test2p1ValidCallbackInputs() {
215215 + " expectedTimeoutThreshold={3}" )
216216 @ MethodSource
217217 void testValidCallbackInputsTimeoutWhenTimeoutMsIsSet (final String testName ,
218- final int timeoutMs ,
219- final int serverSelectionTimeoutMS ,
220- final int expectedTimeoutThreshold ) {
218+ final long timeoutMs ,
219+ final long serverSelectionTimeoutMS ,
220+ final long expectedTimeoutThreshold ) {
221221 TestCallback callback1 = createCallback ();
222222
223223 OidcCallback callback2 = (context ) -> {
@@ -242,40 +242,50 @@ void testValidCallbackInputsTimeoutWhenTimeoutMsIsSet(final String testName,
242242 assertEquals (1 , callback1 .getInvocations ());
243243 long elapsed = msElapsedSince (start );
244244
245- assertFalse (elapsed > (timeoutMs == 0 ? serverSelectionTimeoutMS : min (serverSelectionTimeoutMS , timeoutMs )),
245+
246+ assertFalse (elapsed > minTimeout (timeoutMs , serverSelectionTimeoutMS ),
246247 format ("Elapsed time %d is greater then minimum of serverSelectionTimeoutMS and timeoutMs, which is %d. "
247248 + "This indicates that the callback was not called with the expected timeout." ,
248- min (serverSelectionTimeoutMS , timeoutMs ),
249- elapsed ));
249+ elapsed ,
250+ minTimeout (timeoutMs , serverSelectionTimeoutMS )));
251+
250252 }
251253 }
252254
253255 private static Stream <Arguments > testValidCallbackInputsTimeoutWhenTimeoutMsIsSet () {
256+ long rtt = ClusterFixture .getPrimaryRTT ();
254257 return Stream .of (
255258 Arguments .of ("serverSelectionTimeoutMS honored for oidc callback if it's lower than timeoutMS" ,
256- 1000 , // timeoutMS
257- 500 , // serverSelectionTimeoutMS
258- 499 ), // expectedTimeoutThreshold
259+ 1000 + rtt , // timeoutMS
260+ 500 + rtt , // serverSelectionTimeoutMS
261+ 499 + rtt ), // expectedTimeoutThreshold
259262 Arguments .of ("timeoutMS honored for oidc callback if it's lower than serverSelectionTimeoutMS" ,
260- 500 , // timeoutMS
261- 1000 , // serverSelectionTimeoutMS
262- 499 ), // expectedTimeoutThreshold
263+ 500 + rtt , // timeoutMS
264+ 1000 + rtt , // serverSelectionTimeoutMS
265+ 499 + rtt ), // expectedTimeoutThreshold
266+ Arguments .of ("timeoutMS honored for oidc callback if serverSelectionTimeoutMS is infinite" ,
267+ 500 + rtt , // timeoutMS
268+ -1 , // serverSelectionTimeoutMS
269+ 499 + rtt ), // expectedTimeoutThreshold,
263270 Arguments .of ("serverSelectionTimeoutMS honored for oidc callback if timeoutMS=0" ,
264271 0 , // infinite timeoutMS
265- 500 , // serverSelectionTimeoutMS
266- 499 ) // expectedTimeoutThreshold
272+ 500 + rtt , // serverSelectionTimeoutMS
273+ 499 + rtt ) // expectedTimeoutThreshold
267274 );
268275 }
269276
270277 // Not a prose test
271- @ ParameterizedTest (name = "test callback timeout when server selection timeout is "
272- + "infinite and timeoutMs is set to {0}" )
273- @ ValueSource (ints = {0 , 100 })
274- void testCallbackTimeoutWhenServerSelectionTimeoutIsInfiniteTimeoutMsIsSet (final int timeoutMs ) {
278+ @ Test
279+ @ DisplayName ("test callback timeout when serverSelectionTimeoutMS and timeoutMS are infinite" )
280+ void testCallbackTimeoutWhenServerSelectionTimeoutMsIsInfiniteTimeoutMsIsSet () {
275281 TestCallback callback1 = createCallback ();
282+ Duration expectedTimeout = ChronoUnit .FOREVER .getDuration ();
276283
277284 OidcCallback callback2 = (context ) -> {
278- assertEquals (context .getTimeout (), ChronoUnit .FOREVER .getDuration ());
285+ assertEquals (expectedTimeout , context .getTimeout (),
286+ format ("Expected timeout to be infinite (%s), but was %s" ,
287+ expectedTimeout , context .getTimeout ()));
288+
279289 return callback1 .onRequest (context );
280290 };
281291
@@ -284,7 +294,7 @@ void testCallbackTimeoutWhenServerSelectionTimeoutIsInfiniteTimeoutMsIsSet(final
284294 builder .serverSelectionTimeout (
285295 -1 , // -1 means infinite
286296 TimeUnit .MILLISECONDS ))
287- .timeout (timeoutMs , TimeUnit .MILLISECONDS )
297+ .timeout (0 , TimeUnit .MILLISECONDS )
288298 .build ();
289299
290300 try (MongoClient mongoClient = createMongoClient (clientSettings )) {
@@ -1242,4 +1252,10 @@ public TestCallback createHumanCallback() {
12421252 private long msElapsedSince (final long timeOfStart ) {
12431253 return TimeUnit .NANOSECONDS .toMillis (System .nanoTime () - timeOfStart );
12441254 }
1255+
1256+ private static long minTimeout (final long timeoutMs , final long serverSelectionTimeoutMS ) {
1257+ long timeoutMsEffective = timeoutMs != 0 ? timeoutMs : Long .MAX_VALUE ;
1258+ long serverSelectionTimeoutMSEffective = serverSelectionTimeoutMS != -1 ? serverSelectionTimeoutMS : Long .MAX_VALUE ;
1259+ return Math .min (timeoutMsEffective , serverSelectionTimeoutMSEffective );
1260+ }
12451261}
0 commit comments