Search in sources :

Example 11 with Vertx

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

the class AsynchronousLockTest method testAcquireOnExecuteBlocking.

@Test
public void testAcquireOnExecuteBlocking() {
    Vertx vertx = getVertx();
    SharedData sharedData = vertx.sharedData();
    AtomicReference<Long> start = new AtomicReference<>();
    vertx.<Lock>executeBlocking(future -> {
        CountDownLatch acquireLatch = new CountDownLatch(1);
        AtomicReference<AsyncResult<Lock>> lockReference = new AtomicReference<>();
        sharedData.getLock("foo", ar -> {
            lockReference.set(ar);
            acquireLatch.countDown();
        });
        try {
            awaitLatch(acquireLatch);
            AsyncResult<Lock> ar = lockReference.get();
            if (ar.succeeded()) {
                future.complete(ar.result());
            } else {
                future.fail(ar.cause());
            }
        } catch (InterruptedException e) {
            future.fail(e);
        }
    }, ar -> {
        if (ar.succeeded()) {
            start.set(System.currentTimeMillis());
            vertx.setTimer(1000, tid -> {
                ar.result().release();
            });
            vertx.executeBlocking(future -> {
                CountDownLatch acquireLatch = new CountDownLatch(1);
                AtomicReference<AsyncResult<Lock>> lockReference = new AtomicReference<>();
                sharedData.getLock("foo", ar2 -> {
                    lockReference.set(ar2);
                    acquireLatch.countDown();
                });
                try {
                    awaitLatch(acquireLatch);
                    AsyncResult<Lock> ar3 = lockReference.get();
                    if (ar3.succeeded()) {
                        future.complete(ar3.result());
                    } else {
                        future.fail(ar3.cause());
                    }
                } catch (InterruptedException e) {
                    future.fail(e);
                }
            }, ar4 -> {
                if (ar4.succeeded()) {
                    assertTrue(System.currentTimeMillis() - start.get() >= 1000);
                    testComplete();
                } else {
                    fail(ar4.cause());
                }
            });
        } else {
            fail(ar.cause());
        }
    });
    await();
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) Vertx(io.vertx.core.Vertx) CountDownLatch(java.util.concurrent.CountDownLatch) AsyncResult(io.vertx.core.AsyncResult) SharedData(io.vertx.core.shareddata.SharedData) Lock(io.vertx.core.shareddata.Lock) Test(org.junit.Test)

Example 12 with Vertx

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

the class AsynchronousLockTest method testAcquireOnSameEventLoop.

@Test
public void testAcquireOnSameEventLoop() {
    Vertx vertx = getVertx();
    Context context = vertx.getOrCreateContext();
    SharedData sharedData = vertx.sharedData();
    AtomicReference<Long> start = new AtomicReference<>();
    context.runOnContext(v -> {
        sharedData.getLock("foo", ar -> {
            assertTrue(ar.succeeded());
            start.set(System.currentTimeMillis());
            Lock lock = ar.result();
            vertx.setTimer(1000, tid -> {
                lock.release();
            });
            context.runOnContext(v2 -> {
                sharedData.getLock("foo", ar2 -> {
                    assertTrue(ar2.succeeded());
                    assertTrue(System.currentTimeMillis() - start.get() >= 1000);
                    testComplete();
                });
            });
        });
    });
    await();
}
Also used : Context(io.vertx.core.Context) AtomicReference(java.util.concurrent.atomic.AtomicReference) Vertx(io.vertx.core.Vertx) SharedData(io.vertx.core.shareddata.SharedData) Lock(io.vertx.core.shareddata.Lock) Test(org.junit.Test)

Example 13 with Vertx

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

the class LauncherExtensibilityTest method testThatCustomLauncherCanCustomizeTheClusteredOption.

@Test
public void testThatCustomLauncherCanCustomizeTheClusteredOption() {
    Launcher myLauncher = new Launcher() {

        @Override
        protected String getMainVerticle() {
            return HttpTestVerticle.class.getName();
        }

        @Override
        public void afterStartingVertx(Vertx vertx) {
            LauncherExtensibilityTest.this.vertx = vertx;
        }

        @Override
        public void beforeStartingVertx(VertxOptions options) {
            options.setClustered(true);
        }
    };
    myLauncher.dispatch(new String[0]);
    waitUntil(() -> {
        try {
            return RunCommandTest.getHttpCode() == 200;
        } catch (IOException e) {
            return false;
        }
    });
    assertThat(this.vertx.isClustered()).isTrue();
}
Also used : Launcher(io.vertx.core.Launcher) IOException(java.io.IOException) Vertx(io.vertx.core.Vertx) VertxOptions(io.vertx.core.VertxOptions) Test(org.junit.Test) RunCommandTest(io.vertx.core.impl.launcher.commands.RunCommandTest)

Example 14 with Vertx

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

the class LauncherExtensibilityTest method testExtendingMainVerticle.

@Test
public void testExtendingMainVerticle() {
    Launcher myLauncher = new Launcher() {

        @Override
        protected String getMainVerticle() {
            return HttpTestVerticle.class.getName();
        }

        @Override
        public void afterStartingVertx(Vertx vertx) {
            LauncherExtensibilityTest.this.vertx = vertx;
        }
    };
    myLauncher.dispatch(new String[0]);
    waitUntil(() -> {
        try {
            return RunCommandTest.getHttpCode() == 200;
        } catch (IOException e) {
            return false;
        }
    });
}
Also used : Launcher(io.vertx.core.Launcher) IOException(java.io.IOException) Vertx(io.vertx.core.Vertx) Test(org.junit.Test) RunCommandTest(io.vertx.core.impl.launcher.commands.RunCommandTest)

Example 15 with Vertx

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

the class VertxTestBase method tearDown.

protected void tearDown() throws Exception {
    if (vertx != null) {
        CountDownLatch latch = new CountDownLatch(1);
        vertx.close(ar -> {
            latch.countDown();
        });
        awaitLatch(latch);
    }
    if (created != null) {
        CountDownLatch latch = new CountDownLatch(created.size());
        for (Vertx v : created) {
            v.close(ar -> {
                if (ar.failed()) {
                    log.error("Failed to shutdown vert.x", ar.cause());
                }
                latch.countDown();
            });
        }
        assertTrue(latch.await(180, TimeUnit.SECONDS));
    }
    // Bit ugly
    FakeClusterManager.reset();
    super.tearDown();
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) Vertx(io.vertx.core.Vertx)

Aggregations

Vertx (io.vertx.core.Vertx)43 Test (org.junit.Test)17 VertxOptions (io.vertx.core.VertxOptions)11 Buffer (io.vertx.core.buffer.Buffer)10 Handler (io.vertx.core.Handler)8 NetSocket (io.vertx.core.net.NetSocket)7 Pump (io.vertx.core.streams.Pump)7 CountDownLatch (java.util.concurrent.CountDownLatch)7 DeploymentOptions (io.vertx.core.DeploymentOptions)6 VertxInternal (io.vertx.core.impl.VertxInternal)6 NetServer (io.vertx.core.net.NetServer)6 NetServerOptions (io.vertx.core.net.NetServerOptions)6 JsonObject (io.vertx.core.json.JsonObject)5 Launcher (io.vertx.core.Launcher)4 RunCommandTest (io.vertx.core.impl.launcher.commands.RunCommandTest)4 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 AbstractVerticle (io.vertx.core.AbstractVerticle)3 AsyncResult (io.vertx.core.AsyncResult)3