Fix sharable check logic

Motivation:

There is an logic issue when checking if ChannelHandler is sharable.

Modification:

Corrected || to &&
This commit is contained in:
Ning Sun 2017-11-08 23:29:14 +08:00 committed by Scott Mitchell
parent ef5ebb40c9
commit 73e8122fc1
2 changed files with 31 additions and 1 deletions

View File

@ -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");
}

View File

@ -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());
}
}