From e8afa3ca23b927de83128f9afc027a5908c8cbc3 Mon Sep 17 00:00:00 2001 From: Jakob Buchgraber Date: Thu, 9 Apr 2015 12:00:51 -0700 Subject: [PATCH] Fix bug where the stream id would not get incremented for buffered streams. --- .../io/grpc/transport/netty/NettyClientHandler.java | 10 +++++++++- .../grpc/transport/netty/NettyClientHandlerTest.java | 11 +++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/netty/src/main/java/io/grpc/transport/netty/NettyClientHandler.java b/netty/src/main/java/io/grpc/transport/netty/NettyClientHandler.java index 3d91b6cf4b..a9635b4c07 100644 --- a/netty/src/main/java/io/grpc/transport/netty/NettyClientHandler.java +++ b/netty/src/main/java/io/grpc/transport/netty/NettyClientHandler.java @@ -68,6 +68,7 @@ class NettyClientHandler extends Http2ConnectionHandler { private Throwable connectionError; private Status goAwayStatus; private ChannelHandlerContext ctx; + private int nextStreamId; public NettyClientHandler(Http2ConnectionEncoder encoder, Http2Connection connection, Http2FrameReader frameReader, Http2LocalFlowController inboundFlow, @@ -84,6 +85,7 @@ class NettyClientHandler extends Http2ConnectionHandler { // Disallow stream creation by the server. connection.remote().maxActiveStreams(0); connection.local().allowPushTo(false); + nextStreamId = connection.local().nextStreamId(); } @Nullable @@ -232,7 +234,7 @@ class NettyClientHandler extends Http2ConnectionHandler { */ private void createStream(CreateStreamCommand command, final ChannelPromise promise) { final Http2Connection.Endpoint local = connection().local(); - final int streamId = local.nextStreamId(); + final int streamId = getAndIncrementNextStreamId(); final NettyClientStream stream = command.stream(); final Http2Headers headers = command.headers(); // TODO: Send GO_AWAY if streamId overflows @@ -339,6 +341,12 @@ class NettyClientHandler extends Http2ConnectionHandler { return connection().activeStreams().toArray(new Http2Stream[0]); } + private int getAndIncrementNextStreamId() { + int id = nextStreamId; + nextStreamId += 2; + return id; + } + /** * Initializes the connection window if we haven't already. */ diff --git a/netty/src/test/java/io/grpc/transport/netty/NettyClientHandlerTest.java b/netty/src/test/java/io/grpc/transport/netty/NettyClientHandlerTest.java index ecd9c165cf..89cd5a54a2 100644 --- a/netty/src/test/java/io/grpc/transport/netty/NettyClientHandlerTest.java +++ b/netty/src/test/java/io/grpc/transport/netty/NettyClientHandlerTest.java @@ -359,6 +359,17 @@ public class NettyClientHandlerTest extends NettyHandlerTestBase { handler.decoder().flowController().windowSize(handler.connection().connectionStream())); } + @Test + public void createIncrementsIdsForActualAndBufferdStreams() throws Exception { + receiveMaxConcurrentStreams(2); + handler.write(ctx, new CreateStreamCommand(grpcHeaders, stream), promise); + verify(stream).id(eq(3)); + handler.write(ctx, new CreateStreamCommand(grpcHeaders, stream), promise); + verify(stream).id(eq(5)); + handler.write(ctx, new CreateStreamCommand(grpcHeaders, stream), promise); + verify(stream).id(eq(7)); + } + private void receiveMaxConcurrentStreams(int max) throws Exception { ByteBuf serializedSettings = serializeSettings(new Http2Settings().maxConcurrentStreams(max)); handler.channelRead(ctx, serializedSettings);