Skip to content

Commit 0a110dc

Browse files
vbabaninstIncMale
andauthored
Fix OIDC tests. (#1753)
JAVA-5337 --------- Co-authored-by: Valentin Kovalenko <[email protected]>
1 parent 141ee1e commit 0a110dc

File tree

1 file changed

+38
-22
lines changed

1 file changed

+38
-22
lines changed

driver-sync/src/test/functional/com/mongodb/internal/connection/OidcAuthenticationProseTests.java

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.mongodb.internal.connection;
1818

19+
import com.mongodb.ClusterFixture;
1920
import com.mongodb.ConnectionString;
2021
import com.mongodb.MongoClientSettings;
2122
import com.mongodb.MongoCommandException;
@@ -41,11 +42,11 @@
4142
import org.bson.Document;
4243
import org.junit.jupiter.api.AfterEach;
4344
import org.junit.jupiter.api.BeforeEach;
45+
import org.junit.jupiter.api.DisplayName;
4446
import org.junit.jupiter.api.Test;
4547
import org.junit.jupiter.params.ParameterizedTest;
4648
import org.junit.jupiter.params.provider.Arguments;
4749
import org.junit.jupiter.params.provider.MethodSource;
48-
import org.junit.jupiter.params.provider.ValueSource;
4950

5051
import java.io.IOException;
5152
import java.lang.reflect.Field;
@@ -79,7 +80,6 @@
7980
import static com.mongodb.MongoCredential.TOKEN_RESOURCE_KEY;
8081
import static com.mongodb.assertions.Assertions.assertNotNull;
8182
import static com.mongodb.testing.MongoAssertions.assertCause;
82-
import static java.lang.Math.min;
8383
import static java.lang.String.format;
8484
import static java.lang.System.getenv;
8585
import 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

Comments
 (0)