Search in sources :

Example 31 with ByteBuf

use of io.netty.buffer.ByteBuf in project netty by netty.

the class DefaultHttp2ConnectionEncoderTest method emptyFrameShouldSplitPadding.

@Test
public void emptyFrameShouldSplitPadding() throws Exception {
    ByteBuf data = Unpooled.buffer(0);
    assertSplitPaddingOnEmptyBuffer(data);
    assertEquals(0, data.refCnt());
}
Also used : ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Example 32 with ByteBuf

use of io.netty.buffer.ByteBuf in project netty by netty.

the class DefaultHttp2ConnectionEncoderTest method setup.

@Before
public void setup() throws Exception {
    MockitoAnnotations.initMocks(this);
    when(channel.isActive()).thenReturn(true);
    when(channel.pipeline()).thenReturn(pipeline);
    when(writer.configuration()).thenReturn(writerConfig);
    when(writerConfig.frameSizePolicy()).thenReturn(frameSizePolicy);
    when(frameSizePolicy.maxFrameSize()).thenReturn(64);
    doAnswer(new Answer<ChannelFuture>() {

        @Override
        public ChannelFuture answer(InvocationOnMock in) throws Throwable {
            return ((ChannelPromise) in.getArguments()[2]).setSuccess();
        }
    }).when(writer).writeSettings(eq(ctx), any(Http2Settings.class), any(ChannelPromise.class));
    doAnswer(new Answer<ChannelFuture>() {

        @Override
        public ChannelFuture answer(InvocationOnMock in) throws Throwable {
            ((ByteBuf) in.getArguments()[3]).release();
            return ((ChannelPromise) in.getArguments()[4]).setSuccess();
        }
    }).when(writer).writeGoAway(eq(ctx), anyInt(), anyInt(), any(ByteBuf.class), any(ChannelPromise.class));
    writtenData = new ArrayList<String>();
    writtenPadding = new ArrayList<Integer>();
    when(writer.writeData(eq(ctx), anyInt(), any(ByteBuf.class), anyInt(), anyBoolean(), any(ChannelPromise.class))).then(new Answer<ChannelFuture>() {

        @Override
        public ChannelFuture answer(InvocationOnMock in) throws Throwable {
            // Make sure we only receive stream closure on the last frame and that void promises
            // are used for all writes except the last one.
            ChannelPromise promise = (ChannelPromise) in.getArguments()[5];
            if (streamClosed) {
                fail("Stream already closed");
            } else {
                streamClosed = (Boolean) in.getArguments()[4];
            }
            writtenPadding.add((Integer) in.getArguments()[3]);
            ByteBuf data = (ByteBuf) in.getArguments()[2];
            writtenData.add(data.toString(UTF_8));
            // Release the buffer just as DefaultHttp2FrameWriter does
            data.release();
            // Let the promise succeed to trigger listeners.
            return promise.setSuccess();
        }
    });
    when(writer.writeHeaders(eq(ctx), anyInt(), any(Http2Headers.class), anyInt(), anyShort(), anyBoolean(), anyInt(), anyBoolean(), any(ChannelPromise.class))).then(new Answer<ChannelFuture>() {

        @Override
        public ChannelFuture answer(InvocationOnMock invocationOnMock) throws Throwable {
            ChannelPromise promise = (ChannelPromise) invocationOnMock.getArguments()[8];
            if (streamClosed) {
                fail("Stream already closed");
            } else {
                streamClosed = (Boolean) invocationOnMock.getArguments()[5];
            }
            return promise.setSuccess();
        }
    });
    payloadCaptor = ArgumentCaptor.forClass(Http2RemoteFlowController.FlowControlled.class);
    doNothing().when(remoteFlow).addFlowControlled(any(Http2Stream.class), payloadCaptor.capture());
    when(ctx.alloc()).thenReturn(UnpooledByteBufAllocator.DEFAULT);
    when(ctx.channel()).thenReturn(channel);
    doAnswer(new Answer<ChannelPromise>() {

        @Override
        public ChannelPromise answer(InvocationOnMock in) throws Throwable {
            return newPromise();
        }
    }).when(ctx).newPromise();
    doAnswer(new Answer<ChannelFuture>() {

        @Override
        public ChannelFuture answer(InvocationOnMock in) throws Throwable {
            return newSucceededFuture();
        }
    }).when(ctx).newSucceededFuture();
    when(ctx.flush()).thenThrow(new AssertionFailedError("forbidden"));
    when(channel.alloc()).thenReturn(PooledByteBufAllocator.DEFAULT);
    // Use a server-side connection so we can test server push.
    connection = new DefaultHttp2Connection(true);
    connection.remote().flowController(remoteFlow);
    encoder = new DefaultHttp2ConnectionEncoder(connection, writer);
    encoder.lifecycleManager(lifecycleManager);
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) ChannelPromise(io.netty.channel.ChannelPromise) DefaultChannelPromise(io.netty.channel.DefaultChannelPromise) ByteBuf(io.netty.buffer.ByteBuf) FlowControlled(io.netty.handler.codec.http2.Http2RemoteFlowController.FlowControlled) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Mockito.anyBoolean(org.mockito.Mockito.anyBoolean) AssertionFailedError(junit.framework.AssertionFailedError) Before(org.junit.Before)

Example 33 with ByteBuf

use of io.netty.buffer.ByteBuf in project netty by netty.

the class CompressorHttp2ConnectionEncoder method cleanup.

/**
     * Release remaining content from {@link EmbeddedChannel} and remove the compressor from the {@link Http2Stream}.
     *
     * @param stream The stream for which {@code compressor} is the compressor for
     * @param compressor The compressor for {@code stream}
     */
void cleanup(Http2Stream stream, EmbeddedChannel compressor) {
    if (compressor.finish()) {
        for (; ; ) {
            final ByteBuf buf = compressor.readOutbound();
            if (buf == null) {
                break;
            }
            buf.release();
        }
    }
    stream.removeProperty(propertyKey);
}
Also used : ByteBuf(io.netty.buffer.ByteBuf)

Example 34 with ByteBuf

use of io.netty.buffer.ByteBuf in project netty by netty.

the class DefaultHttp2FrameReader method readDataFrame.

private void readDataFrame(ChannelHandlerContext ctx, ByteBuf payload, Http2FrameListener listener) throws Http2Exception {
    int padding = readPadding(payload);
    verifyPadding(padding);
    // Determine how much data there is to read by removing the trailing
    // padding.
    int dataLength = lengthWithoutTrailingPadding(payload.readableBytes(), padding);
    ByteBuf data = payload.readSlice(dataLength);
    listener.onDataRead(ctx, streamId, data, padding, flags.endOfStream());
    payload.skipBytes(payload.readableBytes());
}
Also used : ByteBuf(io.netty.buffer.ByteBuf)

Example 35 with ByteBuf

use of io.netty.buffer.ByteBuf in project netty by netty.

the class DefaultHttp2FrameReader method readGoAwayFrame.

private static void readGoAwayFrame(ChannelHandlerContext ctx, ByteBuf payload, Http2FrameListener listener) throws Http2Exception {
    int lastStreamId = readUnsignedInt(payload);
    long errorCode = payload.readUnsignedInt();
    ByteBuf debugData = payload.readSlice(payload.readableBytes());
    listener.onGoAwayRead(ctx, lastStreamId, errorCode, debugData);
}
Also used : ByteBuf(io.netty.buffer.ByteBuf)

Aggregations

ByteBuf (io.netty.buffer.ByteBuf)1557 Test (org.junit.Test)668 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)162 IOException (java.io.IOException)99 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)89 CompositeByteBuf (io.netty.buffer.CompositeByteBuf)81 Test (org.testng.annotations.Test)68 InetSocketAddress (java.net.InetSocketAddress)60 Channel (io.netty.channel.Channel)57 ChannelFuture (io.netty.channel.ChannelFuture)56 ArrayList (java.util.ArrayList)55 Map (java.util.Map)45 ChannelPromise (io.netty.channel.ChannelPromise)41 AtomicReference (java.util.concurrent.atomic.AtomicReference)36 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)35 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)34 HashMap (java.util.HashMap)34 CountDownLatch (java.util.concurrent.CountDownLatch)34 RecyclableDuplicateByteBuf (io.netty.buffer.RecyclableDuplicateByteBuf)32 EventLoopGroup (io.netty.channel.EventLoopGroup)32