netty中EmbeddedChannel如何使用
发表于:2025-12-01 作者:千家信息网编辑
千家信息网最后更新 2025年12月01日,这篇文章给大家介绍netty中EmbeddedChannel如何使用,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。一种特殊的Channel实现----EmbeddedChanne
千家信息网最后更新 2025年12月01日netty中EmbeddedChannel如何使用
这篇文章给大家介绍netty中EmbeddedChannel如何使用,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
一种特殊的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, Listpublic 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
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, Listpublic 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(); }}关于netty中EmbeddedChannel如何使用就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
消息
测试
东西
方法
内容
可以通过
数据
更多
帮助
不错
特殊
兴趣
单元
名称
小伙
小伙伴
文章
标记
知识
篇文章
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
服务器的速度由哪里决定
我的世界自由建造的服务器推荐
鹤壁打车软件开发
图书管理系统数据库的创建表
腾讯云服务器免费试用15天
服务器主板跳线的20针跳线
数据库的二叉树是什么意思
苹果4s服务器在
怎么用应用软件开发游戏
人大网络安全应急处置预案
服务器上架视频
小学生注意网络安全
附加数据库的步骤
能够用到串口服务器的设备
苹果应用软件开发者有啥风险
dede新闻数据库
大学生正确利用网络技术
天津网络安全保卫中心
互联网科技服务费
项目表数据库
怎么购买阿里云服务器
泰拉瑞亚国内版服务器电脑
新中大财务软件修复数据库
家里路由器提示dns服务器异常
sdept in数据库
川沙附近有教软件开发的吗
中国网络安全技术大赛
我的世界如何进入自己开的服务器
大学生正确利用网络技术
数据库的简单介绍