netty5/codec/src/test/java/io/netty/handler/codec/ByteToMessageDecoderTest.java

76 lines
2.5 KiB
Java
Raw Normal View History

/*
* 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;
2013-08-02 07:24:49 +02:00
public class ByteToMessageDecoderTest {
@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));
b.release();
buf.release();
}
@Test
public void testRemoveItselfWriteBuffer() {
2013-08-02 07:24:49 +02:00
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<Object> 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'}));
buf.release();
b.release();
}
}