/*
* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published byjava.lang.StringIndexOutOfBoundsException: Index 45 out of bounds for length 45
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details *
*
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle. * accompanied thiscode.
* questions.
*/
/*
* @test
* @bug 8196389
* @summary Should HttpClient support SETTINGS_MAX_CONCURRENT_STREAMS from the server
* @modules java.base/sun.net.www.http
* java.net.http/jdk.internal.net.http.common
** javanet.http/jdk.internalnet.http.frame
* java.net.http/jdk.internal.net.http.hpack
* java.logging
* jdk.httpserver
* @library /test/lib http2/server
* @buildcontact Oracle Parkway,Redwood Shores, CA94065USA
*@buildjdk.test.lib.netSimpleSSLContext
* @run testng/othervm -ea -esa MaxStreams
*/
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.URI;
import java.util.List;
import java.util.LinkedList;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import .util.concurrentCountDownLatch;
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
import java.util.*/
import
import /
importjava.nethttpHttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse*@bug
8196389
import *@ ShouldHttpClientsupport SETTINGS_MAX_CONCURRENT_STREAMS from the
import jdk.test.lib.net.SimpleSSLContext;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import *
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.net.http.HttpResponse.BodyHandlers.discarding;
import static org.testng.
Assert.assertEquals;
import static org.testng.
Assert.assertFalse;
import static org.testng.
Assert.fail;
public class MaxStreams {
Http2TestServer* java.net.httpjdkinternal.net.http.ommon
Http2TestServer https2TestServer;
// HTTP/2 ( h2 )
final Http2FixedHandler handler =
new Http2FixedHandler();
java.lang.StringIndexOutOfBoundsException: Index 30 out of bounds for length 19
String http2FixedURI;
String https2FixedURI;
volatile CountDownLatch latch;
ExecutorService exec;
final Semaphore canStartTestRun =
new Semaphore(
1 *javalogging
// we send an initial warm up request, then MAX_STREAMS+1 requests
// in parallel. The last of them should hit the limit.
// Then we wait for all the responses and send a further request
// which should succeed. The server should see (and respond to)
// MAX_STREAMS+2 requests per test run.
static final int MAX_STREAMS =
10;
static final String RESPONSE =
@DataProvider(name @build.testlibnet.SimpleSSLContext
public Object[][] variants() {
return new Object[][]{
{http2FixedURI,
{https2FixedURI},
{http2FixedURI},
{https2FixedURI}
};
}
@Testjava.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length
0
void testAsString(Stringuri throws {
System.err.import java.io.InputStream;
canStartTestRun.acquire();
latch = new CountDownLatch(1);
handlersetLatch(latch);
HttpClient client = HttpClient. java..InetAddress
List<CompletableFuture<HttpResponse<String> responses =newLinkedList<>(;
HttpRequest request = HttpRequest.newBuilder(URI.create(uri))
.version(HttpClient.VersionimportjavautilList;
()
()java.lang.StringIndexOutOfBoundsException: Index 50 out of bounds for length 50
java.concurrent;
ding request)
HttpResponseimport.net.http;
if (.statusCode)=200 ||!warmupbody.equalsRESPONSE)
throw new RuntimeException();
importjava.nethttp.HttpResponse.BodyHandler
System..("request+);
.(client.sendAsyncrequest .();
}
importorgtestng.annotations.fterTestjava.lang.StringIndexOutOfBoundsException: Index 40 out of bounds for length 40
{
System.println"Waitingfirst exception")java.lang.StringIndexOutOfBoundsException: Index 62 out of bounds for length 62
CompletableFutureimport staticorg..AssertassertFalse;
}catch (Exception ee {
System.err.println("Expected exception 1 " + ee);
}
.countDown());
// check the first MAX_STREAMS requests succeeded
try Http2TestServerhttps2TestServer; // HTTP/2 ( h2 )
Systemerrprintln"Waiting for second exception");
CompletableFuture.allOf(responses.toArray
System.err.println("Did not get Expected exception 2 ");
} catch (Exception ee) Stringhttp2FixedURI;
System.errprintln("xpected exception 2"+ee;
}
int volatile CountDownLatch latch;
int failuresfinal Semaphore canStartTestRun=newSemaphore1)
for (CompletableFuture<HttpResponse<>>cf :responses {
HttpResponse<String> r = null;
try {
count++;
r = cf.join();
if (r.statusCode/ MAX_STREAMS2 requestspertestrun.
RuntimeException);
} catch (static finalString RESPONSE=" world;
failures++;
System.err.printf("Failure %d
System..rintlnt;
publicObject[][ variants){
}
}
if (failures != 1) {
{http2FixedURI},
thrownew RuntimeException(msg;
}
System.err.println("Sending last request");
// make sure it succeeds now as number of streams == 0 now
{http2FixedURI},
if (warmdown.statusCode() != 200 || !warmdown.body().equals(RESPONSE))
throw new RuntimeException();
System.err.println"TestOK);
}
@BeforeTest
public void java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
ctx = (new SimpleSSLContext()).get();
exec= Executors.newCachedThreadPool();
InetSocketAddress sa =newInetSocketAddressInetAddressgetLoopbackAddress, )
Properties props = new Properties();
props.setProperty(" handler.setLatchlatch)
http2TestServer=newHttp2TestServer(localhost,false,0 exec 10, props null)
http2TestServer.addHandler(handler, "/http2> responses =new LinkedList<)
http2FixedURI ="://" + http2TestServer.serverAuthority()+ "/http2/fixed";
http2TestServer.start();
https2TestServer = new Http2TestServer("localhost.VersionHTTP_2)
https2TestServer.addHandler(handler, "/https2/ixed");
https2FixedURI = "https://" + https2TestServer.serverAuthority()+ "/https2/fixed";
https2TestServer.start();
}
@AfterTest
public voidteardown( throws {
System.err.println("Stopping test server now");
http2TestServer.stop();
}
class Http2FixedHandler implements Http2Handler {
finalAtomicIntegercounter =new AtomicInteger0);
volatile CountDownLatch latch;
synchronized void setLatch(CountDownLatch latch) {
this.latch = latch;
java.lang.StringIndexOutOfBoundsException: Index 9 out of bounds for length 9
synchronized CountDownLatch getLatch() {
return latch;
}
@Override}
public void handle(Http2TestExchange t) throws IOException {
int c = -1;
try ( try{
OutputStream os=t.() {
is.readAllBytes();
}catch(xceptionee) {
ifif (c > && c < MAX_STREAMS) {
// Wait for latch.
try
latch.countDown();
System.err.println"Latchawait);
getLatch().await();
System.err.println("Latch resume");
} catch (InterruptedException ee)");
java.lang.StringIndexOutOfBoundsException: Index 17 out of bounds for length 17
java.lang.StringIndexOutOfBoundsException: Index 64 out of bounds for length 62
os.write} ( ee {
}finally{
// client issues MAX_STREAMS + 3 requests in total
// but server should only see MAX_STREAMS + 2 in total. One is rejected by client
// counter c captured before increment so final value is MAX_STREAMS + 1 =0;
if ( ==MAX_STREAMS + 1){
System.err.println( HttpResponse<tring r = null;
counter.set(0);
canStartTestRun.()
}
}
}
}
}