From adece2766910e3160d4fb9d46f00e2218dcc894a Mon Sep 17 00:00:00 2001 From: "slav.babanin" Date: Wed, 25 Jun 2025 14:02:42 -0700 Subject: [PATCH 1/9] Fix OIDC tests. JAVA-5337 --- .../OidcAuthenticationProseTests.java | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/driver-sync/src/test/functional/com/mongodb/internal/connection/OidcAuthenticationProseTests.java b/driver-sync/src/test/functional/com/mongodb/internal/connection/OidcAuthenticationProseTests.java index 93c062f13f..4c3a8ce4f0 100644 --- a/driver-sync/src/test/functional/com/mongodb/internal/connection/OidcAuthenticationProseTests.java +++ b/driver-sync/src/test/functional/com/mongodb/internal/connection/OidcAuthenticationProseTests.java @@ -41,6 +41,7 @@ import org.bson.Document; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; @@ -260,6 +261,10 @@ private static Stream testValidCallbackInputsTimeoutWhenTimeoutMsIsSe 500, // timeoutMS 1000, // serverSelectionTimeoutMS 499), // expectedTimeoutThreshold + Arguments.of("timeoutMS honored for oidc callback if serverSelectionTimeoutMS is infinite", + 500, // timeoutMS + -1, // serverSelectionTimeoutMS + 499), // expectedTimeoutThreshold, Arguments.of("serverSelectionTimeoutMS honored for oidc callback if timeoutMS=0", 0, // infinite timeoutMS 500, // serverSelectionTimeoutMS @@ -268,14 +273,18 @@ private static Stream testValidCallbackInputsTimeoutWhenTimeoutMsIsSe } // Not a prose test - @ParameterizedTest(name = "test callback timeout when server selection timeout is " - + "infinite and timeoutMs is set to {0}") - @ValueSource(ints = {0, 100}) - void testCallbackTimeoutWhenServerSelectionTimeoutIsInfiniteTimeoutMsIsSet(final int timeoutMs) { + @Test + @DisplayName("test callback timeout when server selection timeout and " + + "timeoutMs are infinite") + void testCallbackTimeoutWhenServerSelectionTimeoutIsInfiniteTimeoutMsIsSet() { TestCallback callback1 = createCallback(); OidcCallback callback2 = (context) -> { - assertEquals(context.getTimeout(), ChronoUnit.FOREVER.getDuration()); + assertEquals(ChronoUnit.FOREVER.getDuration(), context.getTimeout(), + format("Expected timeout to be infinite (%d seconds), but was %d seconds", + ChronoUnit.FOREVER.getDuration().getSeconds(), + context.getTimeout().getSeconds())); + return callback1.onRequest(context); }; @@ -284,7 +293,7 @@ void testCallbackTimeoutWhenServerSelectionTimeoutIsInfiniteTimeoutMsIsSet(final builder.serverSelectionTimeout( -1, // -1 means infinite TimeUnit.MILLISECONDS)) - .timeout(timeoutMs, TimeUnit.MILLISECONDS) + .timeout(0, TimeUnit.MILLISECONDS) .build(); try (MongoClient mongoClient = createMongoClient(clientSettings)) { From fde5b54dc432c711d4807099aec76342cc0beb77 Mon Sep 17 00:00:00 2001 From: "slav.babanin" Date: Wed, 25 Jun 2025 14:10:04 -0700 Subject: [PATCH 2/9] Refactor OIDC test to use a variable for expected infinite timeout. JAVA-5337 --- .../internal/connection/OidcAuthenticationProseTests.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/driver-sync/src/test/functional/com/mongodb/internal/connection/OidcAuthenticationProseTests.java b/driver-sync/src/test/functional/com/mongodb/internal/connection/OidcAuthenticationProseTests.java index 4c3a8ce4f0..aba10cb28f 100644 --- a/driver-sync/src/test/functional/com/mongodb/internal/connection/OidcAuthenticationProseTests.java +++ b/driver-sync/src/test/functional/com/mongodb/internal/connection/OidcAuthenticationProseTests.java @@ -278,11 +278,12 @@ private static Stream testValidCallbackInputsTimeoutWhenTimeoutMsIsSe + "timeoutMs are infinite") void testCallbackTimeoutWhenServerSelectionTimeoutIsInfiniteTimeoutMsIsSet() { TestCallback callback1 = createCallback(); + Duration expectedTimeout = ChronoUnit.FOREVER.getDuration(); OidcCallback callback2 = (context) -> { - assertEquals(ChronoUnit.FOREVER.getDuration(), context.getTimeout(), + assertEquals(expectedTimeout, context.getTimeout(), format("Expected timeout to be infinite (%d seconds), but was %d seconds", - ChronoUnit.FOREVER.getDuration().getSeconds(), + expectedTimeout.getSeconds(), context.getTimeout().getSeconds())); return callback1.onRequest(context); From e9d1f1a4cbe908dffa9045764717ecde4d481cdc Mon Sep 17 00:00:00 2001 From: "slav.babanin" Date: Wed, 25 Jun 2025 17:29:29 -0700 Subject: [PATCH 3/9] Fix static checks. JAVA-5337 --- .../internal/connection/OidcAuthenticationProseTests.java | 1 - 1 file changed, 1 deletion(-) diff --git a/driver-sync/src/test/functional/com/mongodb/internal/connection/OidcAuthenticationProseTests.java b/driver-sync/src/test/functional/com/mongodb/internal/connection/OidcAuthenticationProseTests.java index aba10cb28f..6cf1fe497b 100644 --- a/driver-sync/src/test/functional/com/mongodb/internal/connection/OidcAuthenticationProseTests.java +++ b/driver-sync/src/test/functional/com/mongodb/internal/connection/OidcAuthenticationProseTests.java @@ -46,7 +46,6 @@ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; -import org.junit.jupiter.params.provider.ValueSource; import java.io.IOException; import java.lang.reflect.Field; From 342a8311bea8a3ebeb755f85e5159941f1932663 Mon Sep 17 00:00:00 2001 From: Viacheslav Babanin Date: Thu, 26 Jun 2025 15:50:13 -0700 Subject: [PATCH 4/9] Update driver-sync/src/test/functional/com/mongodb/internal/connection/OidcAuthenticationProseTests.java Co-authored-by: Valentin Kovalenko --- .../internal/connection/OidcAuthenticationProseTests.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/driver-sync/src/test/functional/com/mongodb/internal/connection/OidcAuthenticationProseTests.java b/driver-sync/src/test/functional/com/mongodb/internal/connection/OidcAuthenticationProseTests.java index 6cf1fe497b..5be6ec930e 100644 --- a/driver-sync/src/test/functional/com/mongodb/internal/connection/OidcAuthenticationProseTests.java +++ b/driver-sync/src/test/functional/com/mongodb/internal/connection/OidcAuthenticationProseTests.java @@ -273,9 +273,8 @@ private static Stream testValidCallbackInputsTimeoutWhenTimeoutMsIsSe // Not a prose test @Test - @DisplayName("test callback timeout when server selection timeout and " - + "timeoutMs are infinite") - void testCallbackTimeoutWhenServerSelectionTimeoutIsInfiniteTimeoutMsIsSet() { + @DisplayName("test callback timeout when serverSelectionTimeoutMS and timeoutMS are infinite") + void testCallbackTimeoutWhenServerSelectionTimeoutMsIsInfiniteTimeoutMsIsSet() { TestCallback callback1 = createCallback(); Duration expectedTimeout = ChronoUnit.FOREVER.getDuration(); From 233b66be547f4f60bff30c06e3ba5212e25b9fc3 Mon Sep 17 00:00:00 2001 From: Viacheslav Babanin Date: Thu, 26 Jun 2025 15:52:38 -0700 Subject: [PATCH 5/9] Update driver-sync/src/test/functional/com/mongodb/internal/connection/OidcAuthenticationProseTests.java Co-authored-by: Valentin Kovalenko --- .../internal/connection/OidcAuthenticationProseTests.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/driver-sync/src/test/functional/com/mongodb/internal/connection/OidcAuthenticationProseTests.java b/driver-sync/src/test/functional/com/mongodb/internal/connection/OidcAuthenticationProseTests.java index 5be6ec930e..2140243b08 100644 --- a/driver-sync/src/test/functional/com/mongodb/internal/connection/OidcAuthenticationProseTests.java +++ b/driver-sync/src/test/functional/com/mongodb/internal/connection/OidcAuthenticationProseTests.java @@ -280,9 +280,8 @@ void testCallbackTimeoutWhenServerSelectionTimeoutMsIsInfiniteTimeoutMsIsSet() { OidcCallback callback2 = (context) -> { assertEquals(expectedTimeout, context.getTimeout(), - format("Expected timeout to be infinite (%d seconds), but was %d seconds", - expectedTimeout.getSeconds(), - context.getTimeout().getSeconds())); + format("Expected timeout to be infinite (%s), but was %s", + expectedTimeout, context.getTimeout())); return callback1.onRequest(context); }; From de3091c586e682198590fb2f35f54f6a9efd4efa Mon Sep 17 00:00:00 2001 From: "slav.babanin" Date: Thu, 26 Jun 2025 16:26:41 -0700 Subject: [PATCH 6/9] Fix test. --- .../connection/OidcAuthenticationProseTests.java | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/driver-sync/src/test/functional/com/mongodb/internal/connection/OidcAuthenticationProseTests.java b/driver-sync/src/test/functional/com/mongodb/internal/connection/OidcAuthenticationProseTests.java index 2140243b08..671acf1044 100644 --- a/driver-sync/src/test/functional/com/mongodb/internal/connection/OidcAuthenticationProseTests.java +++ b/driver-sync/src/test/functional/com/mongodb/internal/connection/OidcAuthenticationProseTests.java @@ -79,7 +79,6 @@ import static com.mongodb.MongoCredential.TOKEN_RESOURCE_KEY; import static com.mongodb.assertions.Assertions.assertNotNull; import static com.mongodb.testing.MongoAssertions.assertCause; -import static java.lang.Math.min; import static java.lang.String.format; import static java.lang.System.getenv; import static java.util.Arrays.asList; @@ -242,11 +241,13 @@ void testValidCallbackInputsTimeoutWhenTimeoutMsIsSet(final String testName, assertEquals(1, callback1.getInvocations()); long elapsed = msElapsedSince(start); - assertFalse(elapsed > (timeoutMs == 0 ? serverSelectionTimeoutMS : min(serverSelectionTimeoutMS, timeoutMs)), + + assertFalse(elapsed > minTimeout(serverSelectionTimeoutMS, timeoutMs), format("Elapsed time %d is greater then minimum of serverSelectionTimeoutMS and timeoutMs, which is %d. " + "This indicates that the callback was not called with the expected timeout.", - min(serverSelectionTimeoutMS, timeoutMs), - elapsed)); + elapsed, + minTimeout(serverSelectionTimeoutMS, timeoutMs))); + } } @@ -1249,4 +1250,10 @@ public TestCallback createHumanCallback() { private long msElapsedSince(final long timeOfStart) { return TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - timeOfStart); } + + private static long minTimeout(final int timeoutMs, final int serverSelectionTimeoutMS) { + long timeoutMsEffective = timeoutMs != 0 ? timeoutMs : Long.MAX_VALUE; + long serverSelectionTimeoutMSEffective = serverSelectionTimeoutMS != -1 ? serverSelectionTimeoutMS : Long.MAX_VALUE; + return Math.min(timeoutMsEffective, serverSelectionTimeoutMSEffective); + } } From 6609b5cbe78fface884713a8987886664c1ae151 Mon Sep 17 00:00:00 2001 From: "slav.babanin" Date: Thu, 26 Jun 2025 17:57:03 -0700 Subject: [PATCH 7/9] Fix assertion order in OIDC timeout test. --- .../internal/connection/OidcAuthenticationProseTests.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/driver-sync/src/test/functional/com/mongodb/internal/connection/OidcAuthenticationProseTests.java b/driver-sync/src/test/functional/com/mongodb/internal/connection/OidcAuthenticationProseTests.java index 671acf1044..85cd771619 100644 --- a/driver-sync/src/test/functional/com/mongodb/internal/connection/OidcAuthenticationProseTests.java +++ b/driver-sync/src/test/functional/com/mongodb/internal/connection/OidcAuthenticationProseTests.java @@ -242,11 +242,11 @@ void testValidCallbackInputsTimeoutWhenTimeoutMsIsSet(final String testName, long elapsed = msElapsedSince(start); - assertFalse(elapsed > minTimeout(serverSelectionTimeoutMS, timeoutMs), + assertFalse(elapsed > minTimeout(timeoutMs, serverSelectionTimeoutMS), format("Elapsed time %d is greater then minimum of serverSelectionTimeoutMS and timeoutMs, which is %d. " + "This indicates that the callback was not called with the expected timeout.", elapsed, - minTimeout(serverSelectionTimeoutMS, timeoutMs))); + minTimeout(timeoutMs, serverSelectionTimeoutMS))); } } From 173bd5e37e61e738be9f5d72b613745740fb0152 Mon Sep 17 00:00:00 2001 From: "slav.babanin" Date: Wed, 2 Jul 2025 13:54:07 -0700 Subject: [PATCH 8/9] Adjust OIDC timeout test to account for round-trip time in timeout calculations. --- .../OidcAuthenticationProseTests.java | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/driver-sync/src/test/functional/com/mongodb/internal/connection/OidcAuthenticationProseTests.java b/driver-sync/src/test/functional/com/mongodb/internal/connection/OidcAuthenticationProseTests.java index 85cd771619..36746bbcc0 100644 --- a/driver-sync/src/test/functional/com/mongodb/internal/connection/OidcAuthenticationProseTests.java +++ b/driver-sync/src/test/functional/com/mongodb/internal/connection/OidcAuthenticationProseTests.java @@ -16,6 +16,7 @@ package com.mongodb.internal.connection; +import com.mongodb.ClusterFixture; import com.mongodb.ConnectionString; import com.mongodb.MongoClientSettings; import com.mongodb.MongoCommandException; @@ -217,12 +218,16 @@ void testValidCallbackInputsTimeoutWhenTimeoutMsIsSet(final String testName, final int timeoutMs, final int serverSelectionTimeoutMS, final int expectedTimeoutThreshold) { + long rtt = ClusterFixture.getPrimaryRTT(); + final long timeoutMsWithRtt = timeoutMs + rtt; + final long serverSelectionTimeoutMSWithRtt = serverSelectionTimeoutMS + rtt; + final long expectedTimeoutThresholdWithRtt = expectedTimeoutThreshold + rtt; TestCallback callback1 = createCallback(); OidcCallback callback2 = (context) -> { - assertTrue(context.getTimeout().toMillis() < expectedTimeoutThreshold, + assertTrue(context.getTimeout().toMillis() < expectedTimeoutThresholdWithRtt, format("Expected timeout to be less than %d, but was %d", - expectedTimeoutThreshold, + expectedTimeoutThresholdWithRtt, context.getTimeout().toMillis())); return callback1.onRequest(context); }; @@ -230,9 +235,9 @@ void testValidCallbackInputsTimeoutWhenTimeoutMsIsSet(final String testName, MongoClientSettings clientSettings = MongoClientSettings.builder(createSettings(callback2)) .applyToClusterSettings(builder -> builder.serverSelectionTimeout( - serverSelectionTimeoutMS, + serverSelectionTimeoutMSWithRtt, TimeUnit.MILLISECONDS)) - .timeout(timeoutMs, TimeUnit.MILLISECONDS) + .timeout(timeoutMsWithRtt, TimeUnit.MILLISECONDS) .build(); try (MongoClient mongoClient = createMongoClient(clientSettings)) { @@ -242,11 +247,11 @@ void testValidCallbackInputsTimeoutWhenTimeoutMsIsSet(final String testName, long elapsed = msElapsedSince(start); - assertFalse(elapsed > minTimeout(timeoutMs, serverSelectionTimeoutMS), + assertFalse(elapsed > minTimeout(timeoutMsWithRtt, serverSelectionTimeoutMSWithRtt), format("Elapsed time %d is greater then minimum of serverSelectionTimeoutMS and timeoutMs, which is %d. " + "This indicates that the callback was not called with the expected timeout.", elapsed, - minTimeout(timeoutMs, serverSelectionTimeoutMS))); + minTimeout(timeoutMsWithRtt, serverSelectionTimeoutMSWithRtt))); } } @@ -1251,7 +1256,7 @@ private long msElapsedSince(final long timeOfStart) { return TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - timeOfStart); } - private static long minTimeout(final int timeoutMs, final int serverSelectionTimeoutMS) { + private static long minTimeout(final long timeoutMs, final long serverSelectionTimeoutMS) { long timeoutMsEffective = timeoutMs != 0 ? timeoutMs : Long.MAX_VALUE; long serverSelectionTimeoutMSEffective = serverSelectionTimeoutMS != -1 ? serverSelectionTimeoutMS : Long.MAX_VALUE; return Math.min(timeoutMsEffective, serverSelectionTimeoutMSEffective); From aa5891a41260d8ea3bf2eef3b8da81aae9ae972c Mon Sep 17 00:00:00 2001 From: "slav.babanin" Date: Wed, 2 Jul 2025 16:27:41 -0700 Subject: [PATCH 9/9] Add RTT is method source. --- .../OidcAuthenticationProseTests.java | 43 +++++++++---------- 1 file changed, 20 insertions(+), 23 deletions(-) diff --git a/driver-sync/src/test/functional/com/mongodb/internal/connection/OidcAuthenticationProseTests.java b/driver-sync/src/test/functional/com/mongodb/internal/connection/OidcAuthenticationProseTests.java index 36746bbcc0..51ed12210f 100644 --- a/driver-sync/src/test/functional/com/mongodb/internal/connection/OidcAuthenticationProseTests.java +++ b/driver-sync/src/test/functional/com/mongodb/internal/connection/OidcAuthenticationProseTests.java @@ -215,19 +215,15 @@ public void test2p1ValidCallbackInputs() { + " expectedTimeoutThreshold={3}") @MethodSource void testValidCallbackInputsTimeoutWhenTimeoutMsIsSet(final String testName, - final int timeoutMs, - final int serverSelectionTimeoutMS, - final int expectedTimeoutThreshold) { - long rtt = ClusterFixture.getPrimaryRTT(); - final long timeoutMsWithRtt = timeoutMs + rtt; - final long serverSelectionTimeoutMSWithRtt = serverSelectionTimeoutMS + rtt; - final long expectedTimeoutThresholdWithRtt = expectedTimeoutThreshold + rtt; + final long timeoutMs, + final long serverSelectionTimeoutMS, + final long expectedTimeoutThreshold) { TestCallback callback1 = createCallback(); OidcCallback callback2 = (context) -> { - assertTrue(context.getTimeout().toMillis() < expectedTimeoutThresholdWithRtt, + assertTrue(context.getTimeout().toMillis() < expectedTimeoutThreshold, format("Expected timeout to be less than %d, but was %d", - expectedTimeoutThresholdWithRtt, + expectedTimeoutThreshold, context.getTimeout().toMillis())); return callback1.onRequest(context); }; @@ -235,9 +231,9 @@ void testValidCallbackInputsTimeoutWhenTimeoutMsIsSet(final String testName, MongoClientSettings clientSettings = MongoClientSettings.builder(createSettings(callback2)) .applyToClusterSettings(builder -> builder.serverSelectionTimeout( - serverSelectionTimeoutMSWithRtt, + serverSelectionTimeoutMS, TimeUnit.MILLISECONDS)) - .timeout(timeoutMsWithRtt, TimeUnit.MILLISECONDS) + .timeout(timeoutMs, TimeUnit.MILLISECONDS) .build(); try (MongoClient mongoClient = createMongoClient(clientSettings)) { @@ -247,33 +243,34 @@ void testValidCallbackInputsTimeoutWhenTimeoutMsIsSet(final String testName, long elapsed = msElapsedSince(start); - assertFalse(elapsed > minTimeout(timeoutMsWithRtt, serverSelectionTimeoutMSWithRtt), + assertFalse(elapsed > minTimeout(timeoutMs, serverSelectionTimeoutMS), format("Elapsed time %d is greater then minimum of serverSelectionTimeoutMS and timeoutMs, which is %d. " + "This indicates that the callback was not called with the expected timeout.", elapsed, - minTimeout(timeoutMsWithRtt, serverSelectionTimeoutMSWithRtt))); + minTimeout(timeoutMs, serverSelectionTimeoutMS))); } } private static Stream testValidCallbackInputsTimeoutWhenTimeoutMsIsSet() { + long rtt = ClusterFixture.getPrimaryRTT(); return Stream.of( Arguments.of("serverSelectionTimeoutMS honored for oidc callback if it's lower than timeoutMS", - 1000, // timeoutMS - 500, // serverSelectionTimeoutMS - 499), // expectedTimeoutThreshold + 1000 + rtt, // timeoutMS + 500 + rtt, // serverSelectionTimeoutMS + 499 + rtt), // expectedTimeoutThreshold Arguments.of("timeoutMS honored for oidc callback if it's lower than serverSelectionTimeoutMS", - 500, // timeoutMS - 1000, // serverSelectionTimeoutMS - 499), // expectedTimeoutThreshold + 500 + rtt, // timeoutMS + 1000 + rtt, // serverSelectionTimeoutMS + 499 + rtt), // expectedTimeoutThreshold Arguments.of("timeoutMS honored for oidc callback if serverSelectionTimeoutMS is infinite", - 500, // timeoutMS + 500 + rtt, // timeoutMS -1, // serverSelectionTimeoutMS - 499), // expectedTimeoutThreshold, + 499 + rtt), // expectedTimeoutThreshold, Arguments.of("serverSelectionTimeoutMS honored for oidc callback if timeoutMS=0", 0, // infinite timeoutMS - 500, // serverSelectionTimeoutMS - 499) // expectedTimeoutThreshold + 500 + rtt, // serverSelectionTimeoutMS + 499 + rtt) // expectedTimeoutThreshold ); }