Search in sources :

Example 6 with ChannelOutboundHandlerAdapter

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

the class Http2MultiplexCodecTest method failedOutboundStreamCreationThrowsAndClosesChannel.

/**
     * Test failing the promise of the first headers frame of an outbound stream. In practice this error case would most
     * likely happen due to the max concurrent streams limit being hit or the channel running out of stream identifiers.
     */
@Test(expected = Http2NoMoreStreamIdsException.class)
public void failedOutboundStreamCreationThrowsAndClosesChannel() throws Exception {
    parentChannel.pipeline().addFirst(new ChannelOutboundHandlerAdapter() {

        @Override
        public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
            promise.tryFailure(new Http2NoMoreStreamIdsException());
        }
    });
    LastInboundHandler inboundHandler = new LastInboundHandler();
    childChannelInitializer.handler = inboundHandler;
    Http2StreamChannelBootstrap b = new Http2StreamChannelBootstrap();
    Channel childChannel = b.parentChannel(parentChannel).handler(childChannelInitializer).connect().channel();
    assertTrue(childChannel.isActive());
    childChannel.writeAndFlush(new DefaultHttp2HeadersFrame(new DefaultHttp2Headers()));
    parentChannel.flush();
    assertFalse(childChannel.isActive());
    assertFalse(childChannel.isOpen());
    inboundHandler.checkException();
}
Also used : EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Channel(io.netty.channel.Channel) ChannelOutboundHandlerAdapter(io.netty.channel.ChannelOutboundHandlerAdapter) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelPromise(io.netty.channel.ChannelPromise) Test(org.junit.Test)

Example 7 with ChannelOutboundHandlerAdapter

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

the class EmbeddedChannelTest method testWriteScheduled.

@Test
public void testWriteScheduled() throws InterruptedException {
    final int delay = 500;
    EmbeddedChannel channel = new EmbeddedChannel(new ChannelOutboundHandlerAdapter() {

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

                @Override
                public void run() {
                    ctx.writeAndFlush(msg, promise);
                }
            }, delay, TimeUnit.MILLISECONDS);
        }
    });
    Object msg = new Object();
    assertFalse(channel.writeOutbound(msg));
    Thread.sleep(delay * 2);
    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)

Aggregations

ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)7 ChannelOutboundHandlerAdapter (io.netty.channel.ChannelOutboundHandlerAdapter)7 ChannelPromise (io.netty.channel.ChannelPromise)6 Test (org.junit.Test)6 ClosedChannelException (java.nio.channels.ClosedChannelException)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 Channel (io.netty.channel.Channel)2 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)2 ByteBuf (io.netty.buffer.ByteBuf)1 ChannelFuture (io.netty.channel.ChannelFuture)1 ChannelHandler (io.netty.channel.ChannelHandler)1 ChannelPipeline (io.netty.channel.ChannelPipeline)1 IOException (java.io.IOException)1 Iterator (java.util.Iterator)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 EnumPacketDirection (net.minecraft.network.EnumPacketDirection)1 NettyVarint21FrameDecoder (net.minecraft.network.NettyVarint21FrameDecoder)1 NettyVarint21FrameEncoder (net.minecraft.network.NettyVarint21FrameEncoder)1 Packet (net.minecraft.network.Packet)1 PacketBuffer (net.minecraft.network.PacketBuffer)1