From 73e8122fc1d1d5ba1b913ad5df377067cdf37242 Mon Sep 17 00:00:00 2001 From: Ning Sun Date: Wed, 8 Nov 2017 23:29:14 +0800 Subject: [PATCH] Fix sharable check logic Motivation: There is an logic issue when checking if ChannelHandler is sharable. Modification: Corrected || to && --- .../http2/Http2MultiplexCodecBuilder.java | 2 +- .../http2/Http2MultiplexCodecBuilderTest.java | 30 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/codec-http2/src/main/java/io/netty/handler/codec/http2/Http2MultiplexCodecBuilder.java b/codec-http2/src/main/java/io/netty/handler/codec/http2/Http2MultiplexCodecBuilder.java index dacac7aed0..8e0929094a 100644 --- a/codec-http2/src/main/java/io/netty/handler/codec/http2/Http2MultiplexCodecBuilder.java +++ b/codec-http2/src/main/java/io/netty/handler/codec/http2/Http2MultiplexCodecBuilder.java @@ -36,7 +36,7 @@ public class Http2MultiplexCodecBuilder } private static ChannelHandler checkSharable(ChannelHandler handler) { - if ((handler instanceof ChannelHandlerAdapter && !((ChannelHandlerAdapter) handler).isSharable()) || + if ((handler instanceof ChannelHandlerAdapter && !((ChannelHandlerAdapter) handler).isSharable()) && !handler.getClass().isAnnotationPresent(ChannelHandler.Sharable.class)) { throw new IllegalArgumentException("The handler must be Sharable"); } diff --git a/codec-http2/src/test/java/io/netty/handler/codec/http2/Http2MultiplexCodecBuilderTest.java b/codec-http2/src/test/java/io/netty/handler/codec/http2/Http2MultiplexCodecBuilderTest.java index c394689fb6..c12f5f8b7c 100644 --- a/codec-http2/src/test/java/io/netty/handler/codec/http2/Http2MultiplexCodecBuilderTest.java +++ b/codec-http2/src/test/java/io/netty/handler/codec/http2/Http2MultiplexCodecBuilderTest.java @@ -23,6 +23,7 @@ import io.netty.buffer.Unpooled; import io.netty.channel.Channel; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandler.Sharable; +import io.netty.channel.ChannelHandlerAdapter; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.channel.ChannelInitializer; @@ -225,4 +226,33 @@ public class Http2MultiplexCodecBuilderTest { ctx.fireChannelInactive(); } } + + private static class SharableChannelHandler1 extends ChannelHandlerAdapter { + @Override + public boolean isSharable() { + return true; + } + } + + @Sharable + private static class SharableChannelHandler2 extends ChannelHandlerAdapter { + } + + private static class UnsharableChannelHandler extends ChannelHandlerAdapter { + @Override + public boolean isSharable() { + return false; + } + } + + @Test + public void testSharableCheck() { + assertNotNull(Http2MultiplexCodecBuilder.forServer(new SharableChannelHandler1())); + assertNotNull(Http2MultiplexCodecBuilder.forServer(new SharableChannelHandler2())); + } + + @Test(expected = IllegalArgumentException.class) + public void testUnsharableHandler() { + Http2MultiplexCodecBuilder.forServer(new UnsharableChannelHandler()); + } }