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);
}
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());
}
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());
}
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);
}
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));
}
Aggregations