Search in sources :

Example 1 with TaskQueue

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

the class ContextTest method testInternalExecuteBlockingWithQueue.

@Test
public void testInternalExecuteBlockingWithQueue() {
    ContextInternal context = (ContextInternal) vertx.getOrCreateContext();
    TaskQueue[] queues = new TaskQueue[] { new TaskQueue(), new TaskQueue() };
    AtomicReference<Thread>[] current = new AtomicReference[queues.length];
    waitFor(queues.length);
    for (int i = 0; i < queues.length; i++) {
        current[i] = new AtomicReference<>();
    }
    CyclicBarrier barrier = new CyclicBarrier(queues.length);
    int numTasks = 10;
    for (int i = 0; i < numTasks; i++) {
        int ival = i;
        for (int j = 0; j < queues.length; j++) {
            int jval = j;
            context.executeBlocking(fut -> {
                if (ival == 0) {
                    current[jval].set(Thread.currentThread());
                } else {
                    assertSame(Thread.currentThread(), current[jval].get());
                }
                try {
                    barrier.await();
                } catch (Exception e) {
                    fail(e);
                }
                if (ival == numTasks - 1) {
                    complete();
                }
            }, queues[j], ar -> {
            });
        }
    }
    await();
}
Also used : TaskQueue(io.vertx.core.impl.TaskQueue) ContextInternal(io.vertx.core.impl.ContextInternal) AtomicReference(java.util.concurrent.atomic.AtomicReference) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) CyclicBarrier(java.util.concurrent.CyclicBarrier) Test(org.junit.Test)

Aggregations

ContextInternal (io.vertx.core.impl.ContextInternal)1 TaskQueue (io.vertx.core.impl.TaskQueue)1 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)1 CyclicBarrier (java.util.concurrent.CyclicBarrier)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Test (org.junit.Test)1