From 501662a77f8166382eefd18a8620983d27f59d4a Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Mon, 5 Feb 2018 11:57:49 +0100 Subject: [PATCH] Use long for http2 ping payload. Motivation: At the moment we use a ByteBuf as the payload for a http2 frame. This complicates life-time management a lot with no real gain and also may produce more objects then needed. We should just use a long as it is required to be 8 bytes anyway. Modifications: Use long for ping payloads. Result: Fixes [#7629]. --- .../http2/DecoratingHttp2FrameWriter.java | 2 +- .../http2/DefaultHttp2ConnectionDecoder.java | 10 +-- .../http2/DefaultHttp2ConnectionEncoder.java | 2 +- .../codec/http2/DefaultHttp2FrameReader.java | 5 +- .../codec/http2/DefaultHttp2FrameWriter.java | 38 +++-------- .../codec/http2/DefaultHttp2PingFrame.java | 66 +++---------------- .../codec/http2/Http2EventAdapter.java | 4 +- .../codec/http2/Http2FrameAdapter.java | 4 +- .../handler/codec/http2/Http2FrameCodec.java | 8 +-- .../codec/http2/Http2FrameListener.java | 10 ++- .../http2/Http2FrameListenerDecorator.java | 4 +- .../handler/codec/http2/Http2FrameLogger.java | 12 ++-- .../handler/codec/http2/Http2FrameWriter.java | 4 +- .../codec/http2/Http2InboundFrameLogger.java | 4 +- .../codec/http2/Http2OutboundFrameLogger.java | 2 +- .../handler/codec/http2/Http2PingFrame.java | 7 +- .../DefaultHttp2ConnectionDecoderTest.java | 11 ++-- .../DefaultHttp2ConnectionEncoderTest.java | 9 ++- .../http2/Http2ConnectionRoundtripTest.java | 19 +++--- .../codec/http2/Http2FrameCodecTest.java | 12 ++-- .../codec/http2/Http2FrameRoundtripTest.java | 16 ++--- .../codec/http2/Http2MultiplexCodecTest.java | 4 +- .../handler/codec/http2/Http2TestUtil.java | 8 +-- .../server/HelloWorldHttp2Handler.java | 4 +- .../http2/HelloWorldHttp2Handler.java | 4 +- 25 files changed, 89 insertions(+), 180 deletions(-) diff --git a/codec-http2/src/main/java/io/netty/handler/codec/http2/DecoratingHttp2FrameWriter.java b/codec-http2/src/main/java/io/netty/handler/codec/http2/DecoratingHttp2FrameWriter.java index 4aae116a75..f7caa48dda 100644 --- a/codec-http2/src/main/java/io/netty/handler/codec/http2/DecoratingHttp2FrameWriter.java +++ b/codec-http2/src/main/java/io/netty/handler/codec/http2/DecoratingHttp2FrameWriter.java @@ -76,7 +76,7 @@ public class DecoratingHttp2FrameWriter implements Http2FrameWriter { } @Override - public ChannelFuture writePing(ChannelHandlerContext ctx, boolean ack, ByteBuf data, ChannelPromise promise) { + public ChannelFuture writePing(ChannelHandlerContext ctx, boolean ack, long data, ChannelPromise promise) { return delegate.writePing(ctx, ack, data, promise); } diff --git a/codec-http2/src/main/java/io/netty/handler/codec/http2/DefaultHttp2ConnectionDecoder.java b/codec-http2/src/main/java/io/netty/handler/codec/http2/DefaultHttp2ConnectionDecoder.java index 4027978651..49335e95e7 100644 --- a/codec-http2/src/main/java/io/netty/handler/codec/http2/DefaultHttp2ConnectionDecoder.java +++ b/codec-http2/src/main/java/io/netty/handler/codec/http2/DefaultHttp2ConnectionDecoder.java @@ -424,16 +424,16 @@ public class DefaultHttp2ConnectionDecoder implements Http2ConnectionDecoder { } @Override - public void onPingRead(ChannelHandlerContext ctx, ByteBuf data) throws Http2Exception { + public void onPingRead(ChannelHandlerContext ctx, long data) throws Http2Exception { // Send an ack back to the remote client. // Need to retain the buffer here since it will be released after the write completes. - encoder.writePing(ctx, true, data.retainedSlice(), ctx.newPromise()); + encoder.writePing(ctx, true, data, ctx.newPromise()); listener.onPingRead(ctx, data); } @Override - public void onPingAckRead(ChannelHandlerContext ctx, ByteBuf data) throws Http2Exception { + public void onPingAckRead(ChannelHandlerContext ctx, long data) throws Http2Exception { listener.onPingAckRead(ctx, data); } @@ -636,13 +636,13 @@ public class DefaultHttp2ConnectionDecoder implements Http2ConnectionDecoder { } @Override - public void onPingRead(ChannelHandlerContext ctx, ByteBuf data) throws Http2Exception { + public void onPingRead(ChannelHandlerContext ctx, long data) throws Http2Exception { verifyPrefaceReceived(); internalFrameListener.onPingRead(ctx, data); } @Override - public void onPingAckRead(ChannelHandlerContext ctx, ByteBuf data) throws Http2Exception { + public void onPingAckRead(ChannelHandlerContext ctx, long data) throws Http2Exception { verifyPrefaceReceived(); internalFrameListener.onPingAckRead(ctx, data); } diff --git a/codec-http2/src/main/java/io/netty/handler/codec/http2/DefaultHttp2ConnectionEncoder.java b/codec-http2/src/main/java/io/netty/handler/codec/http2/DefaultHttp2ConnectionEncoder.java index acfbe930bc..d0c5944cb9 100644 --- a/codec-http2/src/main/java/io/netty/handler/codec/http2/DefaultHttp2ConnectionEncoder.java +++ b/codec-http2/src/main/java/io/netty/handler/codec/http2/DefaultHttp2ConnectionEncoder.java @@ -272,7 +272,7 @@ public class DefaultHttp2ConnectionEncoder implements Http2ConnectionEncoder { } @Override - public ChannelFuture writePing(ChannelHandlerContext ctx, boolean ack, ByteBuf data, ChannelPromise promise) { + public ChannelFuture writePing(ChannelHandlerContext ctx, boolean ack, long data, ChannelPromise promise) { return frameWriter.writePing(ctx, ack, data, promise); } diff --git a/codec-http2/src/main/java/io/netty/handler/codec/http2/DefaultHttp2FrameReader.java b/codec-http2/src/main/java/io/netty/handler/codec/http2/DefaultHttp2FrameReader.java index b9025125c8..63e184bc7b 100644 --- a/codec-http2/src/main/java/io/netty/handler/codec/http2/DefaultHttp2FrameReader.java +++ b/codec-http2/src/main/java/io/netty/handler/codec/http2/DefaultHttp2FrameReader.java @@ -266,7 +266,7 @@ public class DefaultHttp2FrameReader implements Http2FrameReader, Http2FrameSize readPushPromiseFrame(ctx, payload, listener); break; case PING: - readPingFrame(ctx, payload, listener); + readPingFrame(ctx, payload.readLong(), listener); break; case GO_AWAY: readGoAwayFrame(ctx, payload, listener); @@ -574,9 +574,8 @@ public class DefaultHttp2FrameReader implements Http2FrameReader, Http2FrameSize resetHeadersContinuationIfEnd(flags.endOfHeaders()); } - private void readPingFrame(ChannelHandlerContext ctx, ByteBuf payload, + private void readPingFrame(ChannelHandlerContext ctx, long data, Http2FrameListener listener) throws Http2Exception { - ByteBuf data = payload.readSlice(payload.readableBytes()); if (flags.ack()) { listener.onPingAckRead(ctx, data); } else { diff --git a/codec-http2/src/main/java/io/netty/handler/codec/http2/DefaultHttp2FrameWriter.java b/codec-http2/src/main/java/io/netty/handler/codec/http2/DefaultHttp2FrameWriter.java index d0e40c6a97..3fa8413704 100644 --- a/codec-http2/src/main/java/io/netty/handler/codec/http2/DefaultHttp2FrameWriter.java +++ b/codec-http2/src/main/java/io/netty/handler/codec/http2/DefaultHttp2FrameWriter.java @@ -332,36 +332,14 @@ public class DefaultHttp2FrameWriter implements Http2FrameWriter, Http2FrameSize } @Override - public ChannelFuture writePing(ChannelHandlerContext ctx, boolean ack, ByteBuf data, ChannelPromise promise) { - final SimpleChannelPromiseAggregator promiseAggregator = - new SimpleChannelPromiseAggregator(promise, ctx.channel(), ctx.executor()); - try { - verifyPingPayload(data); - Http2Flags flags = ack ? new Http2Flags().ack(true) : new Http2Flags(); - int payloadLength = data.readableBytes(); - ByteBuf buf = ctx.alloc().buffer(FRAME_HEADER_LENGTH); - // Assume nothing below will throw until buf is written. That way we don't have to take care of ownership - // in the catch block. - writeFrameHeaderInternal(buf, payloadLength, PING, flags, 0); - ctx.write(buf, promiseAggregator.newPromise()); - } catch (Throwable t) { - try { - data.release(); - } finally { - promiseAggregator.setFailure(t); - promiseAggregator.doneAllocatingPromises(); - } - return promiseAggregator; - } - - try { - // Write the debug data. - ctx.write(data, promiseAggregator.newPromise()); - } catch (Throwable t) { - promiseAggregator.setFailure(t); - } - - return promiseAggregator.doneAllocatingPromises(); + public ChannelFuture writePing(ChannelHandlerContext ctx, boolean ack, long data, ChannelPromise promise) { + Http2Flags flags = ack ? new Http2Flags().ack(true) : new Http2Flags(); + ByteBuf buf = ctx.alloc().buffer(FRAME_HEADER_LENGTH + PING_FRAME_PAYLOAD_LENGTH); + // Assume nothing below will throw until buf is written. That way we don't have to take care of ownership + // in the catch block. + writeFrameHeaderInternal(buf, PING_FRAME_PAYLOAD_LENGTH, PING, flags, 0); + buf.writeLong(data); + return ctx.write(buf, promise); } @Override diff --git a/codec-http2/src/main/java/io/netty/handler/codec/http2/DefaultHttp2PingFrame.java b/codec-http2/src/main/java/io/netty/handler/codec/http2/DefaultHttp2PingFrame.java index 92400d14b4..be5f2da02c 100644 --- a/codec-http2/src/main/java/io/netty/handler/codec/http2/DefaultHttp2PingFrame.java +++ b/codec-http2/src/main/java/io/netty/handler/codec/http2/DefaultHttp2PingFrame.java @@ -16,8 +16,6 @@ package io.netty.handler.codec.http2; -import io.netty.buffer.ByteBuf; -import io.netty.buffer.DefaultByteBufHolder; import io.netty.util.internal.StringUtil; import io.netty.util.internal.UnstableApi; @@ -25,19 +23,20 @@ import io.netty.util.internal.UnstableApi; * The default {@link Http2PingFrame} implementation. */ @UnstableApi -public class DefaultHttp2PingFrame extends DefaultByteBufHolder implements Http2PingFrame { +public class DefaultHttp2PingFrame implements Http2PingFrame { + private final long content; private final boolean ack; - public DefaultHttp2PingFrame(ByteBuf content) { + public DefaultHttp2PingFrame(long content) { this(content, false); } /** * A user cannot send a ping ack, as this is done automatically when a ping is received. */ - DefaultHttp2PingFrame(ByteBuf content, boolean ack) { - super(mustBeEightBytes(content)); + DefaultHttp2PingFrame(long content, boolean ack) { + this.content = content; this.ack = ack; } @@ -52,47 +51,8 @@ public class DefaultHttp2PingFrame extends DefaultByteBufHolder implements Http2 } @Override - public DefaultHttp2PingFrame copy() { - return replace(content().copy()); - } - - @Override - public DefaultHttp2PingFrame duplicate() { - return replace(content().duplicate()); - } - - @Override - public DefaultHttp2PingFrame retainedDuplicate() { - return replace(content().retainedDuplicate()); - } - - @Override - public DefaultHttp2PingFrame replace(ByteBuf content) { - return new DefaultHttp2PingFrame(content, ack); - } - - @Override - public DefaultHttp2PingFrame retain() { - super.retain(); - return this; - } - - @Override - public DefaultHttp2PingFrame retain(int increment) { - super.retain(increment); - return this; - } - - @Override - public DefaultHttp2PingFrame touch() { - super.touch(); - return this; - } - - @Override - public DefaultHttp2PingFrame touch(Object hint) { - super.touch(hint); - return this; + public long content() { + return content; } @Override @@ -101,7 +61,7 @@ public class DefaultHttp2PingFrame extends DefaultByteBufHolder implements Http2 return false; } Http2PingFrame other = (Http2PingFrame) o; - return super.equals(o) && ack == other.ack(); + return ack == other.ack() && content == other.content(); } @Override @@ -111,16 +71,8 @@ public class DefaultHttp2PingFrame extends DefaultByteBufHolder implements Http2 return hash; } - private static ByteBuf mustBeEightBytes(ByteBuf content) { - if (content.readableBytes() != 8) { - throw new IllegalArgumentException("PING frames require 8 bytes of content. Was " + - content.readableBytes() + " bytes."); - } - return content; - } - @Override public String toString() { - return StringUtil.simpleClassName(this) + "(content=" + contentToString() + ", ack=" + ack + ')'; + return StringUtil.simpleClassName(this) + "(content=" + content + ", ack=" + ack + ')'; } } diff --git a/codec-http2/src/main/java/io/netty/handler/codec/http2/Http2EventAdapter.java b/codec-http2/src/main/java/io/netty/handler/codec/http2/Http2EventAdapter.java index 1b017c9cf0..6b2a2a7122 100644 --- a/codec-http2/src/main/java/io/netty/handler/codec/http2/Http2EventAdapter.java +++ b/codec-http2/src/main/java/io/netty/handler/codec/http2/Http2EventAdapter.java @@ -58,11 +58,11 @@ public class Http2EventAdapter implements Http2Connection.Listener, Http2FrameLi } @Override - public void onPingRead(ChannelHandlerContext ctx, ByteBuf data) throws Http2Exception { + public void onPingRead(ChannelHandlerContext ctx, long data) throws Http2Exception { } @Override - public void onPingAckRead(ChannelHandlerContext ctx, ByteBuf data) throws Http2Exception { + public void onPingAckRead(ChannelHandlerContext ctx, long data) throws Http2Exception { } @Override diff --git a/codec-http2/src/main/java/io/netty/handler/codec/http2/Http2FrameAdapter.java b/codec-http2/src/main/java/io/netty/handler/codec/http2/Http2FrameAdapter.java index f9346ecf08..63bbad24e9 100644 --- a/codec-http2/src/main/java/io/netty/handler/codec/http2/Http2FrameAdapter.java +++ b/codec-http2/src/main/java/io/netty/handler/codec/http2/Http2FrameAdapter.java @@ -61,11 +61,11 @@ public class Http2FrameAdapter implements Http2FrameListener { } @Override - public void onPingRead(ChannelHandlerContext ctx, ByteBuf data) throws Http2Exception { + public void onPingRead(ChannelHandlerContext ctx, long data) throws Http2Exception { } @Override - public void onPingAckRead(ChannelHandlerContext ctx, ByteBuf data) throws Http2Exception { + public void onPingAckRead(ChannelHandlerContext ctx, long data) throws Http2Exception { } @Override diff --git a/codec-http2/src/main/java/io/netty/handler/codec/http2/Http2FrameCodec.java b/codec-http2/src/main/java/io/netty/handler/codec/http2/Http2FrameCodec.java index 4b542bc7e9..046790974b 100644 --- a/codec-http2/src/main/java/io/netty/handler/codec/http2/Http2FrameCodec.java +++ b/codec-http2/src/main/java/io/netty/handler/codec/http2/Http2FrameCodec.java @@ -499,13 +499,13 @@ public class Http2FrameCodec extends Http2ConnectionHandler { } @Override - public void onPingRead(ChannelHandlerContext ctx, ByteBuf data) { - onHttp2Frame(ctx, new DefaultHttp2PingFrame(data, false).retain()); + public void onPingRead(ChannelHandlerContext ctx, long data) { + onHttp2Frame(ctx, new DefaultHttp2PingFrame(data, false)); } @Override - public void onPingAckRead(ChannelHandlerContext ctx, ByteBuf data) { - onHttp2Frame(ctx, new DefaultHttp2PingFrame(data, true).retain()); + public void onPingAckRead(ChannelHandlerContext ctx, long data) { + onHttp2Frame(ctx, new DefaultHttp2PingFrame(data, true)); } @Override diff --git a/codec-http2/src/main/java/io/netty/handler/codec/http2/Http2FrameListener.java b/codec-http2/src/main/java/io/netty/handler/codec/http2/Http2FrameListener.java index 1d6e376da5..e48923b537 100644 --- a/codec-http2/src/main/java/io/netty/handler/codec/http2/Http2FrameListener.java +++ b/codec-http2/src/main/java/io/netty/handler/codec/http2/Http2FrameListener.java @@ -144,19 +144,17 @@ public interface Http2FrameListener { * Handles an inbound {@code PING} frame. * * @param ctx the context from the handler where the frame was read. - * @param data the payload of the frame. If this buffer needs to be retained by the listener - * they must make a copy. + * @param data the payload of the frame. */ - void onPingRead(ChannelHandlerContext ctx, ByteBuf data) throws Http2Exception; + void onPingRead(ChannelHandlerContext ctx, long data) throws Http2Exception; /** * Handles an inbound {@code PING} acknowledgment. * * @param ctx the context from the handler where the frame was read. - * @param data the payload of the frame. If this buffer needs to be retained by the listener - * they must make a copy. + * @param data the payload of the frame. */ - void onPingAckRead(ChannelHandlerContext ctx, ByteBuf data) throws Http2Exception; + void onPingAckRead(ChannelHandlerContext ctx, long data) throws Http2Exception; /** * Handles an inbound {@code PUSH_PROMISE} frame. Only called if {@code END_HEADERS} encountered. diff --git a/codec-http2/src/main/java/io/netty/handler/codec/http2/Http2FrameListenerDecorator.java b/codec-http2/src/main/java/io/netty/handler/codec/http2/Http2FrameListenerDecorator.java index 203ee3fbaf..e2eecd18c6 100644 --- a/codec-http2/src/main/java/io/netty/handler/codec/http2/Http2FrameListenerDecorator.java +++ b/codec-http2/src/main/java/io/netty/handler/codec/http2/Http2FrameListenerDecorator.java @@ -70,12 +70,12 @@ public class Http2FrameListenerDecorator implements Http2FrameListener { } @Override - public void onPingRead(ChannelHandlerContext ctx, ByteBuf data) throws Http2Exception { + public void onPingRead(ChannelHandlerContext ctx, long data) throws Http2Exception { listener.onPingRead(ctx, data); } @Override - public void onPingAckRead(ChannelHandlerContext ctx, ByteBuf data) throws Http2Exception { + public void onPingAckRead(ChannelHandlerContext ctx, long data) throws Http2Exception { listener.onPingAckRead(ctx, data); } diff --git a/codec-http2/src/main/java/io/netty/handler/codec/http2/Http2FrameLogger.java b/codec-http2/src/main/java/io/netty/handler/codec/http2/Http2FrameLogger.java index 84ee9834de..8a67e5f87a 100644 --- a/codec-http2/src/main/java/io/netty/handler/codec/http2/Http2FrameLogger.java +++ b/codec-http2/src/main/java/io/netty/handler/codec/http2/Http2FrameLogger.java @@ -98,14 +98,14 @@ public class Http2FrameLogger extends ChannelHandlerAdapter { logger.log(level, "{} {} SETTINGS: ack=false settings={}", ctx.channel(), direction.name(), settings); } - public void logPing(Direction direction, ChannelHandlerContext ctx, ByteBuf data) { - logger.log(level, "{} {} PING: ack=false length={} bytes={}", ctx.channel(), - direction.name(), data.readableBytes(), toString(data)); + public void logPing(Direction direction, ChannelHandlerContext ctx, long data) { + logger.log(level, "{} {} PING: ack=false bytes={}", ctx.channel(), + direction.name(), data); } - public void logPingAck(Direction direction, ChannelHandlerContext ctx, ByteBuf data) { - logger.log(level, "{} {} PING: ack=true length={} bytes={}", ctx.channel(), - direction.name(), data.readableBytes(), toString(data)); + public void logPingAck(Direction direction, ChannelHandlerContext ctx, long data) { + logger.log(level, "{} {} PING: ack=true bytes={}", ctx.channel(), + direction.name(), data); } public void logPushPromise(Direction direction, ChannelHandlerContext ctx, int streamId, int promisedStreamId, diff --git a/codec-http2/src/main/java/io/netty/handler/codec/http2/Http2FrameWriter.java b/codec-http2/src/main/java/io/netty/handler/codec/http2/Http2FrameWriter.java index f3749e30e3..1ae2e2e494 100644 --- a/codec-http2/src/main/java/io/netty/handler/codec/http2/Http2FrameWriter.java +++ b/codec-http2/src/main/java/io/netty/handler/codec/http2/Http2FrameWriter.java @@ -147,11 +147,11 @@ public interface Http2FrameWriter extends Http2DataWriter, Closeable { * @param ctx the context to use for writing. * @param ack indicates whether this is an ack of a PING frame previously received from the * remote endpoint. - * @param data the payload of the frame. This will be released by this method. + * @param data the payload of the frame. * @param promise the promise for the write. * @return the future for the write. */ - ChannelFuture writePing(ChannelHandlerContext ctx, boolean ack, ByteBuf data, + ChannelFuture writePing(ChannelHandlerContext ctx, boolean ack, long data, ChannelPromise promise); /** diff --git a/codec-http2/src/main/java/io/netty/handler/codec/http2/Http2InboundFrameLogger.java b/codec-http2/src/main/java/io/netty/handler/codec/http2/Http2InboundFrameLogger.java index 395eeadd5c..806b872105 100644 --- a/codec-http2/src/main/java/io/netty/handler/codec/http2/Http2InboundFrameLogger.java +++ b/codec-http2/src/main/java/io/netty/handler/codec/http2/Http2InboundFrameLogger.java @@ -94,13 +94,13 @@ public class Http2InboundFrameLogger implements Http2FrameReader { } @Override - public void onPingRead(ChannelHandlerContext ctx, ByteBuf data) throws Http2Exception { + public void onPingRead(ChannelHandlerContext ctx, long data) throws Http2Exception { logger.logPing(INBOUND, ctx, data); listener.onPingRead(ctx, data); } @Override - public void onPingAckRead(ChannelHandlerContext ctx, ByteBuf data) throws Http2Exception { + public void onPingAckRead(ChannelHandlerContext ctx, long data) throws Http2Exception { logger.logPingAck(INBOUND, ctx, data); listener.onPingAckRead(ctx, data); } diff --git a/codec-http2/src/main/java/io/netty/handler/codec/http2/Http2OutboundFrameLogger.java b/codec-http2/src/main/java/io/netty/handler/codec/http2/Http2OutboundFrameLogger.java index 5803113292..5a86ac5364 100644 --- a/codec-http2/src/main/java/io/netty/handler/codec/http2/Http2OutboundFrameLogger.java +++ b/codec-http2/src/main/java/io/netty/handler/codec/http2/Http2OutboundFrameLogger.java @@ -90,7 +90,7 @@ public class Http2OutboundFrameLogger implements Http2FrameWriter { @Override public ChannelFuture writePing(ChannelHandlerContext ctx, boolean ack, - ByteBuf data, ChannelPromise promise) { + long data, ChannelPromise promise) { if (ack) { logger.logPingAck(OUTBOUND, ctx, data); } else { diff --git a/codec-http2/src/main/java/io/netty/handler/codec/http2/Http2PingFrame.java b/codec-http2/src/main/java/io/netty/handler/codec/http2/Http2PingFrame.java index 113f007e06..6dd023e39c 100644 --- a/codec-http2/src/main/java/io/netty/handler/codec/http2/Http2PingFrame.java +++ b/codec-http2/src/main/java/io/netty/handler/codec/http2/Http2PingFrame.java @@ -16,15 +16,13 @@ package io.netty.handler.codec.http2; -import io.netty.buffer.ByteBuf; -import io.netty.buffer.ByteBufHolder; import io.netty.util.internal.UnstableApi; /** * HTTP/2 PING Frame. */ @UnstableApi -public interface Http2PingFrame extends Http2Frame, ByteBufHolder { +public interface Http2PingFrame extends Http2Frame { /** * When {@code true}, indicates that this ping is a ping response. @@ -34,6 +32,5 @@ public interface Http2PingFrame extends Http2Frame, ByteBufHolder { /** * Returns the eight byte opaque data. */ - @Override - ByteBuf content(); + long content(); } diff --git a/codec-http2/src/test/java/io/netty/handler/codec/http2/DefaultHttp2ConnectionDecoderTest.java b/codec-http2/src/test/java/io/netty/handler/codec/http2/DefaultHttp2ConnectionDecoderTest.java index 3fcf560eff..8d71ddeaff 100644 --- a/codec-http2/src/test/java/io/netty/handler/codec/http2/DefaultHttp2ConnectionDecoderTest.java +++ b/codec-http2/src/test/java/io/netty/handler/codec/http2/DefaultHttp2ConnectionDecoderTest.java @@ -37,7 +37,6 @@ import java.util.concurrent.atomic.AtomicInteger; import static io.netty.buffer.Unpooled.EMPTY_BUFFER; import static io.netty.buffer.Unpooled.wrappedBuffer; import static io.netty.handler.codec.http2.Http2CodecUtil.DEFAULT_PRIORITY_WEIGHT; -import static io.netty.handler.codec.http2.Http2CodecUtil.emptyPingBuf; import static io.netty.handler.codec.http2.Http2Error.PROTOCOL_ERROR; import static io.netty.handler.codec.http2.Http2Stream.State.IDLE; import static io.netty.handler.codec.http2.Http2Stream.State.OPEN; @@ -714,15 +713,15 @@ public class DefaultHttp2ConnectionDecoderTest { @Test public void pingReadWithAckShouldNotifyListener() throws Exception { - decode().onPingAckRead(ctx, emptyPingBuf()); - verify(listener).onPingAckRead(eq(ctx), eq(emptyPingBuf())); + decode().onPingAckRead(ctx, 0L); + verify(listener).onPingAckRead(eq(ctx), eq(0L)); } @Test public void pingReadShouldReplyWithAck() throws Exception { - decode().onPingRead(ctx, emptyPingBuf()); - verify(encoder).writePing(eq(ctx), eq(true), eq(emptyPingBuf()), eq(promise)); - verify(listener, never()).onPingAckRead(eq(ctx), any(ByteBuf.class)); + decode().onPingRead(ctx, 0L); + verify(encoder).writePing(eq(ctx), eq(true), eq(0L), eq(promise)); + verify(listener, never()).onPingAckRead(eq(ctx), any(long.class)); } @Test diff --git a/codec-http2/src/test/java/io/netty/handler/codec/http2/DefaultHttp2ConnectionEncoderTest.java b/codec-http2/src/test/java/io/netty/handler/codec/http2/DefaultHttp2ConnectionEncoderTest.java index 8b4db95fdd..5f9a6b1871 100644 --- a/codec-http2/src/test/java/io/netty/handler/codec/http2/DefaultHttp2ConnectionEncoderTest.java +++ b/codec-http2/src/test/java/io/netty/handler/codec/http2/DefaultHttp2ConnectionEncoderTest.java @@ -45,7 +45,6 @@ import java.util.List; import static io.netty.buffer.Unpooled.EMPTY_BUFFER; import static io.netty.buffer.Unpooled.wrappedBuffer; import static io.netty.handler.codec.http2.Http2CodecUtil.DEFAULT_PRIORITY_WEIGHT; -import static io.netty.handler.codec.http2.Http2CodecUtil.emptyPingBuf; import static io.netty.handler.codec.http2.Http2Error.PROTOCOL_ERROR; import static io.netty.handler.codec.http2.Http2Stream.State.HALF_CLOSED_REMOTE; import static io.netty.handler.codec.http2.Http2Stream.State.RESERVED_LOCAL; @@ -639,15 +638,15 @@ public class DefaultHttp2ConnectionEncoderTest { public void pingWriteAfterGoAwayShouldSucceed() throws Exception { ChannelPromise promise = newPromise(); goAwayReceived(0); - encoder.writePing(ctx, false, emptyPingBuf(), promise); - verify(writer).writePing(eq(ctx), eq(false), eq(emptyPingBuf()), eq(promise)); + encoder.writePing(ctx, false, 0L, promise); + verify(writer).writePing(eq(ctx), eq(false), eq(0L), eq(promise)); } @Test public void pingWriteShouldSucceed() throws Exception { ChannelPromise promise = newPromise(); - encoder.writePing(ctx, false, emptyPingBuf(), promise); - verify(writer).writePing(eq(ctx), eq(false), eq(emptyPingBuf()), eq(promise)); + encoder.writePing(ctx, false, 0L, promise); + verify(writer).writePing(eq(ctx), eq(false), eq(0L), eq(promise)); } @Test diff --git a/codec-http2/src/test/java/io/netty/handler/codec/http2/Http2ConnectionRoundtripTest.java b/codec-http2/src/test/java/io/netty/handler/codec/http2/Http2ConnectionRoundtripTest.java index 605a24b536..9860d28fee 100644 --- a/codec-http2/src/test/java/io/netty/handler/codec/http2/Http2ConnectionRoundtripTest.java +++ b/codec-http2/src/test/java/io/netty/handler/codec/http2/Http2ConnectionRoundtripTest.java @@ -57,7 +57,6 @@ import static io.netty.handler.codec.http2.Http2CodecUtil.DEFAULT_PRIORITY_WEIGH import static io.netty.handler.codec.http2.Http2Error.PROTOCOL_ERROR; import static io.netty.handler.codec.http2.Http2TestUtil.randomString; import static io.netty.handler.codec.http2.Http2TestUtil.runInChannel; -import static io.netty.util.CharsetUtil.UTF_8; import static java.util.concurrent.TimeUnit.SECONDS; import static org.hamcrest.CoreMatchers.instanceOf; import static org.hamcrest.CoreMatchers.not; @@ -871,24 +870,23 @@ public class Http2ConnectionRoundtripTest { @Test public void stressTest() throws Exception { final Http2Headers headers = dummyHeaders(); - final String pingMsg = "12345678"; int length = 10; final ByteBuf data = randomBytes(length); final String dataAsHex = ByteBufUtil.hexDump(data); - final ByteBuf pingData = Unpooled.copiedBuffer(pingMsg, UTF_8); + final long pingData = 8; final int numStreams = 2000; // Collect all the ping buffers as we receive them at the server. - final String[] receivedPings = new String[numStreams]; + final long[] receivedPings = new long[numStreams]; doAnswer(new Answer() { int nextIndex; @Override public Void answer(InvocationOnMock in) throws Throwable { - receivedPings[nextIndex++] = ((ByteBuf) in.getArguments()[1]).toString(UTF_8); + receivedPings[nextIndex++] = (Long) in.getArguments()[1]; return null; } - }).when(serverListener).onPingRead(any(ChannelHandlerContext.class), any(ByteBuf.class)); + }).when(serverListener).onPingRead(any(ChannelHandlerContext.class), any(Long.class)); // Collect all the data buffers as we receive them at the server. final StringBuilder[] receivedData = new StringBuilder[numStreams]; @@ -921,7 +919,7 @@ public class Http2ConnectionRoundtripTest { // Send a bunch of data on each stream. http2Client.encoder().writeHeaders(ctx(), streamId, headers, 0, (short) 16, false, 0, false, newPromise()); - http2Client.encoder().writePing(ctx(), false, pingData.retainedSlice(), + http2Client.encoder().writePing(ctx(), false, pingData, newPromise()); http2Client.encoder().writeData(ctx(), streamId, data.retainedSlice(), 0, false, newPromise()); @@ -940,20 +938,19 @@ public class Http2ConnectionRoundtripTest { verify(serverListener, times(numStreams)).onHeadersRead(any(ChannelHandlerContext.class), anyInt(), eq(headers), eq(0), eq((short) 16), eq(false), eq(0), eq(true)); verify(serverListener, times(numStreams)).onPingRead(any(ChannelHandlerContext.class), - any(ByteBuf.class)); + any(long.class)); verify(serverListener, never()).onDataRead(any(ChannelHandlerContext.class), anyInt(), any(ByteBuf.class), eq(0), eq(true)); for (StringBuilder builder : receivedData) { assertEquals(dataAsHex, builder.toString()); } - for (String receivedPing : receivedPings) { - assertEquals(pingMsg, receivedPing); + for (long receivedPing : receivedPings) { + assertEquals(pingData, receivedPing); } } finally { // Don't wait for server to close streams http2Client.gracefulShutdownTimeoutMillis(0); data.release(); - pingData.release(); } } diff --git a/codec-http2/src/test/java/io/netty/handler/codec/http2/Http2FrameCodecTest.java b/codec-http2/src/test/java/io/netty/handler/codec/http2/Http2FrameCodecTest.java index 44676fc82e..99468bcc33 100644 --- a/codec-http2/src/test/java/io/netty/handler/codec/http2/Http2FrameCodecTest.java +++ b/codec-http2/src/test/java/io/netty/handler/codec/http2/Http2FrameCodecTest.java @@ -615,24 +615,20 @@ public class Http2FrameCodecTest { @Test public void receivePing() throws Http2Exception { - ByteBuf data = Unpooled.buffer(8).writeLong(12345); - frameListener.onPingRead(http2HandlerCtx, data); + frameListener.onPingRead(http2HandlerCtx, 12345L); Http2PingFrame pingFrame = inboundHandler.readInbound(); assertNotNull(pingFrame); - assertEquals(data, pingFrame.content()); + assertEquals(12345, pingFrame.content()); assertFalse(pingFrame.ack()); - pingFrame.release(); - data.release(); } @Test public void sendPing() { - ByteBuf data = Unpooled.buffer(8).writeLong(12345); - channel.writeAndFlush(new DefaultHttp2PingFrame(data)); + channel.writeAndFlush(new DefaultHttp2PingFrame(12345)); - verify(frameWriter).writePing(eq(http2HandlerCtx), eq(false), eq(data), anyChannelPromise()); + verify(frameWriter).writePing(eq(http2HandlerCtx), eq(false), eq(12345L), anyChannelPromise()); } @Test diff --git a/codec-http2/src/test/java/io/netty/handler/codec/http2/Http2FrameRoundtripTest.java b/codec-http2/src/test/java/io/netty/handler/codec/http2/Http2FrameRoundtripTest.java index 379b5fc43a..cf938a1332 100644 --- a/codec-http2/src/test/java/io/netty/handler/codec/http2/Http2FrameRoundtripTest.java +++ b/codec-http2/src/test/java/io/netty/handler/codec/http2/Http2FrameRoundtripTest.java @@ -351,26 +351,22 @@ public class Http2FrameRoundtripTest { @Test public void pingFrameShouldMatch() throws Exception { - final ByteBuf data = buf("01234567".getBytes(UTF_8)); - - writer.writePing(ctx, false, data.slice(), ctx.newPromise()); + writer.writePing(ctx, false, 1234567, ctx.newPromise()); readFrames(); - ArgumentCaptor captor = ArgumentCaptor.forClass(ByteBuf.class); + ArgumentCaptor captor = ArgumentCaptor.forClass(long.class); verify(listener).onPingRead(eq(ctx), captor.capture()); - assertEquals(data, captor.getValue()); + assertEquals(1234567, (long) captor.getValue()); } @Test public void pingAckFrameShouldMatch() throws Exception { - final ByteBuf data = buf("01234567".getBytes(UTF_8)); - - writer.writePing(ctx, true, data.slice(), ctx.newPromise()); + writer.writePing(ctx, true, 1234567, ctx.newPromise()); readFrames(); - ArgumentCaptor captor = ArgumentCaptor.forClass(ByteBuf.class); + ArgumentCaptor captor = ArgumentCaptor.forClass(long.class); verify(listener).onPingAckRead(eq(ctx), captor.capture()); - assertEquals(data, captor.getValue()); + assertEquals(1234567, (long) captor.getValue()); } @Test diff --git a/codec-http2/src/test/java/io/netty/handler/codec/http2/Http2MultiplexCodecTest.java b/codec-http2/src/test/java/io/netty/handler/codec/http2/Http2MultiplexCodecTest.java index 57f17bcd35..f71d669776 100644 --- a/codec-http2/src/test/java/io/netty/handler/codec/http2/Http2MultiplexCodecTest.java +++ b/codec-http2/src/test/java/io/netty/handler/codec/http2/Http2MultiplexCodecTest.java @@ -175,13 +175,11 @@ public class Http2MultiplexCodecTest { @Test public void unhandledHttp2FramesShouldBePropagated() { - ByteBuf content = UnpooledByteBufAllocator.DEFAULT.buffer(8).writeLong(0); - Http2PingFrame decodedFrame = new DefaultHttp2PingFrame(content); + Http2PingFrame decodedFrame = new DefaultHttp2PingFrame(0); codec.onHttp2Frame(decodedFrame); Http2PingFrame receivedPing = parentChannel.readInbound(); assertSame(receivedPing, decodedFrame); - assertTrue(receivedPing.release()); } @Test diff --git a/codec-http2/src/test/java/io/netty/handler/codec/http2/Http2TestUtil.java b/codec-http2/src/test/java/io/netty/handler/codec/http2/Http2TestUtil.java index 8fafd435d8..e7130ee5d9 100644 --- a/codec-http2/src/test/java/io/netty/handler/codec/http2/Http2TestUtil.java +++ b/codec-http2/src/test/java/io/netty/handler/codec/http2/Http2TestUtil.java @@ -249,13 +249,13 @@ public final class Http2TestUtil { } @Override - public void onPingRead(ChannelHandlerContext ctx, ByteBuf data) throws Http2Exception { + public void onPingRead(ChannelHandlerContext ctx, long data) throws Http2Exception { listener.onPingRead(ctx, data); latch.countDown(); } @Override - public void onPingAckRead(ChannelHandlerContext ctx, ByteBuf data) throws Http2Exception { + public void onPingAckRead(ChannelHandlerContext ctx, long data) throws Http2Exception { listener.onPingAckRead(ctx, data); latch.countDown(); } @@ -384,13 +384,13 @@ public final class Http2TestUtil { } @Override - public void onPingRead(ChannelHandlerContext ctx, ByteBuf data) throws Http2Exception { + public void onPingRead(ChannelHandlerContext ctx, long data) throws Http2Exception { listener.onPingRead(ctx, data); messageLatch.countDown(); } @Override - public void onPingAckRead(ChannelHandlerContext ctx, ByteBuf data) throws Http2Exception { + public void onPingAckRead(ChannelHandlerContext ctx, long data) throws Http2Exception { listener.onPingAckRead(ctx, data); messageLatch.countDown(); } diff --git a/example/src/main/java/io/netty/example/http2/helloworld/server/HelloWorldHttp2Handler.java b/example/src/main/java/io/netty/example/http2/helloworld/server/HelloWorldHttp2Handler.java index cb92bfaa94..9a012d6599 100644 --- a/example/src/main/java/io/netty/example/http2/helloworld/server/HelloWorldHttp2Handler.java +++ b/example/src/main/java/io/netty/example/http2/helloworld/server/HelloWorldHttp2Handler.java @@ -138,11 +138,11 @@ public final class HelloWorldHttp2Handler extends Http2ConnectionHandler impleme } @Override - public void onPingRead(ChannelHandlerContext ctx, ByteBuf data) { + public void onPingRead(ChannelHandlerContext ctx, long data) { } @Override - public void onPingAckRead(ChannelHandlerContext ctx, ByteBuf data) { + public void onPingAckRead(ChannelHandlerContext ctx, long data) { } @Override diff --git a/testsuite-http2/src/main/java/io/netty/testsuite/http2/HelloWorldHttp2Handler.java b/testsuite-http2/src/main/java/io/netty/testsuite/http2/HelloWorldHttp2Handler.java index a65b22dcc2..774c1ec13c 100644 --- a/testsuite-http2/src/main/java/io/netty/testsuite/http2/HelloWorldHttp2Handler.java +++ b/testsuite-http2/src/main/java/io/netty/testsuite/http2/HelloWorldHttp2Handler.java @@ -138,11 +138,11 @@ public final class HelloWorldHttp2Handler extends Http2ConnectionHandler impleme } @Override - public void onPingRead(ChannelHandlerContext ctx, ByteBuf data) { + public void onPingRead(ChannelHandlerContext ctx, long data) { } @Override - public void onPingAckRead(ChannelHandlerContext ctx, ByteBuf data) { + public void onPingAckRead(ChannelHandlerContext ctx, long data) { } @Override