Search in sources :

Example 81 with ChannelHandlerContext

use of io.netty.channel.ChannelHandlerContext in project netty by netty.

the class EmbeddedChannelTest method testFireChannelInactiveAndUnregistered.

private static void testFireChannelInactiveAndUnregistered(Action action) throws InterruptedException {
    final CountDownLatch latch = new CountDownLatch(3);
    EmbeddedChannel channel = new EmbeddedChannel(new ChannelInboundHandlerAdapter() {

        @Override
        public void channelInactive(ChannelHandlerContext ctx) throws Exception {
            latch.countDown();
            ctx.executor().execute(new Runnable() {

                @Override
                public void run() {
                    // Should be executed.
                    latch.countDown();
                }
            });
        }

        @Override
        public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
            latch.countDown();
        }
    });
    action.doRun(channel).syncUninterruptibly();
    latch.await();
}
Also used : ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) CountDownLatch(java.util.concurrent.CountDownLatch) ClosedChannelException(java.nio.channels.ClosedChannelException) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter)

Example 82 with ChannelHandlerContext

use of io.netty.channel.ChannelHandlerContext in project netty by netty.

the class EmbeddedChannelTest method testConstructWithChannelInitializer.

@Test
public void testConstructWithChannelInitializer() {
    final Integer first = 1;
    final Integer second = 2;
    final ChannelHandler handler = new ChannelInboundHandlerAdapter() {

        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
            ctx.fireChannelRead(first);
            ctx.fireChannelRead(second);
        }
    };
    EmbeddedChannel channel = new EmbeddedChannel(new ChannelInitializer<Channel>() {

        @Override
        protected void initChannel(Channel ch) throws Exception {
            ch.pipeline().addLast(handler);
        }
    });
    ChannelPipeline pipeline = channel.pipeline();
    assertSame(handler, pipeline.firstContext().handler());
    assertTrue(channel.writeInbound(3));
    assertTrue(channel.finish());
    assertSame(first, channel.readInbound());
    assertSame(second, channel.readInbound());
    assertNull(channel.readInbound());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Channel(io.netty.channel.Channel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelHandler(io.netty.channel.ChannelHandler) ClosedChannelException(java.nio.channels.ClosedChannelException) ChannelPipeline(io.netty.channel.ChannelPipeline) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.Test)

Example 83 with ChannelHandlerContext

use of io.netty.channel.ChannelHandlerContext in project netty by netty.

the class EmbeddedChannelTest method testWriteLater.

@Test
public void testWriteLater() {
    EmbeddedChannel channel = new EmbeddedChannel(new ChannelOutboundHandlerAdapter() {

        @Override
        public void write(final ChannelHandlerContext ctx, final Object msg, final ChannelPromise promise) throws Exception {
            ctx.executor().execute(new Runnable() {

                @Override
                public void run() {
                    ctx.write(msg, promise);
                }
            });
        }
    });
    Object msg = new Object();
    assertTrue(channel.writeOutbound(msg));
    assertTrue(channel.finish());
    assertSame(msg, channel.readOutbound());
    assertNull(channel.readOutbound());
}
Also used : ChannelOutboundHandlerAdapter(io.netty.channel.ChannelOutboundHandlerAdapter) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelPromise(io.netty.channel.ChannelPromise) ClosedChannelException(java.nio.channels.ClosedChannelException) Test(org.junit.Test)

Example 84 with ChannelHandlerContext

use of io.netty.channel.ChannelHandlerContext in project netty by netty.

the class EmbeddedChannelTest method testHandlerAddedExecutedInEventLoop.

@Test(timeout = 3000)
public void testHandlerAddedExecutedInEventLoop() throws Throwable {
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<Throwable> error = new AtomicReference<Throwable>();
    final ChannelHandler handler = new ChannelHandlerAdapter() {

        @Override
        public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
            try {
                assertTrue(ctx.executor().inEventLoop());
            } catch (Throwable cause) {
                error.set(cause);
            } finally {
                latch.countDown();
            }
        }
    };
    EmbeddedChannel channel = new EmbeddedChannel(handler);
    assertFalse(channel.finish());
    latch.await();
    Throwable cause = error.get();
    if (cause != null) {
        throw cause;
    }
}
Also used : ChannelHandlerAdapter(io.netty.channel.ChannelHandlerAdapter) AtomicReference(java.util.concurrent.atomic.AtomicReference) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelHandler(io.netty.channel.ChannelHandler) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 85 with ChannelHandlerContext

use of io.netty.channel.ChannelHandlerContext in project netty by netty.

the class EmbeddedChannelTest method testWriteOneOutbound.

@Test
public void testWriteOneOutbound() throws InterruptedException {
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicInteger flushCount = new AtomicInteger(0);
    EmbeddedChannel channel = new EmbeddedChannel(new ChannelOutboundHandlerAdapter() {

        @Override
        public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
            ctx.write(msg, promise);
            latch.countDown();
        }

        @Override
        public void flush(ChannelHandlerContext ctx) throws Exception {
            flushCount.incrementAndGet();
        }
    });
    // This shouldn't trigger a #flush()
    channel.writeOneOutbound("Hello, Netty!");
    if (!latch.await(1L, TimeUnit.SECONDS)) {
        fail("Nobody called #write() in time.");
    }
    channel.close().syncUninterruptibly();
    // There was no #flushOutbound() call so nobody should have called #flush()
    assertEquals(0, flushCount.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ChannelOutboundHandlerAdapter(io.netty.channel.ChannelOutboundHandlerAdapter) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelPromise(io.netty.channel.ChannelPromise) CountDownLatch(java.util.concurrent.CountDownLatch) ClosedChannelException(java.nio.channels.ClosedChannelException) Test(org.junit.Test)

Aggregations

ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)266 Test (org.junit.Test)150 Channel (io.netty.channel.Channel)106 ByteBuf (io.netty.buffer.ByteBuf)94 ChannelFuture (io.netty.channel.ChannelFuture)87 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)77 Bootstrap (io.netty.bootstrap.Bootstrap)69 ChannelPipeline (io.netty.channel.ChannelPipeline)68 ClosedChannelException (java.nio.channels.ClosedChannelException)63 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)57 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)56 InetSocketAddress (java.net.InetSocketAddress)54 AtomicReference (java.util.concurrent.atomic.AtomicReference)53 EventLoopGroup (io.netty.channel.EventLoopGroup)48 CountDownLatch (java.util.concurrent.CountDownLatch)47 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)45 ArrayList (java.util.ArrayList)45 List (java.util.List)43 IOException (java.io.IOException)42 Map (java.util.Map)40