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;
|
|
|
|
import static org.mockito.Matchers.any;
|
|
|
|
import static org.mockito.Matchers.anyBoolean;
|
|
|
|
import static org.mockito.Matchers.anyInt;
|
|
|
|
import static org.mockito.Matchers.anyLong;
|
|
|
|
import static org.mockito.Matchers.eq;
|
|
|
|
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-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-07-08 20:38:22 +02:00
|
|
|
import io.netty.handler.codec.http2.StreamBufferingEncoder.Http2ChannelClosedException;
|
|
|
|
import io.netty.handler.codec.http2.StreamBufferingEncoder.Http2GoAwayException;
|
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-30 19:10:17 +02:00
|
|
|
|
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;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
@Mock
|
|
|
|
private ChannelPromise promise;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-06-03 05:15:59 +02:00
|
|
|
when(writer.writeRstStream(eq(ctx), anyInt(), anyLong(), eq(promise))).thenAnswer(
|
|
|
|
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);
|
|
|
|
|
|
|
|
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-09-27 02:44:11 +02:00
|
|
|
Http2ConnectionHandler handler = new Http2ConnectionHandler.Builder()
|
|
|
|
.frameListener(mock(Http2FrameListener.class)).build(decoder, encoder);
|
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-06-23 00:38:53 +02:00
|
|
|
when(ctx.newPromise()).thenReturn(promise);
|
2015-06-02 02:24:49 +02:00
|
|
|
when(ctx.executor()).thenReturn(executor);
|
2015-06-30 19:10:17 +02:00
|
|
|
when(promise.channel()).thenReturn(channel);
|
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() {
|
|
|
|
encoder.writeSettingsAck(ctx, promise);
|
|
|
|
encoderWriteHeaders(3, promise);
|
|
|
|
assertEquals(0, encoder.numBufferedStreams());
|
2015-09-17 00:34:44 +02:00
|
|
|
ByteBuf data = data();
|
|
|
|
final int expectedBytes = data.readableBytes() * 3;
|
|
|
|
encoder.writeData(ctx, 3, data, 0, false, promise);
|
2015-06-03 05:15:59 +02:00
|
|
|
encoder.writeData(ctx, 3, data(), 0, false, promise);
|
|
|
|
encoder.writeData(ctx, 3, data(), 0, false, promise);
|
|
|
|
encoderWriteHeaders(3, promise);
|
|
|
|
|
|
|
|
writeVerifyWriteHeaders(times(2), 3, promise);
|
2015-06-04 20:55:18 +02:00
|
|
|
// Contiguous data writes are coalesced
|
|
|
|
ArgumentCaptor<ByteBuf> bufCaptor = ArgumentCaptor.forClass(ByteBuf.class);
|
|
|
|
verify(writer, times(1))
|
|
|
|
.writeData(eq(ctx), eq(3), bufCaptor.capture(), eq(0), eq(false), eq(promise));
|
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() {
|
|
|
|
encoder.writeSettingsAck(ctx, promise);
|
|
|
|
setMaxConcurrentStreams(1);
|
|
|
|
|
|
|
|
encoderWriteHeaders(3, promise);
|
|
|
|
assertEquals(0, encoder.numBufferedStreams());
|
|
|
|
|
|
|
|
// This one gets buffered.
|
|
|
|
encoderWriteHeaders(5, promise);
|
|
|
|
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.
|
|
|
|
writeVerifyWriteHeaders(times(1), 3, promise);
|
|
|
|
writeVerifyWriteHeaders(never(), 5, promise);
|
|
|
|
assertEquals(0, connection.numActiveStreams());
|
|
|
|
assertEquals(1, encoder.numBufferedStreams());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void alternatingWritesToActiveAndBufferedStreams() {
|
|
|
|
encoder.writeSettingsAck(ctx, promise);
|
|
|
|
setMaxConcurrentStreams(1);
|
|
|
|
|
|
|
|
encoderWriteHeaders(3, promise);
|
|
|
|
assertEquals(0, encoder.numBufferedStreams());
|
|
|
|
|
|
|
|
encoderWriteHeaders(5, promise);
|
|
|
|
assertEquals(1, connection.numActiveStreams());
|
|
|
|
assertEquals(1, encoder.numBufferedStreams());
|
|
|
|
|
2015-06-23 00:38:53 +02:00
|
|
|
encoder.writeData(ctx, 3, EMPTY_BUFFER, 0, false, promise);
|
2015-06-03 05:15:59 +02:00
|
|
|
writeVerifyWriteHeaders(times(1), 3, promise);
|
2015-06-23 00:38:53 +02:00
|
|
|
encoder.writeData(ctx, 5, EMPTY_BUFFER, 0, false, promise);
|
2015-06-03 05:15:59 +02:00
|
|
|
verify(writer, never())
|
|
|
|
.writeData(eq(ctx), eq(5), any(ByteBuf.class), eq(0), eq(false), eq(promise));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void bufferingNewStreamFailsAfterGoAwayReceived() {
|
|
|
|
encoder.writeSettingsAck(ctx, promise);
|
|
|
|
setMaxConcurrentStreams(0);
|
2015-07-20 21:44:52 +02:00
|
|
|
connection.goAwayReceived(1, 8, EMPTY_BUFFER);
|
2015-06-03 05:15:59 +02:00
|
|
|
|
|
|
|
promise = mock(ChannelPromise.class);
|
|
|
|
encoderWriteHeaders(3, promise);
|
|
|
|
assertEquals(0, encoder.numBufferedStreams());
|
|
|
|
verify(promise).setFailure(any(Throwable.class));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void receivingGoAwayFailsBufferedStreams() {
|
|
|
|
encoder.writeSettingsAck(ctx, promise);
|
|
|
|
setMaxConcurrentStreams(5);
|
|
|
|
|
|
|
|
int streamId = 3;
|
|
|
|
for (int i = 0; i < 9; i++) {
|
|
|
|
encoderWriteHeaders(streamId, promise);
|
|
|
|
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());
|
|
|
|
// The 4 buffered streams must have been failed.
|
|
|
|
verify(promise, times(4)).setFailure(any(Throwable.class));
|
|
|
|
assertEquals(0, encoder.numBufferedStreams());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void sendingGoAwayShouldNotFailStreams() {
|
|
|
|
encoder.writeSettingsAck(ctx, promise);
|
|
|
|
setMaxConcurrentStreams(1);
|
|
|
|
|
|
|
|
encoderWriteHeaders(3, promise);
|
|
|
|
assertEquals(0, encoder.numBufferedStreams());
|
|
|
|
encoderWriteHeaders(5, promise);
|
|
|
|
assertEquals(1, encoder.numBufferedStreams());
|
|
|
|
encoderWriteHeaders(7, promise);
|
|
|
|
assertEquals(2, encoder.numBufferedStreams());
|
|
|
|
|
|
|
|
ByteBuf empty = Unpooled.buffer(0);
|
|
|
|
encoder.writeGoAway(ctx, 3, CANCEL.code(), empty, promise);
|
|
|
|
|
|
|
|
assertEquals(1, connection.numActiveStreams());
|
|
|
|
assertEquals(2, encoder.numBufferedStreams());
|
2015-07-08 20:38:22 +02:00
|
|
|
verify(promise, never()).setFailure(any(Http2GoAwayException.class));
|
2015-06-03 05:15:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void endStreamDoesNotFailBufferedStream() {
|
|
|
|
encoder.writeSettingsAck(ctx, promise);
|
|
|
|
setMaxConcurrentStreams(0);
|
|
|
|
|
|
|
|
encoderWriteHeaders(3, promise);
|
|
|
|
assertEquals(1, encoder.numBufferedStreams());
|
|
|
|
|
2015-06-23 00:38:53 +02:00
|
|
|
encoder.writeData(ctx, 3, EMPTY_BUFFER, 0, true, promise);
|
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);
|
|
|
|
encoder.writeSettingsAck(ctx, promise);
|
|
|
|
|
|
|
|
assertEquals(1, connection.numActiveStreams());
|
|
|
|
assertEquals(0, encoder.numBufferedStreams());
|
|
|
|
assertEquals(HALF_CLOSED_LOCAL, connection.stream(3).state());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void rstStreamClosesBufferedStream() {
|
|
|
|
encoder.writeSettingsAck(ctx, promise);
|
|
|
|
setMaxConcurrentStreams(0);
|
|
|
|
|
|
|
|
encoderWriteHeaders(3, promise);
|
|
|
|
assertEquals(1, encoder.numBufferedStreams());
|
|
|
|
|
|
|
|
verify(promise, never()).setSuccess();
|
|
|
|
ChannelPromise rstStreamPromise = mock(ChannelPromise.class);
|
|
|
|
encoder.writeRstStream(ctx, 3, CANCEL.code(), rstStreamPromise);
|
|
|
|
verify(promise).setSuccess();
|
|
|
|
verify(rstStreamPromise).setSuccess();
|
|
|
|
assertEquals(0, encoder.numBufferedStreams());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void bufferUntilActiveStreamsAreReset() {
|
|
|
|
encoder.writeSettingsAck(ctx, promise);
|
|
|
|
setMaxConcurrentStreams(1);
|
|
|
|
|
|
|
|
encoderWriteHeaders(3, promise);
|
|
|
|
assertEquals(0, encoder.numBufferedStreams());
|
|
|
|
encoderWriteHeaders(5, promise);
|
|
|
|
assertEquals(1, encoder.numBufferedStreams());
|
|
|
|
encoderWriteHeaders(7, promise);
|
|
|
|
assertEquals(2, encoder.numBufferedStreams());
|
|
|
|
|
|
|
|
writeVerifyWriteHeaders(times(1), 3, promise);
|
|
|
|
writeVerifyWriteHeaders(never(), 5, promise);
|
|
|
|
writeVerifyWriteHeaders(never(), 7, promise);
|
|
|
|
|
|
|
|
encoder.writeRstStream(ctx, 3, CANCEL.code(), promise);
|
|
|
|
assertEquals(1, connection.numActiveStreams());
|
|
|
|
assertEquals(1, encoder.numBufferedStreams());
|
|
|
|
encoder.writeRstStream(ctx, 5, CANCEL.code(), promise);
|
|
|
|
assertEquals(1, connection.numActiveStreams());
|
|
|
|
assertEquals(0, encoder.numBufferedStreams());
|
|
|
|
encoder.writeRstStream(ctx, 7, CANCEL.code(), promise);
|
|
|
|
assertEquals(0, connection.numActiveStreams());
|
|
|
|
assertEquals(0, encoder.numBufferedStreams());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void bufferUntilMaxStreamsIncreased() {
|
|
|
|
encoder.writeSettingsAck(ctx, promise);
|
|
|
|
setMaxConcurrentStreams(2);
|
|
|
|
|
|
|
|
encoderWriteHeaders(3, promise);
|
|
|
|
encoderWriteHeaders(5, promise);
|
|
|
|
encoderWriteHeaders(7, promise);
|
|
|
|
encoderWriteHeaders(9, promise);
|
|
|
|
assertEquals(2, encoder.numBufferedStreams());
|
|
|
|
|
|
|
|
writeVerifyWriteHeaders(times(1), 3, promise);
|
|
|
|
writeVerifyWriteHeaders(times(1), 5, promise);
|
|
|
|
writeVerifyWriteHeaders(never(), 7, promise);
|
|
|
|
writeVerifyWriteHeaders(never(), 9, promise);
|
|
|
|
|
|
|
|
// Simulate that we received a SETTINGS frame which
|
|
|
|
// increased MAX_CONCURRENT_STREAMS to 5.
|
|
|
|
setMaxConcurrentStreams(5);
|
|
|
|
encoder.writeSettingsAck(ctx, promise);
|
|
|
|
|
|
|
|
assertEquals(0, encoder.numBufferedStreams());
|
|
|
|
writeVerifyWriteHeaders(times(1), 7, promise);
|
|
|
|
writeVerifyWriteHeaders(times(1), 9, promise);
|
|
|
|
|
|
|
|
encoderWriteHeaders(11, promise);
|
|
|
|
|
|
|
|
writeVerifyWriteHeaders(times(1), 11, promise);
|
|
|
|
|
|
|
|
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) {
|
|
|
|
encoderWriteHeaders(nextStreamId, promise);
|
|
|
|
if (ix < initialLimit) {
|
|
|
|
writeVerifyWriteHeaders(times(1), nextStreamId, promise);
|
|
|
|
} else {
|
|
|
|
writeVerifyWriteHeaders(never(), nextStreamId, promise);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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) {
|
|
|
|
encoderWriteHeaders(nextStreamId, promise);
|
|
|
|
if (ix < initialLimit) {
|
|
|
|
writeVerifyWriteHeaders(times(1), nextStreamId, promise);
|
|
|
|
} else {
|
|
|
|
writeVerifyWriteHeaders(never(), nextStreamId, promise);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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.
|
|
|
|
encoderWriteHeaders(Integer.MAX_VALUE, promise);
|
|
|
|
|
|
|
|
// Disallow any further streams.
|
|
|
|
setMaxConcurrentStreams(0);
|
|
|
|
|
|
|
|
// Simulate numeric overflow for the next stream ID.
|
|
|
|
encoderWriteHeaders(-1, promise);
|
|
|
|
|
|
|
|
// Verify that the write fails.
|
|
|
|
verify(promise).setFailure(any(Http2Exception.class));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void closedBufferedStreamReleasesByteBuf() {
|
|
|
|
encoder.writeSettingsAck(ctx, promise);
|
|
|
|
setMaxConcurrentStreams(0);
|
|
|
|
ByteBuf data = mock(ByteBuf.class);
|
|
|
|
encoderWriteHeaders(3, promise);
|
|
|
|
assertEquals(1, encoder.numBufferedStreams());
|
|
|
|
encoder.writeData(ctx, 3, data, 0, false, promise);
|
|
|
|
|
|
|
|
ChannelPromise rstPromise = mock(ChannelPromise.class);
|
|
|
|
encoder.writeRstStream(ctx, 3, CANCEL.code(), rstPromise);
|
|
|
|
|
|
|
|
assertEquals(0, encoder.numBufferedStreams());
|
|
|
|
verify(rstPromise).setSuccess();
|
|
|
|
verify(promise, times(2)).setSuccess();
|
|
|
|
verify(data).release();
|
|
|
|
}
|
|
|
|
|
2015-07-08 20:38:22 +02:00
|
|
|
@Test
|
|
|
|
public void closeShouldCancelAllBufferedStreams() {
|
|
|
|
encoder.writeSettingsAck(ctx, promise);
|
|
|
|
connection.local().maxActiveStreams(0);
|
|
|
|
|
|
|
|
encoderWriteHeaders(3, promise);
|
|
|
|
encoderWriteHeaders(5, promise);
|
|
|
|
encoderWriteHeaders(7, promise);
|
|
|
|
|
|
|
|
encoder.close();
|
|
|
|
verify(promise, times(3)).setFailure(any(Http2ChannelClosedException.class));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void headersAfterCloseShouldImmediatelyFail() {
|
|
|
|
encoder.writeSettingsAck(ctx, promise);
|
|
|
|
encoder.close();
|
|
|
|
|
|
|
|
encoderWriteHeaders(3, promise);
|
|
|
|
verify(promise).setFailure(any(Http2ChannelClosedException.class));
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void encoderWriteHeaders(int streamId, ChannelPromise promise) {
|
|
|
|
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();
|
|
|
|
} catch (Http2Exception e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
2015-06-03 05:15:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void writeVerifyWriteHeaders(VerificationMode mode, int streamId,
|
|
|
|
ChannelPromise promise) {
|
|
|
|
verify(writer, mode).writeHeaders(eq(ctx), eq(streamId), any(Http2Headers.class), eq(0),
|
|
|
|
eq(DEFAULT_PRIORITY_WEIGHT), eq(false), eq(0),
|
|
|
|
eq(false), eq(promise));
|
|
|
|
}
|
|
|
|
|
|
|
|
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-06-03 05:15:59 +02:00
|
|
|
ChannelPromise future =
|
|
|
|
new DefaultChannelPromise(channel, ImmediateEventExecutor.INSTANCE);
|
|
|
|
future.setSuccess();
|
|
|
|
return future;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
private static ByteBuf data() {
|
|
|
|
ByteBuf buf = Unpooled.buffer(10);
|
|
|
|
for (int i = 0; i < buf.writableBytes(); i++) {
|
|
|
|
buf.writeByte(i);
|
|
|
|
}
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
}
|