netty使用EmbeddedChannel對channel

一種特殊的Channel實現(xiàn)----EmbeddedChannel,它是Netty專門為改進針對ChannelHandler的單元測試而提供的。

創(chuàng)新互聯(lián)建站擁有網(wǎng)站維護技術(shù)和項目管理團隊,建立的售前、實施和售后服務(wù)體系,為客戶提供定制化的成都網(wǎng)站設(shè)計、成都網(wǎng)站制作、網(wǎng)站維護、成都移動云計算中心解決方案。為客戶網(wǎng)站安全和日常運維提供整體管家式外包優(yōu)質(zhì)服務(wù)。我們的網(wǎng)站維護服務(wù)覆蓋集團企業(yè)、上市公司、外企網(wǎng)站、商城開發(fā)、政府網(wǎng)站等各類型客戶群體,為全球上千余家企業(yè)提供全方位網(wǎng)站維護、服務(wù)器維護解決方案。

名稱職責
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<Object> out) throws Exception {
        while (in.readableBytes() >= frameLength) {
            ByteBuf buf = in.readBytes(frameLength);
            out.add(buf);
        }
    }
}
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<ByteBuf> {
    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, ByteBuf in, List<Object> out) throws Exception {
        while (in.readableBytes() >= 4) {
            int value = Math.abs(in.readInt());
            out.add(value);
        }
    }
}
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<Object> out) throws Exception {
        int readableBytes = in.readableBytes();
        if (readableBytes > maxFrameSize) {
            in.clear();
            throw new TooLongFrameException();
        }
        ByteBuf buf = in.readBytes(readableBytes);
        out.add(buf);
    }
}
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();
    }
}

新聞名稱:netty使用EmbeddedChannel對channel
URL分享:http://m.kartarina.com/article28/pipgcp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈Google網(wǎng)站收錄網(wǎng)站建設(shè)企業(yè)建站關(guān)鍵詞優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作
主站蜘蛛池模板: 国产aⅴ激情无码久久| 亚洲精品无码久久久久秋霞| 无码8090精品久久一区| 无码中文人妻视频2019| 亚洲av无码无在线观看红杏| 亚洲AV综合色区无码一区爱AV| 久久久亚洲精品无码| 亚洲欧洲免费无码| 国产成人无码精品久久久久免费| 中文无码喷潮在线播放| 玖玖资源站无码专区| 国产乱人伦中文无无码视频试看| 好爽毛片一区二区三区四无码三飞| 国产精品午夜无码体验区| 亚洲AV永久无码精品水牛影视| 中文字幕av无码无卡免费| 成年男人裸j照无遮挡无码| 国产aⅴ激情无码久久| 久久久久久久久无码精品亚洲日韩 | 无码日韩精品一区二区免费暖暖 | 国产成人精品无码免费看| 无码人妻少妇色欲AV一区二区 | 国产午夜精华无码网站| 精品无码免费专区毛片| 超清无码一区二区三区| 亚洲人AV在线无码影院观看| 在线a亚洲v天堂网2019无码| 亚洲爆乳无码精品AAA片蜜桃| 国产高清无码视频| 精品久久久久久中文字幕无码 | 国模GOGO无码人体啪啪| 无码国产精成人午夜视频不卡 | 精品无码成人网站久久久久久| 亚洲国产精品无码专区| HEYZO无码综合国产精品| 亚洲AV无码国产精品永久一区| 亚洲va无码va在线va天堂| 亚洲国产一二三精品无码| 久久亚洲精品无码观看不卡| 少妇人妻偷人精品无码AV| 日日摸日日碰夜夜爽无码|