摘要:一種特殊的實現(xiàn),它是專門為改進針對的單元測試而提供的。名稱職責將入站消息寫到中。如果沒有任何可供讀取的,則返回將標記為完成,如果有可讀取的入站或出站數(shù)據(jù),則返回。這個方法還將會調(diào)用上的方法測試入站消息測試出站消息測試異常處理
一種特殊的Channel實現(xiàn)----EmbeddedChannel,它是Netty專門為改進針對ChannelHandler的單元測試而提供的。
名稱 | 職責 |
---|---|
writeInbound | 將入站消息寫到EmbeddedChannel中。如果可以通過readInbound方法從EmbeddedChannel中讀取數(shù)據(jù),則返回true |
readInbound | 從EmbeddedChannel中讀取入站消息。任何返回東西都經(jīng)過整個ChannelPipeline。如果沒有任何可供讀取的,則返回null |
writeOutbound | 將出站消息寫到EmbeddedChannel中,如果現(xiàn)在可以通過readOutbound從EmbeddedChannel中讀取到東西,則返回true |
readOutbound | 從EmbeddedChannel中讀取出站消息。任何返回東西都經(jīng)過整個ChannelPipeline。如果沒有任何可供讀取的,則返回null |
finish | 將EmbeddedChannel標記為完成,如果有可讀取的入站或出站數(shù)據(jù),則返回true。這個方法還將會調(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(); } }
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/75743.html
摘要:支持很多協(xié)議,并且提供用于數(shù)據(jù)處理的容器。我們已經(jīng)知道由特定事件觸發(fā)??蓪S糜趲缀跛械膭幼鳎▽⒁粋€對象轉(zhuǎn)為字節(jié)或相反,執(zhí)行過程中拋出的異常處理。提供了一個容器給鏈并提供了一個用于管理沿著鏈入站和出站事件的流動。子類通過進行注冊。 前兩天寫了一點netty相關(guān)的知識,并寫了一個demo,但是對其原理還是沒有深入,今天我們來做一次研究吧 首先讓我們來認識一下netty的幾個核心人物吧...
摘要:可以用來接收入站事件和數(shù)據(jù),隨后使用應用程序的業(yè)務邏輯進行處理。因為用戶并不是關(guān)心所有的事件,因此提供了抽象類和。抽象類最常見的一個情況,你的應用程序會利用一個來接受解碼消息,并對該數(shù)據(jù)應用業(yè)務邏輯。 Channel、EventLoop和ChannelFuture Channel——Socket; EventLoop——控制流、多線程處理、并發(fā) ChannelFuture異步通知 ...
摘要:轉(zhuǎn)發(fā)自 轉(zhuǎn)發(fā)自 http://netty.io/wiki/referenc... Since Netty version 4, the life cycle of certain objects are managed by their reference counts, so that Netty can return them (or their shared resources)...
摘要:只有在詳盡的測試之后才應設置為這值使用的默認采樣率檢測并報告任何發(fā)現(xiàn)的泄漏。這是默認級別,適合絕大部分情況使用默認的采樣率,報告所發(fā)現(xiàn)的任何的泄漏以及對應的消息被訪問的位置類似于但是其將會對每次對消息的訪問都進行采樣。 ChannelHandler Channel生命周期 狀態(tài) 描述 ChannelUnregistered Channel已經(jīng)被創(chuàng)建,但未注冊到EventLoo...
摘要:服務器構(gòu)成至少一個該組件實現(xiàn)了服務器對從客戶端接受的數(shù)據(jù)的處理,即它的業(yè)務邏輯引導配置服務器的啟動代碼。至少,它會將服務器綁定到它要監(jiān)聽連接請求的端口上。需要注意的是,由服務器發(fā)送的消息可能會被分塊接受。 Netty服務器構(gòu)成 至少一個ChannelHandler——該組件實現(xiàn)了服務器對從客戶端接受的數(shù)據(jù)的處理,即它的業(yè)務邏輯 引導——配置服務器的啟動代碼。至少,它會將服務器綁定...
閱讀 3041·2023-04-25 18:06
閱讀 3318·2021-11-22 09:34
閱讀 2873·2021-08-12 13:30
閱讀 2061·2019-08-30 15:44
閱讀 1675·2019-08-30 13:09
閱讀 1642·2019-08-30 12:45
閱讀 1726·2019-08-29 11:13
閱讀 3621·2019-08-28 17:51