netty使用EmbeddedChannel对channel的出入站进行单元测试
嵌入式通道是Netty专门提供的一种特殊的通道实现,用于改进ChannelHandler的单元测试。
名称
责任 |
writeInbound
将入站消息写入嵌入式通道。如果可以通过readInbound方法从EmbeddedChannel读取数据,则返回true
。
readInbound
从嵌入式通道读取入站消息。返回的所有内容都经过整个通道管道。如果没有要读取的内容,它将返回null
writeOutbound
将出站消息写入EmbeddedChannel,如果现在可以通过readOutbound从EmbeddedChannel读取某些内容,则返回true
。
readOutbound
从嵌入式通道读取出站消息。返回的所有内容都经过整个通道管道。如果没有要读取的内容,它将返回null
finish
将嵌入式通道标记为完成,如果有可读的入站或出站数据,则返回true。该方法还将调用嵌入式通道上的close方法
。
测试入站消息
publiccla
ss="superseo">ssFixedLengthFrameDecoderextendsByteToMessageDecoder{
privatefinalintframeLength;
publicFixedLengthFrameDecoder(intframeLength){
if(frameLength<=0){
thrownewIllegalArgumentException("frameLengthmustbepositiveinteger:"+frameLength);
}
this.frameLength=frameLength;
}
@Override
protectedvoiddecode(ChannelHandlerContextctx,ByteBufin,List<Object>out)throwsException{
while(in.readableBytes()>=frameLength){
ByteBufbuf=in.readBytes(frameLength);
out.add(buf);
}
}
}
publicclassFixedLengthFrameDecoderTest{
@Test
publicvoidtestFramesDecoded(){
ByteBufbuf=Unpooled.buffer();
for(inti=0;i<9;i++){
buf.writeByte(i);
}
ByteBufinput=buf.duplicate();
EmbeddedChannelchannel=newEmbeddedChannel(newFixedLengthFrameDecoder(3));
Assert.assertTrue(channel.writeInbound(input.retain()));
Assert.assertTrue(channel.finish());
ByteBufread=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
publicvoidtestFramesDecoded2(){
ByteBufbuf=Unpooled.buffer();
for(inti=0;i<9;i++){
buf.writeByte(i);
}
ByteBufinput=buf.duplicate();
EmbeddedChannelchannel=newEmbeddedChannel(newFixedLengthFrameDecoder(3));
Assert.assertFalse(channel.writeInbound(input.readBytes(2)));
Assert.assertTrue(channel.writeInbound(input.readBytes(7)));
Assert.assertTrue(channel.finish());
ByteBufread=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();
}
}
测试出站消息
publicclassAbsIntegerEncoderextendsMessageToMessageEncoder<ByteBuf>{
@Override
protectedvoidencode(ChannelHandlerContextchannelHandlerContext,ByteBufin,List<Object>out)throwsException{
while(in.readableBytes()>=4){
int
value=Math.abs(in.readInt());
out.add(value);
}
}
}
publicclassAbsIntegerEncoderTest{
@Test
publicvoidtestEncoded(){
ByteBufbuf=Unpooled.buffer();
for(inti=0;i<10;i++){
buf.writeInt(i*-1);
}
EmbeddedChannelchannel=newEmbeddedChannel(newAbsIntegerEncoder());
Assert.assertTrue(channel.writeOutbound(buf));
Assert.assertTrue(channel.finish());
for(inti=0;i<10;i++){
Assert.assertEquals(Integer.valueOf(i),channel.readOutbound());
}
Assert.assertNull(channel.readOutbound());
}
}
测试异常处理
publicclassFrameChunkDecoderextendsByteToMessageDecoder{
privatefinalintmaxFrameSize;
publicFrameChunkDecoder(intmaxFrameSize){
this.maxFrameSize=maxFrameSize;
}
@Override
protectedvoiddecode(ChannelHandlerContextctx,ByteBufin,List<Object>out)throwsException{
intreadableBytes=in.readableBytes();
if(readableBytes>maxFrameSize){
in.clear();
thrownewTooLongFrameException();
}
ByteBufbuf=in.readBytes(readableBytes);
out.add(buf);
}
}
publicclassFrameChunkDecoderTest{
@Test
publicvoidtestFramesDecoded(){
ByteBufbuf=Unpooled.buffer();
for(inti=0;i<9;i++){
buf.writeByte(i);
}
ByteBufinput=buf.duplicate();
EmbeddedChannelchannel=newEmbeddedChannel(newFrameChunkDecoder(3));
Assert.assertTrue(channel.writeInbound(input.readBytes(2)));
try{
channel.writeInbound(input.readBytes(4));
Assert.fail();
}catch(TooLongFrameExceptione){
}
Assert.assertTrue(channel.writeInbound(input.readBytes(3)));
Assert.assertTrue(channel.finish());
ByteBufread=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();
}
}
评论列表(0条)