Search in sources :

Example 6 with AddressResolverOptions

use of io.vertx.core.dns.AddressResolverOptions in project vert.x by eclipse.

the class HostnameResolutionTest method testSearchDomainWithNdots2.

@Test
public void testSearchDomainWithNdots2() throws Exception {
    Map<String, String> records = new HashMap<>();
    records.put("host1.sub.foo.com", "127.0.0.1");
    records.put("host2.sub.foo.com", "127.0.0.2");
    records.put("host2.sub", "127.0.0.3");
    dnsServer.stop();
    dnsServer = FakeDNSServer.testResolveA(records);
    dnsServer.start();
    VertxInternal vertx = (VertxInternal) vertx(new VertxOptions().setAddressResolverOptions(new AddressResolverOptions().addServer(dnsServerAddress.getAddress().getHostAddress() + ":" + dnsServerAddress.getPort()).setOptResourceEnabled(false).addSearchDomain("foo.com").setNdots(2)));
    CountDownLatch latch1 = new CountDownLatch(1);
    vertx.resolveAddress("host1.sub", onSuccess(resolved -> {
        assertEquals("127.0.0.1", resolved.getHostAddress());
        latch1.countDown();
    }));
    awaitLatch(latch1);
    // "host2.sub" is resolved with the foo.com search domain as ndots = 2
    CountDownLatch latch2 = new CountDownLatch(1);
    vertx.resolveAddress("host2.sub", onSuccess(resolved -> {
        assertEquals("127.0.0.2", resolved.getHostAddress());
        latch2.countDown();
    }));
    awaitLatch(latch2);
}
Also used : AddressResolverOptions(io.vertx.core.dns.AddressResolverOptions) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) VertxException(io.vertx.core.VertxException) Arrays(java.util.Arrays) HttpServer(io.vertx.core.http.HttpServer) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) AddressResolverOptions(io.vertx.core.dns.AddressResolverOptions) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) InetAddress(java.net.InetAddress) Locale(java.util.Locale) Map(java.util.Map) JsonObject(io.vertx.core.json.JsonObject) FakeDNSServer(io.vertx.test.fakedns.FakeDNSServer) NetClient(io.vertx.core.net.NetClient) VertxImpl(io.vertx.core.impl.VertxImpl) VertxInternal(io.vertx.core.impl.VertxInternal) ChannelInitializer(io.netty.channel.ChannelInitializer) AddressResolver(io.vertx.core.impl.AddressResolver) VertxOptions(io.vertx.core.VertxOptions) Test(org.junit.Test) InetSocketAddress(java.net.InetSocketAddress) UnknownHostException(java.net.UnknownHostException) File(java.io.File) ChannelFuture(io.netty.channel.ChannelFuture) Channel(io.netty.channel.Channel) TimeUnit(java.util.concurrent.TimeUnit) Bootstrap(io.netty.bootstrap.Bootstrap) CountDownLatch(java.util.concurrent.CountDownLatch) NetServerOptions(io.vertx.core.net.NetServerOptions) List(java.util.List) Buffer(io.vertx.core.buffer.Buffer) NetServer(io.vertx.core.net.NetServer) Collections(java.util.Collections) HttpClient(io.vertx.core.http.HttpClient) VertxInternal(io.vertx.core.impl.VertxInternal) HashMap(java.util.HashMap) CountDownLatch(java.util.concurrent.CountDownLatch) VertxOptions(io.vertx.core.VertxOptions) Test(org.junit.Test)

Example 7 with AddressResolverOptions

use of io.vertx.core.dns.AddressResolverOptions in project vert.x by eclipse.

the class HostnameResolutionTest method testServerSelection.

private void testServerSelection(boolean rotateServers) throws Exception {
    int num = VertxOptions.DEFAULT_EVENT_LOOP_POOL_SIZE + 4;
    List<FakeDNSServer> dnsServers = new ArrayList<>();
    try {
        for (int index = 1; index <= num; index++) {
            FakeDNSServer server = new FakeDNSServer(FakeDNSServer.A_store(Collections.singletonMap("vertx.io", "127.0.0." + index)));
            server.port(FakeDNSServer.PORT + index);
            server.start();
            dnsServers.add(server);
        }
        AddressResolverOptions options = new AddressResolverOptions();
        options.setRotateServers(rotateServers);
        options.setOptResourceEnabled(false);
        for (int i = 0; i < num; i++) {
            InetSocketAddress dnsServerAddress = dnsServers.get(i).localAddress();
            options.addServer(dnsServerAddress.getAddress().getHostAddress() + ":" + dnsServerAddress.getPort());
        }
        AddressResolver resolver = new AddressResolver((VertxImpl) vertx, options);
        for (int i = 0; i < num; i++) {
            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();
            int expected;
            if (rotateServers) {
                // Expect from [1,...,DEFAULT_EVENT_LOOP_POOL_SIZE[ as we round robin but then we cache the result
                // so it checks that round robin cross event loops
                expected = 1 + i % VertxOptions.DEFAULT_EVENT_LOOP_POOL_SIZE;
            } else {
                expected = 1;
            }
            assertEquals("127.0.0." + expected, resolved);
        }
    } finally {
        dnsServers.forEach(FakeDNSServer::stop);
    }
}
Also used : AddressResolverOptions(io.vertx.core.dns.AddressResolverOptions) AddressResolver(io.vertx.core.impl.AddressResolver) CompletableFuture(java.util.concurrent.CompletableFuture) InetSocketAddress(java.net.InetSocketAddress) ArrayList(java.util.ArrayList) InetAddress(java.net.InetAddress) FakeDNSServer(io.vertx.test.fakedns.FakeDNSServer)

Example 8 with AddressResolverOptions

use of io.vertx.core.dns.AddressResolverOptions in project vert.x by eclipse.

the class HostnameResolutionTest method testNetSearchDomain.

@Test
public void testNetSearchDomain() throws Exception {
    Map<String, String> records = new HashMap<>();
    records.put("host1.foo.com", "127.0.0.1");
    dnsServer.stop();
    dnsServer = FakeDNSServer.testResolveA(records);
    dnsServer.start();
    vertx.close();
    vertx = vertx(new VertxOptions().setAddressResolverOptions(new AddressResolverOptions().addServer(dnsServerAddress.getAddress().getHostAddress() + ":" + dnsServerAddress.getPort()).setOptResourceEnabled(false).addSearchDomain("foo.com")));
    testNet("host1");
}
Also used : AddressResolverOptions(io.vertx.core.dns.AddressResolverOptions) HashMap(java.util.HashMap) VertxOptions(io.vertx.core.VertxOptions) Test(org.junit.Test)

Example 9 with AddressResolverOptions

use of io.vertx.core.dns.AddressResolverOptions 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();
    }
}
Also used : AddressResolverOptions(io.vertx.core.dns.AddressResolverOptions) AddressResolver(io.vertx.core.impl.AddressResolver) CompletableFuture(java.util.concurrent.CompletableFuture) InetSocketAddress(java.net.InetSocketAddress) InetAddress(java.net.InetAddress) FakeDNSServer(io.vertx.test.fakedns.FakeDNSServer) Test(org.junit.Test)

Example 10 with AddressResolverOptions

use of io.vertx.core.dns.AddressResolverOptions 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();
}
Also used : AddressResolverOptions(io.vertx.core.dns.AddressResolverOptions) AddressResolver(io.vertx.core.impl.AddressResolver) Test(org.junit.Test)

Aggregations

AddressResolverOptions (io.vertx.core.dns.AddressResolverOptions)16 Test (org.junit.Test)15 VertxOptions (io.vertx.core.VertxOptions)11 AddressResolver (io.vertx.core.impl.AddressResolver)11 InetAddress (java.net.InetAddress)11 CompletableFuture (java.util.concurrent.CompletableFuture)11 FakeDNSServer (io.vertx.test.fakedns.FakeDNSServer)10 InetSocketAddress (java.net.InetSocketAddress)10 ArrayList (java.util.ArrayList)10 VertxException (io.vertx.core.VertxException)9 VertxInternal (io.vertx.core.impl.VertxInternal)9 JsonObject (io.vertx.core.json.JsonObject)9 NetClient (io.vertx.core.net.NetClient)9 NetServer (io.vertx.core.net.NetServer)9 NetServerOptions (io.vertx.core.net.NetServerOptions)9 HashMap (java.util.HashMap)9 Bootstrap (io.netty.bootstrap.Bootstrap)8 Channel (io.netty.channel.Channel)8 ChannelFuture (io.netty.channel.ChannelFuture)8 ChannelInitializer (io.netty.channel.ChannelInitializer)8