Search in sources :

Example 21 with Context

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

the class ExecuteBlockingTest method testExecuteBlockingParallel.

@Test
public void testExecuteBlockingParallel() throws Exception {
    long start = System.currentTimeMillis();
    int numExecBlocking = 10;
    long pause = 1000;
    CountDownLatch latch = new CountDownLatch(numExecBlocking);
    vertx.runOnContext(v -> {
        Context ctx = vertx.getOrCreateContext();
        assertTrue(ctx.isEventLoopContext());
        for (int i = 0; i < numExecBlocking; i++) {
            vertx.executeBlocking(future -> {
                assertSame(ctx, vertx.getOrCreateContext());
                assertTrue(Thread.currentThread().getName().startsWith("vert.x-worker-thread"));
                assertTrue(Context.isOnWorkerThread());
                assertFalse(Context.isOnEventLoopThread());
                try {
                    Thread.sleep(pause);
                } catch (Exception ignore) {
                }
                future.complete("done!");
            }, false, onSuccess(res -> {
                assertSame(ctx, vertx.getOrCreateContext());
                assertTrue(Thread.currentThread().getName().startsWith("vert.x-eventloop-thread"));
                assertFalse(Context.isOnWorkerThread());
                assertTrue(Context.isOnEventLoopThread());
                assertEquals("done!", res);
                latch.countDown();
            }));
        }
    });
    awaitLatch(latch);
    long now = System.currentTimeMillis();
    long leeway = 1000;
    assertTrue(now - start < pause + leeway);
}
Also used : Context(io.vertx.core.Context) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test) Context(io.vertx.core.Context) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 22 with Context

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

the class ExecuteBlockingTest method testExecuteBlockingContext.

@Test
public void testExecuteBlockingContext() {
    vertx.runOnContext(v -> {
        Context ctx = vertx.getOrCreateContext();
        assertTrue(ctx.isEventLoopContext());
        vertx.executeBlocking(future -> {
            assertSame(ctx, vertx.getOrCreateContext());
            assertTrue(Thread.currentThread().getName().startsWith("vert.x-worker-thread"));
            assertTrue(Context.isOnWorkerThread());
            assertFalse(Context.isOnEventLoopThread());
            try {
                Thread.sleep(1000);
            } catch (Exception ignore) {
            }
            vertx.runOnContext(v2 -> {
                assertSame(ctx, vertx.getOrCreateContext());
                assertTrue(Thread.currentThread().getName().startsWith("vert.x-eventloop-thread"));
                assertFalse(Context.isOnWorkerThread());
                assertTrue(Context.isOnEventLoopThread());
                future.complete("done!");
            });
        }, onSuccess(res -> {
            assertSame(ctx, vertx.getOrCreateContext());
            assertTrue(Thread.currentThread().getName().startsWith("vert.x-eventloop-thread"));
            assertFalse(Context.isOnWorkerThread());
            assertTrue(Context.isOnEventLoopThread());
            assertEquals("done!", res);
            testComplete();
        }));
    });
    await();
}
Also used : Context(io.vertx.core.Context) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test) Context(io.vertx.core.Context) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Example 23 with Context

use of io.vertx.core.Context 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 24 with Context

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

the class ContextTest method testGettingContextContextUnderContextAnotherInstanceShouldReturnDifferentContext.

@Test
public void testGettingContextContextUnderContextAnotherInstanceShouldReturnDifferentContext() throws Exception {
    Vertx other = vertx();
    Context context = vertx.getOrCreateContext();
    context.runOnContext(v -> {
        Context otherContext = other.getOrCreateContext();
        assertNotSame(otherContext, context);
        testComplete();
    });
    await();
}
Also used : Context(io.vertx.core.Context) Vertx(io.vertx.core.Vertx) Test(org.junit.Test)

Example 25 with Context

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

the class ContextTest method testContextExceptionHandlerFailing.

@Test
public void testContextExceptionHandlerFailing() {
    RuntimeException failure = new RuntimeException();
    Context context = vertx.getOrCreateContext();
    AtomicInteger count = new AtomicInteger();
    context.exceptionHandler(err -> {
        if (count.getAndIncrement() == 0) {
            throw new RuntimeException();
        } else {
            assertSame(failure, err);
            testComplete();
        }
    });
    context.runOnContext(v -> {
        throw new RuntimeException();
    });
    context.runOnContext(v -> {
        throw failure;
    });
    await();
}
Also used : Context(io.vertx.core.Context) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Aggregations

Context (io.vertx.core.Context)125 Test (org.junit.Test)99 Handler (io.vertx.core.Handler)75 Vertx (io.vertx.core.Vertx)75 Before (org.junit.Before)58 Buffer (io.vertx.core.buffer.Buffer)57 TestContext (io.vertx.ext.unit.TestContext)57 Future (io.vertx.core.Future)53 Async (io.vertx.ext.unit.Async)53 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)53 RunWith (org.junit.runner.RunWith)53 JsonObject (io.vertx.core.json.JsonObject)49 HttpURLConnection (java.net.HttpURLConnection)48 StandardCharsets (java.nio.charset.StandardCharsets)48 Assert.assertThat (org.junit.Assert.assertThat)48 Mockito (org.mockito.Mockito)48 Rule (org.junit.Rule)39 CoreMatchers.is (org.hamcrest.CoreMatchers.is)38 ArgumentCaptor (org.mockito.ArgumentCaptor)38 Timeout (org.junit.rules.Timeout)36