diff --git a/src/main/java/io/vertx/core/impl/AddressResolver.java b/src/main/java/io/vertx/core/impl/AddressResolver.java index 61c303ce51e..99889cafe96 100644 --- a/src/main/java/io/vertx/core/impl/AddressResolver.java +++ b/src/main/java/io/vertx/core/impl/AddressResolver.java @@ -27,6 +27,7 @@ import java.net.InetAddress; import java.net.InetSocketAddress; import java.nio.file.Files; +import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -87,10 +88,27 @@ public void resolveHostname(String hostname, Handler> r } public io.netty.util.concurrent.Future resolveHostname(EventLoop eventLoop, String hostname) { - io.netty.resolver.AddressResolver resolver = resolverGroup.getResolver(eventLoop); + io.netty.resolver.AddressResolver resolver = getResolver(eventLoop); return resolver.resolve(InetSocketAddress.createUnresolved(hostname, 0)); } + public void resolveHostnameAll(String hostname, Handler>> resultHandler) { + ContextInternal context = (ContextInternal) vertx.getOrCreateContext(); + io.netty.util.concurrent.Future> fut = resolveHostnameAll(context.nettyEventLoop(), hostname); + PromiseInternal> promise = context.promise(); + fut.addListener(promise); + promise.future().onComplete(resultHandler); + } + + public io.netty.util.concurrent.Future> resolveHostnameAll(EventLoop eventLoop, String hostname) { + io.netty.resolver.AddressResolver resolver = getResolver(eventLoop); + return resolver.resolveAll(InetSocketAddress.createUnresolved(hostname, 0)); + } + + public io.netty.resolver.AddressResolver getResolver(EventLoop eventLoop){ + return resolverGroup.getResolver(eventLoop); + } + AddressResolverGroup nettyAddressResolverGroup() { return resolverGroup; } diff --git a/src/test/java/io/vertx/core/dns/HostnameResolutionTest.java b/src/test/java/io/vertx/core/dns/HostnameResolutionTest.java index 1c395d825f8..fcefa5d7999 100644 --- a/src/test/java/io/vertx/core/dns/HostnameResolutionTest.java +++ b/src/test/java/io/vertx/core/dns/HostnameResolutionTest.java @@ -31,13 +31,7 @@ import io.vertx.test.core.TestUtils; import io.vertx.test.core.VertxTestBase; import io.vertx.test.fakedns.FakeDNSServer; -import org.apache.directory.server.dns.DnsException; -import org.apache.directory.server.dns.messages.QuestionRecord; -import org.apache.directory.server.dns.messages.RecordClass; -import org.apache.directory.server.dns.messages.RecordType; import org.apache.directory.server.dns.messages.ResourceRecord; -import org.apache.directory.server.dns.store.DnsAttribute; -import org.apache.directory.server.dns.store.RecordStore; import org.junit.Test; import java.io.File; @@ -50,6 +44,7 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicReference; import java.util.function.Function; +import java.util.stream.Collectors; /** * @author Julien Viet @@ -93,14 +88,9 @@ public void testAsyncResolve() throws Exception { } @Test - public void testAsyncResolveTruncated() throws Exception { + public void testAsyncResolveTruncated() { - dnsServer.store(new RecordStore() { - @Override - public Set getRecords(QuestionRecord question) throws DnsException { - return Collections.singleton(new FakeDNSServer.VertxResourceRecord("vertx.io", "127.0.0.1").setTruncated(true)); - } - }); + dnsServer.store(question -> Collections.singleton(new FakeDNSServer.VertxResourceRecord("vertx.io", "127.0.0.1").setTruncated(true))); ((VertxImpl) vertx).resolveAddress("vertx.io", onSuccess(resolved -> { @@ -111,7 +101,7 @@ public Set getRecords(QuestionRecord question) throws DnsExcepti } @Test - public void testAsyncResolveFail() throws Exception { + public void testAsyncResolveFail() { ((VertxImpl) vertx).resolveAddress("vertx.com", onFailure(failure -> { assertTrue("Was expecting " + failure + " to be an instanceof UnknownHostException", failure instanceof UnknownHostException); testComplete(); @@ -810,6 +800,28 @@ public void testResolveLocalhost() { await(); } + @Test + public void testResolveAll() { + + List expectedIPAddresses = Arrays.asList("127.0.0.2", "127.0.0.3"); + + addRecordsToStore(dnsServer, "fakeAddress.com", expectedIPAddresses.toArray(new String[0])); + + AddressResolver resolver = new AddressResolver(vertx, getAddressResolverOptions()); + + resolver.resolveHostnameAll("fakeAddress.com", res -> { + if (res.succeeded()) { + assertEquals(expectedIPAddresses, res.result().stream().map(e -> e.getAddress().getHostAddress().toLowerCase(Locale.ENGLISH)).collect(Collectors.toList())); + testComplete(); + } else { + fail(res.cause()); + } + }); + + await(); + + } + @Test public void testRotationalServerSelection() throws Exception { testServerSelection(true, false); @@ -884,14 +896,22 @@ public void testAddressSelectionWithoutRoundRobin() throws Exception { testAddressSelection(getAddressResolverOptions().setRoundRobinInetAddress(false), 1); } - private void testAddressSelection(AddressResolverOptions options, int expected) throws Exception { - Function createRecord = ipAddress -> new FakeDNSServer.VertxResourceRecord("vertx.io", ipAddress); + private void addRecordsToStore(FakeDNSServer server,String domainName,String ...entries){ + + final Set records = new LinkedHashSet<>(); - Set records = new LinkedHashSet<>(); - records.add(createRecord.apply("127.0.0.1")); - records.add(createRecord.apply("127.0.0.2")); + Function createRecord = ipAddress -> new FakeDNSServer.VertxResourceRecord(domainName, ipAddress); + + for (String e : entries){ + records.add(createRecord.apply(e)); + } + + server.store(x -> records); + } + + private void testAddressSelection(AddressResolverOptions options, int expected) throws Exception { - dnsServer.store(question -> records); + addRecordsToStore(dnsServer,"vertx.io","127.0.0.1","127.0.0.2"); AddressResolver resolver = new AddressResolver(vertx, options); Set resolved = Collections.synchronizedSet(new HashSet<>());