From ebee56ef494bea72576f4efb6db2ea6a78a7ad8a Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Fri, 2 Aug 2013 07:23:46 +0200 Subject: [PATCH] More tests related to self removal from pipeline in ByteToMessageDecoder and ReplayingDecoder --- .../codec/ByteToMessageHandlerTest.java | 23 ++++++++++++++++++ .../handler/codec/ReplayingDecoderTest.java | 24 ++++++++++++++++++- 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/codec/src/test/java/io/netty/handler/codec/ByteToMessageHandlerTest.java b/codec/src/test/java/io/netty/handler/codec/ByteToMessageHandlerTest.java index 17f64ba6a8..6ff5b2b133 100644 --- a/codec/src/test/java/io/netty/handler/codec/ByteToMessageHandlerTest.java +++ b/codec/src/test/java/io/netty/handler/codec/ByteToMessageHandlerTest.java @@ -45,4 +45,27 @@ public class ByteToMessageHandlerTest { ByteBuf b = (ByteBuf) channel.readInbound(); Assert.assertEquals(b, buf.skipBytes(1)); } + + @Test + public void testRemoveItselfWriteBuffer() { + final ByteBuf buf = Unpooled.buffer().writeBytes(new byte[] {'a', 'b', 'c'}); + EmbeddedChannel channel = new EmbeddedChannel(new ByteToMessageDecoder() { + private boolean removed; + + @Override + protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) throws Exception { + Assert.assertFalse(removed); + in.readByte(); + ctx.pipeline().remove(this); + + // This should not let it keep call decode + buf.writeByte('d'); + removed = true; + } + }); + + channel.writeInbound(buf.copy()); + ByteBuf b = (ByteBuf) channel.readInbound(); + Assert.assertEquals(b, Unpooled.wrappedBuffer(new byte[] { 'b', 'c'})); + } } diff --git a/codec/src/test/java/io/netty/handler/codec/ReplayingDecoderTest.java b/codec/src/test/java/io/netty/handler/codec/ReplayingDecoderTest.java index c6a03e97d9..7459d49f39 100644 --- a/codec/src/test/java/io/netty/handler/codec/ReplayingDecoderTest.java +++ b/codec/src/test/java/io/netty/handler/codec/ReplayingDecoderTest.java @@ -20,7 +20,6 @@ import io.netty.buffer.Unpooled; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.channel.embedded.EmbeddedChannel; -import org.junit.Assert; import org.junit.Test; import java.util.List; @@ -147,4 +146,27 @@ public class ReplayingDecoderTest { assertEquals("Expect to have still all bytes in the buffer", b, buf); } + + @Test + public void testRemoveItselfWriteBuffer() { + final ByteBuf buf = Unpooled.buffer().writeBytes(new byte[] {'a', 'b', 'c'}); + EmbeddedChannel channel = new EmbeddedChannel(new ReplayingDecoder() { + private boolean removed; + + @Override + protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) throws Exception { + assertFalse(removed); + in.readByte(); + ctx.pipeline().remove(this); + + // This should not let it keep call decode + buf.writeByte('d'); + removed = true; + } + }); + + channel.writeInbound(buf.copy()); + ByteBuf b = (ByteBuf) channel.readInbound(); + assertEquals(b, Unpooled.wrappedBuffer(new byte[] { 'b', 'c'})); + } }