Search in sources :

Example 6 with WorkerExecutor

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

the class NamedWorkerPoolTest method testDestroyWorkerPoolWhenVerticleUndeploys.

@Test
public void testDestroyWorkerPoolWhenVerticleUndeploys() throws Exception {
    String poolName = "vert.x-" + TestUtils.randomAlphaString(10);
    AtomicReference<Thread> thread = new AtomicReference<>();
    CompletableFuture<String> deploymentIdRef = new CompletableFuture<>();
    vertx.deployVerticle(new AbstractVerticle() {

        @Override
        public void start() throws Exception {
            WorkerExecutor pool = vertx.createSharedWorkerExecutor(poolName);
            pool.executeBlocking(fut -> {
                thread.set(Thread.currentThread());
            }, ar -> {
            });
        }
    }, onSuccess(deploymentIdRef::complete));
    waitUntil(() -> thread.get() != null);
    String deploymentId = deploymentIdRef.get(20, TimeUnit.SECONDS);
    vertx.undeploy(deploymentId, onSuccess(v -> {
    }));
    waitUntil(() -> thread.get().getState() == Thread.State.TERMINATED);
}
Also used : Vertx(io.vertx.core.Vertx) Set(java.util.Set) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) CompletableFuture(java.util.concurrent.CompletableFuture) Context(io.vertx.core.Context) AtomicReference(java.util.concurrent.atomic.AtomicReference) WorkerExecutor(io.vertx.core.WorkerExecutor) HashSet(java.util.HashSet) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) DeploymentOptions(io.vertx.core.DeploymentOptions) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AbstractVerticle(io.vertx.core.AbstractVerticle) Collections(java.util.Collections) CompletableFuture(java.util.concurrent.CompletableFuture) WorkerExecutor(io.vertx.core.WorkerExecutor) AtomicReference(java.util.concurrent.atomic.AtomicReference) AbstractVerticle(io.vertx.core.AbstractVerticle) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) Test(org.junit.Test)

Example 7 with WorkerExecutor

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

the class NamedWorkerPoolTest method testPoolSize.

@Test
public void testPoolSize() throws Exception {
    String poolName = "vert.x-" + TestUtils.randomAlphaString(10);
    int poolSize = 5;
    waitFor(poolSize);
    WorkerExecutor worker = vertx.createSharedWorkerExecutor(poolName, poolSize);
    CountDownLatch latch1 = new CountDownLatch(poolSize * 100);
    Set<String> names = Collections.synchronizedSet(new HashSet<>());
    for (int i = 0; i < poolSize * 100; i++) {
        worker.executeBlocking(fut -> {
            names.add(Thread.currentThread().getName());
            latch1.countDown();
        }, false, ar -> {
        });
    }
    awaitLatch(latch1);
    assertEquals(5, names.size());
}
Also used : WorkerExecutor(io.vertx.core.WorkerExecutor) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 8 with WorkerExecutor

use of io.vertx.core.WorkerExecutor in project vertx-camel-bridge by vert-x3.

the class OutboundEndpointTest method testWithBlockingWithWorker.

@Test
public void testWithBlockingWithWorker() throws Exception {
    AtomicBoolean calledSpy = new AtomicBoolean();
    AtomicBoolean startedSpy = new AtomicBoolean();
    vertx.createHttpServer().requestHandler(request -> {
        calledSpy.set(true);
        request.response().end("Alright");
    }).listen(8081, ar -> {
        startedSpy.set(ar.succeeded());
    });
    await().atMost(DEFAULT_TIMEOUT).untilAtomic(startedSpy, is(true));
    camel.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from("direct:my-route").process(exchange -> Thread.sleep(3000)).to("http://localhost:8081");
        }
    });
    WorkerExecutor pool = vertx.createSharedWorkerExecutor("some-fancy-name");
    bridge = CamelBridge.create(vertx, new CamelBridgeOptions(camel).addOutboundMapping(fromVertx("camel-route").toCamel("direct:my-route").setBlocking(true).setWorkerExecutor(pool)));
    camel.start();
    BridgeHelper.startBlocking(bridge);
    vertx.eventBus().send("camel-route", "hello");
    await().atMost(DEFAULT_TIMEOUT).untilAtomic(calledSpy, is(true));
}
Also used : TestContext(io.vertx.ext.unit.TestContext) CoreMatchers.is(org.hamcrest.CoreMatchers.is) Async(io.vertx.ext.unit.Async) DeliveryOptions(io.vertx.core.eventbus.DeliveryOptions) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RunWith(org.junit.runner.RunWith) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Exchange(org.apache.camel.Exchange) Random(java.util.Random) Endpoint(org.apache.camel.Endpoint) AtomicReference(java.util.concurrent.atomic.AtomicReference) WorkerExecutor(io.vertx.core.WorkerExecutor) Processor(org.apache.camel.Processor) After(org.junit.After) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) Before(org.junit.Before) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Awaitility.await(com.jayway.awaitility.Awaitility.await) Vertx(io.vertx.core.Vertx) FileUtils(org.apache.commons.io.FileUtils) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) File(java.io.File) Duration(com.jayway.awaitility.Duration) OutboundMapping.fromVertx(io.vertx.camel.OutboundMapping.fromVertx) Buffer(io.vertx.core.buffer.Buffer) RouteBuilder(org.apache.camel.builder.RouteBuilder) MapEntry.entry(org.assertj.core.data.MapEntry.entry) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RouteBuilder(org.apache.camel.builder.RouteBuilder) WorkerExecutor(io.vertx.core.WorkerExecutor) Test(org.junit.Test)

Aggregations

WorkerExecutor (io.vertx.core.WorkerExecutor)8 Test (org.junit.Test)8 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 CountDownLatch (java.util.concurrent.CountDownLatch)4 Vertx (io.vertx.core.Vertx)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 Context (io.vertx.core.Context)2 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)2 Awaitility.await (com.jayway.awaitility.Awaitility.await)1 Duration (com.jayway.awaitility.Duration)1 OutboundMapping.fromVertx (io.vertx.camel.OutboundMapping.fromVertx)1 AbstractVerticle (io.vertx.core.AbstractVerticle)1 DeploymentOptions (io.vertx.core.DeploymentOptions)1 Buffer (io.vertx.core.buffer.Buffer)1 DeliveryOptions (io.vertx.core.eventbus.DeliveryOptions)1 Async (io.vertx.ext.unit.Async)1 TestContext (io.vertx.ext.unit.TestContext)1 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)1 File (java.io.File)1 Collections (java.util.Collections)1