Add testcases to prove that remove ReplayingDecoder/ByteToMessageDecoder from within the decode(...) method works
This commit is contained in:
parent
3f2000fa3a
commit
487c28d93e
@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Copyright 2013 The Netty Project
|
||||
*
|
||||
* The Netty Project licenses this file to you under the Apache License,
|
||||
* version 2.0 (the "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package io.netty.handler.codec;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.embedded.EmbeddedChannel;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class ByteToMessageHandlerTest {
|
||||
|
||||
@Test
|
||||
public void testRemoveItself() {
|
||||
EmbeddedChannel channel = new EmbeddedChannel(new ByteToMessageDecoder() {
|
||||
private boolean removed;
|
||||
|
||||
@Override
|
||||
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
|
||||
Assert.assertFalse(removed);
|
||||
in.readByte();
|
||||
ctx.pipeline().remove(this);
|
||||
removed = true;
|
||||
}
|
||||
});
|
||||
|
||||
ByteBuf buf = Unpooled.wrappedBuffer(new byte[] {'a', 'b', 'c'});
|
||||
channel.writeInbound(buf.copy());
|
||||
ByteBuf b = (ByteBuf) channel.readInbound();
|
||||
Assert.assertEquals(b, buf.skipBytes(1));
|
||||
}
|
||||
}
|
@ -20,6 +20,7 @@ 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;
|
||||
@ -103,4 +104,47 @@ public class ReplayingDecoderTest {
|
||||
assertEquals(Unpooled.wrappedBuffer(new byte[] {'B' }), ch.readInbound());
|
||||
assertNull(ch.readInbound());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemoveItself() {
|
||||
EmbeddedChannel channel = new EmbeddedChannel(new ReplayingDecoder() {
|
||||
private boolean removed;
|
||||
|
||||
@Override
|
||||
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
|
||||
assertFalse(removed);
|
||||
in.readByte();
|
||||
ctx.pipeline().remove(this);
|
||||
removed = true;
|
||||
}
|
||||
});
|
||||
|
||||
ByteBuf buf = Unpooled.wrappedBuffer(new byte[] {'a', 'b', 'c'});
|
||||
channel.writeInbound(buf.copy());
|
||||
ByteBuf b = (ByteBuf) channel.readInbound();
|
||||
assertEquals(b, buf.skipBytes(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemoveItselfWithReplayError() {
|
||||
EmbeddedChannel channel = new EmbeddedChannel(new ReplayingDecoder() {
|
||||
private boolean removed;
|
||||
|
||||
@Override
|
||||
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
|
||||
assertFalse(removed);
|
||||
ctx.pipeline().remove(this);
|
||||
|
||||
in.readBytes(1000);
|
||||
|
||||
removed = true;
|
||||
}
|
||||
});
|
||||
|
||||
ByteBuf buf = Unpooled.wrappedBuffer(new byte[] {'a', 'b', 'c'});
|
||||
channel.writeInbound(buf.copy());
|
||||
ByteBuf b = (ByteBuf) channel.readInbound();
|
||||
|
||||
assertEquals("Expect to have still all bytes in the buffer", b, buf);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user