Search in sources :

Example 1 with WorkerExecutor

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

the class NamedWorkerPoolTest method testCloseWorkerPoolsWhenVertxCloses.

@Test
public void testCloseWorkerPoolsWhenVertxCloses() {
    Vertx vertx = Vertx.vertx();
    WorkerExecutor exec = vertx.createSharedWorkerExecutor("vert.x-123");
    vertx.close(v -> {
        try {
            vertx.executeBlocking(fut -> fail(), ar -> fail());
            fail();
        } catch (RejectedExecutionException ignore) {
        }
        try {
            exec.executeBlocking(fut -> fail(), ar -> fail());
            fail();
        } catch (RejectedExecutionException ignore) {
        }
        exec.close();
        testComplete();
    });
    await();
}
Also used : WorkerExecutor(io.vertx.core.WorkerExecutor) Vertx(io.vertx.core.Vertx) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) Test(org.junit.Test)

Example 2 with WorkerExecutor

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

the class NamedWorkerPoolTest method testCloseWorkerPool.

@Test
public void testCloseWorkerPool() throws Exception {
    String poolName = "vert.x-" + TestUtils.randomAlphaString(10);
    AtomicReference<Thread> thread = new AtomicReference<>();
    WorkerExecutor worker1 = vertx.createSharedWorkerExecutor(poolName);
    WorkerExecutor worker2 = vertx.createSharedWorkerExecutor(poolName);
    worker1.executeBlocking(fut -> {
        thread.set(Thread.currentThread());
    }, ar -> {
    });
    waitUntil(() -> thread.get() != null);
    worker1.close();
    assertNotSame(thread.get().getState(), Thread.State.TERMINATED);
    worker2.close();
    waitUntil(() -> thread.get().getState() == Thread.State.TERMINATED);
}
Also used : WorkerExecutor(io.vertx.core.WorkerExecutor) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Example 3 with WorkerExecutor

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

the class NamedWorkerPoolTest method testOrdered.

@Test
public void testOrdered() {
    String poolName = "vert.x-" + TestUtils.randomAlphaString(10);
    WorkerExecutor worker = vertx.createSharedWorkerExecutor(poolName);
    int num = 1000;
    AtomicReference<Thread> t = new AtomicReference<>();
    CountDownLatch submitted = new CountDownLatch(1);
    Context ctx = vertx.getOrCreateContext();
    ctx.runOnContext(v -> {
        for (int i = 0; i < num; i++) {
            boolean first = i == 0;
            boolean last = i == num - 1;
            worker.executeBlocking(fut -> {
                if (first) {
                    try {
                        awaitLatch(submitted);
                    } catch (InterruptedException e) {
                        fail(e);
                        return;
                    }
                    assertNull(t.get());
                    t.set(Thread.currentThread());
                } else {
                    assertEquals(t.get(), Thread.currentThread());
                }
                assertTrue(Thread.currentThread().getName().startsWith(poolName + "-"));
                fut.complete(null);
            }, ar -> {
                if (last) {
                    testComplete();
                }
            });
        }
    });
    submitted.countDown();
    await();
}
Also used : Context(io.vertx.core.Context) WorkerExecutor(io.vertx.core.WorkerExecutor) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 4 with WorkerExecutor

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

the class NamedWorkerPoolTest method testUnordered.

@Test
public void testUnordered() throws Exception {
    String poolName = "vert.x-" + TestUtils.randomAlphaString(10);
    int num = 5;
    waitFor(num);
    WorkerExecutor worker = vertx.createSharedWorkerExecutor(poolName);
    CountDownLatch latch1 = new CountDownLatch(num);
    CountDownLatch latch2 = new CountDownLatch(1);
    for (int i = 0; i < num; i++) {
        worker.executeBlocking(fut -> {
            latch1.countDown();
            try {
                awaitLatch(latch2);
            } catch (InterruptedException e) {
                fail(e);
                return;
            }
            assertTrue(Thread.currentThread().getName().startsWith(poolName + "-"));
            fut.complete(null);
        }, false, ar -> {
            complete();
        });
    }
    awaitLatch(latch1);
    latch2.countDown();
    await();
}
Also used : WorkerExecutor(io.vertx.core.WorkerExecutor) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 5 with WorkerExecutor

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

the class NamedWorkerPoolTest method testThread.

@Test
public void testThread() {
    String poolName = TestUtils.randomAlphaString(10);
    WorkerExecutor worker = vertx.createSharedWorkerExecutor(poolName);
    AtomicBoolean onVertxThread = new AtomicBoolean();
    AtomicBoolean onWorkerThread = new AtomicBoolean();
    AtomicBoolean onEventLoopThread = new AtomicBoolean();
    AtomicReference<String> threadName = new AtomicReference<>();
    worker.executeBlocking(fut -> {
        onVertxThread.set(Context.isOnVertxThread());
        onWorkerThread.set(Context.isOnWorkerThread());
        onEventLoopThread.set(Context.isOnEventLoopThread());
        threadName.set(Thread.currentThread().getName());
        fut.complete(null);
    }, ar -> {
        testComplete();
    });
    // Use regular assertions because the thread name does not start with "vert.x-"
    // and it confuses the VertxTestBase asserts
    waitUntil(() -> threadName.get() != null);
    assertTrue(onVertxThread.get());
    assertTrue(onWorkerThread.get());
    assertFalse(onEventLoopThread.get());
    assertTrue(threadName.get().startsWith(poolName + "-"));
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) WorkerExecutor(io.vertx.core.WorkerExecutor) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Aggregations

WorkerExecutor (io.vertx.core.WorkerExecutor)10 Test (org.junit.Test)10 CountDownLatch (java.util.concurrent.CountDownLatch)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 Vertx (io.vertx.core.Vertx)4 Context (io.vertx.core.Context)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 Async (io.vertx.ext.unit.Async)2 TestContext (io.vertx.ext.unit.TestContext)2 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)2 Collections (java.util.Collections)2 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 RunWith (org.junit.runner.RunWith)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 Promise (io.vertx.core.Promise)1