use of io.servicetalk.client.api.ConsumableEvent in project servicetalk by apple.
the class H2PriorKnowledgeFeatureParityTest method clientRespectsSettingsFrame.
@ParameterizedTest(name = "{displayName} [{index}] client={0}, h2PriorKnowledge={1}")
@MethodSource("clientExecutors")
void clientRespectsSettingsFrame(HttpTestExecutionStrategy strategy, boolean h2PriorKnowledge) throws Exception {
setUp(strategy, h2PriorKnowledge);
assumeTrue(h2PriorKnowledge, "Only HTTP/2 supports SETTINGS frames");
int expectedMaxConcurrent = 1;
BlockingQueue<FilterableStreamingHttpConnection> connectionQueue = new LinkedBlockingQueue<>();
BlockingQueue<Publisher<? extends ConsumableEvent<Integer>>> maxConcurrentPubQueue = new LinkedBlockingQueue<>();
AtomicReference<Channel> serverParentChannelRef = new AtomicReference<>();
CountDownLatch serverChannelLatch = new CountDownLatch(1);
CountDownLatch serverSettingsAckLatch = new CountDownLatch(2);
serverAcceptorChannel = bindH2Server(serverEventLoopGroup, new ChannelInitializer<Channel>() {
@Override
protected void initChannel(final Channel ch) {
ch.pipeline().addLast(new EchoHttp2Handler());
}
}, parentPipeline -> parentPipeline.addLast(new ChannelInboundHandlerAdapter() {
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
if (serverParentChannelRef.compareAndSet(null, ctx.channel())) {
serverChannelLatch.countDown();
}
super.channelActive(ctx);
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if (msg instanceof Http2SettingsAckFrame) {
serverSettingsAckLatch.countDown();
}
super.channelRead(ctx, msg);
}
}), identity());
InetSocketAddress serverAddress = (InetSocketAddress) serverAcceptorChannel.localAddress();
try (StreamingHttpClient client = forSingleAddress(HostAndPort.of(serverAddress)).protocols(h2PriorKnowledge ? h2Default() : h1Default()).executionStrategy(clientExecutionStrategy).appendConnectionFilter(conn -> new TestConnectionFilter(conn, connectionQueue, maxConcurrentPubQueue)).buildStreaming()) {
Processor<Buffer, Buffer> requestPayload = newProcessor();
client.request(client.post("/0").payloadBody(fromSource(requestPayload))).toFuture().get();
serverChannelLatch.await();
Channel serverParentChannel = serverParentChannelRef.get();
serverParentChannel.writeAndFlush(new DefaultHttp2SettingsFrame(new Http2Settings().maxConcurrentStreams(expectedMaxConcurrent))).sync();
Iterator<? extends ConsumableEvent<Integer>> maxItr = maxConcurrentPubQueue.take().toIterable().iterator();
// Verify that the initial maxConcurrency value is the default number
assertThat("No initial maxConcurrency value", maxItr.hasNext(), is(true));
ConsumableEvent<Integer> next = maxItr.next();
assertThat(next, is(notNullValue()));
assertThat("First event is not the default", next.event(), is(SMALLEST_MAX_CONCURRENT_STREAMS));
// We previously made a request, and intentionally didn't complete the request body. We want to verify
// that we have received the SETTINGS frame reducing the total number of streams to 1.
assertThat("No maxConcurrency value received", maxItr.hasNext(), is(true));
next = maxItr.next();
assertThat(next, is(notNullValue()));
assertThat("maxConcurrency did not change to the expected value", next.event(), is(expectedMaxConcurrent));
// Wait for a server to receive a settings ack
serverSettingsAckLatch.await();
// After this point we want to issue a new request and verify that client selects a new connection.
Processor<Buffer, Buffer> requestPayload2 = newProcessor();
client.request(client.post("/1").payloadBody(fromSource(requestPayload2))).toFuture().get();
// We expect 2 connections to be created.
assertNotSame(connectionQueue.take(), connectionQueue.take());
requestPayload.onComplete();
requestPayload2.onComplete();
}
}
Aggregations