千家信息网

netty使用EmbeddedChannel对channel

发表于:2026-10-31 作者:千家信息网编辑
千家信息网最后更新 2026年10月31日,一种特殊的Channel实现----EmbeddedChannel,它是Netty专门为改进针对ChannelHandler的单元测试而提供的。名称职责writeInbound将入站消息写到Embed
千家信息网最后更新 2026年10月31日netty使用EmbeddedChannel对channel

一种特殊的Channel实现----EmbeddedChannel,它是Netty专门为改进针对ChannelHandler的单元测试而提供的。

名称职责
writeInbound将入站消息写到EmbeddedChannel中。如果可以通过readInbound方法从EmbeddedChannel中读取数据,则返回true
readInbound从EmbeddedChannel中读取入站消息。任何返回东西都经过整个ChannelPipeline。如果没有任何可供读取的,则返回null
writeOutbound将出站消息写到EmbeddedChannel中,如果现在可以通过readOutbound从EmbeddedChannel中读取到东西,则返回true
readOutbound从EmbeddedChannel中读取出站消息。任何返回东西都经过整个ChannelPipeline。如果没有任何可供读取的,则返回null
finish将EmbeddedChannel标记为完成,如果有可读取的入站或出站数据,则返回true。这个方法还将会调用EmbeddedChannel上的close方法

测试入站消息

public class FixedLengthFrameDecoder extends ByteToMessageDecoder {    private final int frameLength;    public FixedLengthFrameDecoder(int frameLength) {        if (frameLength <= 0) {            throw new IllegalArgumentException("frameLength must be positive integer: " + frameLength);        }        this.frameLength = frameLength;    }    @Override    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) throws Exception {        while (in.readableBytes() >= frameLength) {            ByteBuf buf = in.readBytes(frameLength);            out.add(buf);        }    }}
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));        Assert.assertTrue(channel.writeInbound(input.retain()));        Assert.assertTrue(channel.finish());        ByteBuf read = channel.readInbound();        Assert.assertEquals(buf.readSlice(3), read);        read.release();        read = channel.readInbound();        Assert.assertEquals(buf.readSlice(3), read);        read.release();        read = channel.readInbound();        Assert.assertEquals(buf.readSlice(3), read);        read.release();        Assert.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));        Assert.assertFalse(channel.writeInbound(input.readBytes(2)));        Assert.assertTrue(channel.writeInbound(input.readBytes(7)));        Assert.assertTrue(channel.finish());        ByteBuf read = channel.readInbound();        Assert.assertEquals(buf.readSlice(3), read);        read.release();        read = channel.readInbound();        Assert.assertEquals(buf.readSlice(3), read);        read.release();        read = channel.readInbound();        Assert.assertEquals(buf.readSlice(3), read);        read.release();        Assert.assertNull(channel.readInbound());        buf.release();    }}

测试出站消息

public class AbsIntegerEncoder extends MessageToMessageEncoder {    @Override    protected void encode(ChannelHandlerContext channelHandlerContext, ByteBuf in, List out) throws Exception {        while (in.readableBytes() >= 4) {            int value = Math.abs(in.readInt());            out.add(value);        }    }}
public class AbsIntegerEncoderTest {    @Test    public void testEncoded() {        ByteBuf buf = Unpooled.buffer();        for (int i = 0; i < 10; i++) {            buf.writeInt(i * -1);        }        EmbeddedChannel channel = new EmbeddedChannel(new AbsIntegerEncoder());        Assert.assertTrue(channel.writeOutbound(buf));        Assert.assertTrue(channel.finish());        for (int i = 0; i < 10; i++) {            Assert.assertEquals(Integer.valueOf(i), channel.readOutbound());        }        Assert.assertNull(channel.readOutbound());    }}

测试异常处理

public class FrameChunkDecoder extends ByteToMessageDecoder {    private final int maxFrameSize;    public FrameChunkDecoder(int maxFrameSize) {        this.maxFrameSize = maxFrameSize;    }    @Override    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) throws Exception {        int readableBytes = in.readableBytes();        if (readableBytes > maxFrameSize) {            in.clear();            throw new TooLongFrameException();        }        ByteBuf buf = in.readBytes(readableBytes);        out.add(buf);    }}
public class FrameChunkDecoderTest {    @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 FrameChunkDecoder(3));        Assert.assertTrue(channel.writeInbound(input.readBytes(2)));        try {            channel.writeInbound(input.readBytes(4));            Assert.fail();        } catch (TooLongFrameException e) {        }        Assert.assertTrue(channel.writeInbound(input.readBytes(3)));        Assert.assertTrue(channel.finish());        ByteBuf read = channel.readInbound();        Assert.assertEquals(buf.readSlice(2), read);        read.release();        read = channel.readInbound();        Assert.assertEquals(buf.skipBytes(4).readSlice(3), read);        read.release();        buf.release();    }}
消息 测试 东西 方法 可以通过 数据 特殊 单元 名称 标记 职责 处理 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 河南云网互联网络科技有限公司 山东特亿宝互联网科技有限公司 为想互联网科技怎么样 互联网科技官网 互联网和科技创新 互联网的科技创新 华中科技大学互联网 齐家互联网科技有限公司 互联网科技股有哪些 新华互联网科技校企合作 须眉科技是互联网公司吗 银川新华互联网科技职业技术培训学校图片 互联网经济与金融科技 中国航天科技大学国际工业互联网 江阴互联网智能科技产品进口 科技类公司包括互联网吗 河南住房之家互联网科技 每天都有哪些好的互联网科技 风投北京互联网科技 承德科技产业互联网平台 互联网科技 科技大学互联网专业好不好 中国的互联网科技企业的崛起 福州新华互联网科技学校 知乎 互联网系的金融科技公司 互联网新科技动漫ppt素材 每天都有哪些好的互联网科技 城市宝互联网科技有限公司 上海同邻互联网科技有限公司规模 张家口科技产业互联网平台
0