Search in sources :

Example 71 with EmbeddedChannel

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

the class ChannelOutboundBufferTest method testUserDefinedWritability.

@Test
public void testUserDefinedWritability() {
    final StringBuilder buf = new StringBuilder();
    EmbeddedChannel ch = new EmbeddedChannel(new ChannelInboundHandlerAdapter() {

        @Override
        public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
            buf.append(ctx.channel().isWritable());
            buf.append(' ');
        }
    });
    ch.config().setWriteBufferLowWaterMark(128);
    ch.config().setWriteBufferHighWaterMark(256);
    ChannelOutboundBuffer cob = ch.unsafe().outboundBuffer();
    // Ensure that the default value of a user-defined writability flag is true.
    for (int i = 1; i <= 30; i++) {
        assertThat(cob.getUserDefinedWritability(i), is(true));
    }
    // Ensure that setting a user-defined writability flag to false affects channel.isWritable();
    cob.setUserDefinedWritability(1, false);
    ch.runPendingTasks();
    assertThat(buf.toString(), is("false "));
    // Ensure that setting a user-defined writability flag to true affects channel.isWritable();
    cob.setUserDefinedWritability(1, true);
    ch.runPendingTasks();
    assertThat(buf.toString(), is("false true "));
    safeClose(ch);
}
Also used : EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Test(org.junit.Test)

Example 72 with EmbeddedChannel

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

the class ChannelOutboundBufferTest method testMixedWritability.

@Test
public void testMixedWritability() {
    final StringBuilder buf = new StringBuilder();
    EmbeddedChannel ch = new EmbeddedChannel(new ChannelInboundHandlerAdapter() {

        @Override
        public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
            buf.append(ctx.channel().isWritable());
            buf.append(' ');
        }
    });
    ch.config().setWriteBufferLowWaterMark(128);
    ch.config().setWriteBufferHighWaterMark(256);
    ChannelOutboundBuffer cob = ch.unsafe().outboundBuffer();
    // Trigger channelWritabilityChanged() by writing a lot.
    ch.write(buffer().writeZero(257));
    assertThat(buf.toString(), is("false "));
    // Ensure that setting a user-defined writability flag to false does not trigger channelWritabilityChanged()
    cob.setUserDefinedWritability(1, false);
    ch.runPendingTasks();
    assertThat(buf.toString(), is("false "));
    // Ensure reducing the totalPendingWriteBytes down to zero does not trigger channelWritabilityChannged()
    // because of the user-defined writability flag.
    ch.flush();
    assertThat(cob.totalPendingWriteBytes(), is(0L));
    assertThat(buf.toString(), is("false "));
    // Ensure that setting the user-defined writability flag to true triggers channelWritabilityChanged()
    cob.setUserDefinedWritability(1, true);
    ch.runPendingTasks();
    assertThat(buf.toString(), is("false true "));
    safeClose(ch);
}
Also used : EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Test(org.junit.Test)

Example 73 with EmbeddedChannel

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

the class DeflateDecoder method decode.

@Override
protected void decode(ChannelHandlerContext ctx, WebSocketFrame msg, List<Object> out) throws Exception {
    if (decoder == null) {
        if (!(msg instanceof TextWebSocketFrame) && !(msg instanceof BinaryWebSocketFrame)) {
            throw new CodecException("unexpected initial frame type: " + msg.getClass().getName());
        }
        decoder = new EmbeddedChannel(ZlibCodecFactory.newZlibDecoder(ZlibWrapper.NONE));
    }
    boolean readable = msg.content().isReadable();
    decoder.writeInbound(msg.content().retain());
    if (appendFrameTail(msg)) {
        decoder.writeInbound(Unpooled.wrappedBuffer(FRAME_TAIL));
    }
    CompositeByteBuf compositeUncompressedContent = ctx.alloc().compositeBuffer();
    for (; ; ) {
        ByteBuf partUncompressedContent = decoder.readInbound();
        if (partUncompressedContent == null) {
            break;
        }
        if (!partUncompressedContent.isReadable()) {
            partUncompressedContent.release();
            continue;
        }
        compositeUncompressedContent.addComponent(true, partUncompressedContent);
    }
    // See https://github.com/netty/netty/issues/4348
    if (readable && compositeUncompressedContent.numComponents() <= 0) {
        compositeUncompressedContent.release();
        throw new CodecException("cannot read uncompressed buffer");
    }
    if (msg.isFinalFragment() && noContext) {
        cleanup();
    }
    WebSocketFrame outMsg;
    if (msg instanceof TextWebSocketFrame) {
        outMsg = new TextWebSocketFrame(msg.isFinalFragment(), newRsv(msg), compositeUncompressedContent);
    } else if (msg instanceof BinaryWebSocketFrame) {
        outMsg = new BinaryWebSocketFrame(msg.isFinalFragment(), newRsv(msg), compositeUncompressedContent);
    } else if (msg instanceof ContinuationWebSocketFrame) {
        outMsg = new ContinuationWebSocketFrame(msg.isFinalFragment(), newRsv(msg), compositeUncompressedContent);
    } else {
        throw new CodecException("unexpected frame type: " + msg.getClass().getName());
    }
    out.add(outMsg);
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ContinuationWebSocketFrame(io.netty.handler.codec.http.websocketx.ContinuationWebSocketFrame) TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) BinaryWebSocketFrame(io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) CodecException(io.netty.handler.codec.CodecException) WebSocketFrame(io.netty.handler.codec.http.websocketx.WebSocketFrame) ContinuationWebSocketFrame(io.netty.handler.codec.http.websocketx.ContinuationWebSocketFrame) BinaryWebSocketFrame(io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame) TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf)

Example 74 with EmbeddedChannel

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

the class ZlibTest method testDecompressOnly.

// Test for https://github.com/netty/netty/issues/2572
private void testDecompressOnly(ZlibWrapper decoderWrapper, byte[] compressed, byte[] data) throws Exception {
    EmbeddedChannel chDecoder = new EmbeddedChannel(createDecoder(decoderWrapper));
    chDecoder.writeInbound(Unpooled.wrappedBuffer(compressed));
    assertTrue(chDecoder.finish());
    ByteBuf decoded = Unpooled.buffer(data.length);
    for (; ; ) {
        ByteBuf buf = chDecoder.readInbound();
        if (buf == null) {
            break;
        }
        decoded.writeBytes(buf);
        buf.release();
    }
    assertEquals(Unpooled.wrappedBuffer(data), decoded);
    decoded.release();
}
Also used : EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ByteBuf(io.netty.buffer.ByteBuf)

Example 75 with EmbeddedChannel

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

the class ZlibTest method testCompressNone.

private void testCompressNone(ZlibWrapper encoderWrapper, ZlibWrapper decoderWrapper) throws Exception {
    EmbeddedChannel chEncoder = new EmbeddedChannel(createEncoder(encoderWrapper));
    EmbeddedChannel chDecoderZlib = new EmbeddedChannel(createDecoder(decoderWrapper));
    try {
        // Closing an encoder channel without writing anything should generate both header and footer.
        assertTrue(chEncoder.finish());
        for (; ; ) {
            ByteBuf deflatedData = chEncoder.readOutbound();
            if (deflatedData == null) {
                break;
            }
            chDecoderZlib.writeInbound(deflatedData);
        }
        // Decoder should not generate anything at all.
        boolean decoded = false;
        for (; ; ) {
            ByteBuf buf = chDecoderZlib.readInbound();
            if (buf == null) {
                break;
            }
            buf.release();
            decoded = true;
        }
        assertFalse("should decode nothing", decoded);
        assertFalse(chDecoderZlib.finish());
    } finally {
        dispose(chEncoder);
        dispose(chDecoderZlib);
    }
}
Also used : EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ByteBuf(io.netty.buffer.ByteBuf)

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