Search in sources :

Example 1 with FakeDNSServer

use of io.vertx.test.fakedns.FakeDNSServer in project vert.x by eclipse.

the class HostnameResolutionTest method testServerSelection.

private void testServerSelection(boolean rotateServers, boolean cache) 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().store(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);
        if (!cache) {
            options.setCacheMaxTimeToLive(0);
        }
        for (int i = 0; i < num; i++) {
            InetSocketAddress dnsServerAddress = dnsServers.get(i).localAddress();
            options.addServer(dnsServerAddress.getAddress().getHostAddress() + ":" + dnsServerAddress.getPort());
        }
        AddressResolver resolver = new AddressResolver(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 && !cache) {
                expected = 1 + i;
            } else {
                expected = 1;
            }
            assertEquals("127.0.0." + expected, resolved);
        }
    } finally {
        dnsServers.forEach(FakeDNSServer::stop);
    }
}
Also used : 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)

Example 2 with FakeDNSServer

use of io.vertx.test.fakedns.FakeDNSServer in project vert.x by eclipse.

the class HostnameResolutionTest method testServerFailover.

@Test
public void testServerFailover() throws Exception {
    FakeDNSServer server = new FakeDNSServer().store(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 : 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 3 with FakeDNSServer

use of io.vertx.test.fakedns.FakeDNSServer in project vert.x by eclipse.

the class DNSTest method setUp.

@Override
public void setUp() throws Exception {
    dnsServer = new FakeDNSServer();
    dnsServer.start();
    super.setUp();
}
Also used : FakeDNSServer(io.vertx.test.fakedns.FakeDNSServer)

Example 4 with FakeDNSServer

use of io.vertx.test.fakedns.FakeDNSServer in project vert.x by eclipse.

the class VertxStartFailureTest method testEventBusStartFailure.

@Test
public void testEventBusStartFailure() throws Exception {
    FakeDNSServer dnsServer = new FakeDNSServer().testResolveASameServer("127.0.0.1");
    dnsServer.start();
    try {
        InetSocketAddress dnsServerAddress = dnsServer.localAddress();
        // will trigger java.net.UnknownHostException
        String hostName = "zoom.zoom.zen.tld";
        FakeClusterManager clusterManager = new FakeClusterManager();
        VertxOptions options = new VertxOptions().setClusterManager(clusterManager);
        options.getAddressResolverOptions().addServer(dnsServerAddress.getAddress().getHostAddress() + ":" + dnsServerAddress.getPort());
        options.getEventBusOptions().setHost(hostName);
        Throwable failure = failStart(options);
        assertTrue("Was expecting failure to be an instance of UnknownHostException", failure instanceof UnknownHostException);
    } finally {
        dnsServer.stop();
    }
}
Also used : UnknownHostException(java.net.UnknownHostException) InetSocketAddress(java.net.InetSocketAddress) FakeDNSServer(io.vertx.test.fakedns.FakeDNSServer) FakeClusterManager(io.vertx.test.fakecluster.FakeClusterManager) Test(org.junit.Test)

Example 5 with FakeDNSServer

use of io.vertx.test.fakedns.FakeDNSServer 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)

Aggregations

FakeDNSServer (io.vertx.test.fakedns.FakeDNSServer)6 InetSocketAddress (java.net.InetSocketAddress)5 AddressResolver (io.vertx.core.impl.AddressResolver)4 InetAddress (java.net.InetAddress)4 CompletableFuture (java.util.concurrent.CompletableFuture)4 Test (org.junit.Test)3 AddressResolverOptions (io.vertx.core.dns.AddressResolverOptions)2 FakeClusterManager (io.vertx.test.fakecluster.FakeClusterManager)1 UnknownHostException (java.net.UnknownHostException)1 ArrayList (java.util.ArrayList)1