2013-08-01 20:37:26 +02:00
|
|
|
/*
|
|
|
|
* 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 {
|
2013-08-01 20:37:26 +02:00
|
|
|
|
|
|
|
@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));
|
2013-12-06 12:40:11 +01:00
|
|
|
b.release();
|
|
|
|
buf.release();
|
2013-08-01 20:37:26 +02:00
|
|
|
}
|
2013-08-02 07:23:46 +02:00
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testRemoveItselfWriteBuffer() {
|
2013-08-02 07:24:49 +02:00
|
|
|
final ByteBuf buf = Unpooled.buffer().writeBytes(new byte[]{'a', 'b', 'c'});
|
2013-08-02 07:23:46 +02:00
|
|
|
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'}));
|
2013-12-06 12:40:11 +01:00
|
|
|
buf.release();
|
|
|
|
b.release();
|
2013-08-02 07:23:46 +02:00
|
|
|
}
|
2013-08-01 20:37:26 +02:00
|
|
|
}
|