Skip to content

Commit

Permalink
Improve default dns client configuration - fixes #2303
Browse files Browse the repository at this point in the history
  • Loading branch information
vietj committed Jan 30, 2018
1 parent 13e9512 commit df533fc
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
11 changes: 6 additions & 5 deletions src/main/java/io/vertx/core/dns/DnsClientOptions.java
Expand Up @@ -12,6 +12,7 @@
package io.vertx.core.dns;

import io.vertx.codegen.annotations.DataObject;
import io.vertx.core.VertxOptions;
import io.vertx.core.json.JsonObject;

/**
Expand All @@ -23,17 +24,17 @@
public class DnsClientOptions {

/**
* The default value for the port = 53
* The default value for the port = {@code -1} (configured by {@link VertxOptions#getAddressResolverOptions()})
*/
public static final int DEFAULT_PORT = 53;
public static final int DEFAULT_PORT = -1;

/**
* The default value for the host = localhost
* The default value for the host = {@code null} (configured by {@link VertxOptions#getAddressResolverOptions()})
*/
public static final String DEFAULT_HOST = "localhost";
public static final String DEFAULT_HOST = null;

/**
* The default value for the query timeout in millis = 5000
* The default value for the query timeout in millis = {@code 5000}
*/
public static final long DEFAULT_QUERY_TIMEOUT = 5000;

Expand Down
14 changes: 10 additions & 4 deletions src/main/java/io/vertx/core/impl/VertxImpl.java
Expand Up @@ -410,14 +410,20 @@ public DnsClient createDnsClient(int port, String host) {

@Override
public DnsClient createDnsClient() {
DnsResolverProvider provider = new DnsResolverProvider(this, addressResolverOptions);
InetSocketAddress address = provider.nameServerAddresses().get(0);
return new DnsClientImpl(this, address.getPort(), address.getAddress().getHostAddress(), DnsClientOptions.DEFAULT_QUERY_TIMEOUT);
return createDnsClient(new DnsClientOptions());
}

@Override
public DnsClient createDnsClient(DnsClientOptions options) {
return new DnsClientImpl(this, options.getPort(), options.getHost(), options.getQueryTimeout());
String host = options.getHost();
int port = options.getPort();
if (host == null || port < 0) {
DnsResolverProvider provider = new DnsResolverProvider(this, addressResolverOptions);
InetSocketAddress address = provider.nameServerAddresses().get(0);
host = address.getAddress().getHostAddress();
port = address.getPort();
}
return new DnsClientImpl(this, port, host, options.getQueryTimeout());
}

private VertxMetrics initialiseMetrics(VertxOptions options) {
Expand Down
14 changes: 12 additions & 2 deletions src/test/java/io/vertx/test/core/DNSTest.java
Expand Up @@ -29,6 +29,7 @@

import java.net.InetSocketAddress;
import java.util.List;
import java.util.function.Function;

import static io.vertx.test.core.TestUtils.assertIllegalStateException;
import static io.vertx.test.core.TestUtils.assertNullPointerException;
Expand Down Expand Up @@ -62,14 +63,23 @@ public void testIllegalArguments() throws Exception {

@Test
public void testDefaultDnsClient() throws Exception {
testDefaultDnsClient(vertx -> vertx.createDnsClient());
}

@Test
public void testDefaultDnsClientWithOptions() throws Exception {
testDefaultDnsClient(vertx -> vertx.createDnsClient(new DnsClientOptions()));
}

private void testDefaultDnsClient(Function<Vertx, DnsClient> clientProvider) throws Exception {
final String ip = "10.0.0.1";
FakeDNSServer fakeDNSServer = FakeDNSServer.testLookup4(ip);
fakeDNSServer.start();
VertxOptions vertxOptions = new VertxOptions();
InetSocketAddress fakeServerAddress = fakeDNSServer.localAddress();
vertxOptions.getAddressResolverOptions().addServer(fakeServerAddress.getHostString() + ":" + fakeServerAddress.getPort());
Vertx vertxWithFakeDns = Vertx.vertx(vertxOptions);
DnsClient dnsClient = vertxWithFakeDns.createDnsClient();
DnsClient dnsClient = clientProvider.apply(vertxWithFakeDns);

dnsClient.lookup4("vertx.io", onSuccess(result -> {
assertEquals(ip, result);
Expand Down Expand Up @@ -259,7 +269,7 @@ public void testLookup() throws Exception {

@Test
public void testTimeout() throws Exception {
DnsClient dns = vertx.createDnsClient(new DnsClientOptions().setPort(10000).setQueryTimeout(5000));
DnsClient dns = vertx.createDnsClient(new DnsClientOptions().setHost("localhost").setPort(10000).setQueryTimeout(5000));

dns.lookup("vertx.io", onFailure(result -> {
assertEquals(VertxException.class, result.getClass());
Expand Down

0 comments on commit df533fc

Please sign in to comment.