Skip to content

Commit cc5469d

Browse files
committed
Add OIDC test for infinite server selection timeout.
1 parent 63a6752 commit cc5469d

File tree

2 files changed

+38
-10
lines changed

2 files changed

+38
-10
lines changed

driver-core/src/main/com/mongodb/internal/connection/OidcAuthenticator.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import com.mongodb.MongoSecurityException;
2727
import com.mongodb.ServerAddress;
2828
import com.mongodb.ServerApi;
29-
import com.mongodb.assertions.Assertions;
3029
import com.mongodb.connection.ClusterConnectionMode;
3130
import com.mongodb.connection.ConnectionDescription;
3231
import com.mongodb.internal.Locks;
@@ -47,6 +46,7 @@
4746
import java.nio.file.Files;
4847
import java.nio.file.Paths;
4948
import java.time.Duration;
49+
import java.time.temporal.ChronoUnit;
5050
import java.util.Arrays;
5151
import java.util.Collections;
5252
import java.util.List;
@@ -131,10 +131,9 @@ private Duration getCallbackTimeout(final TimeoutContext timeoutContext) {
131131

132132
if (timeoutContext.hasTimeoutMS()) {
133133
return assertNotNull(timeoutContext.getTimeout()).call(TimeUnit.MILLISECONDS,
134-
() -> {
135-
Assertions.fail();
136-
return null;
137-
},
134+
() ->
135+
// we can get here if server selection timeout was set to infinite.
136+
ChronoUnit.FOREVER.getDuration(),
138137
(renamingMs) -> Duration.ofMillis(renamingMs),
139138
() -> throwMongoTimeoutException());
140139

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

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@
4141
import org.bson.Document;
4242
import org.junit.jupiter.api.AfterEach;
4343
import org.junit.jupiter.api.BeforeEach;
44-
import org.junit.jupiter.api.DisplayName;
4544
import org.junit.jupiter.api.Test;
4645
import org.junit.jupiter.params.ParameterizedTest;
4746
import org.junit.jupiter.params.provider.Arguments;
4847
import org.junit.jupiter.params.provider.MethodSource;
48+
import org.junit.jupiter.params.provider.ValueSource;
4949

5050
import java.io.IOException;
5151
import java.lang.reflect.Field;
@@ -54,6 +54,7 @@
5454
import java.nio.file.Path;
5555
import java.nio.file.Paths;
5656
import java.time.Duration;
57+
import java.time.temporal.ChronoUnit;
5758
import java.util.ArrayList;
5859
import java.util.Arrays;
5960
import java.util.Collections;
@@ -208,10 +209,12 @@ public void test2p1ValidCallbackInputs() {
208209
}
209210

210211
// Not a prose test
211-
@ParameterizedTest
212+
@ParameterizedTest(name = "{testName}. "
213+
+ "Parameters: timeoutMs={1}, "
214+
+ "serverSelectionTimeoutMS={2},"
215+
+ " expectedTimeoutThreshold={3}")
212216
@MethodSource
213-
@DisplayName("{testName}")
214-
void testValidCallbackInputsTimeout(final String testName,
217+
void testValidCallbackInputsTimeoutWhenTimeoutMsIsSet(final String testName,
215218
final int timeoutMs,
216219
final int serverSelectionTimeoutMS,
217220
final int expectedTimeoutThreshold) {
@@ -247,7 +250,7 @@ void testValidCallbackInputsTimeout(final String testName,
247250
}
248251
}
249252

250-
private static Stream<Arguments> testValidCallbackInputsTimeout() {
253+
private static Stream<Arguments> testValidCallbackInputsTimeoutWhenTimeoutMsIsSet() {
251254
return Stream.of(
252255
Arguments.of("serverSelectionTimeoutMS honored for oidc callback if it's lower than timeoutMS",
253256
1000, // timeoutMS
@@ -264,6 +267,32 @@ private static Stream<Arguments> testValidCallbackInputsTimeout() {
264267
);
265268
}
266269

270+
// 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) {
275+
TestCallback callback1 = createCallback();
276+
277+
OidcCallback callback2 = (context) -> {
278+
assertEquals(context.getTimeout(), ChronoUnit.FOREVER.getDuration());
279+
return callback1.onRequest(context);
280+
};
281+
282+
MongoClientSettings clientSettings = MongoClientSettings.builder(createSettings(callback2))
283+
.applyToClusterSettings(builder ->
284+
builder.serverSelectionTimeout(
285+
-1, // -1 means infinite
286+
TimeUnit.MILLISECONDS))
287+
.timeout(timeoutMs, TimeUnit.MILLISECONDS)
288+
.build();
289+
290+
try (MongoClient mongoClient = createMongoClient(clientSettings)) {
291+
performFind(mongoClient);
292+
assertEquals(1, callback1.getInvocations());
293+
}
294+
}
295+
267296
@Test
268297
public void test2p2RequestCallbackReturnsNull() {
269298
//noinspection ConstantConditions

0 commit comments

Comments
 (0)