|
| 1 | +/* |
| 2 | + * ==================================================================== |
| 3 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 4 | + * or more contributor license agreements. See the NOTICE file |
| 5 | + * distributed with this work for additional information |
| 6 | + * regarding copyright ownership. The ASF licenses this file |
| 7 | + * to you under the Apache License, Version 2.0 (the |
| 8 | + * "License"); you may not use this file except in compliance |
| 9 | + * with the License. You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, |
| 14 | + * software distributed under the License is distributed on an |
| 15 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 | + * KIND, either express or implied. See the License for the |
| 17 | + * specific language governing permissions and limitations |
| 18 | + * under the License. |
| 19 | + * ==================================================================== |
| 20 | + * |
| 21 | + * This software consists of voluntary contributions made by many |
| 22 | + * individuals on behalf of the Apache Software Foundation. For more |
| 23 | + * information on the Apache Software Foundation, please see |
| 24 | + * <http://www.apache.org/>. |
| 25 | + * |
| 26 | + */ |
| 27 | +package org.apache.hc.core5.http2.impl.nio.bootstrap; |
| 28 | + |
| 29 | +import java.net.InetSocketAddress; |
| 30 | +import java.util.concurrent.CountDownLatch; |
| 31 | +import java.util.concurrent.ExecutorService; |
| 32 | +import java.util.concurrent.Executors; |
| 33 | +import java.util.concurrent.RejectedExecutionException; |
| 34 | +import java.util.concurrent.ScheduledExecutorService; |
| 35 | +import java.util.concurrent.TimeUnit; |
| 36 | +import java.util.concurrent.atomic.AtomicInteger; |
| 37 | + |
| 38 | +import org.apache.hc.core5.concurrent.FutureCallback; |
| 39 | +import org.apache.hc.core5.http.ContentType; |
| 40 | +import org.apache.hc.core5.http.EntityDetails; |
| 41 | +import org.apache.hc.core5.http.HttpHost; |
| 42 | +import org.apache.hc.core5.http.HttpRequest; |
| 43 | +import org.apache.hc.core5.http.HttpResponse; |
| 44 | +import org.apache.hc.core5.http.Message; |
| 45 | +import org.apache.hc.core5.http.URIScheme; |
| 46 | +import org.apache.hc.core5.http.impl.bootstrap.HttpAsyncServer; |
| 47 | +import org.apache.hc.core5.http.nio.AsyncRequestConsumer; |
| 48 | +import org.apache.hc.core5.http.nio.AsyncServerRequestHandler; |
| 49 | +import org.apache.hc.core5.http.nio.entity.DiscardingEntityConsumer; |
| 50 | +import org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer; |
| 51 | +import org.apache.hc.core5.http.nio.support.AsyncRequestBuilder; |
| 52 | +import org.apache.hc.core5.http.nio.support.AsyncResponseBuilder; |
| 53 | +import org.apache.hc.core5.http.nio.support.BasicRequestConsumer; |
| 54 | +import org.apache.hc.core5.http.nio.support.BasicResponseConsumer; |
| 55 | +import org.apache.hc.core5.http.protocol.HttpContext; |
| 56 | +import org.apache.hc.core5.http.protocol.HttpCoreContext; |
| 57 | +import org.apache.hc.core5.http2.HttpVersionPolicy; |
| 58 | +import org.apache.hc.core5.http2.config.H2Config; |
| 59 | +import org.apache.hc.core5.io.CloseMode; |
| 60 | +import org.apache.hc.core5.reactor.IOReactorConfig; |
| 61 | +import org.apache.hc.core5.reactor.ListenerEndpoint; |
| 62 | +import org.apache.hc.core5.util.Timeout; |
| 63 | +import org.junit.jupiter.api.Assertions; |
| 64 | +import org.junit.jupiter.api.Test; |
| 65 | + |
| 66 | +class TestH2MultiplexingRequesterMaxRequestsPerConnection { |
| 67 | + |
| 68 | + @Test |
| 69 | + @org.junit.jupiter.api.Timeout(value = 60, unit = TimeUnit.SECONDS) |
| 70 | + void testRejectsWhenLimitReached() throws Exception { |
| 71 | + final int maxPerConn = 2; |
| 72 | + final int totalRequests = 30; |
| 73 | + final Timeout timeout = Timeout.ofSeconds(30); |
| 74 | + final long serverDelayMillis = 5000; |
| 75 | + |
| 76 | + final IOReactorConfig ioReactorConfig = IOReactorConfig.custom() |
| 77 | + .setIoThreadCount(1) |
| 78 | + .build(); |
| 79 | + |
| 80 | + final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(); |
| 81 | + |
| 82 | + final H2Config serverH2Config = H2Config.custom() |
| 83 | + .setPushEnabled(false) |
| 84 | + .setMaxConcurrentStreams(1) // force backlog |
| 85 | + .build(); |
| 86 | + |
| 87 | + final HttpAsyncServer server = H2ServerBootstrap.bootstrap() |
| 88 | + .setIOReactorConfig(ioReactorConfig) |
| 89 | + .setH2Config(serverH2Config) |
| 90 | + .setVersionPolicy(HttpVersionPolicy.FORCE_HTTP_2) |
| 91 | + .setCanonicalHostName("127.0.0.1") // avoid 421 |
| 92 | + .register("*", new AsyncServerRequestHandler<Message<HttpRequest, Void>>() { |
| 93 | + |
| 94 | + @Override |
| 95 | + public AsyncRequestConsumer<Message<HttpRequest, Void>> prepare( |
| 96 | + final HttpRequest request, |
| 97 | + final EntityDetails entityDetails, |
| 98 | + final HttpContext context) { |
| 99 | + return new BasicRequestConsumer<>( |
| 100 | + entityDetails != null ? new DiscardingEntityConsumer<>() : null); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public void handle( |
| 105 | + final Message<HttpRequest, Void> message, |
| 106 | + final ResponseTrigger responseTrigger, |
| 107 | + final HttpContext localContext) { |
| 108 | + |
| 109 | + final HttpCoreContext context = HttpCoreContext.cast(localContext); |
| 110 | + final String path = message.getHead().getPath(); |
| 111 | + |
| 112 | + final Runnable task = () -> { |
| 113 | + try { |
| 114 | + responseTrigger.submitResponse( |
| 115 | + AsyncResponseBuilder.create(200) |
| 116 | + .setEntity("ok\n", ContentType.TEXT_PLAIN) |
| 117 | + .build(), |
| 118 | + context); |
| 119 | + } catch (final Exception ignore) { |
| 120 | + // ignore |
| 121 | + } |
| 122 | + }; |
| 123 | + |
| 124 | + if ("/warmup".equals(path)) { |
| 125 | + task.run(); |
| 126 | + } else { |
| 127 | + scheduler.schedule(task, serverDelayMillis, TimeUnit.MILLISECONDS); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + }) |
| 132 | + .create(); |
| 133 | + |
| 134 | + server.start(); |
| 135 | + final ListenerEndpoint ep = server.listen(new InetSocketAddress("127.0.0.1", 0), URIScheme.HTTP).get(); |
| 136 | + final int port = ((InetSocketAddress) ep.getAddress()).getPort(); |
| 137 | + |
| 138 | + final H2MultiplexingRequester requester = H2MultiplexingRequesterBootstrap.bootstrap() |
| 139 | + .setIOReactorConfig(ioReactorConfig) |
| 140 | + .setH2Config(H2Config.custom().setPushEnabled(false).build()) |
| 141 | + .setMaxCommandsPerConnection(maxPerConn) |
| 142 | + .create(); |
| 143 | + |
| 144 | + |
| 145 | + requester.start(); |
| 146 | + |
| 147 | + try { |
| 148 | + final HttpHost target = new HttpHost("http", "127.0.0.1", port); |
| 149 | + |
| 150 | + // Warmup |
| 151 | + requester.execute( |
| 152 | + target, |
| 153 | + AsyncRequestBuilder.get().setHttpHost(target).setPath("/warmup").build(), |
| 154 | + new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), |
| 155 | + timeout, |
| 156 | + HttpCoreContext.create(), |
| 157 | + null).get(); |
| 158 | + |
| 159 | + final AtomicInteger ok = new AtomicInteger(0); |
| 160 | + final AtomicInteger rejected = new AtomicInteger(0); |
| 161 | + final AtomicInteger failed = new AtomicInteger(0); |
| 162 | + |
| 163 | + final CountDownLatch done = new CountDownLatch(totalRequests); |
| 164 | + final CountDownLatch start = new CountDownLatch(1); |
| 165 | + final ExecutorService exec = Executors.newFixedThreadPool(16); |
| 166 | + |
| 167 | + for (int i = 0; i < totalRequests; i++) { |
| 168 | + final int id = i; |
| 169 | + exec.execute(new Runnable() { |
| 170 | + @Override |
| 171 | + public void run() { |
| 172 | + try { |
| 173 | + start.await(); |
| 174 | + } catch (final InterruptedException e) { |
| 175 | + Thread.currentThread().interrupt(); |
| 176 | + done.countDown(); |
| 177 | + return; |
| 178 | + } |
| 179 | + |
| 180 | + requester.execute( |
| 181 | + target, |
| 182 | + AsyncRequestBuilder.get().setHttpHost(target).setPath("/slow?i=" + id).build(), |
| 183 | + new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), |
| 184 | + timeout, |
| 185 | + HttpCoreContext.create(), |
| 186 | + new FutureCallback<Message<HttpResponse, String>>() { |
| 187 | + |
| 188 | + @Override |
| 189 | + public void completed(final Message<HttpResponse, String> message) { |
| 190 | + ok.incrementAndGet(); |
| 191 | + done.countDown(); |
| 192 | + } |
| 193 | + |
| 194 | + @Override |
| 195 | + public void failed(final Exception ex) { |
| 196 | + if (ex instanceof RejectedExecutionException) { |
| 197 | + rejected.incrementAndGet(); |
| 198 | + } else { |
| 199 | + failed.incrementAndGet(); |
| 200 | + } |
| 201 | + done.countDown(); |
| 202 | + } |
| 203 | + |
| 204 | + @Override |
| 205 | + public void cancelled() { |
| 206 | + failed.incrementAndGet(); |
| 207 | + done.countDown(); |
| 208 | + } |
| 209 | + }); |
| 210 | + } |
| 211 | + }); |
| 212 | + } |
| 213 | + |
| 214 | + start.countDown(); |
| 215 | + |
| 216 | + final boolean allDone = done.await(60, TimeUnit.SECONDS); |
| 217 | + exec.shutdownNow(); |
| 218 | + |
| 219 | + Assertions.assertTrue(allDone, "Timed out"); |
| 220 | + Assertions.assertEquals(totalRequests, ok.get() + rejected.get() + failed.get()); |
| 221 | + Assertions.assertTrue(rejected.get() > 0, "Expected at least one RejectedExecutionException"); |
| 222 | + Assertions.assertEquals(0, failed.get(), "Unexpected non-rejection failures: " + failed.get()); |
| 223 | + } finally { |
| 224 | + requester.close(CloseMode.GRACEFUL); |
| 225 | + server.close(CloseMode.GRACEFUL); |
| 226 | + scheduler.shutdownNow(); |
| 227 | + } |
| 228 | + } |
| 229 | +} |
0 commit comments