Skip to content

Commit 16acf92

Browse files
committed
Improve DNS configuration tests
JAVA-4911
1 parent 454ebd6 commit 16acf92

File tree

1 file changed

+44
-3
lines changed

1 file changed

+44
-3
lines changed

driver-sync/src/test/functional/com/mongodb/client/AbstractDnsConfigurationTest.java

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,31 @@
1919
import com.mongodb.ConnectionString;
2020
import com.mongodb.MongoClientSettings;
2121
import com.mongodb.MongoException;
22+
import com.mongodb.MongoSocketException;
2223
import com.mongodb.ServerAddress;
24+
import com.mongodb.connection.ServerConnectionState;
2325
import com.mongodb.connection.ServerDescription;
2426
import com.mongodb.event.ClusterDescriptionChangedEvent;
2527
import com.mongodb.event.ClusterListener;
2628
import com.mongodb.spi.dns.DnsClient;
2729
import com.mongodb.spi.dns.DnsException;
2830
import com.mongodb.spi.dns.InetAddressResolver;
2931
import org.junit.jupiter.api.Test;
32+
import org.junit.jupiter.params.ParameterizedTest;
33+
import org.junit.jupiter.params.provider.ValueSource;
3034

3135
import java.net.UnknownHostException;
3236
import java.util.Collections;
3337
import java.util.concurrent.CompletableFuture;
3438
import java.util.concurrent.ExecutionException;
3539
import java.util.concurrent.TimeoutException;
3640

41+
import static com.mongodb.ClusterFixture.getSslSettings;
3742
import static java.util.concurrent.TimeUnit.SECONDS;
3843
import static org.junit.jupiter.api.Assertions.assertEquals;
44+
import static org.junit.jupiter.api.Assertions.assertTrue;
3945

46+
@SuppressWarnings("try")
4047
public abstract class AbstractDnsConfigurationTest {
4148

4249
protected abstract MongoClient createMongoClient(MongoClientSettings settings);
@@ -48,7 +55,7 @@ public void testInetAddressResolverConfiguration() throws InterruptedException,
4855
throw exception;
4956
};
5057

51-
CompletableFuture<UnknownHostException> exceptionReceived = new CompletableFuture<>();
58+
CompletableFuture<Throwable> exceptionReceivedFuture = new CompletableFuture<>();
5259
MongoClientSettings settings = MongoClientSettings.builder()
5360
.applyToClusterSettings(builder ->
5461
builder.hosts(Collections.singletonList(new ServerAddress("some.host")))
@@ -57,15 +64,49 @@ public void testInetAddressResolverConfiguration() throws InterruptedException,
5764
public void clusterDescriptionChanged(final ClusterDescriptionChangedEvent event) {
5865
ServerDescription serverDescription = event.getNewDescription().getServerDescriptions().get(0);
5966
if (serverDescription.getException() != null) {
60-
exceptionReceived.complete(exception);
67+
exceptionReceivedFuture.complete(serverDescription.getException());
6168
}
6269
}
6370
}))
6471
.inetAddressResolver(resolver)
6572
.build();
6673

6774
try (MongoClient ignored = createMongoClient(settings)) {
68-
assertEquals(exception, exceptionReceived.get(10, SECONDS));
75+
Throwable exceptionReceived = exceptionReceivedFuture.get(1, SECONDS);
76+
assertEquals(MongoSocketException.class, exceptionReceived.getClass());
77+
assertEquals(exception, exceptionReceived.getCause());
78+
}
79+
}
80+
81+
@ParameterizedTest(name = "InetAddressResolver should not be used to resolve IP literal {0}")
82+
@ValueSource(strings = {"127.0.0.1", "::1", "[0:0:0:0:0:0:0:1]"})
83+
public void testInetAddressResolverDoesNotResultIpLiteral(final String ipLiteral) throws InterruptedException, ExecutionException,
84+
TimeoutException {
85+
// should not be invoked for IP literals
86+
InetAddressResolver resolver = host -> {
87+
throw new UnknownHostException();
88+
};
89+
90+
CompletableFuture<Boolean> serverConnectedFuture = new CompletableFuture<>();
91+
MongoClientSettings settings = MongoClientSettings.builder()
92+
.applyToClusterSettings(builder ->
93+
builder.hosts(Collections.singletonList(new ServerAddress(ipLiteral)))
94+
.addClusterListener(new ClusterListener() {
95+
@Override
96+
public void clusterDescriptionChanged(final ClusterDescriptionChangedEvent event) {
97+
ServerDescription serverDescription = event.getNewDescription().getServerDescriptions().get(0);
98+
// If the resolver that throws an exception is invoked, the state will not be CONNECTED
99+
if (serverDescription.getState() == ServerConnectionState.CONNECTED) {
100+
serverConnectedFuture.complete(true);
101+
}
102+
}
103+
}))
104+
.applyToSslSettings(builder -> builder.applySettings(getSslSettings()))
105+
.inetAddressResolver(resolver)
106+
.build();
107+
108+
try (MongoClient ignored = createMongoClient(settings)) {
109+
assertTrue(serverConnectedFuture.get(1, SECONDS));
69110
}
70111
}
71112

0 commit comments

Comments
 (0)