use of io.vertx.core.impl.AddressResolver in project vert.x by eclipse.
the class HostnameResolutionTest method testServerFailover.
@Test
public void testServerFailover() throws Exception {
FakeDNSServer server = new FakeDNSServer(FakeDNSServer.A_store(Collections.singletonMap("vertx.io", "127.0.0.1"))).port(FakeDNSServer.PORT + 2);
try {
AddressResolverOptions options = new AddressResolverOptions();
options.setOptResourceEnabled(false);
// 2 + 2
options.setMaxQueries(4);
server.start();
InetSocketAddress dnsServerAddress = server.localAddress();
// First server is unreachable
options.addServer(dnsServerAddress.getAddress().getHostAddress() + ":" + (FakeDNSServer.PORT + 1));
// Second server is the failed over server
options.addServer(dnsServerAddress.getAddress().getHostAddress() + ":" + dnsServerAddress.getPort());
AddressResolver resolver = new AddressResolver((VertxImpl) vertx, options);
CompletableFuture<InetAddress> result = new CompletableFuture<>();
resolver.resolveHostname("vertx.io", ar -> {
if (ar.succeeded()) {
result.complete(ar.result());
} else {
result.completeExceptionally(ar.cause());
}
});
String resolved = result.get(10, TimeUnit.SECONDS).getHostAddress();
assertEquals("127.0.0.1", resolved);
} finally {
server.stop();
}
}
use of io.vertx.core.impl.AddressResolver in project vert.x by eclipse.
the class HostnameResolutionTest method testResolveLocalhost.
@Test
public void testResolveLocalhost() {
AddressResolver resolver = new AddressResolver((VertxImpl) vertx, new AddressResolverOptions());
resolver.resolveHostname("LOCALHOST", res -> {
if (res.succeeded()) {
assertEquals("localhost", res.result().getHostName().toLowerCase(Locale.ENGLISH));
resolver.resolveHostname("LocalHost", res2 -> {
if (res2.succeeded()) {
assertEquals("localhost", res2.result().getHostName().toLowerCase(Locale.ENGLISH));
resolver.resolveHostname("localhost", res3 -> {
if (res3.succeeded()) {
assertEquals("localhost", res3.result().getHostName().toLowerCase(Locale.ENGLISH));
testComplete();
} else {
fail(res3.cause());
}
});
} else {
fail(res2.cause());
}
});
} else {
fail(res.cause());
}
});
await();
}
use of io.vertx.core.impl.AddressResolver in project vert.x by eclipse.
the class DatagramSocketImpl method send.
@Override
public Future<Void> send(Buffer packet, int port, String host) {
Objects.requireNonNull(packet, "no null packet accepted");
Objects.requireNonNull(host, "no null host accepted");
if (port < 0 || port > 65535) {
throw new IllegalArgumentException("port out of range:" + port);
}
AddressResolver resolver = context.owner().addressResolver();
PromiseInternal<Void> promise = context.promise();
io.netty.util.concurrent.Future<InetSocketAddress> f1 = resolver.resolveHostname(context.nettyEventLoop(), host);
f1.addListener((GenericFutureListener<io.netty.util.concurrent.Future<InetSocketAddress>>) res1 -> {
if (res1.isSuccess()) {
ChannelFuture f2 = channel.writeAndFlush(new DatagramPacket(packet.getByteBuf(), new InetSocketAddress(f1.getNow().getAddress(), port)));
if (metrics != null) {
f2.addListener(fut -> {
if (fut.isSuccess()) {
metrics.bytesWritten(null, SocketAddress.inetSocketAddress(port, host), packet.length());
}
});
}
f2.addListener(promise);
} else {
promise.fail(res1.cause());
}
});
return promise.future();
}
use of io.vertx.core.impl.AddressResolver in project vert.x by eclipse.
the class HostnameResolutionTest method testAddressSelection.
private void testAddressSelection(AddressResolverOptions options, int expected) throws Exception {
Function<String, ResourceRecord> createRecord = ipAddress -> new ResourceRecord() {
@Override
public String getDomainName() {
return "vertx.io";
}
@Override
public RecordType getRecordType() {
return RecordType.A;
}
@Override
public RecordClass getRecordClass() {
return RecordClass.IN;
}
@Override
public int getTimeToLive() {
return 100;
}
@Override
public String get(String s) {
return DnsAttribute.IP_ADDRESS.equals(s) ? ipAddress : null;
}
};
Set<ResourceRecord> records = new LinkedHashSet<>();
records.add(createRecord.apply("127.0.0.1"));
records.add(createRecord.apply("127.0.0.2"));
dnsServer.store(question -> records);
AddressResolver resolver = new AddressResolver(vertx, options);
Set<String> resolved = Collections.synchronizedSet(new HashSet<>());
// due to the random nature of netty's round robin algorithm
// the below outcome is generally non-deterministic and will fail once in about 2^100 runs (virtually never)
CountDownLatch latch = new CountDownLatch(100);
for (int i = 0; i < 100; i++) {
resolver.resolveHostname("vertx.io", onSuccess(inetAddress -> {
resolved.add(inetAddress.getHostAddress());
latch.countDown();
}));
}
awaitLatch(latch);
assertEquals(expected, resolved.size());
}
Aggregations