diff --git a/transport/src/main/java/io/netty/channel/DefaultChannelPipeline.java b/transport/src/main/java/io/netty/channel/DefaultChannelPipeline.java index aaa35ed8f4..c5d8bb8d89 100644 --- a/transport/src/main/java/io/netty/channel/DefaultChannelPipeline.java +++ b/transport/src/main/java/io/netty/channel/DefaultChannelPipeline.java @@ -675,7 +675,7 @@ final class DefaultChannelPipeline implements ChannelPipeline { @Override public ChannelHandlerContext firstContext() { DefaultChannelHandlerContext first = head.next; - if (first == head) { + if (first == tail) { return null; } return head.next; diff --git a/transport/src/test/java/io/netty/channel/DefaultChannelPipelineTest.java b/transport/src/test/java/io/netty/channel/DefaultChannelPipelineTest.java index c783a8d79c..9958fe20e9 100644 --- a/transport/src/test/java/io/netty/channel/DefaultChannelPipelineTest.java +++ b/transport/src/test/java/io/netty/channel/DefaultChannelPipelineTest.java @@ -524,6 +524,30 @@ public class DefaultChannelPipelineTest { assertEquals(0, buffer.refCnt()); } + @Test + public void testFirstContextEmptyPipeline() throws Exception { + ChannelPipeline pipeline = new LocalChannel().pipeline(); + assertNull(pipeline.firstContext()); + } + + @Test + public void testLastContextEmptyPipeline() throws Exception { + ChannelPipeline pipeline = new LocalChannel().pipeline(); + assertNull(pipeline.lastContext()); + } + + @Test + public void testFirstHandlerEmptyPipeline() throws Exception { + ChannelPipeline pipeline = new LocalChannel().pipeline(); + assertNull(pipeline.first()); + } + + @Test + public void testLastHandlerEmptyPipeline() throws Exception { + ChannelPipeline pipeline = new LocalChannel().pipeline(); + assertNull(pipeline.last()); + } + private static int next(DefaultChannelHandlerContext ctx) { DefaultChannelHandlerContext next = ctx.next; if (next == null) {