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);
}
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();
}
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();
}
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();
}
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();
}
Aggregations