Search in sources :

Example 76 with ByteBuf

use of io.netty.buffer.ByteBuf in project grpc-java by grpc.

the class NettyHandlerTestBase method grpcDataFrame.

protected ByteBuf grpcDataFrame(int streamId, boolean endStream, byte[] content) {
    final ByteBuf compressionFrame = Unpooled.buffer(content.length);
    MessageFramer framer = new MessageFramer(new MessageFramer.Sink() {

        @Override
        public void deliverFrame(WritableBuffer frame, boolean endOfStream, boolean flush) {
            if (frame != null) {
                ByteBuf bytebuf = ((NettyWritableBuffer) frame).bytebuf();
                compressionFrame.writeBytes(bytebuf);
            }
        }
    }, new NettyWritableBufferAllocator(ByteBufAllocator.DEFAULT), StatsTraceContext.NOOP);
    framer.writePayload(new ByteArrayInputStream(content));
    framer.flush();
    ChannelHandlerContext ctx = newMockContext();
    new DefaultHttp2FrameWriter().writeData(ctx, streamId, compressionFrame, 0, endStream, newPromise());
    return captureWrite(ctx);
}
Also used : MessageFramer(io.grpc.internal.MessageFramer) WritableBuffer(io.grpc.internal.WritableBuffer) ByteArrayInputStream(java.io.ByteArrayInputStream) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ByteBuf(io.netty.buffer.ByteBuf) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) DefaultHttp2FrameWriter(io.netty.handler.codec.http2.DefaultHttp2FrameWriter)

Example 77 with ByteBuf

use of io.netty.buffer.ByteBuf in project grpc-java by grpc.

the class NettyHandlerTestBase method dataPingAckIsRecognized.

@Test
public void dataPingAckIsRecognized() throws Exception {
    makeStream();
    AbstractNettyHandler handler = (AbstractNettyHandler) handler();
    handler.setAutoTuneFlowControl(true);
    channelRead(dataFrame(3, false, content()));
    long pingData = handler.flowControlPing().payload();
    ByteBuf payload = handler.ctx().alloc().buffer(8);
    payload.writeLong(pingData);
    channelRead(pingFrame(true, payload));
    assertEquals(1, handler.flowControlPing().getPingCount());
    assertEquals(1, handler.flowControlPing().getPingReturn());
}
Also used : ByteBuf(io.netty.buffer.ByteBuf) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) Test(org.junit.Test)

Example 78 with ByteBuf

use of io.netty.buffer.ByteBuf in project grpc-java by grpc.

the class NettyHandlerTestBase method dataSizeSincePingAccumulates.

@Test
public void dataSizeSincePingAccumulates() throws Exception {
    makeStream();
    AbstractNettyHandler handler = (AbstractNettyHandler) handler();
    handler.setAutoTuneFlowControl(true);
    long frameData = 123456;
    ByteBuf buff = ctx().alloc().buffer(16);
    buff.writeLong(frameData);
    int length = buff.readableBytes();
    channelRead(dataFrame(3, false, buff.copy()));
    channelRead(dataFrame(3, false, buff.copy()));
    channelRead(dataFrame(3, false, buff.copy()));
    assertEquals(length * 3, handler.flowControlPing().getDataSincePing());
}
Also used : ByteBuf(io.netty.buffer.ByteBuf) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) Test(org.junit.Test)

Example 79 with ByteBuf

use of io.netty.buffer.ByteBuf in project grpc-java by grpc.

the class NettyServerHandlerTest method inboundDataShouldForwardToStreamListener.

private void inboundDataShouldForwardToStreamListener(boolean endStream) throws Exception {
    createStream();
    stream.request(1);
    // Create a data frame and then trigger the handler to read it.
    ByteBuf frame = grpcDataFrame(STREAM_ID, endStream, contentAsArray());
    channelRead(frame);
    ArgumentCaptor<InputStream> captor = ArgumentCaptor.forClass(InputStream.class);
    verify(streamListener).messageRead(captor.capture());
    assertArrayEquals(ByteBufUtil.getBytes(content()), ByteStreams.toByteArray(captor.getValue()));
    captor.getValue().close();
    if (endStream) {
        verify(streamListener).halfClosed();
    }
    verify(streamListener, atLeastOnce()).onReady();
    verifyNoMoreInteractions(streamListener);
}
Also used : InputStream(java.io.InputStream) ByteBuf(io.netty.buffer.ByteBuf)

Example 80 with ByteBuf

use of io.netty.buffer.ByteBuf in project grpc-java by grpc.

the class NettyServerHandlerTest method headersWithInvalidContentTypeShouldFail.

@Test
public void headersWithInvalidContentTypeShouldFail() throws Exception {
    Http2Headers headers = new DefaultHttp2Headers().method(HTTP_METHOD).set(CONTENT_TYPE_HEADER, new AsciiString("application/bad", UTF_8)).set(TE_HEADER, TE_TRAILERS).path(new AsciiString("/foo/bar"));
    ByteBuf headersFrame = headersFrame(STREAM_ID, headers);
    channelRead(headersFrame);
    verifyWrite().writeRstStream(eq(ctx()), eq(STREAM_ID), eq(Http2Error.REFUSED_STREAM.code()), any(ChannelPromise.class));
}
Also used : Http2Headers(io.netty.handler.codec.http2.Http2Headers) DefaultHttp2Headers(io.netty.handler.codec.http2.DefaultHttp2Headers) DefaultHttp2Headers(io.netty.handler.codec.http2.DefaultHttp2Headers) AsciiString(io.netty.util.AsciiString) ChannelPromise(io.netty.channel.ChannelPromise) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

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