Skip to content

Commit

Permalink
feat(Address Resolver): expose resolveAll method
Browse files Browse the repository at this point in the history
  • Loading branch information
yeikel authored and vietj committed Jul 27, 2022
1 parent 3d27653 commit e67b947
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 21 deletions.
20 changes: 19 additions & 1 deletion src/main/java/io/vertx/core/impl/AddressResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -87,10 +88,27 @@ public void resolveHostname(String hostname, Handler<AsyncResult<InetAddress>> r
}

public io.netty.util.concurrent.Future<InetSocketAddress> resolveHostname(EventLoop eventLoop, String hostname) {
io.netty.resolver.AddressResolver<InetSocketAddress> resolver = resolverGroup.getResolver(eventLoop);
io.netty.resolver.AddressResolver<InetSocketAddress> resolver = getResolver(eventLoop);
return resolver.resolve(InetSocketAddress.createUnresolved(hostname, 0));
}

public void resolveHostnameAll(String hostname, Handler<AsyncResult<List<InetSocketAddress>>> resultHandler) {
ContextInternal context = (ContextInternal) vertx.getOrCreateContext();
io.netty.util.concurrent.Future<List<InetSocketAddress>> fut = resolveHostnameAll(context.nettyEventLoop(), hostname);
PromiseInternal<List<InetSocketAddress>> promise = context.promise();
fut.addListener(promise);
promise.future().onComplete(resultHandler);
}

public io.netty.util.concurrent.Future<List<InetSocketAddress>> resolveHostnameAll(EventLoop eventLoop, String hostname) {
io.netty.resolver.AddressResolver<InetSocketAddress> resolver = getResolver(eventLoop);
return resolver.resolveAll(InetSocketAddress.createUnresolved(hostname, 0));
}

public io.netty.resolver.AddressResolver<InetSocketAddress> getResolver(EventLoop eventLoop){
return resolverGroup.getResolver(eventLoop);
}

AddressResolverGroup<InetSocketAddress> nettyAddressResolverGroup() {
return resolverGroup;
}
Expand Down
60 changes: 40 additions & 20 deletions src/test/java/io/vertx/core/dns/HostnameResolutionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 <a href="mailto:[email protected]">Julien Viet</a>
Expand Down Expand Up @@ -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<ResourceRecord> 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 -> {
Expand All @@ -111,7 +101,7 @@ public Set<ResourceRecord> 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();
Expand Down Expand Up @@ -810,6 +800,28 @@ public void testResolveLocalhost() {
await();
}

@Test
public void testResolveAll() {

List<String> 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);
Expand Down Expand Up @@ -884,14 +896,22 @@ public void testAddressSelectionWithoutRoundRobin() throws Exception {
testAddressSelection(getAddressResolverOptions().setRoundRobinInetAddress(false), 1);
}

private void testAddressSelection(AddressResolverOptions options, int expected) throws Exception {
Function<String, ResourceRecord> createRecord = ipAddress -> new FakeDNSServer.VertxResourceRecord("vertx.io", ipAddress);
private void addRecordsToStore(FakeDNSServer server,String domainName,String ...entries){

final Set<ResourceRecord> records = new LinkedHashSet<>();

Set<ResourceRecord> records = new LinkedHashSet<>();
records.add(createRecord.apply("127.0.0.1"));
records.add(createRecord.apply("127.0.0.2"));
Function<String, ResourceRecord> 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<String> resolved = Collections.synchronizedSet(new HashSet<>());
Expand Down

0 comments on commit e67b947

Please sign in to comment.