netty使用EmbeddedChannel對channel的出入站進行單元測試

一種特殊的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<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();
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末见秤,一起剝皮案震驚了整個濱河市砂竖,隨后出現(xiàn)的幾起案子真椿,更是在濱河造成了極大的恐慌,老刑警劉巖乎澄,帶你破解...
    沈念sama閱讀 219,270評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件突硝,死亡現(xiàn)場離奇詭異,居然都是意外死亡置济,警方通過查閱死者的電腦和手機解恰,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,489評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來浙于,“玉大人护盈,你說我怎么就攤上這事⌒咝铮” “怎么了腐宋?”我有些...
    開封第一講書人閱讀 165,630評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長檀轨。 經(jīng)常有香客問我胸竞,道長,這世上最難降的妖魔是什么参萄? 我笑而不...
    開封第一講書人閱讀 58,906評論 1 295
  • 正文 為了忘掉前任卫枝,我火速辦了婚禮,結(jié)果婚禮上讹挎,老公的妹妹穿的比我還像新娘校赤。我一直安慰自己,他們只是感情好筒溃,可當我...
    茶點故事閱讀 67,928評論 6 392
  • 文/花漫 我一把揭開白布马篮。 她就那樣靜靜地躺著,像睡著了一般铡羡。 火紅的嫁衣襯著肌膚如雪积蔚。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,718評論 1 305
  • 那天烦周,我揣著相機與錄音尽爆,去河邊找鬼。 笑死读慎,一個胖子當著我的面吹牛漱贱,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播夭委,決...
    沈念sama閱讀 40,442評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼幅狮,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起崇摄,我...
    開封第一講書人閱讀 39,345評論 0 276
  • 序言:老撾萬榮一對情侶失蹤擎值,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后逐抑,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體鸠儿,經(jīng)...
    沈念sama閱讀 45,802評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,984評論 3 337
  • 正文 我和宋清朗相戀三年厕氨,在試婚紗的時候發(fā)現(xiàn)自己被綠了进每。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,117評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡命斧,死狀恐怖田晚,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情国葬,我是刑警寧澤贤徒,帶...
    沈念sama閱讀 35,810評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站胃惜,受9級特大地震影響泞莉,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜船殉,卻給世界環(huán)境...
    茶點故事閱讀 41,462評論 3 331
  • 文/蒙蒙 一鲫趁、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧利虫,春花似錦挨厚、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,011評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至硼讽,卻和暖如春巢价,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背固阁。 一陣腳步聲響...
    開封第一講書人閱讀 33,139評論 1 272
  • 我被黑心中介騙來泰國打工壤躲, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人备燃。 一個月前我還...
    沈念sama閱讀 48,377評論 3 373
  • 正文 我出身青樓碉克,卻偏偏與公主長得像,于是被迫代替她去往敵國和親并齐。 傳聞我的和親對象是個殘疾皇子漏麦,可洞房花燭夜當晚...
    茶點故事閱讀 45,060評論 2 355

推薦閱讀更多精彩內(nèi)容