< prev index next >

test/jdk/java/net/httpclient/MaxStreams.java

Print this page

 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 /*
 25  * @test
 26  * @bug 8196389
 27  * @summary Should HttpClient support SETTINGS_MAX_CONCURRENT_STREAMS from the server
 28  * @library /test/lib /test/jdk/java/net/httpclient/lib
 29  * @build jdk.httpclient.test.lib.http2.Http2TestServer jdk.test.lib.net.SimpleSSLContext
 30  * @run testng/othervm -ea -esa MaxStreams
 31  */
 32 
 33 import java.io.IOException;
 34 import java.io.InputStream;
 35 import java.io.OutputStream;
 36 import java.net.InetAddress;
 37 import java.net.InetSocketAddress;
 38 import java.net.URI;
 39 import java.util.List;
 40 import java.util.LinkedList;
 41 import java.util.Properties;
 42 import java.util.concurrent.atomic.AtomicInteger;
 43 import java.util.concurrent.CompletableFuture;
 44 import java.util.concurrent.CompletionException;
 45 import java.util.concurrent.CountDownLatch;
 46 import java.util.concurrent.Executors;
 47 import java.util.concurrent.ExecutorService;
 48 import java.util.concurrent.Semaphore;
 49 import javax.net.ssl.SSLContext;
 50 import java.net.http.HttpClient;
 51 import java.net.http.HttpRequest;
 52 import java.net.http.HttpResponse;
 53 import java.net.http.HttpResponse.BodyHandler;
 54 import java.net.http.HttpResponse.BodyHandlers;
 55 import jdk.httpclient.test.lib.http2.Http2TestServer;
 56 import jdk.httpclient.test.lib.http2.Http2TestExchange;
 57 import jdk.httpclient.test.lib.http2.Http2Handler;
 58 import jdk.test.lib.net.SimpleSSLContext;
 59 import org.testng.annotations.AfterTest;
 60 import org.testng.annotations.BeforeTest;
 61 import org.testng.annotations.DataProvider;
 62 import org.testng.annotations.Test;
 63 import static java.nio.charset.StandardCharsets.UTF_8;
 64 import static java.net.http.HttpResponse.BodyHandlers.discarding;
 65 import static org.testng.Assert.assertEquals;
 66 import static org.testng.Assert.assertFalse;
 67 import static org.testng.Assert.fail;
 68 
 69 public class MaxStreams {
 70 
 71     Http2TestServer http2TestServer;   // HTTP/2 ( h2c )
 72     Http2TestServer https2TestServer;   // HTTP/2 ( h2 )
 73     final Http2FixedHandler handler = new Http2FixedHandler();
 74     SSLContext ctx;
 75     String http2FixedURI;
 76     String https2FixedURI;
 77     volatile CountDownLatch latch;
 78     ExecutorService exec;
 79     final Semaphore canStartTestRun = new Semaphore(1);
 80 
 81     // we send an initial warm up request, then MAX_STREAMS+1 requests
 82     // in parallel. The last of them should hit the limit.
 83     // Then we wait for all the responses and send a further request
 84     // which should succeed. The server should see (and respond to)
 85     // MAX_STREAMS+2 requests per test run.
 86 
 87     static final int MAX_STREAMS = 10;
 88     static final String RESPONSE = "Hello world";
 89 
 90     @DataProvider(name = "uris")
 91     public Object[][] variants() {
 92         return new Object[][]{
 93                 {http2FixedURI},
 94                 {https2FixedURI},
 95                 {http2FixedURI},
 96                 {https2FixedURI}
 97         };
 98     }
 99 
100 
101     @Test(dataProvider = "uris", timeOut=20000)
102     void testAsString(String uri) throws Exception {
103         System.err.println("Semaphore acquire");
104         canStartTestRun.acquire();
105         latch = new CountDownLatch(1);
106         handler.setLatch(latch);
107         HttpClient client = HttpClient.newBuilder().sslContext(ctx).build();
108         List<CompletableFuture<HttpResponse<String>>> responses = new LinkedList<>();
109 
110         HttpRequest request = HttpRequest.newBuilder(URI.create(uri))
111                                          .version(HttpClient.Version.HTTP_2)
112                                          .GET()
113                                          .build();
114         // send warmup to ensure we only have one Http2Connection
115         System.err.println("Sending warmup request");
116         HttpResponse<String> warmup = client.send(request, BodyHandlers.ofString());
117         if (warmup.statusCode() != 200 || !warmup.body().equals(RESPONSE))
118             throw new RuntimeException();
119 
120         for (int i=0;i<MAX_STREAMS+1; i++) {
121             System.err.println("Sending request " + i);
122             responses.add(client.sendAsync(request, BodyHandlers.ofString()));
123         }
124 
125         // wait until we get local exception before allow server to proceed

192     @AfterTest
193     public void teardown() throws Exception {
194         System.err.println("Stopping test server now");
195         http2TestServer.stop();
196     }
197 
198     class Http2FixedHandler implements Http2Handler {
199         final AtomicInteger counter = new AtomicInteger(0);
200         volatile CountDownLatch latch;
201 
202         synchronized void setLatch(CountDownLatch latch) {
203             this.latch = latch;
204         }
205 
206         synchronized CountDownLatch getLatch() {
207             return latch;
208         }
209 
210         @Override
211         public void handle(Http2TestExchange t) throws IOException {
212             int c = -1;
213             try (InputStream is = t.getRequestBody();
214                  OutputStream os = t.getResponseBody()) {
215 
216                 is.readAllBytes();
217                 c = counter.getAndIncrement();
218                 if (c > 0 && c <= MAX_STREAMS) {
219                     // Wait for latch.
220                     try {
221                         // don't send any replies until all requests are sent
222                         System.err.println("Latch await");
223                         getLatch().await();
224                         System.err.println("Latch resume");
225                     } catch (InterruptedException ee) {}






226                 }
227                 t.sendResponseHeaders(200, RESPONSE.length());
228                 os.write(RESPONSE.getBytes());
229             } finally {
230                 // client issues MAX_STREAMS + 3 requests in total
231                 // but server should only see MAX_STREAMS + 2 in total. One is rejected by client
232                 // counter c captured before increment so final value is MAX_STREAMS + 1
233                 if (c == MAX_STREAMS + 1) {
234                     System.err.println("Semaphore release");
235                     counter.set(0);
236                     canStartTestRun.release();
237                 }
238             }
239         }
240     }
241 }

 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 /*
 25  * @test
 26  * @bug 8196389
 27  * @summary Should HttpClient support SETTINGS_MAX_CONCURRENT_STREAMS from the server
 28  * @library /test/lib /test/jdk/java/net/httpclient/lib
 29  * @build jdk.httpclient.test.lib.http2.Http2TestServer jdk.test.lib.net.SimpleSSLContext
 30  * @run testng/othervm MaxStreams
 31  */
 32 
 33 import java.io.IOException;
 34 import java.io.InputStream;
 35 import java.io.OutputStream;
 36 import java.net.InetAddress;
 37 import java.net.InetSocketAddress;
 38 import java.net.URI;
 39 import java.util.List;
 40 import java.util.LinkedList;
 41 import java.util.Properties;
 42 import java.util.concurrent.atomic.AtomicInteger;
 43 import java.util.concurrent.CompletableFuture;
 44 import java.util.concurrent.CompletionException;
 45 import java.util.concurrent.CountDownLatch;
 46 import java.util.concurrent.Executors;
 47 import java.util.concurrent.ExecutorService;

 48 import javax.net.ssl.SSLContext;
 49 import java.net.http.HttpClient;
 50 import java.net.http.HttpRequest;
 51 import java.net.http.HttpResponse;
 52 import java.net.http.HttpResponse.BodyHandler;
 53 import java.net.http.HttpResponse.BodyHandlers;
 54 import jdk.httpclient.test.lib.http2.Http2TestServer;
 55 import jdk.httpclient.test.lib.http2.Http2TestExchange;
 56 import jdk.httpclient.test.lib.http2.Http2Handler;
 57 import jdk.test.lib.net.SimpleSSLContext;
 58 import org.testng.annotations.AfterTest;
 59 import org.testng.annotations.BeforeTest;
 60 import org.testng.annotations.DataProvider;
 61 import org.testng.annotations.Test;
 62 import static java.nio.charset.StandardCharsets.UTF_8;
 63 import static java.net.http.HttpResponse.BodyHandlers.discarding;
 64 import static org.testng.Assert.assertEquals;
 65 import static org.testng.Assert.assertFalse;
 66 import static org.testng.Assert.fail;
 67 
 68 public class MaxStreams {
 69 
 70     Http2TestServer http2TestServer;   // HTTP/2 ( h2c )
 71     Http2TestServer https2TestServer;   // HTTP/2 ( h2 )
 72     final Http2FixedHandler handler = new Http2FixedHandler();
 73     SSLContext ctx;
 74     String http2FixedURI;
 75     String https2FixedURI;

 76     ExecutorService exec;

 77 
 78     // we send an initial warm up request, then MAX_STREAMS+1 requests
 79     // in parallel. The last of them should hit the limit.
 80     // Then we wait for all the responses and send a further request
 81     // which should succeed. The server should see (and respond to)
 82     // MAX_STREAMS+2 requests per test run.
 83 
 84     static final int MAX_STREAMS = 10;
 85     static final String RESPONSE = "Hello world";
 86 
 87     @DataProvider(name = "uris")
 88     public Object[][] variants() {
 89         return new Object[][]{
 90                 {http2FixedURI},
 91                 {https2FixedURI},
 92                 {http2FixedURI},
 93                 {https2FixedURI}
 94         };
 95     }
 96 
 97 
 98     @Test(dataProvider = "uris")
 99     void testAsString(String uri) throws Exception {
100         CountDownLatch latch = new CountDownLatch(1);


101         handler.setLatch(latch);
102         HttpClient client = HttpClient.newBuilder().sslContext(ctx).build();
103         List<CompletableFuture<HttpResponse<String>>> responses = new LinkedList<>();
104 
105         HttpRequest request = HttpRequest.newBuilder(URI.create(uri))
106                                          .version(HttpClient.Version.HTTP_2)
107                                          .GET()
108                                          .build();
109         // send warmup to ensure we only have one Http2Connection
110         System.err.println("Sending warmup request");
111         HttpResponse<String> warmup = client.send(request, BodyHandlers.ofString());
112         if (warmup.statusCode() != 200 || !warmup.body().equals(RESPONSE))
113             throw new RuntimeException();
114 
115         for (int i=0;i<MAX_STREAMS+1; i++) {
116             System.err.println("Sending request " + i);
117             responses.add(client.sendAsync(request, BodyHandlers.ofString()));
118         }
119 
120         // wait until we get local exception before allow server to proceed

187     @AfterTest
188     public void teardown() throws Exception {
189         System.err.println("Stopping test server now");
190         http2TestServer.stop();
191     }
192 
193     class Http2FixedHandler implements Http2Handler {
194         final AtomicInteger counter = new AtomicInteger(0);
195         volatile CountDownLatch latch;
196 
197         synchronized void setLatch(CountDownLatch latch) {
198             this.latch = latch;
199         }
200 
201         synchronized CountDownLatch getLatch() {
202             return latch;
203         }
204 
205         @Override
206         public void handle(Http2TestExchange t) throws IOException {

207             try (InputStream is = t.getRequestBody();
208                  OutputStream os = t.getResponseBody()) {
209 
210                 is.readAllBytes();
211                 int c = counter.getAndIncrement();
212                 if (c > 0 && c <= MAX_STREAMS) {
213                     // Wait for latch.
214                     try {
215                         // don't send any replies until all requests are sent
216                         System.err.println("Latch await");
217                         getLatch().await();
218                         System.err.println("Latch resume");
219                     } catch (InterruptedException ee) {}
220                 } else if (c == MAX_STREAMS + 1) {
221                     // client issues MAX_STREAMS + 3 requests in total
222                     // but server should only see MAX_STREAMS + 2 in total. One is rejected by client
223                     // counter c captured before increment so final value is MAX_STREAMS + 1
224                     System.err.println("Counter reset");
225                     counter.set(0);
226                 }
227                 t.sendResponseHeaders(200, RESPONSE.length());
228                 os.write(RESPONSE.getBytes());









229             }
230         }
231     }
232 }
< prev index next >