Search in sources :

Example 86 with VertxInternal

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

the class VertxTest method testFinalizeNetClient.

@Test
public void testFinalizeNetClient() throws Exception {
    VertxInternal vertx = (VertxInternal) Vertx.vertx();
    try {
        CountDownLatch latch = new CountDownLatch(1);
        AtomicReference<NetSocket> socketRef = new AtomicReference<>();
        vertx.createNetServer().connectHandler(socketRef::set).listen(1234, "localhost").onComplete(onSuccess(server -> latch.countDown()));
        awaitLatch(latch);
        AtomicBoolean closed = new AtomicBoolean();
        CloseFuture closeFuture = new CloseFuture();
        NetClient client = vertx.createNetClient(new NetClientOptions(), closeFuture);
        vertx.addCloseHook(closeFuture);
        closeFuture.future().onComplete(ar -> closed.set(true));
        closeFuture = null;
        client.connect(1234, "localhost", onSuccess(so -> {
        }));
        WeakReference<NetClient> ref = new WeakReference<>(client);
        client = null;
        assertWaitUntil(() -> socketRef.get() != null);
        for (int i = 0; i < 10; i++) {
            Thread.sleep(10);
            RUNNER.runSystemGC();
            assertFalse(closed.get());
            assertNotNull(ref.get());
        }
        socketRef.get().close();
        long now = System.currentTimeMillis();
        while (true) {
            assertTrue(System.currentTimeMillis() - now < 20_000);
            RUNNER.runSystemGC();
            if (ref.get() == null) {
                assertTrue(closed.get());
                break;
            }
        }
    } finally {
        vertx.close(ar -> {
            testComplete();
        });
    }
    await();
}
Also used : NetSocket(io.vertx.core.net.NetSocket) VertxInternal(io.vertx.core.impl.VertxInternal) URL(java.net.URL) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) AtomicReference(java.util.concurrent.atomic.AtomicReference) NetClientOptions(io.vertx.core.net.NetClientOptions) TimeUnit(java.util.concurrent.TimeUnit) HttpClientRequest(io.vertx.core.http.HttpClientRequest) RepeatRule(io.vertx.test.core.RepeatRule) OptionsBuilder(org.openjdk.jmh.runner.options.OptionsBuilder) CountDownLatch(java.util.concurrent.CountDownLatch) Repeat(io.vertx.test.core.Repeat) URLClassLoader(java.net.URLClassLoader) Rule(org.junit.Rule) AsyncTestBase(io.vertx.test.core.AsyncTestBase) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpMethod(io.vertx.core.http.HttpMethod) CloseFuture(io.vertx.core.impl.CloseFuture) HttpClientOptions(io.vertx.core.http.HttpClientOptions) NetClient(io.vertx.core.net.NetClient) Runner(org.openjdk.jmh.runner.Runner) WeakReference(java.lang.ref.WeakReference) HttpClient(io.vertx.core.http.HttpClient) NetSocket(io.vertx.core.net.NetSocket) CloseFuture(io.vertx.core.impl.CloseFuture) NetClientOptions(io.vertx.core.net.NetClientOptions) VertxInternal(io.vertx.core.impl.VertxInternal) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) NetClient(io.vertx.core.net.NetClient) WeakReference(java.lang.ref.WeakReference) Test(org.junit.Test)

Example 87 with VertxInternal

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

the class HATest method testHaGroups.

@Test
public void testHaGroups() throws Exception {
    vertx1 = startVertx("group1", 1);
    vertx2 = startVertx("group1", 1);
    vertx3 = startVertx("group2", 1);
    vertx4 = startVertx("group2", 1);
    CountDownLatch latch1 = new CountDownLatch(2);
    vertx1.deployVerticle("java:" + HAVerticle1.class.getName(), new DeploymentOptions().setHa(true), ar -> {
        assertTrue(ar.succeeded());
        assertTrue(vertx1.deploymentIDs().contains(ar.result()));
        latch1.countDown();
    });
    vertx3.deployVerticle("java:" + HAVerticle2.class.getName(), new DeploymentOptions().setHa(true), ar -> {
        assertTrue(ar.succeeded());
        assertTrue(vertx3.deploymentIDs().contains(ar.result()));
        latch1.countDown();
    });
    awaitLatch(latch1);
    CountDownLatch latch2 = new CountDownLatch(1);
    ((VertxInternal) vertx1).failoverCompleteHandler((nodeID, haInfo, succeeded) -> {
        fail("Should not failover here 1");
    });
    ((VertxInternal) vertx2).failoverCompleteHandler((nodeID, haInfo, succeeded) -> {
        fail("Should not failover here 2");
    });
    ((VertxInternal) vertx4).failoverCompleteHandler((nodeID, haInfo, succeeded) -> {
        assertTrue(succeeded);
        latch2.countDown();
    });
    ((VertxInternal) vertx3).simulateKill();
    awaitLatch(latch2);
    assertTrue(vertx4.deploymentIDs().size() == 1);
    CountDownLatch latch3 = new CountDownLatch(1);
    ((VertxInternal) vertx2).failoverCompleteHandler((nodeID, haInfo, succeeded) -> {
        assertTrue(succeeded);
        latch3.countDown();
    });
    ((VertxInternal) vertx4).failoverCompleteHandler((nodeID, haInfo, succeeded) -> {
        fail("Should not failover here 4");
    });
    ((VertxInternal) vertx1).simulateKill();
    awaitLatch(latch3);
    assertTrue(vertx2.deploymentIDs().size() == 1);
}
Also used : VertxInternal(io.vertx.core.impl.VertxInternal) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 88 with VertxInternal

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

the class HATest method testNoFailoverToNonHANode.

@Test
public void testNoFailoverToNonHANode() throws Exception {
    vertx1 = startVertx();
    // Create a non HA node
    vertx2 = startVertx(null, 0, false);
    CountDownLatch latch1 = new CountDownLatch(1);
    vertx1.deployVerticle("java:" + HAVerticle1.class.getName(), new DeploymentOptions().setHa(true), ar -> {
        assertTrue(ar.succeeded());
        assertTrue(vertx1.deploymentIDs().contains(ar.result()));
        latch1.countDown();
    });
    awaitLatch(latch1);
    ((VertxInternal) vertx2).failoverCompleteHandler((nodeID, haInfo, succeeded) -> fail("Should not failover here 2"));
    ((VertxInternal) vertx1).failoverCompleteHandler((nodeID, haInfo, succeeded) -> fail("Should not failover here 1"));
    ((VertxInternal) vertx1).simulateKill();
    vertx2.close(ar -> {
        vertx.setTimer(500, tid -> {
            // Wait a bit in case failover happens
            assertEquals("Verticle should still be deployed here 1", 1, vertx1.deploymentIDs().size());
            assertTrue("Verticle should not failover here 2", vertx2.deploymentIDs().isEmpty());
            testComplete();
        });
    });
    await();
}
Also used : VertxInternal(io.vertx.core.impl.VertxInternal) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 89 with VertxInternal

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

the class HATest method testCleanCloseNoFailover.

@Test
public void testCleanCloseNoFailover() throws Exception {
    vertx1 = startVertx();
    vertx2 = startVertx();
    DeploymentOptions options = new DeploymentOptions().setHa(true);
    JsonObject config = new JsonObject().put("foo", "bar");
    options.setConfig(config);
    CountDownLatch deployLatch = new CountDownLatch(1);
    vertx2.deployVerticle("java:" + HAVerticle1.class.getName(), options, ar -> {
        assertTrue(ar.succeeded());
        deployLatch.countDown();
    });
    awaitLatch(deployLatch);
    ((VertxInternal) vertx1).failoverCompleteHandler((nodeID, haInfo, succeeded) -> {
        fail("Should not be called");
    });
    vertx2.close(ar -> {
        vertx.setTimer(500, tid -> {
            // Wait a bit in case failover happens
            testComplete();
        });
    });
    await();
}
Also used : VertxInternal(io.vertx.core.impl.VertxInternal) JsonObject(io.vertx.core.json.JsonObject) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 90 with VertxInternal

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

the class HATest method testCloseRemovesFromCluster.

@Test
public void testCloseRemovesFromCluster() throws Exception {
    vertx1 = startVertx();
    vertx2 = startVertx();
    vertx3 = startVertx();
    CountDownLatch latch1 = new CountDownLatch(1);
    vertx3.deployVerticle("java:" + HAVerticle1.class.getName(), new DeploymentOptions().setHa(true), ar -> {
        assertTrue(ar.succeeded());
        assertTrue(vertx3.deploymentIDs().contains(ar.result()));
        latch1.countDown();
    });
    awaitLatch(latch1);
    CountDownLatch latch2 = new CountDownLatch(1);
    // Close vertx2 - this should not then participate in failover
    vertx2.close(ar -> {
        ((VertxInternal) vertx1).failoverCompleteHandler((nodeID, haInfo, succeeded) -> {
            assertTrue(succeeded);
            latch2.countDown();
        });
        ((VertxInternal) vertx3).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 : VertxInternal(io.vertx.core.impl.VertxInternal) CountDownLatch(java.util.concurrent.CountDownLatch) HAVerticle1(io.vertx.test.verticles.HAVerticle1) 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