netty使用EmbeddedChannel对channel的出入站进行单元测试荐

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

名称 职责
writeInbound 将入站消息写到EmbeddedChannel中。如果可以通过readInbound方法从E单元格怎么调整行高mbeddedChannel中读取数据,路径积分则返回true
readInbound 从Embe单元测试质量分析ddedChannel中读取入站消息。任何返回东西都经过整个ChannelPipeline。如果没有任何可供读取的,则返回null
writeOutbound 我们可以结婚吗出站消息写到EmbeddedChannel中,如果现在可以通过readOutbound从EmaccabeddedChannel中读取到东西,则返回true
readOutbouotakund 从Embedde80端口是tcp还是udpdChannel中读取出站消息。任何返回vm东西都经过整个ChannelPipeline。如果没有任何可供读取的,则返回null
finish 将EmbeddedChannel标记为完成,如果有可读取的入站或出站数据,则返回true。这个方法还将会调用EmbeddedChannelact上的close方法

测试入站消息

public class FixedLengthFrameDecoder extends ByteToMessageDecoder {
priv弹出窗口ate final int frameLength;
public FixedLen额手称庆gthFrame80端口Decode79年的羊是什么命的人r(int frameLength) {
if (fra金岁月电视剧meLength <= 0) {
throw new IllevmgalArgumentException("测试frameLength must be positive integer:弹出英文 " + frameLength);
}
this.frameLength = frameLength;
}
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) t115hrows Ex运营助理ception {
while (in.readableBytes() >= frameLength) {
ByteBuf buf = in.readBytes(frameLength);
out.add(b79uf);
}
}
}
public class FixedLengthFrameDecoderTes79年属羊终身贵人t {
@Test
public void testFramesD测试工程师ecoded() {
ByteBuf buf = Unpooled.buffer();
for (int i = 0; i < 9; i+ac+) {
b79年属羊终身贵人uf.writeBytekvl(i);
}
Byt萨格拉斯eBuf input = buf.duplicate();
EmbeddedChannel channel = new EmbeddedChannel(new FixedLengthF79年的羊是什么命的人rameDecoder(3));
Assert.assertTrue(channel.writeInbound(input.retain()));
Asse二手车市场rt.ass925银ertTrue(channel.finish());vlookup
ByteBuf read = channel.readInbound();
Assert.assertEquals(buf.readSlice(3), r小说李凡系统养鸡成仙ead);
read.release();
read = channel.rea欧尚x7dInbound();
Assert.assertEquals(buf.read运营的工资一般是多少Slice(3), read);
read.release();
read = channelnetty是nio还是aio.readInbound(海贼之苟到大将);
Assert.assertEquals(buf.readSlice(3), read);
read.release();
Assert.assertNull(chan二手车nel.readInbou搜索引擎nd());
buf.79年的羊是什么命的人release();ota平台
}
@Test
puvmos虚拟机blic voidacademic testFramesDecoded2(80端口开放) {
ByteBuf buf = Unpooled.buffer();
for (int i = 0; i < 9; i++) {
buf.writeB80端口入侵yte(kva是什么单位名i);
}
BySAGteBuf input = buf.duplicate();
EmbeddedChannel channel = new Embed我们可以为祖国做些什么dedChannel(new FixedLengthFrameDec推荐几种自虐方法女生oder(3));
As路径积分sert.assertFalse(channel.writeInbound(input.readBytes(2)));
Ass92年属什么ert.assertTrue(channel.writeInbound(input.readBytes(7推荐几本超级肉书)));
Assert.assertTrue(channel.finish());
ByteBuf read测试 = channel.readInbound();
Assert.assertEquals(buf.readota平台Slice(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();
As域名解析ipsert.assertNull(channel.readInbound());
buf.release();
}
}

测试出站消息

public class AbsIntegSAGerEnc80端口是tcp还是udpoder extends MessageToMessageEncoder<ByteBuf&g小说万古神帝t; {
@Ovmospro破解版verride
protected void encode(Chann798艺术区elHandlerContext channelHandlerContext, ByteBuf in, List<Object> out) throws Exception {
while (in.readableBytes() >= 4)运营总监 {
int value = Mat情况说明怎么写h.abs(in.readInt()搜索历史);
out.add(value);
}
}
}
public class AbsIntegerEncoderTest {
@Test
public void testEn量卡code92d() {
ByteBuf buf = Unpooled.buffer();
for (int i = 0; i < 10; i++) {
buf.writeInt(i * -1);
}
EmbeddedChannel channel = new Emb115eddedChannel(new搜索 AbsIntegerEncoder());
Asser好游快爆t.搜索图片识别assertTrue(channel.writeOutbou哈利法塔nd(buf));
Assert.assertTrue(channel.finish());
for (int i = 0; i &l92和95可以混加吗t; 10; i++) {
Asser79t.assertEquals(Integer.valueOf(i), channel.readOutbound());
}
Assert.assertNull(channel.readOutbound());
}
}

测试异常处理

public class FrameChunkDecoder extends ByteToMessageDecoder {
private final int maxFrameSize;
public FrameChunkDecoder(int maxFrameSize) {
this.maxFrameSiz弹出英文e = maxFrameSize;
}
@Override
protected void decode(ChannelHandlerContesagaxt ctx, ByteBuf in, List<Object> out) throws Exc推荐eption {
int readableBytes = in.readableBytesescape();
if (readableBytes > maxFrameSize) {
in.clear(单元格拆分成两个);
throw new TooLongF80端口是干什么的rameException();
}
ByteBuf buf = in.readBytes(reada测试纸怎么看是否怀孕bleBytes);
out域名解析查询.add(buf)欧尚x5;
}
}
public class FrameChunkDecoderTest {
@Test
public void testFramesDecoded() {
ByteBuf buf = Unpooled.buffer();
for (int i = 0; i < 9; i++) {
buf.writeByt搜索引擎e(i);
}
ByteBuf input = buf.duplicate()osi七层模型;
EmbeddedChannel ch搜索引擎排名annel = new EmbeddedChannel(new Fr推荐信ameChunkDecoder(3));
Assert.assertTrue(channel.writeInbound搜索jovi(input.reavmos虚拟机dBytes(2)));
try {
channel.writeInbound(input.readBytes(4));
Assert.fail();
} catcesch (TooLongFrameException e) {
}
Assert.as测试抑郁程度的问卷sertTrue(channHAel.writeInbound(input.readBytes(3)));
Assert.assertTrue(channel.finish());
Byte79年属什么的生肖Buf read = channel.read域名解析ipInbound();
Assert.as域名解析ip地址查询sertEquals(buf.readSlice(2), read流行歌曲);
read.release();
read = channel.readInbound();
Assert.asse单元测试rtEquals(buf.skipBytes(4).readSlice(3), read);
read.release();
buf.release();
}
}