Search in sources :

Example 56 with VertxInternal

use of io.vertx.core.impl.VertxInternal in project vert.x by eclipse.

the class HostnameResolutionTest method testResolveMissingLocalhost.

@Test
public void testResolveMissingLocalhost() throws Exception {
    InetAddress localhost = InetAddress.getByName("localhost");
    // Set a dns resolver that won't resolve localhost
    dnsServer.stop();
    dnsServer = FakeDNSServer.testResolveASameServer("127.0.0.1");
    dnsServer.start();
    dnsServerAddress = (InetSocketAddress) dnsServer.getTransports()[0].getAcceptor().getLocalAddress();
    // Test using the resolver API
    VertxInternal vertx = (VertxInternal) vertx(new VertxOptions().setAddressResolverOptions(new AddressResolverOptions().addServer(dnsServerAddress.getAddress().getHostAddress() + ":" + dnsServerAddress.getPort()).setOptResourceEnabled(false)));
    CompletableFuture<Void> test1 = new CompletableFuture<>();
    vertx.resolveAddress("localhost", ar -> {
        if (ar.succeeded()) {
            InetAddress resolved = ar.result();
            if (resolved.equals(localhost)) {
                test1.complete(null);
            } else {
                test1.completeExceptionally(new AssertionError("Unexpected localhost value " + resolved));
            }
        } else {
            test1.completeExceptionally(ar.cause());
        }
    });
    test1.get(10, TimeUnit.SECONDS);
    CompletableFuture<Void> test2 = new CompletableFuture<>();
    vertx.resolveAddress("LOCALHOST", ar -> {
        if (ar.succeeded()) {
            InetAddress resolved = ar.result();
            if (resolved.equals(localhost)) {
                test2.complete(null);
            } else {
                test2.completeExceptionally(new AssertionError("Unexpected localhost value " + resolved));
            }
        } else {
            test2.completeExceptionally(ar.cause());
        }
    });
    test2.get(10, TimeUnit.SECONDS);
    // Test using bootstrap
    CompletableFuture<Void> test3 = new CompletableFuture<>();
    NetServer server = vertx.createNetServer(new NetServerOptions().setPort(1234).setHost(localhost.getHostAddress()));
    server.connectHandler(so -> {
        so.write("hello").end();
    });
    server.listen(ar -> {
        if (ar.succeeded()) {
            test3.complete(null);
        } else {
            test3.completeExceptionally(ar.cause());
        }
    });
    test3.get(10, TimeUnit.SECONDS);
    CompletableFuture<Void> test4 = new CompletableFuture<>();
    NetClient client = vertx.createNetClient();
    client.connect(1234, "localhost", ar -> {
        if (ar.succeeded()) {
            test4.complete(null);
        } else {
            test4.completeExceptionally(ar.cause());
        }
    });
    test4.get(10, TimeUnit.SECONDS);
}
Also used : AddressResolverOptions(io.vertx.core.dns.AddressResolverOptions) CompletableFuture(java.util.concurrent.CompletableFuture) VertxInternal(io.vertx.core.impl.VertxInternal) NetClient(io.vertx.core.net.NetClient) NetServerOptions(io.vertx.core.net.NetServerOptions) NetServer(io.vertx.core.net.NetServer) InetAddress(java.net.InetAddress) VertxOptions(io.vertx.core.VertxOptions) Test(org.junit.Test)

Example 57 with VertxInternal

use of io.vertx.core.impl.VertxInternal in project vert.x by eclipse.

the class HostnameResolutionTest method testResolveFromFile.

@Test
public void testResolveFromFile() {
    File f = new File(new File(new File(new File("src"), "test"), "resources"), "hosts_config.txt");
    VertxInternal vertx = (VertxInternal) vertx(new VertxOptions().setAddressResolverOptions(new AddressResolverOptions().setHostsPath(f.getAbsolutePath())));
    vertx.resolveAddress("server.net", onSuccess(addr -> {
        assertEquals("192.168.0.15", addr.getHostAddress());
        assertEquals("server.net", addr.getHostName());
        testComplete();
    }));
    await();
}
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) File(java.io.File) VertxOptions(io.vertx.core.VertxOptions) Test(org.junit.Test)

Example 58 with VertxInternal

use of io.vertx.core.impl.VertxInternal in project vert.x by eclipse.

the class HostnameResolutionTest method testSearchDomain.

@Test
public void testSearchDomain() throws Exception {
    Map<String, String> records = new HashMap<>();
    records.put("host1.foo.com", "127.0.0.1");
    records.put("host1", "127.0.0.2");
    records.put("host3", "127.0.0.3");
    records.put("host4.sub.foo.com", "127.0.0.4");
    records.put("host5.sub.foo.com", "127.0.0.5");
    records.put("host5.sub", "127.0.0.6");
    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")));
    // host1 resolves host1.foo.com with foo.com search domain
    CountDownLatch latch1 = new CountDownLatch(1);
    vertx.resolveAddress("host1", onSuccess(resolved -> {
        assertEquals("127.0.0.1", resolved.getHostAddress());
        latch1.countDown();
    }));
    awaitLatch(latch1);
    // "host1." absolute query
    CountDownLatch latch2 = new CountDownLatch(1);
    vertx.resolveAddress("host1.", onSuccess(resolved -> {
        assertEquals("127.0.0.2", resolved.getHostAddress());
        latch2.countDown();
    }));
    awaitLatch(latch2);
    // "host2" not resolved
    CountDownLatch latch3 = new CountDownLatch(1);
    vertx.resolveAddress("host2", onFailure(cause -> {
        assertTrue(cause instanceof UnknownHostException);
        latch3.countDown();
    }));
    awaitLatch(latch3);
    // "host3" does not contain a dot or is not absolute
    CountDownLatch latch4 = new CountDownLatch(1);
    vertx.resolveAddress("host3", onFailure(cause -> {
        assertTrue(cause instanceof UnknownHostException);
        latch4.countDown();
    }));
    awaitLatch(latch4);
    // "host3." does not contain a dot but is absolute
    CountDownLatch latch5 = new CountDownLatch(1);
    vertx.resolveAddress("host3.", onSuccess(resolved -> {
        assertEquals("127.0.0.3", resolved.getHostAddress());
        latch5.countDown();
    }));
    awaitLatch(latch5);
    // "host4.sub" contains a dot but not resolved then resolved to "host4.sub.foo.com" with "foo.com" search domain
    CountDownLatch latch6 = new CountDownLatch(1);
    vertx.resolveAddress("host4.sub", onSuccess(resolved -> {
        assertEquals("127.0.0.4", resolved.getHostAddress());
        latch6.countDown();
    }));
    awaitLatch(latch6);
    // "host5.sub" contains a dot and is resolved
    CountDownLatch latch7 = new CountDownLatch(1);
    vertx.resolveAddress("host5.sub", onSuccess(resolved -> {
        assertEquals("127.0.0.6", resolved.getHostAddress());
        latch7.countDown();
    }));
    awaitLatch(latch7);
}
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) UnknownHostException(java.net.UnknownHostException) HashMap(java.util.HashMap) CountDownLatch(java.util.concurrent.CountDownLatch) VertxOptions(io.vertx.core.VertxOptions) Test(org.junit.Test)

Example 59 with VertxInternal

use of io.vertx.core.impl.VertxInternal in project vert.x by eclipse.

the class HATest method kill.

protected void kill(int pos) {
    VertxInternal v = (VertxInternal) vertices[pos];
    v.executeBlocking(fut -> {
        try {
            v.simulateKill();
            fut.complete();
        } catch (Exception e) {
            fut.fail(e);
        }
    }, false, ar -> {
        if (!ar.succeeded()) {
            fail(ar.cause());
        }
    });
}
Also used : VertxInternal(io.vertx.core.impl.VertxInternal)

Example 60 with VertxInternal

use of io.vertx.core.impl.VertxInternal in project vert.x by eclipse.

the class HATest method testNonHADeployments.

@Test
public void testNonHADeployments() throws Exception {
    vertx1 = startVertx();
    vertx2 = startVertx();
    // Deploy an HA and a non HA deployment
    CountDownLatch latch1 = new CountDownLatch(2);
    vertx2.deployVerticle("java:" + HAVerticle1.class.getName(), new DeploymentOptions().setHa(true), ar -> {
        assertTrue(ar.succeeded());
        assertTrue(vertx2.deploymentIDs().contains(ar.result()));
        latch1.countDown();
    });
    vertx2.deployVerticle("java:" + HAVerticle2.class.getName(), new DeploymentOptions().setHa(false), ar -> {
        assertTrue(ar.succeeded());
        assertTrue(vertx2.deploymentIDs().contains(ar.result()));
        latch1.countDown();
    });
    awaitLatch(latch1);
    CountDownLatch latch2 = new CountDownLatch(1);
    ((VertxInternal) vertx1).failoverCompleteHandler((nodeID, haInfo, succeeded) -> {
        assertTrue(succeeded);
        latch2.countDown();
    });
    ((VertxInternal) vertx2).simulateKill();
    awaitLatch(latch2);
    assertTrue(vertx1.deploymentIDs().size() == 1);
    String depID = vertx1.deploymentIDs().iterator().next();
    assertTrue(((VertxInternal) vertx1).getDeployment(depID).verticleIdentifier().equals("java:" + HAVerticle1.class.getName()));
}
Also used : DeploymentOptions(io.vertx.core.DeploymentOptions) VertxInternal(io.vertx.core.impl.VertxInternal) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

VertxInternal (io.vertx.core.impl.VertxInternal)102 Test (org.junit.Test)73 CountDownLatch (java.util.concurrent.CountDownLatch)46 VertxOptions (io.vertx.core.VertxOptions)30 Buffer (io.vertx.core.buffer.Buffer)29 JsonObject (io.vertx.core.json.JsonObject)29 File (java.io.File)27 AtomicReference (java.util.concurrent.atomic.AtomicReference)27 VertxException (io.vertx.core.VertxException)24 HttpClient (io.vertx.core.http.HttpClient)24 NetClient (io.vertx.core.net.NetClient)24 TimeUnit (java.util.concurrent.TimeUnit)24 NetServerOptions (io.vertx.core.net.NetServerOptions)23 InetAddress (java.net.InetAddress)23 Channel (io.netty.channel.Channel)22 InetSocketAddress (java.net.InetSocketAddress)22 CompletableFuture (java.util.concurrent.CompletableFuture)22 NetServer (io.vertx.core.net.NetServer)21 ChannelFuture (io.netty.channel.ChannelFuture)20 Bootstrap (io.netty.bootstrap.Bootstrap)19