use of io.vertx.core.impl.ContextInternal in project vert.x by eclipse.
the class EventLoopGroupTest method testNettyServerUsesContextEventLoop.
@Test
public void testNettyServerUsesContextEventLoop() throws Exception {
ContextInternal context = (ContextInternal) vertx.getOrCreateContext();
AtomicReference<Thread> contextThread = new AtomicReference<>();
CountDownLatch latch = new CountDownLatch(1);
context.runOnContext(v -> {
contextThread.set(Thread.currentThread());
latch.countDown();
});
awaitLatch(latch);
ServerBootstrap bs = new ServerBootstrap();
bs.group(context.nettyEventLoop());
bs.channel(NioServerSocketChannel.class);
bs.option(ChannelOption.SO_BACKLOG, 100);
bs.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
assertSame(contextThread.get(), Thread.currentThread());
context.executeFromIO(() -> {
assertSame(contextThread.get(), Thread.currentThread());
assertSame(context, Vertx.currentContext());
ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
assertSame(contextThread.get(), Thread.currentThread());
context.executeFromIO(() -> {
assertSame(contextThread.get(), Thread.currentThread());
assertSame(context, Vertx.currentContext());
});
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ByteBuf buf = (ByteBuf) msg;
assertEquals("hello", buf.toString(StandardCharsets.UTF_8));
assertSame(contextThread.get(), Thread.currentThread());
context.executeFromIO(() -> {
assertSame(contextThread.get(), Thread.currentThread());
assertSame(context, Vertx.currentContext());
});
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
assertSame(contextThread.get(), Thread.currentThread());
context.executeFromIO(() -> {
assertSame(contextThread.get(), Thread.currentThread());
assertSame(context, Vertx.currentContext());
ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
});
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
assertSame(contextThread.get(), Thread.currentThread());
context.executeFromIO(() -> {
assertSame(contextThread.get(), Thread.currentThread());
assertSame(context, Vertx.currentContext());
testComplete();
});
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
fail(cause.getMessage());
}
});
});
}
});
bs.bind("localhost", 1234).sync();
vertx.createNetClient(new NetClientOptions()).connect(1234, "localhost", ar -> {
assertTrue(ar.succeeded());
NetSocket so = ar.result();
so.write(Buffer.buffer("hello"));
});
await();
}
use of io.vertx.core.impl.ContextInternal in project vert.x by eclipse.
the class ContextTest method testWorkerExecuteFromIo.
@Test
public void testWorkerExecuteFromIo() throws Exception {
AtomicReference<ContextInternal> workerContext = new AtomicReference<>();
CountDownLatch latch = new CountDownLatch(1);
vertx.deployVerticle(new AbstractVerticle() {
@Override
public void start() throws Exception {
workerContext.set((ContextInternal) context);
latch.countDown();
}
}, new DeploymentOptions().setWorker(true));
awaitLatch(latch);
workerContext.get().nettyEventLoop().execute(() -> {
assertNull(Vertx.currentContext());
workerContext.get().executeFromIO(() -> {
assertSame(workerContext.get(), Vertx.currentContext());
assertTrue(Context.isOnWorkerThread());
testComplete();
});
});
await();
}
use of io.vertx.core.impl.ContextInternal in project vert.x by eclipse.
the class ContextTest method testEventLoopExecuteFromIo.
@Test
public void testEventLoopExecuteFromIo() throws Exception {
ContextInternal eventLoopContext = (ContextInternal) vertx.getOrCreateContext();
// Check from other thread
try {
eventLoopContext.executeFromIO(this::fail);
fail();
} catch (IllegalStateException expected) {
}
// Check from event loop thread
eventLoopContext.nettyEventLoop().execute(() -> {
// Should not be set yet
assertNull(Vertx.currentContext());
Thread vertxThread = Thread.currentThread();
AtomicBoolean nested = new AtomicBoolean(true);
eventLoopContext.executeFromIO(() -> {
assertTrue(nested.get());
assertSame(eventLoopContext, Vertx.currentContext());
assertSame(vertxThread, Thread.currentThread());
});
nested.set(false);
testComplete();
});
await();
}
use of io.vertx.core.impl.ContextInternal 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();
}
Aggregations