Search in sources :

Example 1 with AbstractVerticle

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

the class EventBusTestBase method testNoHandlersCallbackContext.

@Test
public void testNoHandlersCallbackContext() {
    startNodes(2);
    waitFor(4);
    // On an "external" thread
    vertices[0].eventBus().send("blah", "blah", ar -> {
        assertTrue(ar.failed());
        if (ar.cause() instanceof ReplyException) {
            ReplyException cause = (ReplyException) ar.cause();
            assertSame(ReplyFailure.NO_HANDLERS, cause.failureType());
        } else {
            fail(ar.cause());
        }
        assertTrue("Not an EL thread", Context.isOnEventLoopThread());
        complete();
    });
    // On a EL context
    vertices[0].runOnContext(v -> {
        Context ctx = vertices[0].getOrCreateContext();
        vertices[0].eventBus().send("blah", "blah", ar -> {
            assertTrue(ar.failed());
            if (ar.cause() instanceof ReplyException) {
                ReplyException cause = (ReplyException) ar.cause();
                assertSame(ReplyFailure.NO_HANDLERS, cause.failureType());
            } else {
                fail(ar.cause());
            }
            assertSame(ctx, vertices[0].getOrCreateContext());
            complete();
        });
    });
    // On a Worker context
    vertices[0].deployVerticle(new AbstractVerticle() {

        @Override
        public void start() throws Exception {
            Context ctx = getVertx().getOrCreateContext();
            vertices[0].eventBus().send("blah", "blah", ar -> {
                assertTrue(ar.failed());
                if (ar.cause() instanceof ReplyException) {
                    ReplyException cause = (ReplyException) ar.cause();
                    assertSame(ReplyFailure.NO_HANDLERS, cause.failureType());
                } else {
                    fail(ar.cause());
                }
                assertSame(ctx, getVertx().getOrCreateContext());
                complete();
            });
        }
    }, new DeploymentOptions().setWorker(true));
    // Inside executeBlocking
    vertices[0].executeBlocking(fut -> {
        vertices[0].eventBus().send("blah", "blah", ar -> {
            assertTrue(ar.failed());
            if (ar.cause() instanceof ReplyException) {
                ReplyException cause = (ReplyException) ar.cause();
                assertSame(ReplyFailure.NO_HANDLERS, cause.failureType());
            } else {
                fail(ar.cause());
            }
            assertTrue("Not an EL thread", Context.isOnEventLoopThread());
            complete();
        });
        fut.complete();
    }, false, null);
    await();
}
Also used : Context(io.vertx.core.Context) CoreMatchers(org.hamcrest.CoreMatchers) DeliveryOptions(io.vertx.core.eventbus.DeliveryOptions) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) Verticle(io.vertx.core.Verticle) Context(io.vertx.core.Context) Future(io.vertx.core.Future) Consumer(java.util.function.Consumer) JsonArray(io.vertx.core.json.JsonArray) CountDownLatch(java.util.concurrent.CountDownLatch) EventBus(io.vertx.core.eventbus.EventBus) MessageCodec(io.vertx.core.eventbus.MessageCodec) Buffer(io.vertx.core.buffer.Buffer) DeploymentOptions(io.vertx.core.DeploymentOptions) AbstractVerticle(io.vertx.core.AbstractVerticle) CharsetUtil(io.netty.util.CharsetUtil) ReplyFailure(io.vertx.core.eventbus.ReplyFailure) JsonObject(io.vertx.core.json.JsonObject) MessageConsumer(io.vertx.core.eventbus.MessageConsumer) ReplyException(io.vertx.core.eventbus.ReplyException) DeploymentOptions(io.vertx.core.DeploymentOptions) ReplyException(io.vertx.core.eventbus.ReplyException) AbstractVerticle(io.vertx.core.AbstractVerticle) ReplyException(io.vertx.core.eventbus.ReplyException) Test(org.junit.Test)

Example 2 with AbstractVerticle

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

the class EventBusTestBase method testReplyFromWorker.

@Test
public void testReplyFromWorker() throws Exception {
    String expectedBody = TestUtils.randomAlphaString(20);
    startNodes(2);
    CountDownLatch latch = new CountDownLatch(1);
    vertices[0].deployVerticle(new AbstractVerticle() {

        @Override
        public void start() throws Exception {
            vertices[1].eventBus().<String>consumer(ADDRESS1, msg -> {
                msg.reply(expectedBody);
            }).completionHandler(ar -> {
                assertTrue(ar.succeeded());
                latch.countDown();
            });
        }
    }, new DeploymentOptions().setWorker(true));
    awaitLatch(latch);
    vertices[0].eventBus().send(ADDRESS1, "whatever", reply -> {
        assertTrue(reply.succeeded());
        assertEquals(expectedBody, reply.result().body());
        testComplete();
    });
    await();
}
Also used : CoreMatchers(org.hamcrest.CoreMatchers) DeliveryOptions(io.vertx.core.eventbus.DeliveryOptions) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) Verticle(io.vertx.core.Verticle) Context(io.vertx.core.Context) Future(io.vertx.core.Future) Consumer(java.util.function.Consumer) JsonArray(io.vertx.core.json.JsonArray) CountDownLatch(java.util.concurrent.CountDownLatch) EventBus(io.vertx.core.eventbus.EventBus) MessageCodec(io.vertx.core.eventbus.MessageCodec) Buffer(io.vertx.core.buffer.Buffer) DeploymentOptions(io.vertx.core.DeploymentOptions) AbstractVerticle(io.vertx.core.AbstractVerticle) CharsetUtil(io.netty.util.CharsetUtil) ReplyFailure(io.vertx.core.eventbus.ReplyFailure) JsonObject(io.vertx.core.json.JsonObject) MessageConsumer(io.vertx.core.eventbus.MessageConsumer) ReplyException(io.vertx.core.eventbus.ReplyException) DeploymentOptions(io.vertx.core.DeploymentOptions) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractVerticle(io.vertx.core.AbstractVerticle) ReplyException(io.vertx.core.eventbus.ReplyException) Test(org.junit.Test)

Example 3 with AbstractVerticle

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

the class DatagramTest method testUseInMultithreadedWorker.

@Test
public void testUseInMultithreadedWorker() throws Exception {
    class MyVerticle extends AbstractVerticle {

        @Override
        public void start() {
            assertIllegalStateException(() -> peer1 = vertx.createDatagramSocket(new DatagramSocketOptions()));
            testComplete();
        }
    }
    MyVerticle verticle = new MyVerticle();
    vertx.deployVerticle(verticle, new DeploymentOptions().setWorker(true).setMultiThreaded(true));
    await();
}
Also used : DatagramSocketOptions(io.vertx.core.datagram.DatagramSocketOptions) DeploymentOptions(io.vertx.core.DeploymentOptions) AbstractVerticle(io.vertx.core.AbstractVerticle) Test(org.junit.Test)

Example 4 with AbstractVerticle

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

the class ContextTest method testExceptionHandlerOnDeploymentAsyncResultHandlerFailure.

@Test
public void testExceptionHandlerOnDeploymentAsyncResultHandlerFailure() {
    RuntimeException failure = new RuntimeException();
    Context ctx = vertx.getOrCreateContext();
    ctx.exceptionHandler(err -> {
        assertSame(failure, err);
        testComplete();
    });
    ctx.runOnContext(v -> {
        vertx.deployVerticle(new AbstractVerticle() {

            @Override
            public void start() throws Exception {
            }
        }, ar -> {
            throw failure;
        });
    });
    await();
}
Also used : Context(io.vertx.core.Context) AbstractVerticle(io.vertx.core.AbstractVerticle) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) Test(org.junit.Test)

Example 5 with AbstractVerticle

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

the class ContextTest method testWorkerExecuteFromIo.

@Test
public void testWorkerExecuteFromIo() throws Exception {
    AtomicReference<ContextInternal> workerContext = new AtomicReference<>();
    CountDownLatch latch = new CountDownLatch(1);
    vertx.deployVerticle(new AbstractVerticle() {

        @Override
        public void start() throws Exception {
            workerContext.set((ContextInternal) context);
            latch.countDown();
        }
    }, new DeploymentOptions().setWorker(true));
    awaitLatch(latch);
    workerContext.get().nettyEventLoop().execute(() -> {
        assertNull(Vertx.currentContext());
        workerContext.get().executeFromIO(() -> {
            assertSame(workerContext.get(), Vertx.currentContext());
            assertTrue(Context.isOnWorkerThread());
            testComplete();
        });
    });
    await();
}
Also used : DeploymentOptions(io.vertx.core.DeploymentOptions) AtomicReference(java.util.concurrent.atomic.AtomicReference) ContextInternal(io.vertx.core.impl.ContextInternal) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractVerticle(io.vertx.core.AbstractVerticle) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) Test(org.junit.Test)

Aggregations

AbstractVerticle (io.vertx.core.AbstractVerticle)22 Test (org.junit.Test)21 DeploymentOptions (io.vertx.core.DeploymentOptions)16 Context (io.vertx.core.Context)12 CountDownLatch (java.util.concurrent.CountDownLatch)9 Vertx (io.vertx.core.Vertx)8 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)8 Verticle (io.vertx.core.Verticle)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 Future (io.vertx.core.Future)5 CompletableFuture (java.util.concurrent.CompletableFuture)5 WorkerExecutor (io.vertx.core.WorkerExecutor)4 Buffer (io.vertx.core.buffer.Buffer)4 Collections (java.util.Collections)4 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)4 TimeUnit (java.util.concurrent.TimeUnit)4 Consumer (java.util.function.Consumer)4 CharsetUtil (io.netty.util.CharsetUtil)3 VertxOptions (io.vertx.core.VertxOptions)3