Search in sources :

Example 76 with EmbeddedChannel

use of io.netty.channel.embedded.EmbeddedChannel in project netty by netty.

the class ZlibTest method testGZIPCompressOnly0.

private void testGZIPCompressOnly0(byte[] data) throws IOException {
    EmbeddedChannel chEncoder = new EmbeddedChannel(createEncoder(ZlibWrapper.GZIP));
    if (data != null) {
        chEncoder.writeOutbound(Unpooled.wrappedBuffer(data));
    }
    assertTrue(chEncoder.finish());
    ByteBuf encoded = Unpooled.buffer();
    for (; ; ) {
        ByteBuf buf = chEncoder.readOutbound();
        if (buf == null) {
            break;
        }
        encoded.writeBytes(buf);
        buf.release();
    }
    ByteBuf decoded = Unpooled.buffer();
    GZIPInputStream stream = new GZIPInputStream(new ByteBufInputStream(encoded, true));
    try {
        byte[] buf = new byte[8192];
        for (; ; ) {
            int readBytes = stream.read(buf);
            if (readBytes < 0) {
                break;
            }
            decoded.writeBytes(buf, 0, readBytes);
        }
    } finally {
        stream.close();
    }
    if (data != null) {
        assertEquals(Unpooled.wrappedBuffer(data), decoded);
    } else {
        assertFalse(decoded.isReadable());
    }
    decoded.release();
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) ByteBuf(io.netty.buffer.ByteBuf)

Example 77 with EmbeddedChannel

use of io.netty.channel.embedded.EmbeddedChannel in project netty by netty.

the class ZlibTest method testCompress0.

private void testCompress0(ZlibWrapper encoderWrapper, ZlibWrapper decoderWrapper, ByteBuf data) throws Exception {
    EmbeddedChannel chEncoder = new EmbeddedChannel(createEncoder(encoderWrapper));
    EmbeddedChannel chDecoderZlib = new EmbeddedChannel(createDecoder(decoderWrapper));
    try {
        chEncoder.writeOutbound(data.retain());
        chEncoder.flush();
        data.resetReaderIndex();
        for (; ; ) {
            ByteBuf deflatedData = chEncoder.readOutbound();
            if (deflatedData == null) {
                break;
            }
            chDecoderZlib.writeInbound(deflatedData);
        }
        byte[] decompressed = new byte[data.readableBytes()];
        int offset = 0;
        for (; ; ) {
            ByteBuf buf = chDecoderZlib.readInbound();
            if (buf == null) {
                break;
            }
            int length = buf.readableBytes();
            buf.readBytes(decompressed, offset, length);
            offset += length;
            buf.release();
            if (offset == decompressed.length) {
                break;
            }
        }
        assertEquals(data, Unpooled.wrappedBuffer(decompressed));
        assertNull(chDecoderZlib.readInbound());
        // Closing an encoder channel will generate a footer.
        assertTrue(chEncoder.finish());
        for (; ; ) {
            Object msg = chEncoder.readOutbound();
            if (msg == null) {
                break;
            }
            ReferenceCountUtil.release(msg);
        }
        // But, the footer will be decoded into nothing. It's only for validation.
        assertFalse(chDecoderZlib.finish());
        data.release();
    } finally {
        dispose(chEncoder);
        dispose(chDecoderZlib);
    }
}
Also used : EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ByteBuf(io.netty.buffer.ByteBuf)

Example 78 with EmbeddedChannel

use of io.netty.channel.embedded.EmbeddedChannel in project netty by netty.

the class DelimiterBasedFrameDecoderTest method testFailSlowTooLongFrameRecovery.

@Test
public void testFailSlowTooLongFrameRecovery() throws Exception {
    EmbeddedChannel ch = new EmbeddedChannel(new DelimiterBasedFrameDecoder(1, true, false, Delimiters.nulDelimiter()));
    for (int i = 0; i < 2; i++) {
        ch.writeInbound(Unpooled.wrappedBuffer(new byte[] { 1, 2 }));
        try {
            assertTrue(ch.writeInbound(Unpooled.wrappedBuffer(new byte[] { 0 })));
            fail(DecoderException.class.getSimpleName() + " must be raised.");
        } catch (TooLongFrameException e) {
        // Expected
        }
        ch.writeInbound(Unpooled.wrappedBuffer(new byte[] { 'A', 0 }));
        ByteBuf buf = ch.readInbound();
        assertEquals("A", buf.toString(CharsetUtil.ISO_8859_1));
        buf.release();
    }
}
Also used : TooLongFrameException(io.netty.handler.codec.TooLongFrameException) DelimiterBasedFrameDecoder(io.netty.handler.codec.DelimiterBasedFrameDecoder) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Example 79 with EmbeddedChannel

use of io.netty.channel.embedded.EmbeddedChannel in project netty by netty.

the class DelimiterBasedFrameDecoderTest method testFailFastTooLongFrameRecovery.

@Test
public void testFailFastTooLongFrameRecovery() throws Exception {
    EmbeddedChannel ch = new EmbeddedChannel(new DelimiterBasedFrameDecoder(1, Delimiters.nulDelimiter()));
    for (int i = 0; i < 2; i++) {
        try {
            assertTrue(ch.writeInbound(Unpooled.wrappedBuffer(new byte[] { 1, 2 })));
            fail(DecoderException.class.getSimpleName() + " must be raised.");
        } catch (TooLongFrameException e) {
        // Expected
        }
        ch.writeInbound(Unpooled.wrappedBuffer(new byte[] { 0, 'A', 0 }));
        ByteBuf buf = ch.readInbound();
        assertEquals("A", buf.toString(CharsetUtil.ISO_8859_1));
        buf.release();
    }
}
Also used : TooLongFrameException(io.netty.handler.codec.TooLongFrameException) DelimiterBasedFrameDecoder(io.netty.handler.codec.DelimiterBasedFrameDecoder) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Example 80 with EmbeddedChannel

use of io.netty.channel.embedded.EmbeddedChannel in project netty by netty.

the class CompatibleObjectEncoderTest method testMultipleEncodeReferenceCount.

@Test
public void testMultipleEncodeReferenceCount() throws IOException, ClassNotFoundException {
    EmbeddedChannel channel = new EmbeddedChannel(new CompatibleObjectEncoder());
    testEncode(channel, new TestSerializable(6, 8));
    testEncode(channel, new TestSerializable(10, 5));
    testEncode(channel, new TestSerializable(1, 5));
    assertFalse(channel.finishAndReleaseAll());
}
Also used : EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Test(org.junit.Test)

Aggregations

EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)446 Test (org.junit.Test)364 ByteBuf (io.netty.buffer.ByteBuf)162 HttpResponse (io.netty.handler.codec.http.HttpResponse)30 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)28 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)25 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)21 HttpRequest (io.netty.handler.codec.http.HttpRequest)20 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)17 InetSocketAddress (java.net.InetSocketAddress)17 BinaryWebSocketFrame (io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame)15 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)14 TooLongFrameException (io.netty.handler.codec.TooLongFrameException)11 ArrayList (java.util.ArrayList)11 DefaultLastHttpContent (io.netty.handler.codec.http.DefaultLastHttpContent)10 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)10 KvStateRegistry (org.apache.flink.runtime.query.KvStateRegistry)10 UUID (java.util.UUID)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 IOException (java.io.IOException)7