2015-06-03 05:15:59 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2015 The Netty Project
|
|
|
|
*
|
|
|
|
* The Netty Project licenses this file to you under the Apache License, version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the License. You may obtain a
|
|
|
|
* copy of the License at:
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software distributed under the License
|
|
|
|
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
|
|
|
|
* or implied. See the License for the specific language governing permissions and limitations under
|
|
|
|
* the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package io.netty.handler.codec.http2;
|
|
|
|
|
2015-06-23 00:38:53 +02:00
|
|
|
import static io.netty.buffer.Unpooled.EMPTY_BUFFER;
|
|
|
|
import static io.netty.handler.codec.http2.Http2CodecUtil.DEFAULT_MAX_FRAME_SIZE;
|
|
|
|
import static io.netty.handler.codec.http2.Http2CodecUtil.DEFAULT_PRIORITY_WEIGHT;
|
|
|
|
import static io.netty.handler.codec.http2.Http2CodecUtil.SMALLEST_MAX_CONCURRENT_STREAMS;
|
|
|
|
import static io.netty.handler.codec.http2.Http2Error.CANCEL;
|
|
|
|
import static io.netty.handler.codec.http2.Http2Stream.State.HALF_CLOSED_LOCAL;
|
|
|
|
import static org.junit.Assert.assertEquals;
|
2015-11-19 05:46:11 +01:00
|
|
|
import static org.junit.Assert.assertFalse;
|
|
|
|
import static org.junit.Assert.assertNotNull;
|
|
|
|
import static org.junit.Assert.assertTrue;
|
2017-03-09 17:10:38 +01:00
|
|
|
import static org.mockito.Mockito.any;
|
|
|
|
import static org.mockito.Mockito.anyBoolean;
|
|
|
|
import static org.mockito.Mockito.anyInt;
|
|
|
|
import static org.mockito.Mockito.anyLong;
|
|
|
|
import static org.mockito.Mockito.anyShort;
|
|
|
|
import static org.mockito.Mockito.eq;
|
2015-11-19 05:46:11 +01:00
|
|
|
import static org.mockito.Mockito.doAnswer;
|
2015-06-23 00:38:53 +02:00
|
|
|
import static org.mockito.Mockito.mock;
|
|
|
|
import static org.mockito.Mockito.never;
|
|
|
|
import static org.mockito.Mockito.times;
|
|
|
|
import static org.mockito.Mockito.verify;
|
|
|
|
import static org.mockito.Mockito.when;
|
2015-11-19 05:46:11 +01:00
|
|
|
|
2015-06-03 05:15:59 +02:00
|
|
|
import io.netty.buffer.ByteBuf;
|
|
|
|
import io.netty.buffer.Unpooled;
|
|
|
|
import io.netty.buffer.UnpooledByteBufAllocator;
|
|
|
|
import io.netty.channel.Channel;
|
2015-06-30 19:10:17 +02:00
|
|
|
import io.netty.channel.ChannelConfig;
|
2015-06-03 05:15:59 +02:00
|
|
|
import io.netty.channel.ChannelFuture;
|
|
|
|
import io.netty.channel.ChannelHandlerContext;
|
|
|
|
import io.netty.channel.ChannelPromise;
|
|
|
|
import io.netty.channel.DefaultChannelPromise;
|
2015-06-23 00:38:53 +02:00
|
|
|
import io.netty.util.ReferenceCountUtil;
|
2015-06-02 02:24:49 +02:00
|
|
|
import io.netty.util.concurrent.EventExecutor;
|
2015-06-03 05:15:59 +02:00
|
|
|
import io.netty.util.concurrent.ImmediateEventExecutor;
|
2015-06-23 00:38:53 +02:00
|
|
|
import org.junit.After;
|
2015-06-03 05:15:59 +02:00
|
|
|
import org.junit.Before;
|
|
|
|
import org.junit.Test;
|
2015-06-04 20:55:18 +02:00
|
|
|
import org.mockito.ArgumentCaptor;
|
2015-06-03 05:15:59 +02:00
|
|
|
import org.mockito.Mock;
|
|
|
|
import org.mockito.MockitoAnnotations;
|
|
|
|
import org.mockito.invocation.InvocationOnMock;
|
|
|
|
import org.mockito.stubbing.Answer;
|
|
|
|
import org.mockito.verification.VerificationMode;
|
|
|
|
|
2015-11-19 05:46:11 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
2015-06-03 05:15:59 +02:00
|
|
|
/**
|
|
|
|
* Tests for {@link StreamBufferingEncoder}.
|
|
|
|
*/
|
|
|
|
public class StreamBufferingEncoderTest {
|
|
|
|
|
|
|
|
private StreamBufferingEncoder encoder;
|
|
|
|
|
|
|
|
private Http2Connection connection;
|
|
|
|
|
|
|
|
@Mock
|
|
|
|
private Http2FrameWriter writer;
|
|
|
|
|
|
|
|
@Mock
|
|
|
|
private ChannelHandlerContext ctx;
|
|
|
|
|
|
|
|
@Mock
|
|
|
|
private Channel channel;
|
|
|
|
|
2015-06-30 19:10:17 +02:00
|
|
|
@Mock
|
|
|
|
private ChannelConfig config;
|
|
|
|
|
2015-06-02 02:24:49 +02:00
|
|
|
@Mock
|
|
|
|
private EventExecutor executor;
|
|
|
|
|
2015-06-03 05:15:59 +02:00
|
|
|
/**
|
|
|
|
* Init fields and do mocking.
|
|
|
|
*/
|
|
|
|
@Before
|
|
|
|
public void setup() throws Exception {
|
|
|
|
MockitoAnnotations.initMocks(this);
|
|
|
|
|
|
|
|
Http2FrameWriter.Configuration configuration = mock(Http2FrameWriter.Configuration.class);
|
|
|
|
Http2FrameSizePolicy frameSizePolicy = mock(Http2FrameSizePolicy.class);
|
|
|
|
when(writer.configuration()).thenReturn(configuration);
|
|
|
|
when(configuration.frameSizePolicy()).thenReturn(frameSizePolicy);
|
|
|
|
when(frameSizePolicy.maxFrameSize()).thenReturn(DEFAULT_MAX_FRAME_SIZE);
|
2015-09-17 00:34:44 +02:00
|
|
|
when(writer.writeData(any(ChannelHandlerContext.class), anyInt(), any(ByteBuf.class), anyInt(), anyBoolean(),
|
|
|
|
any(ChannelPromise.class))).thenAnswer(successAnswer());
|
2015-11-19 05:46:11 +01:00
|
|
|
when(writer.writeRstStream(eq(ctx), anyInt(), anyLong(), any(ChannelPromise.class))).thenAnswer(
|
2015-06-03 05:15:59 +02:00
|
|
|
successAnswer());
|
2015-09-17 00:34:44 +02:00
|
|
|
when(writer.writeGoAway(any(ChannelHandlerContext.class), anyInt(), anyLong(), any(ByteBuf.class),
|
2015-06-03 05:15:59 +02:00
|
|
|
any(ChannelPromise.class)))
|
|
|
|
.thenAnswer(successAnswer());
|
|
|
|
|
|
|
|
connection = new DefaultHttp2Connection(false);
|
2015-10-30 17:14:52 +01:00
|
|
|
connection.remote().flowController(new DefaultHttp2RemoteFlowController(connection));
|
|
|
|
connection.local().flowController(new DefaultHttp2LocalFlowController(connection).frameWriter(writer));
|
2015-06-03 05:15:59 +02:00
|
|
|
|
|
|
|
DefaultHttp2ConnectionEncoder defaultEncoder =
|
|
|
|
new DefaultHttp2ConnectionEncoder(connection, writer);
|
|
|
|
encoder = new StreamBufferingEncoder(defaultEncoder);
|
|
|
|
DefaultHttp2ConnectionDecoder decoder =
|
2015-10-01 00:22:57 +02:00
|
|
|
new DefaultHttp2ConnectionDecoder(connection, encoder, mock(Http2FrameReader.class));
|
2015-12-16 06:10:28 +01:00
|
|
|
Http2ConnectionHandler handler = new Http2ConnectionHandlerBuilder()
|
|
|
|
.frameListener(mock(Http2FrameListener.class))
|
|
|
|
.codec(decoder, encoder).build();
|
2015-06-03 05:15:59 +02:00
|
|
|
|
|
|
|
// Set LifeCycleManager on encoder and decoder
|
|
|
|
when(ctx.channel()).thenReturn(channel);
|
|
|
|
when(ctx.alloc()).thenReturn(UnpooledByteBufAllocator.DEFAULT);
|
2015-06-30 21:31:08 +02:00
|
|
|
when(channel.alloc()).thenReturn(UnpooledByteBufAllocator.DEFAULT);
|
2015-06-02 02:24:49 +02:00
|
|
|
when(executor.inEventLoop()).thenReturn(true);
|
2015-11-19 05:46:11 +01:00
|
|
|
doAnswer(new Answer<ChannelPromise>() {
|
|
|
|
@Override
|
|
|
|
public ChannelPromise answer(InvocationOnMock invocation) throws Throwable {
|
|
|
|
return newPromise();
|
|
|
|
}
|
|
|
|
}).when(ctx).newPromise();
|
2015-06-02 02:24:49 +02:00
|
|
|
when(ctx.executor()).thenReturn(executor);
|
2015-06-03 05:15:59 +02:00
|
|
|
when(channel.isActive()).thenReturn(false);
|
2015-06-30 19:10:17 +02:00
|
|
|
when(channel.config()).thenReturn(config);
|
|
|
|
when(channel.isWritable()).thenReturn(true);
|
|
|
|
when(channel.bytesBeforeUnwritable()).thenReturn(Long.MAX_VALUE);
|
|
|
|
when(config.getWriteBufferHighWaterMark()).thenReturn(Integer.MAX_VALUE);
|
2015-06-03 05:15:59 +02:00
|
|
|
handler.handlerAdded(ctx);
|
|
|
|
}
|
|
|
|
|
2015-06-23 00:38:53 +02:00
|
|
|
@After
|
|
|
|
public void teardown() {
|
|
|
|
// Close and release any buffered frames.
|
|
|
|
encoder.close();
|
|
|
|
}
|
|
|
|
|
2015-06-03 05:15:59 +02:00
|
|
|
@Test
|
|
|
|
public void multipleWritesToActiveStream() {
|
2015-11-19 05:46:11 +01:00
|
|
|
encoder.writeSettingsAck(ctx, newPromise());
|
|
|
|
encoderWriteHeaders(3, newPromise());
|
2015-06-03 05:15:59 +02:00
|
|
|
assertEquals(0, encoder.numBufferedStreams());
|
2015-09-17 00:34:44 +02:00
|
|
|
ByteBuf data = data();
|
|
|
|
final int expectedBytes = data.readableBytes() * 3;
|
2015-11-19 05:46:11 +01:00
|
|
|
encoder.writeData(ctx, 3, data, 0, false, newPromise());
|
|
|
|
encoder.writeData(ctx, 3, data(), 0, false, newPromise());
|
|
|
|
encoder.writeData(ctx, 3, data(), 0, false, newPromise());
|
|
|
|
encoderWriteHeaders(3, newPromise());
|
2015-06-03 05:15:59 +02:00
|
|
|
|
2015-11-19 05:46:11 +01:00
|
|
|
writeVerifyWriteHeaders(times(2), 3);
|
2015-06-04 20:55:18 +02:00
|
|
|
// Contiguous data writes are coalesced
|
|
|
|
ArgumentCaptor<ByteBuf> bufCaptor = ArgumentCaptor.forClass(ByteBuf.class);
|
|
|
|
verify(writer, times(1))
|
2015-11-19 05:46:11 +01:00
|
|
|
.writeData(eq(ctx), eq(3), bufCaptor.capture(), eq(0), eq(false), any(ChannelPromise.class));
|
2015-09-17 00:34:44 +02:00
|
|
|
assertEquals(expectedBytes, bufCaptor.getValue().readableBytes());
|
2015-06-03 05:15:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void ensureCanCreateNextStreamWhenStreamCloses() {
|
2015-11-19 05:46:11 +01:00
|
|
|
encoder.writeSettingsAck(ctx, newPromise());
|
2015-06-03 05:15:59 +02:00
|
|
|
setMaxConcurrentStreams(1);
|
|
|
|
|
2015-11-19 05:46:11 +01:00
|
|
|
encoderWriteHeaders(3, newPromise());
|
2015-06-03 05:15:59 +02:00
|
|
|
assertEquals(0, encoder.numBufferedStreams());
|
|
|
|
|
|
|
|
// This one gets buffered.
|
2015-11-19 05:46:11 +01:00
|
|
|
encoderWriteHeaders(5, newPromise());
|
2015-06-03 05:15:59 +02:00
|
|
|
assertEquals(1, connection.numActiveStreams());
|
|
|
|
assertEquals(1, encoder.numBufferedStreams());
|
|
|
|
|
|
|
|
// Now prevent us from creating another stream.
|
|
|
|
setMaxConcurrentStreams(0);
|
|
|
|
|
|
|
|
// Close the previous stream.
|
|
|
|
connection.stream(3).close();
|
|
|
|
|
|
|
|
// Ensure that no streams are currently active and that only the HEADERS from the first
|
|
|
|
// stream were written.
|
2015-11-19 05:46:11 +01:00
|
|
|
writeVerifyWriteHeaders(times(1), 3);
|
|
|
|
writeVerifyWriteHeaders(never(), 5);
|
2015-06-03 05:15:59 +02:00
|
|
|
assertEquals(0, connection.numActiveStreams());
|
|
|
|
assertEquals(1, encoder.numBufferedStreams());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void alternatingWritesToActiveAndBufferedStreams() {
|
2015-11-19 05:46:11 +01:00
|
|
|
encoder.writeSettingsAck(ctx, newPromise());
|
2015-06-03 05:15:59 +02:00
|
|
|
setMaxConcurrentStreams(1);
|
|
|
|
|
2015-11-19 05:46:11 +01:00
|
|
|
encoderWriteHeaders(3, newPromise());
|
2015-06-03 05:15:59 +02:00
|
|
|
assertEquals(0, encoder.numBufferedStreams());
|
|
|
|
|
2015-11-19 05:46:11 +01:00
|
|
|
encoderWriteHeaders(5, newPromise());
|
2015-06-03 05:15:59 +02:00
|
|
|
assertEquals(1, connection.numActiveStreams());
|
|
|
|
assertEquals(1, encoder.numBufferedStreams());
|
|
|
|
|
2015-11-19 05:46:11 +01:00
|
|
|
encoder.writeData(ctx, 3, EMPTY_BUFFER, 0, false, newPromise());
|
|
|
|
writeVerifyWriteHeaders(times(1), 3);
|
|
|
|
encoder.writeData(ctx, 5, EMPTY_BUFFER, 0, false, newPromise());
|
2015-06-03 05:15:59 +02:00
|
|
|
verify(writer, never())
|
2015-11-19 05:46:11 +01:00
|
|
|
.writeData(eq(ctx), eq(5), any(ByteBuf.class), eq(0), eq(false), eq(newPromise()));
|
2015-06-03 05:15:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void bufferingNewStreamFailsAfterGoAwayReceived() {
|
2015-11-19 05:46:11 +01:00
|
|
|
encoder.writeSettingsAck(ctx, newPromise());
|
2015-06-03 05:15:59 +02:00
|
|
|
setMaxConcurrentStreams(0);
|
2015-07-20 21:44:52 +02:00
|
|
|
connection.goAwayReceived(1, 8, EMPTY_BUFFER);
|
2015-06-03 05:15:59 +02:00
|
|
|
|
2015-11-19 05:46:11 +01:00
|
|
|
ChannelPromise promise = newPromise();
|
2015-06-03 05:15:59 +02:00
|
|
|
encoderWriteHeaders(3, promise);
|
|
|
|
assertEquals(0, encoder.numBufferedStreams());
|
2015-11-19 05:46:11 +01:00
|
|
|
assertTrue(promise.isDone());
|
|
|
|
assertFalse(promise.isSuccess());
|
2015-06-03 05:15:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void receivingGoAwayFailsBufferedStreams() {
|
2015-11-19 05:46:11 +01:00
|
|
|
encoder.writeSettingsAck(ctx, newPromise());
|
2015-06-03 05:15:59 +02:00
|
|
|
setMaxConcurrentStreams(5);
|
|
|
|
|
|
|
|
int streamId = 3;
|
2015-11-19 05:46:11 +01:00
|
|
|
List<ChannelFuture> futures = new ArrayList<ChannelFuture>();
|
2015-06-03 05:15:59 +02:00
|
|
|
for (int i = 0; i < 9; i++) {
|
2015-11-19 05:46:11 +01:00
|
|
|
futures.add(encoderWriteHeaders(streamId, newPromise()));
|
2015-06-03 05:15:59 +02:00
|
|
|
streamId += 2;
|
|
|
|
}
|
|
|
|
assertEquals(4, encoder.numBufferedStreams());
|
|
|
|
|
2015-07-08 20:38:22 +02:00
|
|
|
connection.goAwayReceived(11, 8, EMPTY_BUFFER);
|
2015-06-03 05:15:59 +02:00
|
|
|
|
|
|
|
assertEquals(5, connection.numActiveStreams());
|
2015-11-19 05:46:11 +01:00
|
|
|
int failCount = 0;
|
|
|
|
for (ChannelFuture f : futures) {
|
|
|
|
if (f.cause() != null) {
|
|
|
|
failCount++;
|
|
|
|
}
|
|
|
|
}
|
2017-01-26 18:03:42 +01:00
|
|
|
assertEquals(9, failCount);
|
2015-06-03 05:15:59 +02:00
|
|
|
assertEquals(0, encoder.numBufferedStreams());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void sendingGoAwayShouldNotFailStreams() {
|
2015-11-19 05:46:11 +01:00
|
|
|
encoder.writeSettingsAck(ctx, newPromise());
|
2015-06-03 05:15:59 +02:00
|
|
|
setMaxConcurrentStreams(1);
|
|
|
|
|
2017-01-26 18:03:42 +01:00
|
|
|
when(writer.writeHeaders(any(ChannelHandlerContext.class), anyInt(), any(Http2Headers.class), anyInt(),
|
|
|
|
anyBoolean(), any(ChannelPromise.class))).thenAnswer(successAnswer());
|
|
|
|
when(writer.writeHeaders(any(ChannelHandlerContext.class), anyInt(), any(Http2Headers.class), anyInt(),
|
|
|
|
anyShort(), anyBoolean(), anyInt(), anyBoolean(), any(ChannelPromise.class))).thenAnswer(successAnswer());
|
|
|
|
|
2015-11-19 05:46:11 +01:00
|
|
|
ChannelFuture f1 = encoderWriteHeaders(3, newPromise());
|
2015-06-03 05:15:59 +02:00
|
|
|
assertEquals(0, encoder.numBufferedStreams());
|
2015-11-19 05:46:11 +01:00
|
|
|
ChannelFuture f2 = encoderWriteHeaders(5, newPromise());
|
2015-06-03 05:15:59 +02:00
|
|
|
assertEquals(1, encoder.numBufferedStreams());
|
2015-11-19 05:46:11 +01:00
|
|
|
ChannelFuture f3 = encoderWriteHeaders(7, newPromise());
|
2015-06-03 05:15:59 +02:00
|
|
|
assertEquals(2, encoder.numBufferedStreams());
|
|
|
|
|
|
|
|
ByteBuf empty = Unpooled.buffer(0);
|
2015-11-19 05:46:11 +01:00
|
|
|
encoder.writeGoAway(ctx, 3, CANCEL.code(), empty, newPromise());
|
2015-06-03 05:15:59 +02:00
|
|
|
|
|
|
|
assertEquals(1, connection.numActiveStreams());
|
|
|
|
assertEquals(2, encoder.numBufferedStreams());
|
2015-11-19 05:46:11 +01:00
|
|
|
assertFalse(f1.isDone());
|
|
|
|
assertFalse(f2.isDone());
|
|
|
|
assertFalse(f3.isDone());
|
2015-06-03 05:15:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void endStreamDoesNotFailBufferedStream() {
|
2015-11-19 05:46:11 +01:00
|
|
|
encoder.writeSettingsAck(ctx, newPromise());
|
2015-06-03 05:15:59 +02:00
|
|
|
setMaxConcurrentStreams(0);
|
|
|
|
|
2015-11-19 05:46:11 +01:00
|
|
|
encoderWriteHeaders(3, newPromise());
|
2015-06-03 05:15:59 +02:00
|
|
|
assertEquals(1, encoder.numBufferedStreams());
|
|
|
|
|
2015-11-19 05:46:11 +01:00
|
|
|
encoder.writeData(ctx, 3, EMPTY_BUFFER, 0, true, newPromise());
|
2015-06-03 05:15:59 +02:00
|
|
|
|
|
|
|
assertEquals(0, connection.numActiveStreams());
|
|
|
|
assertEquals(1, encoder.numBufferedStreams());
|
|
|
|
|
|
|
|
// Simulate that we received a SETTINGS frame which
|
|
|
|
// increased MAX_CONCURRENT_STREAMS to 1.
|
|
|
|
setMaxConcurrentStreams(1);
|
2015-11-19 05:46:11 +01:00
|
|
|
encoder.writeSettingsAck(ctx, newPromise());
|
2015-06-03 05:15:59 +02:00
|
|
|
|
|
|
|
assertEquals(1, connection.numActiveStreams());
|
|
|
|
assertEquals(0, encoder.numBufferedStreams());
|
|
|
|
assertEquals(HALF_CLOSED_LOCAL, connection.stream(3).state());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void rstStreamClosesBufferedStream() {
|
2015-11-19 05:46:11 +01:00
|
|
|
encoder.writeSettingsAck(ctx, newPromise());
|
2015-06-03 05:15:59 +02:00
|
|
|
setMaxConcurrentStreams(0);
|
|
|
|
|
2015-11-19 05:46:11 +01:00
|
|
|
encoderWriteHeaders(3, newPromise());
|
2015-06-03 05:15:59 +02:00
|
|
|
assertEquals(1, encoder.numBufferedStreams());
|
|
|
|
|
2015-11-19 05:46:11 +01:00
|
|
|
ChannelPromise rstStreamPromise = newPromise();
|
2015-06-03 05:15:59 +02:00
|
|
|
encoder.writeRstStream(ctx, 3, CANCEL.code(), rstStreamPromise);
|
2015-11-19 05:46:11 +01:00
|
|
|
assertTrue(rstStreamPromise.isSuccess());
|
2015-06-03 05:15:59 +02:00
|
|
|
assertEquals(0, encoder.numBufferedStreams());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2015-11-19 05:46:11 +01:00
|
|
|
public void bufferUntilActiveStreamsAreReset() throws Exception {
|
|
|
|
encoder.writeSettingsAck(ctx, newPromise());
|
2015-06-03 05:15:59 +02:00
|
|
|
setMaxConcurrentStreams(1);
|
|
|
|
|
2015-11-19 05:46:11 +01:00
|
|
|
encoderWriteHeaders(3, newPromise());
|
2015-06-03 05:15:59 +02:00
|
|
|
assertEquals(0, encoder.numBufferedStreams());
|
2015-11-19 05:46:11 +01:00
|
|
|
encoderWriteHeaders(5, newPromise());
|
2015-06-03 05:15:59 +02:00
|
|
|
assertEquals(1, encoder.numBufferedStreams());
|
2015-11-19 05:46:11 +01:00
|
|
|
encoderWriteHeaders(7, newPromise());
|
2015-06-03 05:15:59 +02:00
|
|
|
assertEquals(2, encoder.numBufferedStreams());
|
|
|
|
|
2015-11-19 05:46:11 +01:00
|
|
|
writeVerifyWriteHeaders(times(1), 3);
|
|
|
|
writeVerifyWriteHeaders(never(), 5);
|
|
|
|
writeVerifyWriteHeaders(never(), 7);
|
2015-06-03 05:15:59 +02:00
|
|
|
|
2015-11-19 05:46:11 +01:00
|
|
|
encoder.writeRstStream(ctx, 3, CANCEL.code(), newPromise());
|
|
|
|
connection.remote().flowController().writePendingBytes();
|
|
|
|
writeVerifyWriteHeaders(times(1), 5);
|
|
|
|
writeVerifyWriteHeaders(never(), 7);
|
2015-06-03 05:15:59 +02:00
|
|
|
assertEquals(1, connection.numActiveStreams());
|
|
|
|
assertEquals(1, encoder.numBufferedStreams());
|
2015-11-19 05:46:11 +01:00
|
|
|
|
|
|
|
encoder.writeRstStream(ctx, 5, CANCEL.code(), newPromise());
|
|
|
|
connection.remote().flowController().writePendingBytes();
|
|
|
|
writeVerifyWriteHeaders(times(1), 7);
|
2015-06-03 05:15:59 +02:00
|
|
|
assertEquals(1, connection.numActiveStreams());
|
|
|
|
assertEquals(0, encoder.numBufferedStreams());
|
2015-11-19 05:46:11 +01:00
|
|
|
|
|
|
|
encoder.writeRstStream(ctx, 7, CANCEL.code(), newPromise());
|
2015-06-03 05:15:59 +02:00
|
|
|
assertEquals(0, connection.numActiveStreams());
|
|
|
|
assertEquals(0, encoder.numBufferedStreams());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void bufferUntilMaxStreamsIncreased() {
|
2015-11-19 05:46:11 +01:00
|
|
|
encoder.writeSettingsAck(ctx, newPromise());
|
2015-06-03 05:15:59 +02:00
|
|
|
setMaxConcurrentStreams(2);
|
|
|
|
|
2015-11-19 05:46:11 +01:00
|
|
|
encoderWriteHeaders(3, newPromise());
|
|
|
|
encoderWriteHeaders(5, newPromise());
|
|
|
|
encoderWriteHeaders(7, newPromise());
|
|
|
|
encoderWriteHeaders(9, newPromise());
|
2015-06-03 05:15:59 +02:00
|
|
|
assertEquals(2, encoder.numBufferedStreams());
|
|
|
|
|
2015-11-19 05:46:11 +01:00
|
|
|
writeVerifyWriteHeaders(times(1), 3);
|
|
|
|
writeVerifyWriteHeaders(times(1), 5);
|
|
|
|
writeVerifyWriteHeaders(never(), 7);
|
|
|
|
writeVerifyWriteHeaders(never(), 9);
|
2015-06-03 05:15:59 +02:00
|
|
|
|
|
|
|
// Simulate that we received a SETTINGS frame which
|
|
|
|
// increased MAX_CONCURRENT_STREAMS to 5.
|
|
|
|
setMaxConcurrentStreams(5);
|
2015-11-19 05:46:11 +01:00
|
|
|
encoder.writeSettingsAck(ctx, newPromise());
|
2015-06-03 05:15:59 +02:00
|
|
|
|
|
|
|
assertEquals(0, encoder.numBufferedStreams());
|
2015-11-19 05:46:11 +01:00
|
|
|
writeVerifyWriteHeaders(times(1), 7);
|
|
|
|
writeVerifyWriteHeaders(times(1), 9);
|
2015-06-03 05:15:59 +02:00
|
|
|
|
2015-11-19 05:46:11 +01:00
|
|
|
encoderWriteHeaders(11, newPromise());
|
2015-06-03 05:15:59 +02:00
|
|
|
|
2015-11-19 05:46:11 +01:00
|
|
|
writeVerifyWriteHeaders(times(1), 11);
|
2015-06-03 05:15:59 +02:00
|
|
|
|
|
|
|
assertEquals(5, connection.local().numActiveStreams());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void bufferUntilSettingsReceived() throws Http2Exception {
|
|
|
|
int initialLimit = SMALLEST_MAX_CONCURRENT_STREAMS;
|
|
|
|
int numStreams = initialLimit * 2;
|
|
|
|
for (int ix = 0, nextStreamId = 3; ix < numStreams; ++ix, nextStreamId += 2) {
|
2015-11-19 05:46:11 +01:00
|
|
|
encoderWriteHeaders(nextStreamId, newPromise());
|
2015-06-03 05:15:59 +02:00
|
|
|
if (ix < initialLimit) {
|
2015-11-19 05:46:11 +01:00
|
|
|
writeVerifyWriteHeaders(times(1), nextStreamId);
|
2015-06-03 05:15:59 +02:00
|
|
|
} else {
|
2015-11-19 05:46:11 +01:00
|
|
|
writeVerifyWriteHeaders(never(), nextStreamId);
|
2015-06-03 05:15:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
assertEquals(numStreams / 2, encoder.numBufferedStreams());
|
|
|
|
|
|
|
|
// Simulate that we received a SETTINGS frame.
|
|
|
|
setMaxConcurrentStreams(initialLimit * 2);
|
|
|
|
|
|
|
|
assertEquals(0, encoder.numBufferedStreams());
|
|
|
|
assertEquals(numStreams, connection.local().numActiveStreams());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void bufferUntilSettingsReceivedWithNoMaxConcurrentStreamValue() throws Http2Exception {
|
|
|
|
int initialLimit = SMALLEST_MAX_CONCURRENT_STREAMS;
|
|
|
|
int numStreams = initialLimit * 2;
|
|
|
|
for (int ix = 0, nextStreamId = 3; ix < numStreams; ++ix, nextStreamId += 2) {
|
2015-11-19 05:46:11 +01:00
|
|
|
encoderWriteHeaders(nextStreamId, newPromise());
|
2015-06-03 05:15:59 +02:00
|
|
|
if (ix < initialLimit) {
|
2015-11-19 05:46:11 +01:00
|
|
|
writeVerifyWriteHeaders(times(1), nextStreamId);
|
2015-06-03 05:15:59 +02:00
|
|
|
} else {
|
2015-11-19 05:46:11 +01:00
|
|
|
writeVerifyWriteHeaders(never(), nextStreamId);
|
2015-06-03 05:15:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
assertEquals(numStreams / 2, encoder.numBufferedStreams());
|
|
|
|
|
|
|
|
// Simulate that we received an empty SETTINGS frame.
|
|
|
|
encoder.remoteSettings(new Http2Settings());
|
|
|
|
|
|
|
|
assertEquals(0, encoder.numBufferedStreams());
|
|
|
|
assertEquals(numStreams, connection.local().numActiveStreams());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void exhaustedStreamsDoNotBuffer() throws Http2Exception {
|
|
|
|
// Write the highest possible stream ID for the client.
|
|
|
|
// This will cause the next stream ID to be negative.
|
2015-11-19 05:46:11 +01:00
|
|
|
encoderWriteHeaders(Integer.MAX_VALUE, newPromise());
|
2015-06-03 05:15:59 +02:00
|
|
|
|
|
|
|
// Disallow any further streams.
|
|
|
|
setMaxConcurrentStreams(0);
|
|
|
|
|
|
|
|
// Simulate numeric overflow for the next stream ID.
|
2015-11-19 05:46:11 +01:00
|
|
|
ChannelFuture f = encoderWriteHeaders(-1, newPromise());
|
2015-06-03 05:15:59 +02:00
|
|
|
|
|
|
|
// Verify that the write fails.
|
2015-11-19 05:46:11 +01:00
|
|
|
assertNotNull(f.cause());
|
2015-06-03 05:15:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void closedBufferedStreamReleasesByteBuf() {
|
2015-11-19 05:46:11 +01:00
|
|
|
encoder.writeSettingsAck(ctx, newPromise());
|
2015-06-03 05:15:59 +02:00
|
|
|
setMaxConcurrentStreams(0);
|
|
|
|
ByteBuf data = mock(ByteBuf.class);
|
2015-11-19 05:46:11 +01:00
|
|
|
ChannelFuture f1 = encoderWriteHeaders(3, newPromise());
|
2015-06-03 05:15:59 +02:00
|
|
|
assertEquals(1, encoder.numBufferedStreams());
|
2015-11-19 05:46:11 +01:00
|
|
|
ChannelFuture f2 = encoder.writeData(ctx, 3, data, 0, false, newPromise());
|
2015-06-03 05:15:59 +02:00
|
|
|
|
|
|
|
ChannelPromise rstPromise = mock(ChannelPromise.class);
|
|
|
|
encoder.writeRstStream(ctx, 3, CANCEL.code(), rstPromise);
|
|
|
|
|
|
|
|
assertEquals(0, encoder.numBufferedStreams());
|
|
|
|
verify(rstPromise).setSuccess();
|
2015-11-19 05:46:11 +01:00
|
|
|
assertTrue(f1.isSuccess());
|
|
|
|
assertTrue(f2.isSuccess());
|
2015-06-03 05:15:59 +02:00
|
|
|
verify(data).release();
|
|
|
|
}
|
|
|
|
|
2015-07-08 20:38:22 +02:00
|
|
|
@Test
|
2016-08-09 22:10:02 +02:00
|
|
|
public void closeShouldCancelAllBufferedStreams() throws Http2Exception {
|
2015-11-19 05:46:11 +01:00
|
|
|
encoder.writeSettingsAck(ctx, newPromise());
|
2017-01-24 21:50:39 +01:00
|
|
|
connection.local().maxActiveStreams(0);
|
2015-07-08 20:38:22 +02:00
|
|
|
|
2015-11-19 05:46:11 +01:00
|
|
|
ChannelFuture f1 = encoderWriteHeaders(3, newPromise());
|
|
|
|
ChannelFuture f2 = encoderWriteHeaders(5, newPromise());
|
|
|
|
ChannelFuture f3 = encoderWriteHeaders(7, newPromise());
|
2015-07-08 20:38:22 +02:00
|
|
|
|
|
|
|
encoder.close();
|
2015-11-19 05:46:11 +01:00
|
|
|
assertNotNull(f1.cause());
|
|
|
|
assertNotNull(f2.cause());
|
|
|
|
assertNotNull(f3.cause());
|
2015-07-08 20:38:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void headersAfterCloseShouldImmediatelyFail() {
|
2015-11-19 05:46:11 +01:00
|
|
|
encoder.writeSettingsAck(ctx, newPromise());
|
2015-07-08 20:38:22 +02:00
|
|
|
encoder.close();
|
|
|
|
|
2015-11-19 05:46:11 +01:00
|
|
|
ChannelFuture f = encoderWriteHeaders(3, newPromise());
|
|
|
|
assertNotNull(f.cause());
|
2015-07-08 20:38:22 +02:00
|
|
|
}
|
|
|
|
|
2015-06-03 05:15:59 +02:00
|
|
|
private void setMaxConcurrentStreams(int newValue) {
|
|
|
|
try {
|
|
|
|
encoder.remoteSettings(new Http2Settings().maxConcurrentStreams(newValue));
|
2015-06-04 20:55:18 +02:00
|
|
|
// Flush the remote flow controller to write data
|
|
|
|
encoder.flowController().writePendingBytes();
|
2015-06-03 05:15:59 +02:00
|
|
|
} catch (Http2Exception e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-19 05:46:11 +01:00
|
|
|
private ChannelFuture encoderWriteHeaders(int streamId, ChannelPromise promise) {
|
2017-01-26 18:03:42 +01:00
|
|
|
encoder.writeHeaders(ctx, streamId, new DefaultHttp2Headers(), 0, DEFAULT_PRIORITY_WEIGHT,
|
|
|
|
false, 0, false, promise);
|
2015-06-04 20:55:18 +02:00
|
|
|
try {
|
|
|
|
encoder.flowController().writePendingBytes();
|
2015-11-19 05:46:11 +01:00
|
|
|
return promise;
|
2015-06-04 20:55:18 +02:00
|
|
|
} catch (Http2Exception e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
2015-06-03 05:15:59 +02:00
|
|
|
}
|
|
|
|
|
2015-11-19 05:46:11 +01:00
|
|
|
private void writeVerifyWriteHeaders(VerificationMode mode, int streamId) {
|
2017-01-26 18:03:42 +01:00
|
|
|
verify(writer, mode).writeHeaders(eq(ctx), eq(streamId), any(Http2Headers.class), eq(0),
|
|
|
|
eq(DEFAULT_PRIORITY_WEIGHT), eq(false), eq(0),
|
|
|
|
eq(false), any(ChannelPromise.class));
|
2015-06-03 05:15:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private Answer<ChannelFuture> successAnswer() {
|
|
|
|
return new Answer<ChannelFuture>() {
|
|
|
|
@Override
|
|
|
|
public ChannelFuture answer(InvocationOnMock invocation) throws Throwable {
|
2015-06-23 00:38:53 +02:00
|
|
|
for (Object a : invocation.getArguments()) {
|
|
|
|
ReferenceCountUtil.safeRelease(a);
|
2015-06-09 07:24:32 +02:00
|
|
|
}
|
|
|
|
|
2015-11-19 05:46:11 +01:00
|
|
|
ChannelPromise future = newPromise();
|
2015-06-03 05:15:59 +02:00
|
|
|
future.setSuccess();
|
|
|
|
return future;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-11-19 05:46:11 +01:00
|
|
|
private ChannelPromise newPromise() {
|
|
|
|
return new DefaultChannelPromise(channel, ImmediateEventExecutor.INSTANCE);
|
|
|
|
}
|
|
|
|
|
2015-06-03 05:15:59 +02:00
|
|
|
private static ByteBuf data() {
|
|
|
|
ByteBuf buf = Unpooled.buffer(10);
|
|
|
|
for (int i = 0; i < buf.writableBytes(); i++) {
|
|
|
|
buf.writeByte(i);
|
|
|
|
}
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
}
|