19
19
import com .mongodb .ConnectionString ;
20
20
import com .mongodb .MongoClientSettings ;
21
21
import com .mongodb .MongoException ;
22
+ import com .mongodb .MongoSocketException ;
22
23
import com .mongodb .ServerAddress ;
24
+ import com .mongodb .connection .ServerConnectionState ;
23
25
import com .mongodb .connection .ServerDescription ;
24
26
import com .mongodb .event .ClusterDescriptionChangedEvent ;
25
27
import com .mongodb .event .ClusterListener ;
26
28
import com .mongodb .spi .dns .DnsClient ;
27
29
import com .mongodb .spi .dns .DnsException ;
28
30
import com .mongodb .spi .dns .InetAddressResolver ;
29
31
import org .junit .jupiter .api .Test ;
32
+ import org .junit .jupiter .params .ParameterizedTest ;
33
+ import org .junit .jupiter .params .provider .ValueSource ;
30
34
31
35
import java .net .UnknownHostException ;
32
36
import java .util .Collections ;
33
37
import java .util .concurrent .CompletableFuture ;
34
38
import java .util .concurrent .ExecutionException ;
35
39
import java .util .concurrent .TimeoutException ;
36
40
41
+ import static com .mongodb .ClusterFixture .getSslSettings ;
37
42
import static java .util .concurrent .TimeUnit .SECONDS ;
38
43
import static org .junit .jupiter .api .Assertions .assertEquals ;
44
+ import static org .junit .jupiter .api .Assertions .assertTrue ;
39
45
46
+ @ SuppressWarnings ("try" )
40
47
public abstract class AbstractDnsConfigurationTest {
41
48
42
49
protected abstract MongoClient createMongoClient (MongoClientSettings settings );
@@ -48,7 +55,7 @@ public void testInetAddressResolverConfiguration() throws InterruptedException,
48
55
throw exception ;
49
56
};
50
57
51
- CompletableFuture <UnknownHostException > exceptionReceived = new CompletableFuture <>();
58
+ CompletableFuture <Throwable > exceptionReceivedFuture = new CompletableFuture <>();
52
59
MongoClientSettings settings = MongoClientSettings .builder ()
53
60
.applyToClusterSettings (builder ->
54
61
builder .hosts (Collections .singletonList (new ServerAddress ("some.host" )))
@@ -57,15 +64,49 @@ public void testInetAddressResolverConfiguration() throws InterruptedException,
57
64
public void clusterDescriptionChanged (final ClusterDescriptionChangedEvent event ) {
58
65
ServerDescription serverDescription = event .getNewDescription ().getServerDescriptions ().get (0 );
59
66
if (serverDescription .getException () != null ) {
60
- exceptionReceived .complete (exception );
67
+ exceptionReceivedFuture .complete (serverDescription . getException () );
61
68
}
62
69
}
63
70
}))
64
71
.inetAddressResolver (resolver )
65
72
.build ();
66
73
67
74
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 ));
69
110
}
70
111
}
71
112
0 commit comments