一種特殊的Channel實(shí)現(xiàn)----EmbeddedChannel,它是Netty專門為改進(jìn)針對ChannelHandler的單元測試而提供的。
創(chuàng)新互聯(lián)建站擁有網(wǎng)站維護(hù)技術(shù)和項(xiàng)目管理團(tuán)隊(duì),建立的售前、實(shí)施和售后服務(wù)體系,為客戶提供定制化的成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作、網(wǎng)站維護(hù)、成都移動(dòng)云計(jì)算中心解決方案。為客戶網(wǎng)站安全和日常運(yùn)維提供整體管家式外包優(yōu)質(zhì)服務(wù)。我們的網(wǎng)站維護(hù)服務(wù)覆蓋集團(tuán)企業(yè)、上市公司、外企網(wǎng)站、商城開發(fā)、政府網(wǎng)站等各類型客戶群體,為全球上千余家企業(yè)提供全方位網(wǎng)站維護(hù)、服務(wù)器維護(hù)解決方案。
名稱 | 職責(zé) |
---|---|
writeInbound | 將入站消息寫到EmbeddedChannel中。如果可以通過readInbound方法從EmbeddedChannel中讀取數(shù)據(jù),則返回true |
readInbound | 從EmbeddedChannel中讀取入站消息。任何返回東西都經(jīng)過整個(gè)ChannelPipeline。如果沒有任何可供讀取的,則返回null |
writeOutbound | 將出站消息寫到EmbeddedChannel中,如果現(xiàn)在可以通過readOutbound從EmbeddedChannel中讀取到東西,則返回true |
readOutbound | 從EmbeddedChannel中讀取出站消息。任何返回東西都經(jīng)過整個(gè)ChannelPipeline。如果沒有任何可供讀取的,則返回null |
finish | 將EmbeddedChannel標(biāo)記為完成,如果有可讀取的入站或出站數(shù)據(jù),則返回true。這個(gè)方法還將會調(diào)用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
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
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
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();
}
}