Search in sources :

Example 36 with ChannelHandler

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

the class MessageToMessageEncoderTest method testIntermediateWriteFailures.

@Test
public void testIntermediateWriteFailures() {
    ChannelHandler encoder = new MessageToMessageEncoder<Object>() {

        @Override
        protected void encode(ChannelHandlerContext ctx, Object msg, List<Object> out) {
            out.add(new Object());
            out.add(msg);
        }
    };
    final Exception firstWriteException = new Exception();
    ChannelHandler writeThrower = new ChannelOutboundHandlerAdapter() {

        private boolean firstWritten;

        @Override
        public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
            if (firstWritten) {
                ctx.write(msg, promise);
            } else {
                firstWritten = true;
                promise.setFailure(firstWriteException);
            }
        }
    };
    EmbeddedChannel channel = new EmbeddedChannel(writeThrower, encoder);
    Object msg = new Object();
    ChannelFuture write = channel.writeAndFlush(msg);
    assertSame(firstWriteException, write.cause());
    assertSame(msg, channel.readOutbound());
    assertFalse(channel.finish());
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) ChannelOutboundHandlerAdapter(io.netty.channel.ChannelOutboundHandlerAdapter) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) List(java.util.List) ChannelPromise(io.netty.channel.ChannelPromise) ChannelHandler(io.netty.channel.ChannelHandler) Test(org.junit.jupiter.api.Test)

Example 37 with ChannelHandler

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

the class OptionalSslHandlerTest method handlerReplaced.

@Test
public void handlerReplaced() throws Exception {
    final ChannelHandler nonSslHandler = Mockito.mock(ChannelHandler.class);
    OptionalSslHandler handler = new OptionalSslHandler(sslContext) {

        @Override
        protected ChannelHandler newNonSslHandler(ChannelHandlerContext context) {
            return nonSslHandler;
        }

        @Override
        protected String newNonSslHandlerName() {
            return HANDLER_NAME;
        }
    };
    final ByteBuf payload = Unpooled.copiedBuffer("plaintext".getBytes());
    try {
        handler.decode(context, payload, null);
        verify(pipeline).replace(handler, HANDLER_NAME, nonSslHandler);
    } finally {
        payload.release();
    }
}
Also used : ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelHandler(io.netty.channel.ChannelHandler) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.jupiter.api.Test)

Example 38 with ChannelHandler

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

the class Http2MultiplexTest method outboundStreamShouldWriteResetFrameOnClose_headersSent.

@Test
public void outboundStreamShouldWriteResetFrameOnClose_headersSent() {
    ChannelHandler handler = new ChannelInboundHandlerAdapter() {

        @Override
        public void channelActive(ChannelHandlerContext ctx) {
            ctx.writeAndFlush(new DefaultHttp2HeadersFrame(new DefaultHttp2Headers()));
            ctx.fireChannelActive();
        }
    };
    Http2StreamChannel childChannel = newOutboundStream(handler);
    assertTrue(childChannel.isActive());
    childChannel.close();
    verify(frameWriter).writeRstStream(eqCodecCtx(), eqStreamId(childChannel), eq(Http2Error.CANCEL.code()), anyChannelPromise());
}
Also used : ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelHandler(io.netty.channel.ChannelHandler) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.jupiter.api.Test)

Example 39 with ChannelHandler

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

the class Http2MultiplexTest method endOfStreamDoesNotDiscardData.

@Test
public void endOfStreamDoesNotDiscardData() {
    AtomicInteger numReads = new AtomicInteger(1);
    final AtomicBoolean shouldDisableAutoRead = new AtomicBoolean();
    Consumer<ChannelHandlerContext> ctxConsumer = new Consumer<ChannelHandlerContext>() {

        @Override
        public void accept(ChannelHandlerContext obj) {
            if (shouldDisableAutoRead.get()) {
                obj.channel().config().setAutoRead(false);
            }
        }
    };
    LastInboundHandler inboundHandler = new LastInboundHandler(ctxConsumer);
    Http2StreamChannel childChannel = newInboundStream(3, false, numReads, inboundHandler);
    childChannel.config().setAutoRead(false);
    Http2DataFrame dataFrame1 = new DefaultHttp2DataFrame(bb("1")).stream(childChannel.stream());
    Http2DataFrame dataFrame2 = new DefaultHttp2DataFrame(bb("2")).stream(childChannel.stream());
    Http2DataFrame dataFrame3 = new DefaultHttp2DataFrame(bb("3")).stream(childChannel.stream());
    Http2DataFrame dataFrame4 = new DefaultHttp2DataFrame(bb("4")).stream(childChannel.stream());
    assertEquals(new DefaultHttp2HeadersFrame(request).stream(childChannel.stream()), inboundHandler.readInbound());
    ChannelHandler readCompleteSupressHandler = new ChannelInboundHandlerAdapter() {

        @Override
        public void channelReadComplete(ChannelHandlerContext ctx) {
        // We want to simulate the parent channel calling channelRead and delay calling channelReadComplete.
        }
    };
    parentChannel.pipeline().addFirst(readCompleteSupressHandler);
    frameInboundWriter.writeInboundData(childChannel.stream().id(), bb("1"), 0, false);
    assertEqualsAndRelease(dataFrame1, inboundHandler.<Http2DataFrame>readInbound());
    // Deliver frames, and then a stream closed while read is inactive.
    frameInboundWriter.writeInboundData(childChannel.stream().id(), bb("2"), 0, false);
    frameInboundWriter.writeInboundData(childChannel.stream().id(), bb("3"), 0, false);
    frameInboundWriter.writeInboundData(childChannel.stream().id(), bb("4"), 0, false);
    shouldDisableAutoRead.set(true);
    childChannel.config().setAutoRead(true);
    numReads.set(1);
    frameInboundWriter.writeInboundRstStream(childChannel.stream().id(), Http2Error.NO_ERROR.code());
    // Detecting EOS should flush all pending data regardless of read calls.
    assertEqualsAndRelease(dataFrame2, inboundHandler.<Http2DataFrame>readInbound());
    assertNull(inboundHandler.readInbound());
    // As we limited the number to 1 we also need to call read() again.
    childChannel.read();
    assertEqualsAndRelease(dataFrame3, inboundHandler.<Http2DataFrame>readInbound());
    assertEqualsAndRelease(dataFrame4, inboundHandler.<Http2DataFrame>readInbound());
    Http2ResetFrame resetFrame = useUserEventForResetFrame() ? inboundHandler.<Http2ResetFrame>readUserEvent() : inboundHandler.<Http2ResetFrame>readInbound();
    assertEquals(childChannel.stream(), resetFrame.stream());
    assertEquals(Http2Error.NO_ERROR.code(), resetFrame.errorCode());
    assertNull(inboundHandler.readInbound());
    // Now we want to call channelReadComplete and simulate the end of the read loop.
    parentChannel.pipeline().remove(readCompleteSupressHandler);
    parentChannel.flushInbound();
    childChannel.closeFuture().syncUninterruptibly();
}
Also used : ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelHandler(io.netty.channel.ChannelHandler) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Consumer(io.netty.handler.codec.http2.LastInboundHandler.Consumer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.jupiter.api.Test)

Example 40 with ChannelHandler

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

the class Http2MultiplexTest method setUp.

@BeforeEach
public void setUp() {
    childChannelInitializer = new TestChannelInitializer();
    parentChannel = new EmbeddedChannel();
    frameInboundWriter = new Http2FrameInboundWriter(parentChannel);
    parentChannel.connect(new InetSocketAddress(0));
    frameWriter = Http2TestUtil.mockedFrameWriter();
    codec = newCodec(childChannelInitializer, frameWriter);
    parentChannel.pipeline().addLast(codec);
    ChannelHandler multiplexer = newMultiplexer(childChannelInitializer);
    if (multiplexer != null) {
        parentChannel.pipeline().addLast(multiplexer);
    }
    parentChannel.runPendingTasks();
    parentChannel.pipeline().fireChannelActive();
    parentChannel.writeInbound(Http2CodecUtil.connectionPrefaceBuf());
    Http2Settings settings = new Http2Settings().initialWindowSize(initialRemoteStreamWindow);
    frameInboundWriter.writeInboundSettings(settings);
    verify(frameWriter).writeSettingsAck(eqCodecCtx(), anyChannelPromise());
    frameInboundWriter.writeInboundSettingsAck();
    Http2SettingsFrame settingsFrame = parentChannel.readInbound();
    assertNotNull(settingsFrame);
    Http2SettingsAckFrame settingsAckFrame = parentChannel.readInbound();
    assertNotNull(settingsAckFrame);
    // Handshake
    verify(frameWriter).writeSettings(eqCodecCtx(), anyHttp2Settings(), anyChannelPromise());
}
Also used : InetSocketAddress(java.net.InetSocketAddress) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ChannelHandler(io.netty.channel.ChannelHandler) Http2TestUtil.anyHttp2Settings(io.netty.handler.codec.http2.Http2TestUtil.anyHttp2Settings) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

ChannelHandler (io.netty.channel.ChannelHandler)186 Test (org.junit.Test)88 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)44 Channel (io.netty.channel.Channel)26 ChannelPipeline (io.netty.channel.ChannelPipeline)25 SslHandler (io.netty.handler.ssl.SslHandler)25 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)22 FilterChainMatchingHandler (io.grpc.xds.FilterChainMatchingProtocolNegotiators.FilterChainMatchingHandler)20 ChannelFuture (io.netty.channel.ChannelFuture)20 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)20 FilterChainSelector (io.grpc.xds.FilterChainMatchingProtocolNegotiators.FilterChainMatchingHandler.FilterChainSelector)19 ChannelHandlerAdapter (io.netty.channel.ChannelHandlerAdapter)18 DownstreamTlsContext (io.grpc.xds.EnvoyServerProtoData.DownstreamTlsContext)17 FilterChain (io.grpc.xds.EnvoyServerProtoData.FilterChain)17 InetSocketAddress (java.net.InetSocketAddress)16 Test (org.junit.jupiter.api.Test)16 LineBasedFrameDecoder (io.netty.handler.codec.LineBasedFrameDecoder)12 AtomicReference (java.util.concurrent.atomic.AtomicReference)12 Bootstrap (io.netty.bootstrap.Bootstrap)11 ArrayList (java.util.ArrayList)11