Search in sources :

Example 1 with ContextInternal

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();
}
Also used : NetSocket(io.vertx.core.net.NetSocket) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) NetClientOptions(io.vertx.core.net.NetClientOptions) ContextInternal(io.vertx.core.impl.ContextInternal) AtomicReference(java.util.concurrent.atomic.AtomicReference) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuf(io.netty.buffer.ByteBuf) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.Test)

Example 2 with ContextInternal

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();
}
Also used : DeploymentOptions(io.vertx.core.DeploymentOptions) AtomicReference(java.util.concurrent.atomic.AtomicReference) ContextInternal(io.vertx.core.impl.ContextInternal) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractVerticle(io.vertx.core.AbstractVerticle) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) Test(org.junit.Test)

Example 3 with ContextInternal

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();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ContextInternal(io.vertx.core.impl.ContextInternal) Test(org.junit.Test)

Example 4 with ContextInternal

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();
}
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)4 Test (org.junit.Test)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)1 ByteBuf (io.netty.buffer.ByteBuf)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)1 SocketChannel (io.netty.channel.socket.SocketChannel)1 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)1 AbstractVerticle (io.vertx.core.AbstractVerticle)1 DeploymentOptions (io.vertx.core.DeploymentOptions)1 TaskQueue (io.vertx.core.impl.TaskQueue)1 NetClientOptions (io.vertx.core.net.NetClientOptions)1 NetSocket (io.vertx.core.net.NetSocket)1 CyclicBarrier (java.util.concurrent.CyclicBarrier)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1