Search in sources :

Example 21 with AbstractVerticle

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

the class EventBusTestBase method testSendWhileUnsubscribing.

@Test
public void testSendWhileUnsubscribing() throws Exception {
    startNodes(2);
    AtomicBoolean unregistered = new AtomicBoolean();
    Verticle sender = new AbstractVerticle() {

        @Override
        public void start() throws Exception {
            getVertx().runOnContext(v -> sendMsg());
        }

        private void sendMsg() {
            if (!unregistered.get()) {
                getVertx().eventBus().send("whatever", "marseille");
                burnCpu();
                getVertx().runOnContext(v -> sendMsg());
            } else {
                getVertx().eventBus().send("whatever", "marseille", ar -> {
                    Throwable cause = ar.cause();
                    assertThat(cause, instanceOf(ReplyException.class));
                    ReplyException replyException = (ReplyException) cause;
                    assertEquals(ReplyFailure.NO_HANDLERS, replyException.failureType());
                    testComplete();
                });
            }
        }
    };
    Verticle receiver = new AbstractVerticle() {

        boolean unregisterCalled;

        @Override
        public void start(Future<Void> startFuture) throws Exception {
            EventBus eventBus = getVertx().eventBus();
            MessageConsumer<String> consumer = eventBus.consumer("whatever");
            consumer.handler(m -> {
                if (!unregisterCalled) {
                    consumer.unregister(v -> unregistered.set(true));
                    unregisterCalled = true;
                }
                m.reply("ok");
            }).completionHandler(startFuture);
        }
    };
    CountDownLatch deployLatch = new CountDownLatch(1);
    vertices[0].exceptionHandler(this::fail).deployVerticle(receiver, onSuccess(receiverId -> {
        vertices[1].exceptionHandler(this::fail).deployVerticle(sender, onSuccess(senderId -> {
            deployLatch.countDown();
        }));
    }));
    awaitLatch(deployLatch);
    await();
    CountDownLatch closeLatch = new CountDownLatch(2);
    vertices[0].close(v -> closeLatch.countDown());
    vertices[1].close(v -> closeLatch.countDown());
    awaitLatch(closeLatch);
}
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) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Verticle(io.vertx.core.Verticle) AbstractVerticle(io.vertx.core.AbstractVerticle) Future(io.vertx.core.Future) EventBus(io.vertx.core.eventbus.EventBus) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractVerticle(io.vertx.core.AbstractVerticle) ReplyException(io.vertx.core.eventbus.ReplyException) Test(org.junit.Test)

Example 22 with AbstractVerticle

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

the class HttpTest method testUseInMultithreadedWorker.

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

        @Override
        public void start() {
            assertIllegalStateException(() -> server = vertx.createHttpServer(new HttpServerOptions()));
            assertIllegalStateException(() -> client = vertx.createHttpClient(new HttpClientOptions()));
            testComplete();
        }
    }
    MyVerticle verticle = new MyVerticle();
    vertx.deployVerticle(verticle, new DeploymentOptions().setWorker(true).setMultiThreaded(true));
    await();
}
Also used : DeploymentOptions(io.vertx.core.DeploymentOptions) HttpServerOptions(io.vertx.core.http.HttpServerOptions) AbstractVerticle(io.vertx.core.AbstractVerticle) HttpClientOptions(io.vertx.core.http.HttpClientOptions) 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