netty(八)--EmbeddedChannel单元测试

最佳实践要求你的测试不仅要能够证明你的实现是正确的,而且容易隔离那些因修改代码而突然出现的问题。这种类型的测试叫做单元测试
EmbeddedChannel是netty专门改进针对ChannelHandler的单元测试而提供的。
核心方法:
在这里插入图片描述
数据流:
在这里插入图片描述


测试入站消息

测试1是直接写入9个字节,被拆分成3个桢,
测试2是分2次写入,第一次2个字节,第二次7个字节,结果和测试1一样。
finish方法将EmbeddedChannel标记为已完成状态。

public class FixedLengthFrameDecoderTest {
    @Test
    public void testFramesDecoded(){
        ByteBuf buf= Unpooled.buffer();
        for (int i=0;i<9;i++){
            buf.writeByte(i);
        }
        ByteBuf input=buf.duplicate();
        EmbeddedChannel channel=new EmbeddedChannel(
                new FixedLengthFrameDecoder(3)
        );
        assertTrue(channel.writeInbound(input.retain()));
        assertTrue(channel.finish());

        //读取消息
        ByteBuf read=channel.readInbound();
        assertEquals(buf.readSlice(3),read);
        read.release();

        read = (ByteBuf) channel.readInbound();
        assertEquals(buf.readSlice(3), read);
        read.release();
        read = (ByteBuf) channel.readInbound();
        assertEquals(buf.readSlice(3), read);
        read.release();
        assertNull(channel.readInbound());
        buf.release();
    }
    @Test
    public void testFramesDecoded2() {
        ByteBuf buf = Unpooled.buffer();
        for (int i = 0; i < 9; i++) {
            buf.writeByte(i);
        }
        ByteBuf input = buf.duplicate();
        EmbeddedChannel channel = new EmbeddedChannel(
                new FixedLengthFrameDecoder(3));
        assertFalse(channel.writeInbound(input.readBytes(2)));
        assertTrue(channel.writeInbound(input.readBytes(7)));
        assertTrue(channel.finish());
        ByteBuf read = (ByteBuf) channel.readInbound();
        ByteBuf b2=buf.readSlice(3);
        assertEquals(b2, read);
        read.release();
        read = (ByteBuf) channel.readInbound();
        assertEquals(buf.readSlice(3), read);
        read.release();
        read = (ByteBuf) channel.readInbound();
        assertEquals(buf.readSlice(3), read);
        read.release();
        assertNull(channel.readInbound());
        buf.release();
    }

}

解码器:

public class FixedLengthFrameDecoder extends ByteToMessageDecoder {
    private final int frameLength;

    public FixedLengthFrameDecoder(int frameLength) {
        this.frameLength = frameLength;
    }

    @Override
    protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception {
        while (byteBuf.readableBytes()>=frameLength){
            ByteBuf buffer=byteBuf.readBytes(frameLength);
            list.add(buffer);
        }
    }
}


测试出站消息

示例按照下列方式工作:

  1. 持有AbsIntegerEncoder的EmbeddedChannel以4字节的负整数形式写出站数据。
  2. 编码器将从传入的ByteBuf中读取每个负整数,并调用Math.abs方法获取绝对值
  3. 编码器将每个负整数的绝对值写到ChannelPipeline中
    在这里插入图片描述

编码器:

public class AbsIntegerEncoder extends MessageToMessageEncoder<ByteBuf> {
    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception {
        while (byteBuf.readableBytes()>=4){
            int value=Math.abs(byteBuf.readInt());
            list.add(value);
        }
    }
}

测试用例:

public class AbsIntegerEncoderTest {
    @Test
    public void testEncoded(){
        ByteBuf buf= Unpooled.buffer();
        for (int i=1;i<10;i++){
            buf.writeInt(i*-1);
        }

        EmbeddedChannel embeddedChannel=new EmbeddedChannel(
            new AbsIntegerEncoder()
        );

        Assert.assertTrue(embeddedChannel.writeOutbound(buf));
        Assert.assertTrue(embeddedChannel.finish());

        for (int i=1;i<10;i++){
            int a= embeddedChannel.readOutbound();
            assertEquals(i,a);
        }

        assertNull(embeddedChannel.readOutbound());
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值