diff --git a/src/main/asciidoc/dataobjects.adoc b/src/main/asciidoc/dataobjects.adoc index 2a0024b6fce..d219eb3567d 100644 --- a/src/main/asciidoc/dataobjects.adoc +++ b/src/main/asciidoc/dataobjects.adoc @@ -1550,11 +1550,39 @@ Add a certificate value [frame="topbot"] |=== ^|Name | Type ^| Description +|[[cacheMaxTimeToLive]]`cacheMaxTimeToLive`|`Number (int)`| ++++ +Set the cache maximum TTL value in seconds. After successful resolution IP addresses are cached with their DNS response TTL, + use this to set a maximum value to all responses TTL. ++++ +|[[cacheMinTimeToLive]]`cacheMinTimeToLive`|`Number (int)`| ++++ +Set the cache minimum TTL value in seconds. After resolution successful IP addresses are cached with their DNS response TTL, + use this to set a minimum value to all responses TTL. ++++ +|[[cacheNegativeTimeToLive]]`cacheNegativeTimeToLive`|`Number (int)`| ++++ +Set the negative cache TTL value in seconds. After a failed hostname resolution, DNS queries won't be retried + for a period of time equals to the negative TTL. This allows to reduce the response time of negative replies + and reduce the amount of messages to DNS servers. ++++ +|[[maxQueries]]`maxQueries`|`Number (int)`| ++++ +Set the maximum number of queries when an hostname is resolved. ++++ |[[optResourceEnabled]]`optResourceEnabled`|`Boolean`| +++ Set to true to enable the automatic inclusion in DNS queries of an optional record that hints the remote DNS server about how much data the resolver can read per response. +++ +|[[queryTimeout]]`queryTimeout`|`Number (long)`| ++++ +Set the query timeout in milliseconds, i.e the amount of time after a query is considered to be failed. ++++ +|[[rdFlag]]`rdFlag`|`Boolean`| ++++ +Set the DNS queries Recursion Desired flag value. ++++ |[[servers]]`servers`|`Array of String`| +++ Set the list of DNS server addresses, an address is the IP of the dns server, followed by an optional diff --git a/src/main/generated/io/vertx/core/dns/HostnameResolverOptionsConverter.java b/src/main/generated/io/vertx/core/dns/HostnameResolverOptionsConverter.java index cacfe40eaa5..3d4d34279c4 100644 --- a/src/main/generated/io/vertx/core/dns/HostnameResolverOptionsConverter.java +++ b/src/main/generated/io/vertx/core/dns/HostnameResolverOptionsConverter.java @@ -27,9 +27,27 @@ public class HostnameResolverOptionsConverter { public static void fromJson(JsonObject json, HostnameResolverOptions obj) { + if (json.getValue("cacheMaxTimeToLive") instanceof Number) { + obj.setCacheMaxTimeToLive(((Number)json.getValue("cacheMaxTimeToLive")).intValue()); + } + if (json.getValue("cacheMinTimeToLive") instanceof Number) { + obj.setCacheMinTimeToLive(((Number)json.getValue("cacheMinTimeToLive")).intValue()); + } + if (json.getValue("cacheNegativeTimeToLive") instanceof Number) { + obj.setCacheNegativeTimeToLive(((Number)json.getValue("cacheNegativeTimeToLive")).intValue()); + } + if (json.getValue("maxQueries") instanceof Number) { + obj.setMaxQueries(((Number)json.getValue("maxQueries")).intValue()); + } if (json.getValue("optResourceEnabled") instanceof Boolean) { obj.setOptResourceEnabled((Boolean)json.getValue("optResourceEnabled")); } + if (json.getValue("queryTimeout") instanceof Number) { + obj.setQueryTimeout(((Number)json.getValue("queryTimeout")).longValue()); + } + if (json.getValue("rdFlag") instanceof Boolean) { + obj.setRdFlag((Boolean)json.getValue("rdFlag")); + } if (json.getValue("servers") instanceof JsonArray) { json.getJsonArray("servers").forEach(item -> { if (item instanceof String) @@ -39,7 +57,13 @@ public static void fromJson(JsonObject json, HostnameResolverOptions obj) { } public static void toJson(HostnameResolverOptions obj, JsonObject json) { + json.put("cacheMaxTimeToLive", obj.getCacheMaxTimeToLive()); + json.put("cacheMinTimeToLive", obj.getCacheMinTimeToLive()); + json.put("cacheNegativeTimeToLive", obj.getCacheNegativeTimeToLive()); + json.put("maxQueries", obj.getMaxQueries()); json.put("optResourceEnabled", obj.isOptResourceEnabled()); + json.put("queryTimeout", obj.getQueryTimeout()); + json.put("rdFlag", obj.getRdFlag()); if (obj.getServers() != null) { json.put("servers", new JsonArray( obj.getServers(). diff --git a/src/main/java/io/vertx/core/dns/HostnameResolverOptions.java b/src/main/java/io/vertx/core/dns/HostnameResolverOptions.java index 67338828c6e..d6ae5a0be5d 100644 --- a/src/main/java/io/vertx/core/dns/HostnameResolverOptions.java +++ b/src/main/java/io/vertx/core/dns/HostnameResolverOptions.java @@ -33,25 +33,53 @@ public class HostnameResolverOptions { /** * The default list of DNS servers = null (uses system name server's list like resolve.conf otherwise Google Public DNS) */ - public static final List DEFAULT_DNS_SERVERS = null; + public static final List DEFAULT_SERVERS = null; /** * The default value for {@link #setOptResourceEnabled} = true */ public static final boolean DEFAULT_OPT_RESOURCE_ENABLED = true; - private List servers = DEFAULT_DNS_SERVERS; - private boolean optResourceEnabled = DEFAULT_OPT_RESOURCE_ENABLED; + public static final int DEFAULT_CACHE_MIN_TIME_TO_LIVE = 0; + public static final int DEFAULT_CACHE_MAX_TIME_TO_LIVE = Integer.MAX_VALUE; + public static final int DEFAULT_CACHE_NEGATIVE_TIME_TO_LIVE = 0; + public static final int DEFAULT_QUERY_TIMEOUT = 5000; + public static final int DEFAULT_MAX_QUERIES = 3; + public static final boolean DEFAULT_RD_FLAG = true; + + private List servers; + private boolean optResourceEnabled; + private int cacheMinTimeToLive; + private int cacheMaxTimeToLive; + private int cacheNegativeTimeToLive; + private long queryTimeout; + private int maxQueries; + private boolean rdFlag; public HostnameResolverOptions() { + servers = DEFAULT_SERVERS; + optResourceEnabled = DEFAULT_OPT_RESOURCE_ENABLED; + cacheMinTimeToLive = DEFAULT_CACHE_MIN_TIME_TO_LIVE; + cacheMaxTimeToLive = DEFAULT_CACHE_MAX_TIME_TO_LIVE; + cacheNegativeTimeToLive = DEFAULT_CACHE_NEGATIVE_TIME_TO_LIVE; + queryTimeout = DEFAULT_QUERY_TIMEOUT; + maxQueries = DEFAULT_MAX_QUERIES; + rdFlag = DEFAULT_RD_FLAG; } public HostnameResolverOptions(HostnameResolverOptions other) { this.servers = other.servers != null ? new ArrayList<>(other.servers) : null; this.optResourceEnabled = other.optResourceEnabled; + this.cacheMinTimeToLive = other.cacheMinTimeToLive; + this.cacheMaxTimeToLive = other.cacheMaxTimeToLive; + this.cacheNegativeTimeToLive = other.cacheNegativeTimeToLive; + this.queryTimeout = other.queryTimeout; + this.maxQueries = other.maxQueries; + this.rdFlag = other.rdFlag; } public HostnameResolverOptions(JsonObject json) { + this(); HostnameResolverOptionsConverter.fromJson(json, this); } @@ -109,12 +137,130 @@ public HostnameResolverOptions setOptResourceEnabled(boolean optResourceEnabled) return this; } + /** + * @return the cache min TTL in seconds + */ + public int getCacheMinTimeToLive() { + return cacheMinTimeToLive; + } + + /** + * Set the cache minimum TTL value in seconds. After resolution successful IP addresses are cached with their DNS response TTL, + * use this to set a minimum value to all responses TTL. + * + * @param cacheMinTimeToLive the cache min TTL in seconds + * @return a reference to this, so the API can be used fluently + */ + public HostnameResolverOptions setCacheMinTimeToLive(int cacheMinTimeToLive) { + this.cacheMinTimeToLive = cacheMinTimeToLive; + return this; + } + + /** + * @return the cache max TTL in seconds + */ + public int getCacheMaxTimeToLive() { + return cacheMaxTimeToLive; + } + + /** + * Set the cache maximum TTL value in seconds. After successful resolution IP addresses are cached with their DNS response TTL, + * use this to set a maximum value to all responses TTL. + * + * @param cacheMaxTimeToLive the cache max TTL in seconds + * @return a reference to this, so the API can be used fluently + */ + public HostnameResolverOptions setCacheMaxTimeToLive(int cacheMaxTimeToLive) { + this.cacheMaxTimeToLive = cacheMaxTimeToLive; + return this; + } + + /** + * @return the cache negative TTL in seconds + */ + public int getCacheNegativeTimeToLive() { + return cacheNegativeTimeToLive; + } + + /** + * Set the negative cache TTL value in seconds. After a failed hostname resolution, DNS queries won't be retried + * for a period of time equals to the negative TTL. This allows to reduce the response time of negative replies + * and reduce the amount of messages to DNS servers. + * + * @param cacheNegativeTimeToLive the cache negative TTL in seconds + * @return a reference to this, so the API can be used fluently + */ + public HostnameResolverOptions setCacheNegativeTimeToLive(int cacheNegativeTimeToLive) { + this.cacheNegativeTimeToLive = cacheNegativeTimeToLive; + return this; + } + + /** + * @return the query timeout in milliseconds + */ + public long getQueryTimeout() { + return queryTimeout; + } + + /** + * Set the query timeout in milliseconds, i.e the amount of time after a query is considered to be failed. + * + * @param queryTimeout the query timeout in milliseconds + * @return a reference to this, so the API can be used fluently + */ + public HostnameResolverOptions setQueryTimeout(long queryTimeout) { + this.queryTimeout = queryTimeout; + return this; + } + + /** + * @return the maximum number of queries to be sent during a resolution + */ + public int getMaxQueries() { + return maxQueries; + } + + /** + * Set the maximum number of queries when an hostname is resolved. + * + * @param maxQueries the max number of queries to be sent + * @return a reference to this, so the API can be used fluently + */ + public HostnameResolverOptions setMaxQueries(int maxQueries) { + this.maxQueries = maxQueries; + return this; + } + + /** + * @return the DNS queries Recursion Desired flag value + */ + public boolean getRdFlag() { + return rdFlag; + } + + /** + * Set the DNS queries Recursion Desired flag value. + * + * @param rdFlag the flag value + * @return a reference to this, so the API can be used fluently + */ + public HostnameResolverOptions setRdFlag(boolean rdFlag) { + this.rdFlag = rdFlag; + return this; + } + @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; HostnameResolverOptions that = (HostnameResolverOptions) o; if (optResourceEnabled != that.optResourceEnabled) return false; + if (cacheMinTimeToLive != that.cacheMinTimeToLive) return false; + if (cacheMaxTimeToLive != that.cacheMaxTimeToLive) return false; + if (cacheNegativeTimeToLive != that.cacheNegativeTimeToLive) return false; + if (queryTimeout != that.queryTimeout) return false; + if (maxQueries != that.maxQueries) return false; + if (rdFlag != that.rdFlag) return false; return servers != null ? servers.equals(that.servers) : that.servers == null; } @@ -122,6 +268,12 @@ public boolean equals(Object o) { public int hashCode() { int result = optResourceEnabled ? 1 : 0; result = 31 * result + (servers != null ? servers.hashCode() : 0); + result = 31 * result + cacheMinTimeToLive; + result = 31 * result + cacheMaxTimeToLive; + result = 31 * result + cacheNegativeTimeToLive; + result = 31 * result + Long.hashCode(queryTimeout); + result = 31 * result + maxQueries; + result = 31 * result + Boolean.hashCode(rdFlag); return result; } diff --git a/src/main/java/io/vertx/core/impl/VertxImpl.java b/src/main/java/io/vertx/core/impl/VertxImpl.java index ed2cdb163fb..7adfda16bb4 100644 --- a/src/main/java/io/vertx/core/impl/VertxImpl.java +++ b/src/main/java/io/vertx/core/impl/VertxImpl.java @@ -149,9 +149,10 @@ public class VertxImpl implements VertxInternal, MetricsProvider { DnsNameResolverBuilder builder = new DnsNameResolverBuilder(createEventLoopContext(null, new JsonObject(), Thread.currentThread().getContextClassLoader()).nettyEventLoop()); builder.channelFactory(NioDatagramChannel::new); - HostnameResolverOptions dnsOptions = options.getHostnameResolverOptions(); - if (dnsOptions != null) { - List dnsServers = dnsOptions.getServers(); + HostnameResolverOptions hostnameResolver = options.getHostnameResolverOptions(); + DnsServerAddresses nameServerAddresses = DnsServerAddresses.defaultAddresses(); + if (hostnameResolver != null) { + List dnsServers = hostnameResolver.getServers(); if (dnsServers != null && dnsServers.size() > 0) { List serverList = new ArrayList<>(); for (String dnsServer : dnsServers) { @@ -171,11 +172,17 @@ public class VertxImpl implements VertxInternal, MetricsProvider { throw new VertxException(e); } } - builder.nameServerAddresses(DnsServerAddresses.sequential(serverList)); + nameServerAddresses = DnsServerAddresses.sequential(serverList); + builder.nameServerAddresses(nameServerAddresses); } - builder.optResourceEnabled(dnsOptions.isOptResourceEnabled()); - } - resolver = builder.build(); + builder.optResourceEnabled(hostnameResolver.isOptResourceEnabled()); + builder.ttl(hostnameResolver.getCacheMinTimeToLive(), hostnameResolver.getCacheMaxTimeToLive()); + builder.negativeTtl(hostnameResolver.getCacheNegativeTimeToLive()); + builder.queryTimeoutMillis(hostnameResolver.getQueryTimeout()); + builder.maxQueriesPerResolve(hostnameResolver.getMaxQueries()); + builder.recursionDesired(hostnameResolver.getRdFlag()); + } + this.resolver = builder.build(); this.fileResolver = new FileResolver(this); this.deploymentManager = new DeploymentManager(this); diff --git a/src/test/java/io/vertx/test/core/DnsResolverTest.java b/src/test/java/io/vertx/test/core/DnsResolverTest.java deleted file mode 100644 index e473c0f13aa..00000000000 --- a/src/test/java/io/vertx/test/core/DnsResolverTest.java +++ /dev/null @@ -1,139 +0,0 @@ -/* - * Copyright (c) 2011-2013 The original author or authors - * ------------------------------------------------------ - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * - * The Eclipse Public License is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * The Apache License v2.0 is available at - * http://www.opensource.org/licenses/apache2.0.php - * - * You may elect to redistribute this code under either of these licenses. - */ - -package io.vertx.test.core; - -import io.vertx.core.VertxOptions; -import io.vertx.core.buffer.Buffer; -import io.vertx.core.http.HttpClient; -import io.vertx.core.http.HttpServer; -import io.vertx.core.impl.VertxImpl; -import io.vertx.core.net.NetClient; -import io.vertx.core.net.NetServer; -import io.vertx.test.fakedns.FakeDNSServer; -import org.junit.Test; - -import java.net.InetSocketAddress; -import java.net.UnknownHostException; -import java.util.concurrent.CountDownLatch; - -/** - * @author Julien Viet - */ -public class DnsResolverTest extends VertxTestBase { - - private FakeDNSServer dnsServer; - private InetSocketAddress dnsServerAddress; - - @Override - public void setUp() throws Exception { - dnsServer = FakeDNSServer.testResolveASameServer("127.0.0.1"); - dnsServer.start(); - dnsServerAddress = (InetSocketAddress) dnsServer.getTransports()[0].getAcceptor().getLocalAddress(); - super.setUp(); - } - - @Override - protected void tearDown() throws Exception { - if (dnsServer.isStarted()) { - dnsServer.stop(); - } - } - - @Override - protected VertxOptions getOptions() { - VertxOptions options = super.getOptions(); - options.getHostnameResolverOptions().addServer(dnsServerAddress.getAddress().getHostAddress() + ":" + dnsServerAddress.getPort()); - options.getHostnameResolverOptions().setOptResourceEnabled(false); - return options; - } - - @Test - public void testAsyncResolve() throws Exception { - ((VertxImpl)vertx).resolveHostname("vertx.io", onSuccess(resolved -> { - assertEquals("127.0.0.1", resolved.getHostAddress()); - testComplete(); - })); - await(); - } - - @Test - public void testAsyncResolveFail() throws Exception { - ((VertxImpl)vertx).resolveHostname("vertx.com", onFailure(failure -> { - assertEquals(UnknownHostException.class, failure.getClass()); - testComplete(); - })); - await(); - } - - @Test - public void testNet() throws Exception { - NetClient client = vertx.createNetClient(); - NetServer server = vertx.createNetServer().connectHandler(so -> { - so.handler(buff -> { - so.write(buff); - so.close(); - }); - }); - try { - CountDownLatch listenLatch = new CountDownLatch(1); - server.listen(1234, "vertx.io", onSuccess(s -> { - listenLatch.countDown(); - })); - awaitLatch(listenLatch); - client.connect(1234, "vertx.io", onSuccess(so -> { - Buffer buffer = Buffer.buffer(); - so.handler(buffer::appendBuffer); - so.closeHandler(v -> { - assertEquals(Buffer.buffer("foo"), buffer); - testComplete(); - }); - so.write(Buffer.buffer("foo")); - })); - await(); - } finally { - client.close(); - server.close(); - } - } - - @Test - public void testHttp() throws Exception { - HttpClient client = vertx.createHttpClient(); - HttpServer server = vertx.createHttpServer().requestHandler(req -> { - req.response().end("foo"); - }); - try { - CountDownLatch listenLatch = new CountDownLatch(1); - server.listen(8080, "vertx.io", onSuccess(s -> { - listenLatch.countDown(); - })); - awaitLatch(listenLatch); - client.getNow(8080, "vertx.io", "/somepath", resp -> { - Buffer buffer = Buffer.buffer(); - resp.handler(buffer::appendBuffer); - resp.endHandler(v -> { - assertEquals(Buffer.buffer("foo"), buffer); - testComplete(); - }); - }); - await(); - } finally { - client.close(); - server.close(); - } - } -} diff --git a/src/test/java/io/vertx/test/core/HostnameResolverTest.java b/src/test/java/io/vertx/test/core/HostnameResolverTest.java new file mode 100644 index 00000000000..d7eaf8fdbc4 --- /dev/null +++ b/src/test/java/io/vertx/test/core/HostnameResolverTest.java @@ -0,0 +1,229 @@ +/* + * Copyright (c) 2011-2013 The original author or authors + * ------------------------------------------------------ + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Apache License v2.0 which accompanies this distribution. + * + * The Eclipse Public License is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * The Apache License v2.0 is available at + * http://www.opensource.org/licenses/apache2.0.php + * + * You may elect to redistribute this code under either of these licenses. + */ + +package io.vertx.test.core; + +import io.vertx.core.VertxOptions; +import io.vertx.core.buffer.Buffer; +import io.vertx.core.dns.HostnameResolverOptions; +import io.vertx.core.http.HttpClient; +import io.vertx.core.http.HttpServer; +import io.vertx.core.impl.VertxImpl; +import io.vertx.core.json.JsonObject; +import io.vertx.core.net.NetClient; +import io.vertx.core.net.NetServer; +import io.vertx.test.fakedns.FakeDNSServer; +import org.junit.Test; + +import java.net.InetSocketAddress; +import java.net.UnknownHostException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.concurrent.CountDownLatch; + +/** + * @author Julien Viet + */ +public class HostnameResolverTest extends VertxTestBase { + + private FakeDNSServer dnsServer; + private InetSocketAddress dnsServerAddress; + + @Override + public void setUp() throws Exception { + dnsServer = FakeDNSServer.testResolveASameServer("127.0.0.1"); + dnsServer.start(); + dnsServerAddress = (InetSocketAddress) dnsServer.getTransports()[0].getAcceptor().getLocalAddress(); + super.setUp(); + } + + @Override + protected void tearDown() throws Exception { + if (dnsServer.isStarted()) { + dnsServer.stop(); + } + } + + @Override + protected VertxOptions getOptions() { + VertxOptions options = super.getOptions(); + options.getHostnameResolverOptions().addServer(dnsServerAddress.getAddress().getHostAddress() + ":" + dnsServerAddress.getPort()); + options.getHostnameResolverOptions().setOptResourceEnabled(false); + return options; + } + + @Test + public void testAsyncResolve() throws Exception { + ((VertxImpl)vertx).resolveHostname("vertx.io", onSuccess(resolved -> { + assertEquals("127.0.0.1", resolved.getHostAddress()); + testComplete(); + })); + await(); + } + + @Test + public void testAsyncResolveFail() throws Exception { + ((VertxImpl)vertx).resolveHostname("vertx.com", onFailure(failure -> { + assertEquals(UnknownHostException.class, failure.getClass()); + testComplete(); + })); + await(); + } + + @Test + public void testNet() throws Exception { + NetClient client = vertx.createNetClient(); + NetServer server = vertx.createNetServer().connectHandler(so -> { + so.handler(buff -> { + so.write(buff); + so.close(); + }); + }); + try { + CountDownLatch listenLatch = new CountDownLatch(1); + server.listen(1234, "vertx.io", onSuccess(s -> { + listenLatch.countDown(); + })); + awaitLatch(listenLatch); + client.connect(1234, "vertx.io", onSuccess(so -> { + Buffer buffer = Buffer.buffer(); + so.handler(buffer::appendBuffer); + so.closeHandler(v -> { + assertEquals(Buffer.buffer("foo"), buffer); + testComplete(); + }); + so.write(Buffer.buffer("foo")); + })); + await(); + } finally { + client.close(); + server.close(); + } + } + + @Test + public void testHttp() throws Exception { + HttpClient client = vertx.createHttpClient(); + HttpServer server = vertx.createHttpServer().requestHandler(req -> { + req.response().end("foo"); + }); + try { + CountDownLatch listenLatch = new CountDownLatch(1); + server.listen(8080, "vertx.io", onSuccess(s -> { + listenLatch.countDown(); + })); + awaitLatch(listenLatch); + client.getNow(8080, "vertx.io", "/somepath", resp -> { + Buffer buffer = Buffer.buffer(); + resp.handler(buffer::appendBuffer); + resp.endHandler(v -> { + assertEquals(Buffer.buffer("foo"), buffer); + testComplete(); + }); + }); + await(); + } finally { + client.close(); + server.close(); + } + } + + @Test + public void testOptions() { + HostnameResolverOptions options = new HostnameResolverOptions(); + assertEquals(HostnameResolverOptions.DEFAULT_OPT_RESOURCE_ENABLED, options.isOptResourceEnabled()); + assertEquals(HostnameResolverOptions.DEFAULT_SERVERS, options.getServers()); + assertEquals(HostnameResolverOptions.DEFAULT_CACHE_MIN_TIME_TO_LIVE, options.getCacheMinTimeToLive()); + assertEquals(HostnameResolverOptions.DEFAULT_CACHE_MAX_TIME_TO_LIVE, options.getCacheMaxTimeToLive()); + assertEquals(HostnameResolverOptions.DEFAULT_CACHE_NEGATIVE_TIME_TO_LIVE, options.getCacheNegativeTimeToLive()); + assertEquals(HostnameResolverOptions.DEFAULT_QUERY_TIMEOUT, options.getQueryTimeout()); + assertEquals(HostnameResolverOptions.DEFAULT_MAX_QUERIES, options.getMaxQueries()); + assertEquals(HostnameResolverOptions.DEFAULT_RD_FLAG, options.getRdFlag()); + + boolean optResourceEnabled = TestUtils.randomBoolean(); + List servers = Arrays.asList("1.2.3.4", "5.6.7.8"); + int minTTL = TestUtils.randomPositiveInt(); + int maxTTL = minTTL + TestUtils.randomPositiveInt(); + int negativeTTL = TestUtils.randomPositiveInt(); + int queryTimeout = 1 + TestUtils.randomPositiveInt(); + int maxQueries = 1 + TestUtils.randomPositiveInt(); + boolean rdFlag = TestUtils.randomBoolean(); + + assertSame(options, options.setOptResourceEnabled(optResourceEnabled)); + assertSame(options, options.setServers(new ArrayList<>(servers))); + assertSame(options, options.setCacheMinTimeToLive(minTTL)); + assertSame(options, options.setCacheMaxTimeToLive(maxTTL)); + assertSame(options, options.setCacheNegativeTimeToLive(negativeTTL)); + assertSame(options, options.setQueryTimeout(queryTimeout)); + assertSame(options, options.setMaxQueries(maxQueries)); + assertSame(options, options.setRdFlag(rdFlag)); + + assertEquals(optResourceEnabled, options.isOptResourceEnabled()); + assertEquals(servers, options.getServers()); + assertEquals(minTTL, options.getCacheMinTimeToLive()); + assertEquals(maxTTL, options.getCacheMaxTimeToLive()); + assertEquals(negativeTTL, options.getCacheNegativeTimeToLive()); + assertEquals(queryTimeout, options.getQueryTimeout()); + assertEquals(maxQueries, options.getMaxQueries()); + assertEquals(rdFlag, options.getRdFlag()); + + // Test copy and json copy + HostnameResolverOptions copy = new HostnameResolverOptions(options); + HostnameResolverOptions jsonCopy = new HostnameResolverOptions(options.toJson()); + + options.setOptResourceEnabled(HostnameResolverOptions.DEFAULT_OPT_RESOURCE_ENABLED); + options.getServers().clear(); + options.setCacheMinTimeToLive(HostnameResolverOptions.DEFAULT_CACHE_MIN_TIME_TO_LIVE); + options.setCacheMaxTimeToLive(HostnameResolverOptions.DEFAULT_CACHE_MAX_TIME_TO_LIVE); + options.setCacheNegativeTimeToLive(HostnameResolverOptions.DEFAULT_CACHE_NEGATIVE_TIME_TO_LIVE); + options.setQueryTimeout(HostnameResolverOptions.DEFAULT_QUERY_TIMEOUT); + options.setMaxQueries(HostnameResolverOptions.DEFAULT_MAX_QUERIES); + options.setRdFlag(HostnameResolverOptions.DEFAULT_RD_FLAG); + + assertEquals(optResourceEnabled, copy.isOptResourceEnabled()); + assertEquals(servers, copy.getServers()); + assertEquals(minTTL, copy.getCacheMinTimeToLive()); + assertEquals(maxTTL, copy.getCacheMaxTimeToLive()); + assertEquals(negativeTTL, copy.getCacheNegativeTimeToLive()); + assertEquals(queryTimeout, copy.getQueryTimeout()); + assertEquals(maxQueries, copy.getMaxQueries()); + assertEquals(rdFlag, copy.getRdFlag()); + + assertEquals(optResourceEnabled, jsonCopy.isOptResourceEnabled()); + assertEquals(servers, jsonCopy.getServers()); + assertEquals(minTTL, jsonCopy.getCacheMinTimeToLive()); + assertEquals(maxTTL, jsonCopy.getCacheMaxTimeToLive()); + assertEquals(negativeTTL, jsonCopy.getCacheNegativeTimeToLive()); + assertEquals(queryTimeout, jsonCopy.getQueryTimeout()); + assertEquals(maxQueries, jsonCopy.getMaxQueries()); + assertEquals(rdFlag, jsonCopy.getRdFlag()); + } + + @Test + public void testDefaultJsonOptions() { + HostnameResolverOptions options = new HostnameResolverOptions(new JsonObject()); + assertEquals(HostnameResolverOptions.DEFAULT_OPT_RESOURCE_ENABLED, options.isOptResourceEnabled()); + assertEquals(HostnameResolverOptions.DEFAULT_SERVERS, options.getServers()); + assertEquals(HostnameResolverOptions.DEFAULT_CACHE_MIN_TIME_TO_LIVE, options.getCacheMinTimeToLive()); + assertEquals(HostnameResolverOptions.DEFAULT_CACHE_MAX_TIME_TO_LIVE, options.getCacheMaxTimeToLive()); + assertEquals(HostnameResolverOptions.DEFAULT_CACHE_NEGATIVE_TIME_TO_LIVE, options.getCacheNegativeTimeToLive()); + assertEquals(HostnameResolverOptions.DEFAULT_QUERY_TIMEOUT, options.getQueryTimeout()); + assertEquals(HostnameResolverOptions.DEFAULT_MAX_QUERIES, options.getMaxQueries()); + assertEquals(HostnameResolverOptions.DEFAULT_RD_FLAG, options.getRdFlag()); + } +} diff --git a/src/test/java/io/vertx/test/core/NetTest.java b/src/test/java/io/vertx/test/core/NetTest.java index f6984a2453d..86f2dc237d7 100644 --- a/src/test/java/io/vertx/test/core/NetTest.java +++ b/src/test/java/io/vertx/test/core/NetTest.java @@ -693,7 +693,6 @@ public void testSocketAddress() throws Exception { assertIllegalArgumentException(() -> new SocketAddressImpl(65536, "someHost")); } - @Repeat(times = 100) @Test public void testEchoBytes() { Buffer sent = TestUtils.randomBuffer(100);